Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Add oauth_base module.
  • Loading branch information
Tim Fletcher committed Nov 7, 2008
1 parent 8cb637f commit 6de41d42f4dbf10218116d4f8eea43281d22eb69
Showing 5 changed files with 40 additions and 40 deletions.
@@ -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() ->
@@ -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}.
@@ -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}.
@@ -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.
@@ -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"},

0 comments on commit 6de41d4

Please sign in to comment.