PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_oauth/deps/erlang-oauth/src/oauth.erl

http://github.com/zotonic/zotonic
Erlang | 107 lines | 88 code | 19 blank | 0 comment | 0 complexity | f3307b19b501671e7f7f1eeacb6aac58 MD5 | raw file
  1-module(oauth).
  2
  3-export(
  4  [ get/5
  5  , header/1
  6  , post/5
  7  , signature/5
  8  , signature_base_string/3
  9  , signed_params/6
 10  , token/1
 11  , token_secret/1
 12  , uri/2
 13  , verify/6
 14  ]).
 15
 16
 17get(URL, ExtraParams, Consumer, Token, TokenSecret) ->
 18  SignedParams = signed_params("GET", URL, ExtraParams, Consumer, Token, TokenSecret),
 19  oauth_http:get(uri(URL, SignedParams)).
 20
 21post(URL, ExtraParams, Consumer, Token, TokenSecret) ->
 22  SignedParams = signed_params("POST", URL, ExtraParams, Consumer, Token, TokenSecret),
 23  oauth_http:post(URL, oauth_uri:params_to_string(SignedParams)).
 24
 25uri(Base, []) ->
 26  Base;
 27uri(Base, Params) ->
 28  lists:concat([Base, "?", oauth_uri:params_to_string(Params)]).
 29
 30header(Params) ->
 31  {"Authorization", "OAuth " ++ oauth_uri:params_to_header_string(Params)}.
 32
 33token(Params) ->
 34  proplists:get_value("oauth_token", Params).
 35
 36token_secret(Params) ->
 37  proplists:get_value("oauth_token_secret", Params).
 38
 39verify(Signature, HttpMethod, URL, Params, Consumer, TokenSecret) ->
 40  case signature_method(Consumer) of
 41    plaintext ->
 42      oauth_plaintext:verify(Signature, consumer_secret(Consumer), TokenSecret);
 43    hmac_sha1 ->
 44      BaseString = signature_base_string(HttpMethod, URL, Params),
 45      oauth_hmac_sha1:verify(Signature, BaseString, consumer_secret(Consumer), TokenSecret);
 46    rsa_sha1 ->
 47      BaseString = signature_base_string(HttpMethod, URL, Params),
 48      oauth_rsa_sha1:verify(Signature, BaseString, consumer_secret(Consumer))
 49  end.
 50
 51signed_params(HttpMethod, URL, ExtraParams, Consumer, Token, TokenSecret) ->
 52  Params = token_param(Token, params(Consumer, ExtraParams)),
 53  [{"oauth_signature", signature(HttpMethod, URL, Params, Consumer, TokenSecret)}|Params].
 54
 55signature(HttpMethod, URL, Params, Consumer, TokenSecret) ->
 56  case signature_method(Consumer) of
 57    plaintext ->
 58      oauth_plaintext:signature(consumer_secret(Consumer), TokenSecret);
 59    hmac_sha1 ->
 60      BaseString = signature_base_string(HttpMethod, URL, Params),
 61      oauth_hmac_sha1:signature(BaseString, consumer_secret(Consumer), TokenSecret);
 62    rsa_sha1 ->
 63      BaseString = signature_base_string(HttpMethod, URL, Params),
 64      oauth_rsa_sha1:signature(BaseString, consumer_secret(Consumer))
 65  end.
 66
 67signature_base_string(HttpMethod, URL, Params) ->
 68  NormalizedURL = oauth_uri:normalize(URL),
 69  NormalizedParams = oauth_uri:params_to_string(lists:sort(Params)),
 70  oauth_uri:calate("&", [HttpMethod, NormalizedURL, NormalizedParams]).
 71
 72token_param("", Params) ->
 73  Params;
 74token_param(Token, Params) ->
 75  [{"oauth_token", Token}|Params].
 76
 77params(Consumer, Params) ->
 78  Nonce = base64:encode_to_string(crypto:rand_bytes(32)), % cf. ruby-oauth
 79  params(Consumer, oauth_unix:timestamp(), Nonce, Params).
 80
 81params(Consumer, Timestamp, Nonce, Params) ->
 82  [ {"oauth_version", "1.0"}
 83  , {"oauth_nonce", Nonce}
 84  , {"oauth_timestamp", integer_to_list(Timestamp)}
 85  , {"oauth_signature_method", signature_method_string(Consumer)}
 86  , {"oauth_consumer_key", consumer_key(Consumer)}
 87  | Params
 88  ].
 89
 90signature_method_string(Consumer) ->
 91  case signature_method(Consumer) of
 92    plaintext ->
 93      "PLAINTEXT";
 94    hmac_sha1 ->
 95      "HMAC-SHA1";
 96    rsa_sha1 ->
 97      "RSA-SHA1"
 98  end.
 99
100signature_method(_Consumer={_, _, Method}) ->
101  Method.
102
103consumer_secret(_Consumer={_, Secret, _}) ->
104  Secret.
105
106consumer_key(_Consumer={Key, _, _}) ->
107  Key.