Skip to content

Commit

Permalink
Add oauth_base module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Fletcher committed Nov 7, 2008
1 parent 8cb637f commit 6de41d4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
8 changes: 4 additions & 4 deletions include/oauth_test_macros.hrl
Expand Up @@ -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() ->
Expand Down
19 changes: 19 additions & 0 deletions 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}.
28 changes: 4 additions & 24 deletions 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}.
3 changes: 2 additions & 1 deletion src/oauth_request.erl
Expand Up @@ -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.
22 changes: 11 additions & 11 deletions test/oauth_unit.erl
Expand Up @@ -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"},
Expand All @@ -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"},
Expand Down

0 comments on commit 6de41d4

Please sign in to comment.