-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoauth.xqy
257 lines (229 loc) · 8.64 KB
/
oauth.xqy
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
xquery version "1.0-ml";
module namespace oa="http://marklogic.com/ns/oauth";
declare namespace xh="xdmp:http";
declare default function namespace "http://www.w3.org/2005/xpath-functions";
declare option xdmp:mapping "false";
(:
let $service :=
<oa:service-provider realm="">
<oa:request-token>
<oa:uri>http://twitter.com/oauth/request_token</oa:uri>
<oa:method>GET</oa:method>
</oa:request-token>
<oa:user-authorization>
<oa:uri>http://twitter.com/oauth/authorize</oa:uri>
</oa:user-authorization>
<oa:user-authentication>
<oa:uri>http://twitter.com/oauth/authenticate</oa:uri>
<oa:additional-params>force_login=true</oa:additional-params>
</oa:user-authentication>
<oa:access-token>
<oa:uri>http://twitter.com/oauth/access_token</oa:uri>
<oa:method>POST</oa:method>
</oa:access-token>
<oa:signature-methods>
<oa:method>HMAC-SHA1</oa:method>
</oa:signature-methods>
<oa:oauth-version>1.0</oa:oauth-version>
<oa:authentication>
<oa:consumer-key>YOUR-CONSUMER-KEY</oa:consumer-key>
<oa:consumer-key-secret>YOUR-CONSUMER-SECRET</oa:consumer-key-secret>
</oa:authentication>
</oa:service-provider>
:)
declare function oa:timestamp() as xs:unsignedLong {
let $epoch := xs:dateTime('1970-01-01T00:00:00Z')
let $now := current-dateTime()
let $d := $now - $epoch
let $seconds
:= 86400 * days-from-duration($d)
+ 3600 * hours-from-duration($d)
+ 60 * minutes-from-duration($d)
+ seconds-from-duration($d)
return
xs:unsignedLong($seconds)
};
declare function oa:sign($key as xs:string, $data as xs:string) as xs:string {
let $uri := concat("http://localhost:8190/cgi-bin/hmac-sha1?",
"key=", encode-for-uri($key),
"&data=",encode-for-uri($data))
let $resp := xdmp:http-get($uri)
return
string($resp/digest/hashb64)
};
declare function oa:signature-method(
$service as element(oa:service-provider)
) as xs:string
{
if ($service/oa:signature-methods/oa:method = "HMAC-SHA1")
then "HMAC-SHA1"
else error(xs:QName("oa:BADSIGMETHOD"),
"Service must support 'HMAC-SHA1' signatures.")
};
declare function oa:http-method(
$proposed-method as xs:string
) as xs:string
{
if (upper-case($proposed-method) = "GET")
then "GET"
else if (upper-case($proposed-method) = "POST")
then "POST"
else error(xs:QName("oa:BADHTTPMETHOD"),
"Service must use HTTP GET or POST.")
};
declare function oa:request-token(
$service as element(oa:service-provider),
$callback as xs:string?)
as element(oa:request-token)
{
let $options := if (empty($callback))
then ()
else
<oa:options>
<oauth_callback>{$callback}</oauth_callback>
</oa:options>
let $data
:= oa:signed-request($service,
$service/oa:request-token/oa:method,
$service/oa:request-token/oa:uri,
$options, (), ())
return
<oa:request-token>
{ if ($data/oa:error)
then
$data/*
else
for $pair in tokenize($data, "&")
return
element { concat("oa:", substring-before($pair, '=')) }
{ substring-after($pair, '=') }
}
</oa:request-token>
};
declare function oa:access-token(
$service as element(oa:service-provider),
$request as element(oa:request-token),
$verifier as xs:string)
as element(oa:access-token)
{
let $options := <oa:options><oauth_verifier>{$verifier}</oauth_verifier></oa:options>
let $data
:= oa:signed-request($service,
$service/oa:access-token/oa:method,
$service/oa:access-token/oa:uri,
$options,
$request/oa:oauth_token,
$request/oa:oaauth_token_secret)
return
<oa:access-token>
{ if ($data/oa:error)
then
$data/*
else
for $pair in tokenize($data, "&")
return
element { concat("oa:", substring-before($pair, '=')) }
{ substring-after($pair, '=') }
}
</oa:access-token>
};
declare function oa:signed-request(
$service as element(oa:service-provider),
$method as xs:string,
$serviceuri as xs:string,
$options as element(oa:options)?,
$token as xs:string?,
$secret as xs:string?)
as element(oa:response)
{
let $realm := string($service/@realm)
let $noncei := xdmp:hash64(concat(current-dateTime(),string(xdmp:random())))
let $nonce := xdmp:integer-to-hex($noncei)
let $stamp := oa:timestamp()
let $key := string($service/oa:authentication/oa:consumer-key)
let $sigkey := concat($service/oa:authentication/oa:consumer-key-secret,
"&", if (empty($secret)) then "" else $secret)
let $version := string($service/oa:oauth-version)
let $sigmethod := oa:signature-method($service)
let $httpmethod := oa:http-method($method)
let $sigstruct
:= <oa:signature-base-string>
<oauth_consumer_key>{$key}</oauth_consumer_key>
<oauth_nonce>{$nonce}</oauth_nonce>
<oauth_signature_method>{$sigmethod}</oauth_signature_method>
<oauth_timestamp>{$stamp}</oauth_timestamp>
<oauth_version>{$version}</oauth_version>
{ if (not(empty($token)))
then <oauth_token>{$token}</oauth_token>
else ()
}
{ if (not(empty($options)))
then $options/*
else ()
}
</oa:signature-base-string>
let $encparams
:= for $field in $sigstruct/*
order by local-name($field)
return
concat(local-name($field), "=", encode-for-uri(string($field)))
let $sigbase := string-join(($httpmethod, encode-for-uri($serviceuri),
encode-for-uri(string-join($encparams,"&"))), "&")
let $signature := encode-for-uri(oa:sign($sigkey, $sigbase))
(: This is a bit of a pragmatic hack, what's the real answer? :)
let $authfields := $sigstruct/*[starts-with(local-name(.), "oauth_")
and not(self::oauth_callback)]
let $authheader := concat("OAuth realm="", $service/@realm, "", ",
"oauth_signature="", $signature, "", ",
string-join(
for $field in $authfields
return
concat(local-name($field),"="", encode-for-uri($field), """),
", "))
let $uriparam := for $field in $options/*
return
concat(local-name($field),"=",encode-for-uri($field))
(: This strikes me as slightly weird. Twitter wants the parameters passed
encoded in the URI even for a POST. I don't know if that's a Twitter
quirk or the natural way that OAuth apps work. Anyway, if you find
this library isn't working for some other OAuth'd API, you might want
to play with this bit.
let $requri := if ($httpmethod = "GET")
then concat($serviceuri,
if (empty($uriparam)) then ''
else concat("?",string-join($uriparam,"&")))
else $serviceuri
let $data := if ($httpmethod = "POST" and not(empty($uriparam)))
then <xh:data>{string-join($uriparam,"&")}</xh:data>
else ()
:)
let $requri := concat($serviceuri,
if (empty($uriparam)) then ''
else concat("?",string-join($uriparam,"&")))
let $data := ()
let $options := <xh:options>
<xh:headers>
<xh:Authorization>{$authheader}</xh:Authorization>
</xh:headers>
{ $data }
</xh:options>
let $tokenreq := if ($httpmethod = "GET")
then xdmp:http-get($requri, $options)
else xdmp:http-post($requri, $options)
(:
let $trace := xdmp:log(concat("requri: ", $requri))
let $trace := xdmp:log(concat("sigbse: ", $sigbase))
let $trace := xdmp:log($options)
let $trace := xdmp:log($tokenreq[2])
:)
return
<oa:response>
{ if (string($tokenreq[1]/xh:code) != "200")
then
(<oa:error>{$tokenreq[1]}</oa:error>,
<oa:error-body>{$tokenreq[2]}</oa:error-body>)
else
$tokenreq[2]
}
</oa:response>
};