Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Add oauth_uri module.
  • Loading branch information
Tim Fletcher committed Nov 6, 2008
1 parent 1a9d23b commit 869c601ee765a295d5a958a09527f969ae0a63b6
Showing 3 changed files with 28 additions and 24 deletions.
@@ -9,20 +9,12 @@ 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, uri_normalize(URL), normalize(Params)]), "&").
string:join(lists:map(fun fmt:percent_encode/1, [Method, oauth_uri:normalize(URL), normalize(Params)]), "&").

normalize(Params) ->
StringParams = lists:map(fun({K, V}) when is_atom(K) -> {atom_to_list(K), V}; (I) -> I end, Params),
oauth_params:to_string(sort(StringParams)).

uri_normalize(URL) ->
case http_uri:parse(URL) of
{error, _Reason} ->
URL;
Parts ->
oauth_util:uri_join(Parts)
end.

sort(Params) ->
lists:sort(fun({K,X},{K,Y}) -> X < Y; ({A,_},{B,_}) -> A < B end, Params).

@@ -0,0 +1,27 @@
-module(oauth_uri).

-export([join/1, normalize/1]).


join({Scheme, UserInfo, Host, Port, Path, Query}) ->
join(Scheme, UserInfo, Host, Port, [Path, Query]).

join(http, UserInfo, Host, 80, URI) ->
join(http, UserInfo, [Host|URI]);
join(https, UserInfo, Host, 443, URI) ->
join(https, UserInfo, [Host|URI]);
join(Scheme, UserInfo, Host, Port, URI) ->
join(Scheme, UserInfo, [Host, ":", Port|URI]).

join(Scheme, [], URI) ->
lists:concat([Scheme, "://"|URI]);
join(Scheme, UserInfo, URI) ->
lists:concat([Scheme, "://", UserInfo, "@"|URI]).

normalize(URI) ->
case http_uri:parse(URI) of
{error, _Reason} ->
URI;
Parts ->
join(Parts)
end.
@@ -32,18 +32,3 @@ percent_decode([$%,A,B|Etc], Decoded) when ?is_hex(A), ?is_hex(B) ->
percent_decode(Etc, [erlang:list_to_integer([A,B], 16)|Decoded]);
percent_decode([C|Etc], Decoded) when ?is_unreserved(C) ->
percent_decode(Etc, [C|Decoded]).

uri_join({Scheme, UserInfo, Host, Port, Path, Query}) ->
uri_join(Scheme, UserInfo, Host, Port, [Path, Query]).

uri_join(http, UserInfo, Host, 80, URI) ->
uri_join(http, UserInfo, [Host|URI]);
uri_join(https, UserInfo, Host, 443, URI) ->
uri_join(https, UserInfo, [Host|URI]);
uri_join(Scheme, UserInfo, Host, Port, URI) ->
uri_join(Scheme, UserInfo, [Host, ":", Port|URI]).

uri_join(Scheme, [], URI) ->
lists:concat([Scheme, "://"|URI]);
uri_join(Scheme, UserInfo, URI) ->
lists:concat([Scheme, "://", UserInfo, "@"|URI]).

0 comments on commit 869c601

Please sign in to comment.