Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add oauth_base module.

  • Loading branch information...
commit 6de41d42f4dbf10218116d4f8eea43281d22eb69 1 parent 8cb637f
@tim tim authored
View
8 include/oauth_test_macros.hrl
@@ -2,12 +2,12 @@
?_assertEqual(ExpectedSignature, oauth_plaintext:signature(ConsumerSecret, TokenSecret))
).
--define(hmac_normalize_test(ExpectedString, Params),
- ?_assertEqual(ExpectedString, oauth_hmac:normalize(Params))
+-define(normalize_test(ExpectedString, Params),
+ ?_assertEqual(ExpectedString, oauth_base:normalize(Params))
).
--define(hmac_base_string_test(Method, URL, Params, Expected), fun() ->
- ?assertEqual(string:join(Expected, ""), oauth_hmac:base_string(Method, URL, Params))
+-define(base_string_test(Method, URL, Params, Expected), fun() ->
+ ?assertEqual(string:join(Expected, ""), oauth_base:string(Method, URL, Params))
end).
-define(hmac_signature_test(ExpectedSignature, ConsumerSecret, TokenSecret, BaseString), fun() ->
View
19 src/oauth_base.erl
@@ -0,0 +1,19 @@
+-module(oauth_base).
+
+-export([string/3, normalize/1]).
+
+
+string(Method, URL, Params) when is_list(Method) ->
+ Unencoded = [Method, oauth_uri:normalize(URL), normalize(Params)],
+ string:join([fmt:percent_encode(Str) || Str <- Unencoded], "&").
+
+normalize(Params) ->
+ oauth_params:to_string(sort([to_string(KV) || KV <- Params])).
+
+sort(Params) ->
+ lists:sort(fun({K,X},{K,Y}) -> X < Y; ({A,_},{B,_}) -> A < B end, Params).
+
+to_string({K, V}) when is_atom(K) ->
+ {atom_to_list(K), V};
+to_string({K, V}) when is_list(K) ->
+ {K, V}.
View
28 src/oauth_hmac.erl
@@ -1,30 +1,10 @@
-module(oauth_hmac).
--export([base_string/3, normalize/1, signature/3]).
+-export([signature/3]).
--import(fmt, [percent_encode/1]).
-
-signature({Method, URL, Params}, ConsumerSecret, TokenSecret) ->
- signature(base_string(Method, URL, Params), ConsumerSecret, TokenSecret);
signature(BaseString, ConsumerSecret, TokenSecret) ->
- Key = key(ConsumerSecret, TokenSecret),
+ CS = fmt:percent_encode(ConsumerSecret),
+ TS = fmt:percent_encode(TokenSecret),
+ Key = fmt:sprintf("%s&%s", [CS, TS]),
base64:encode_to_string(crypto:sha_mac(Key, BaseString)).
-
-base_string(Method, URL, Params) when is_list(Method) ->
- Unencoded = [Method, oauth_uri:normalize(URL), normalize(Params)],
- string:join([percent_encode(Str) || Str <- Unencoded], "&").
-
-normalize(Params) ->
- oauth_params:to_string(sort([to_string(KV) || KV <- Params])).
-
-sort(Params) ->
- lists:sort(fun({K,X},{K,Y}) -> X < Y; ({A,_},{B,_}) -> A < B end, Params).
-
-key(ConsumerSecret, TokenSecret) ->
- fmt:sprintf("%s&%s", [percent_encode(ConsumerSecret), percent_encode(TokenSecret)]).
-
-to_string({K, V}) when is_atom(K) ->
- {atom_to_list(K), V};
-to_string({K, V}) when is_list(K) ->
- {K, V}.
View
3  src/oauth_request.erl
@@ -42,5 +42,6 @@ signature(RequestMethod, URL, Params, Consumer, TokenSecret) ->
"PLAINTEXT" ->
oauth_plaintext:signature(ConsumerSecret, TokenSecret);
"HMAC-SHA1" ->
- oauth_hmac:signature({RequestMethod, URL, Params}, ConsumerSecret, TokenSecret)
+ BaseString = oauth_base:string(RequestMethod, URL, Params),
+ oauth_hmac:signature(BaseString, ConsumerSecret, TokenSecret)
end.
View
22 test/oauth_unit.erl
@@ -26,20 +26,20 @@ plaintext_signature_test_() -> [
?plaintext_signature_test("djr9rjt0jd78jf88", "", "djr9rjt0jd78jf88%26")
].
-hmac_normalize_test_() -> [
+normalize_test_() -> [
% cf. http://wiki.oauth.net/TestCases
- ?hmac_normalize_test("name=", [{name,undefined}]),
- ?hmac_normalize_test("a=b", [{a,b}]),
- ?hmac_normalize_test("a=b&c=d", [{a,b},{c,d}]),
- ?hmac_normalize_test("a=x%20y&a=x%21y", [{a,"x!y"},{a,"x y"}]),
- ?hmac_normalize_test("x=a&x%21y=a", [{"x!y",a},{x,a}])
+ ?normalize_test("name=", [{name,undefined}]),
+ ?normalize_test("a=b", [{a,b}]),
+ ?normalize_test("a=b&c=d", [{a,b},{c,d}]),
+ ?normalize_test("a=x%20y&a=x%21y", [{a,"x!y"},{a,"x y"}]),
+ ?normalize_test("x=a&x%21y=a", [{"x!y",a},{x,a}])
].
-hmac_base_string_test_() -> [
+base_string_test_() -> [
% cf. http://wiki.oauth.net/TestCases
- ?hmac_base_string_test("GET", "http://example.com/", [{n,v}], ["GET&http%3A%2F%2Fexample.com%2F&n%3Dv"]),
- ?hmac_base_string_test("GET", "http://example.com", [{n,v}], ["GET&http%3A%2F%2Fexample.com%2F&n%3Dv"]),
- ?hmac_base_string_test("POST", "https://photos.example.net/request_token", [
+ ?base_string_test("GET", "http://example.com/", [{n,v}], ["GET&http%3A%2F%2Fexample.com%2F&n%3Dv"]),
+ ?base_string_test("GET", "http://example.com", [{n,v}], ["GET&http%3A%2F%2Fexample.com%2F&n%3Dv"]),
+ ?base_string_test("POST", "https://photos.example.net/request_token", [
{oauth_version, "1.0"},
{oauth_consumer_key, "dpf43f3p2l4k3l03"},
{oauth_timestamp, "1191242090"},
@@ -50,7 +50,7 @@ hmac_base_string_test_() -> [
"%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dhsu94j3884jdopsl%26oauth_signature_method",
"%3DPLAINTEXT%26oauth_timestamp%3D1191242090%26oauth_version%3D1.0"
]),
- ?hmac_base_string_test("GET", "http://photos.example.net/photos", [
+ ?base_string_test("GET", "http://photos.example.net/photos", [
{file, "vacation.jpg"},
{size, "original"},
{oauth_version, "1.0"},
Please sign in to comment.
Something went wrong with that request. Please try again.