Added GET parameters support to OAuth
This commit is contained in:
72
lime/net/URIParser.hx
Normal file
72
lime/net/URIParser.hx
Normal file
@@ -0,0 +1,72 @@
|
||||
package lime.net;
|
||||
|
||||
// Based on http://blog.stevenlevithan.com/archives/parseuri
|
||||
class URIParser {
|
||||
|
||||
public static var URI_REGEX = ~/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/;
|
||||
public static var QUERY_REGEX = ~/(?:^|&)([^&=]*)=?([^&]*)/;
|
||||
|
||||
|
||||
public var source:String;
|
||||
public var protocol:String;
|
||||
public var authority:String;
|
||||
public var userInfo:String;
|
||||
public var user:String;
|
||||
public var password:String;
|
||||
public var host:String;
|
||||
public var port:String;
|
||||
public var relative:String;
|
||||
public var path:String;
|
||||
public var directory:String;
|
||||
public var file:String;
|
||||
public var query:String;
|
||||
public var anchor:String;
|
||||
|
||||
public var queryArray:Array<KVPair>;
|
||||
|
||||
public function new(uri:String) {
|
||||
|
||||
if(URI_REGEX.match(uri)) {
|
||||
|
||||
source = uri;
|
||||
protocol = URI_REGEX.matched(1);
|
||||
authority = URI_REGEX.matched(2);
|
||||
userInfo = URI_REGEX.matched(3);
|
||||
user = URI_REGEX.matched(4);
|
||||
password = URI_REGEX.matched(5);
|
||||
host = URI_REGEX.matched(6);
|
||||
port = URI_REGEX.matched(7);
|
||||
relative = URI_REGEX.matched(8);
|
||||
path = URI_REGEX.matched(9);
|
||||
directory = URI_REGEX.matched(10);
|
||||
file = URI_REGEX.matched(11);
|
||||
query = URI_REGEX.matched(12);
|
||||
anchor = URI_REGEX.matched(13);
|
||||
|
||||
if(query != null && query.length > 0) {
|
||||
queryArray = parseQuery(query);
|
||||
}
|
||||
|
||||
} else {
|
||||
trace('URI "$uri" isn\'t well formed.');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static function parseQuery(query:String):Array<KVPair> {
|
||||
|
||||
var result:Array<KVPair> = [];
|
||||
|
||||
for(str in query.split("&")) {
|
||||
if(QUERY_REGEX.match(str)) {
|
||||
result.push({k:QUERY_REGEX.matched(1), v:QUERY_REGEX.matched(2)});
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
typedef KVPair = { k:String, v:String };
|
||||
@@ -3,6 +3,7 @@ package lime.net.oauth;
|
||||
|
||||
import haxe.crypto.Sha1;
|
||||
import lime.net.URLRequestMethod;
|
||||
import lime.net.URLRequest;
|
||||
|
||||
|
||||
class OAuthClient {
|
||||
@@ -19,7 +20,7 @@ class OAuthClient {
|
||||
}
|
||||
|
||||
|
||||
public function createRequest (method:URLRequestMethod, url:String):OAuthRequest {
|
||||
public function createRequest (method:URLRequestMethod, url:String):URLRequest {
|
||||
|
||||
var parameters = new Map<String, String>();
|
||||
|
||||
@@ -29,10 +30,10 @@ class OAuthClient {
|
||||
parameters.set("oauth_timestamp", Std.string(Std.int(Date.now ().getTime () / 1000)));
|
||||
parameters.set("oauth_consumer_key", consumer.key);
|
||||
|
||||
var request = new OAuthRequest (version, method, url, parameters);
|
||||
if(version == V1) request.sign (consumer, OAuthSignatureMethod.HMAC_SHA1);
|
||||
request.requestHeaders.push(request.getHeader());
|
||||
return request;
|
||||
var oauth = new OAuthRequest (version, method, url, parameters);
|
||||
if(version == V1) oauth.sign (consumer, OAuthSignatureMethod.HMAC_SHA1);
|
||||
oauth.request.requestHeaders.push(oauth.getHeader());
|
||||
return oauth.request;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -7,22 +7,29 @@ import haxe.io.Bytes;
|
||||
import lime.net.URLRequestMethod;
|
||||
import lime.net.URLRequestHeader;
|
||||
import lime.net.URLRequest;
|
||||
import lime.net.URIParser;
|
||||
import lime.net.oauth.OAuthToken;
|
||||
|
||||
using StringTools;
|
||||
|
||||
class OAuthRequest extends URLRequest {
|
||||
class OAuthRequest {
|
||||
|
||||
public var version:OAuthVersion = V1;
|
||||
public var parameters:Map<String, String>;
|
||||
public var request:URLRequest;
|
||||
|
||||
private var uri:URIParser;
|
||||
|
||||
|
||||
public function new (version:OAuthVersion = V1, method:URLRequestMethod, url:String, parameters:Map<String, String>) {
|
||||
|
||||
super(url);
|
||||
this.version = version;
|
||||
this.method = method;
|
||||
request = new URLRequest();
|
||||
request.url = url;
|
||||
request.method = method;
|
||||
this.parameters = parameters;
|
||||
|
||||
uri = new URIParser(url);
|
||||
|
||||
}
|
||||
|
||||
@@ -61,7 +68,7 @@ class OAuthRequest extends URLRequest {
|
||||
if(accessToken != null) {
|
||||
key += accessToken.secret == null ? "" : accessToken.secret.urlEncode();
|
||||
}
|
||||
var message = method + "&" + url.urlEncode() + "&" + messageParameters();
|
||||
var message = request.method + "&" + normalizeURI() + "&" + normalizeParameters();
|
||||
var hash = new Hmac (SHA1);
|
||||
var bytes = hash.make (Bytes.ofString (key), Bytes.ofString (message));
|
||||
|
||||
@@ -73,11 +80,13 @@ class OAuthRequest extends URLRequest {
|
||||
/**
|
||||
* Prepares the message parameters for the signing process
|
||||
*/
|
||||
private function messageParameters():String {
|
||||
private function normalizeParameters():String {
|
||||
|
||||
var result = new Array<KVPair>();
|
||||
|
||||
// TODO add get params if GET
|
||||
if(uri.queryArray != null) {
|
||||
result = result.concat(uri.queryArray);
|
||||
}
|
||||
// TODO add data if POST
|
||||
|
||||
for(key in parameters.keys()) {
|
||||
@@ -87,12 +96,20 @@ class OAuthRequest extends URLRequest {
|
||||
|
||||
}
|
||||
|
||||
|
||||
result.sort(OAuthSort);
|
||||
|
||||
return result.map(function(p:KVPair) return p.k.urlEncode()+"="+p.v.urlEncode()).join("&").urlEncode();
|
||||
}
|
||||
|
||||
private function normalizeURI():String {
|
||||
var scheme = uri.protocol == null ? "https" : uri.protocol.toLowerCase();
|
||||
var authority = uri.authority.toLowerCase();
|
||||
var port = (scheme == "https" || scheme == "http") ? null : uri.port;
|
||||
var path = uri.path;
|
||||
var result = (scheme + "://" + authority + (port == null ? "" : ":" + port) + path);
|
||||
return result.urlEncode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parameters are sorted by name, using lexicographical byte value ordering.
|
||||
* If two or more parameters share the same name, they are sorted by their value.
|
||||
|
||||
@@ -3,7 +3,7 @@ package lime.net.oauth;
|
||||
|
||||
@:enum abstract OAuthSignatureMethod(String) {
|
||||
|
||||
var PLAINTEXT = "PLAINTEXT";
|
||||
//var PLAINTEXT = "PLAINTEXT";
|
||||
var HMAC_SHA1 = "HMAC-SHA1";
|
||||
var RSA_SHA1 = "RSA-SHA1";
|
||||
//var RSA_SHA1 = "RSA-SHA1";
|
||||
}
|
||||
Reference in New Issue
Block a user