Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Add rewritten code.
  • Loading branch information
Tim Fletcher committed Jan 13, 2009
1 parent 98bfd9f commit 4ab77bc915ef674413b9900ae8701bed9e9acaec
Showing 15 changed files with 195 additions and 269 deletions.
@@ -1,31 +1,46 @@
-module(oauth).

-export([get/2, get/3, get/4, post/2, post/3, post/4]).
-export([get/5, post/5, token/1, token_secret/1, uri/2, header/1, signed_params/6]).


get(URL, Consumer) ->
get(URL, Consumer, {[], []}, []).
get(URL, ExtraParams, Consumer, Token, TokenSecret) ->
SignedParams = signed_params("GET", URL, ExtraParams, Consumer, Token, TokenSecret),
oauth_http:get(uri(URL, SignedParams)).

get(URL, Consumer, Params) when is_list(Params) ->
get(URL, Consumer, {[], []}, Params);
get(URL, Consumer, TokenPair) ->
get(URL, Consumer, TokenPair, []).
post(URL, ExtraParams, Consumer, Token, TokenSecret) ->
SignedParams = signed_params("GET", URL, ExtraParams, Consumer, Token, TokenSecret),
oauth_http:post(URL, oauth_uri:params_to_string(SignedParams)).

get(URL, Consumer, TokenPair, Params) ->
Request = oauth_request:new("GET", URL, Params),
RequestURL = oauth_request:to_url(Request, Consumer, TokenPair),
http:request(get, {RequestURL, []}, [{autoredirect, false}], []).
token(Params) ->
proplists:get_value("oauth_token", Params).

post(URL, Consumer) ->
post(URL, Consumer, {[], []}, []).
token_secret(Params) ->
proplists:get_value("oauth_token_secret", Params).

post(URL, Consumer, Params) when is_list(Params) ->
post(URL, Consumer, {[], []}, Params);
post(URL, Consumer, TokenPair) ->
post(URL, Consumer, TokenPair, []).
uri(Base, []) ->
Base;
uri(Base, Params) ->
lists:concat([Base, "?", oauth_uri:params_to_string(Params)]).

post(URL, Consumer, TokenPair, Params) ->
Request = oauth_request:new("POST", URL, Params),
Data = oauth_request:to_string(Request, Consumer, TokenPair),
MimeType = "application/x-www-form-urlencoded",
http:request(post, {URL, [], MimeType, Data}, [{autoredirect, false}], []).
header(Params) ->
{"Authorization", "OAuth " ++ oauth_uri:params_to_header_string(Params)}.

signed_params(Method, URL, ExtraParams, Consumer, Token, TokenSecret) ->
Params = token_param(Token, params(Consumer, ExtraParams)),
[{"oauth_signature", oauth_signature:value(Method, URL, Params, Consumer, TokenSecret)}|Params].

token_param("", Params) ->
Params;
token_param(Token, Params) ->
[{"oauth_token", Token}|Params].

params(_Consumer={Key, _, SigMethod}, Params) ->
Nonce = base64:encode_to_string(crypto:rand_bytes(32)), % cf. ruby-oauth
params(Key, SigMethod, oauth_unix:timestamp(), Nonce, Params).

params(ConsumerKey, SigMethod, Timestamp, Nonce, Params) -> [
{"oauth_version", "1.0"},
{"oauth_nonce", Nonce},
{"oauth_timestamp", integer_to_list(Timestamp)},
{"oauth_signature_method", oauth_signature:method_to_string(SigMethod)},
{"oauth_consumer_key", ConsumerKey} | Params].

This file was deleted.

This file was deleted.

This file was deleted.

@@ -0,0 +1,8 @@
-module(oauth_hmac_sha1).

-export([signature/3]).


signature(BaseString, CS, TS) ->
Key = oauth_uri:calate("&", [CS, TS]),
base64:encode_to_string(crypto:sha_mac(Key, BaseString)).
@@ -0,0 +1,22 @@
-module(oauth_http).

-export([get/1, post/2, response_params/1, response_body/1, response_code/1]).


get(URL) ->
request(get, {URL, []}).

post(URL, Data) ->
request(post, {URL, [], "application/x-www-form-urlencoded", Data}).

request(Method, Request) ->
http:request(Method, Request, [{autoredirect, false}], []).

response_params(Response) ->
oauth_uri:params_from_string(response_body(Response)).

response_body({{_, _, _}, _, Body}) ->
Body.

response_code({{_, Code, _}, _, _}) ->
Code.

This file was deleted.

@@ -0,0 +1,7 @@
-module(oauth_plaintext).

-export([signature/2]).


signature(CS, TS) ->
oauth_uri:encode(oauth_uri:calate("&", [CS, TS])).

This file was deleted.

@@ -0,0 +1,9 @@
-module(oauth_rsa_sha1).

-export([signature/2]).


signature(BaseString, PrivateKeyPath) ->
{ok, [Info]} = public_key:pem_to_der(PrivateKeyPath),
{ok, PrivateKey} = public_key:decode_private_key(Info),
base64:encode_to_string(public_key:sign(list_to_binary(BaseString), PrivateKey)).
@@ -0,0 +1,26 @@
-module(oauth_signature).

-export([value/5, base_string/3, method_to_string/1]).


value(HttpMethod, URL, Params, Consumer, TokenSecret) ->
value(base_string(HttpMethod, URL, Params), Consumer, TokenSecret).

value(_, {_, CS, plaintext}, TS) ->
oauth_plaintext:signature(CS, TS);
value(BaseString, {_, CS, hmac_sha1}, TS) ->
oauth_hmac_sha1:signature(BaseString, CS, TS);
value(BaseString, {_, _, {rsa_sha1, PrivateKey}}, _) ->
oauth_rsa_sha1:signature(BaseString, PrivateKey).

base_string(HttpMethod, URL, Params) ->
NormalizedURL = oauth_uri:normalize(URL),
NormalizedParams = oauth_uri:params_to_string(lists:sort(Params)),
oauth_uri:calate("&", [HttpMethod, NormalizedURL, NormalizedParams]).

method_to_string(plaintext) ->
"PLAINTEXT";
method_to_string(hmac_sha1) ->
"HMAC-SHA1";
method_to_string({rsa_sha1, _}) ->
"RSA-SHA1".

This file was deleted.

@@ -0,0 +1,16 @@
-module(oauth_unix).

-export([timestamp/0]).


timestamp() ->
timestamp(calendar:universal_time()).

timestamp(DateTime) ->
seconds(DateTime) - epoch().

epoch() ->
seconds({{1970,1,1},{00,00,00}}).

seconds(DateTime) ->
calendar:datetime_to_gregorian_seconds(DateTime).

0 comments on commit 4ab77bc

Please sign in to comment.