forked from jaked/ooauth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoauth_client.ml
194 lines (161 loc) · 5.39 KB
/
oauth_client.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
module type Lwt =
sig
type 'a _r
val bind : 'a _r -> ('a -> 'b _r) -> 'b _r
val return : 'a -> 'a _r
val fail : exn -> 'a _r
end
module Client (Lwt : Lwt) =
struct
module type Http_client =
sig
val request :
?http_method:[ `Get | `Head | `Post | `Delete | `Put of string ] ->
url:string ->
?headers:(string * string) list ->
?params:(string * string) list ->
?body:string * string -> (* content type * body *)
unit ->
(Nethttp.http_status * (string * string) list * string) Lwt._r
end
module Make (Http_client : Http_client) =
struct
exception Error of Nethttp.http_status * string
open Oauth_common
let (>>=) = Lwt.bind
let authorization_header
~oauth_version ~oauth_signature_method ~oauth_signature
~oauth_consumer_key ?oauth_token
~oauth_timestamp ~oauth_nonce
?oauth_callback ?oauth_verifier
() =
let params =
[
"OAuth realm", "";
"oauth_version", oauth_version;
"oauth_signature_method", string_of_signature_method oauth_signature_method;
"oauth_signature", oauth_signature;
"oauth_consumer_key", oauth_consumer_key;
"oauth_timestamp", string_of_timestamp oauth_timestamp;
"oauth_nonce", oauth_nonce;
] @
opt_param "oauth_token" oauth_token @
opt_param "oauth_callback" oauth_callback @
opt_param "oauth_verifier" oauth_verifier in
"Authorization",
(params |>
List.map (fun (k, v) -> k ^ "=\"" ^ String.escaped (rfc3986_encode v) ^ "\"") |>
String.concat ",")
let parse_response res =
try
let params = Netencoding.Url.dest_url_encoded_parameters res in
(List.assoc "oauth_token" params, List.assoc "oauth_token_secret" params)
with
| _ -> raise (Error (`Internal_server_error, "bad response: " ^ res))
let fetch_request_token
?(http_method = `Post) ~url
?(oauth_version = "1.0") ?(oauth_signature_method = `Hmac_sha1)
~oauth_consumer_key ~oauth_consumer_secret
?(oauth_timestamp = make_timestamp ()) ?(oauth_nonce = make_nonce ())
?(oauth_callback = "oob")
?params ?(headers = [])
() =
let oauth_signature =
sign
~http_method ~url
~oauth_version ~oauth_signature_method
~oauth_consumer_key ~oauth_consumer_secret
~oauth_timestamp ~oauth_nonce
~oauth_callback ?params
() in
let headers =
authorization_header
~oauth_version ~oauth_signature_method ~oauth_signature
~oauth_consumer_key
~oauth_timestamp ~oauth_nonce
~oauth_callback
() :: headers in
let res =
Http_client.request
~http_method
~url
~headers
?params
() in
res >>= (function
| (`Ok, _, res) -> Lwt.return (parse_response res)
| (status, _, res) -> Lwt.fail (Error (status, res)))
let fetch_access_token
?(http_method = `Post) ~url
?(oauth_version = "1.0") ?(oauth_signature_method = `Hmac_sha1)
~oauth_consumer_key ~oauth_consumer_secret
~oauth_token ~oauth_token_secret ~oauth_verifier
?(oauth_timestamp = make_timestamp ()) ?(oauth_nonce = make_nonce ())
?(headers = [])
() =
let oauth_signature =
sign
~http_method ~url
~oauth_version ~oauth_signature_method
~oauth_consumer_key ~oauth_consumer_secret
~oauth_token ~oauth_token_secret ~oauth_verifier
~oauth_timestamp ~oauth_nonce
() in
let headers =
authorization_header
~oauth_version ~oauth_signature_method ~oauth_signature
~oauth_consumer_key ~oauth_token ~oauth_verifier
~oauth_timestamp ~oauth_nonce
() :: headers in
let res =
Http_client.request
~http_method
~url
~headers
() in
res >>= (function
| (`Ok, _, res) -> Lwt.return (parse_response res)
| (status, _, res) -> Lwt.fail (Error (status, res)))
let access_resource
?(http_method = `Post) ~url
?(oauth_version = "1.0") ?(oauth_signature_method = `Hmac_sha1)
~oauth_consumer_key ~oauth_consumer_secret
?oauth_token ?oauth_token_secret
?(oauth_timestamp = make_timestamp ()) ?(oauth_nonce = make_nonce ())
?params ?(headers = []) ?body
() =
let oauth_signature =
sign
~http_method ~url
~oauth_version ~oauth_signature_method
~oauth_consumer_key ~oauth_consumer_secret
?oauth_token ?oauth_token_secret
~oauth_timestamp ~oauth_nonce
?params
() in
let headers =
authorization_header
~oauth_version ~oauth_signature_method ~oauth_signature
~oauth_consumer_key ?oauth_token
~oauth_timestamp ~oauth_nonce
() :: headers in
let res =
Http_client.request
~http_method
~url
~headers
?params
?body
() in
res >>= (function
| (`Ok, _, res) -> Lwt.return res
| (status, _, res) -> Lwt.fail (Error (status, res)))
end
end
module Sync = struct
type 'a _r = 'a
let bind a f = f a
let return a = a
let fail e = raise e
end
include Client(Sync)