Last active
January 13, 2020 02:14
-
-
Save pbiggar/ac62e48a5faa4b864b19eee1fee7b7f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
collect_params : (string, string) list -> string | |
let collect_params params = | |
params | |
(* Collecting parameters #1 - percent encoding *) | |
|> List.map (\k, v -> (pct_encode_key k, pct_encode v)) | |
(* Collecting parameters #2 - sorted*) | |
(* TODO sort lexicographically? *) | |
|> List.sort (\(a, _) (b, _) -> compare a b) | |
(* Collecting parameters #3-6 - combining with a = *) | |
|> List.map (\(a,b) -> a ++ "=" ++ b) | |
(* Collecting parameters #7 - combining all paramss with a & *) | |
|> String.concat "&" | |
(* TODO: no body? *) | |
let sign consumer_secret access_token_secret uri verb params = | |
(* https://dev.twitter.com/oauth/overview/creating-signatures *) | |
(* Collecting parameters *) | |
let collected = collect_params params in | |
(* Creating the signature base string - #1-5 *) | |
let output = | |
String::uppercase verb ++ "&" ^ Twitter::urlencode uri ++ "&" ++ Twitter::urlencode collected | |
in | |
(* Getting a signing key - #1-5*) | |
let signing_key = | |
Twitter::urlencode consumer_secret ++ "&" ++ Twitter::urlencode access_token_secret | |
in | |
output | |
|> Cryptop::sha1mac | |
|> Bytes::base64Encode | |
param_to_string = (string, string) -> string | |
let param_to_string (key, value) = | |
Twitter::urlencode key ++ "=\"" ++ Twitter::urlencode value ++ "\"" | |
type auth = | |
{ consumer_key : string | |
; consumer_secret : string | |
; access_token : string | |
; access_token_secret : string } | |
oauth_params : auth -> string -> string -> (string, string) list -> (string, string) list | |
let oauth_params auth url verb args = | |
let initial_params = | |
[ ("oauth_consumer_key", auth.consumer_key) | |
; ("oauth_nonce", String::random 42) | |
; ("oauth_signature_method", "HMAC-SHA1") | |
; ("oauth_version", "1.0") | |
; ("oauth_timestamp", Date::now |> Date::toSeconds |> toString) | |
; ("oauth_token", auth.access_token) ] | |
in | |
let signature = | |
sign | |
auth.consumer_secret | |
auth.access_token_secret | |
url | |
verb | |
(List.append initial_params args) | |
in | |
[("oauth_signature", signature)] | |
|> List.append initial_params | |
|> List.sort (\(a, _) (b, _) -> compare a b) | |
oauth_header : string -> string -> string -> (string, string) list -> string | |
let oauth_header secret url verb args = | |
oauth_params secret url verb args | |
|> List.map param_to_string | |
|> String.concat ", " | |
|> (++) "OAuth " | |
authorization_header : auth -> string -> string -> (string, string) list -> string | |
let authorization_header auth url verb args = | |
oauth_header auth url verb args |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment