Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Import fmt:percent_encode.
  • Loading branch information
Tim Fletcher committed Nov 6, 2008
1 parent 869c601 commit f1098b0f28bb9ea2e565852a6b4dd089db8f0e66
Showing 1 changed file with 5 additions and 2 deletions.
@@ -2,14 +2,17 @@

-export([base_string/3, normalize/1, 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) ->
b64(crypto:sha_mac(key(ConsumerSecret, TokenSecret), BaseString)).

base_string(Method, URL, Params) when is_list(Method) ->
string:join(lists:map(fun fmt:percent_encode/1, [Method, oauth_uri:normalize(URL), normalize(Params)]), "&").
Unencoded = [Method, oauth_uri:normalize(URL), normalize(Params)],
string:join([percent_encode(Str) || Str <- Unencoded], "&").

normalize(Params) ->
StringParams = lists:map(fun({K, V}) when is_atom(K) -> {atom_to_list(K), V}; (I) -> I end, Params),
@@ -19,7 +22,7 @@ sort(Params) ->
lists:sort(fun({K,X},{K,Y}) -> X < Y; ({A,_},{B,_}) -> A < B end, Params).

key(ConsumerSecret, TokenSecret) ->
fmt:sprintf("%s&%s", [fmt:percent_encode(ConsumerSecret), fmt:percent_encode(TokenSecret)]).
fmt:sprintf("%s&%s", [percent_encode(ConsumerSecret), percent_encode(TokenSecret)]).

b64(Data) ->
base64:encode_to_string(Data).

0 comments on commit f1098b0

Please sign in to comment.