From ec5500c2242bfaf969e2d74da7150c949ba13907 Mon Sep 17 00:00:00 2001 From: MrCdK Date: Wed, 27 Aug 2014 20:29:21 +0200 Subject: [PATCH] Added GET parameters support to OAuth --- lime/net/URIParser.hx | 72 ++++++++++++++++++++++++++ lime/net/oauth/OAuthClient.hx | 11 ++-- lime/net/oauth/OAuthRequest.hx | 31 ++++++++--- lime/net/oauth/OAuthSignatureMethod.hx | 4 +- 4 files changed, 104 insertions(+), 14 deletions(-) create mode 100644 lime/net/URIParser.hx diff --git a/lime/net/URIParser.hx b/lime/net/URIParser.hx new file mode 100644 index 000000000..7cb756c17 --- /dev/null +++ b/lime/net/URIParser.hx @@ -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; + + 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 { + + var result:Array = []; + + 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 }; \ No newline at end of file diff --git a/lime/net/oauth/OAuthClient.hx b/lime/net/oauth/OAuthClient.hx index 2d4c68b66..a3581718d 100644 --- a/lime/net/oauth/OAuthClient.hx +++ b/lime/net/oauth/OAuthClient.hx @@ -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(); @@ -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; } diff --git a/lime/net/oauth/OAuthRequest.hx b/lime/net/oauth/OAuthRequest.hx index b175a9484..9c9bfa3c4 100644 --- a/lime/net/oauth/OAuthRequest.hx +++ b/lime/net/oauth/OAuthRequest.hx @@ -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; + public var request:URLRequest; + + private var uri:URIParser; public function new (version:OAuthVersion = V1, method:URLRequestMethod, url:String, parameters:Map) { - 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(); - // 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. diff --git a/lime/net/oauth/OAuthSignatureMethod.hx b/lime/net/oauth/OAuthSignatureMethod.hx index 4b0d1c7c5..921fece11 100644 --- a/lime/net/oauth/OAuthSignatureMethod.hx +++ b/lime/net/oauth/OAuthSignatureMethod.hx @@ -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"; } \ No newline at end of file