urbit/lib/oauth2.hoon

419 lines
12 KiB
Plaintext
Raw Normal View History

2016-04-07 23:07:21 +03:00
:: OAuth 2.0 %authorization
2016-03-04 23:27:54 +03:00
::
:::: /hoon/oauth2/lib
::
2017-10-24 01:06:38 +03:00
/+ hep-to-cab, interpolate, old-zuse
=, old-zuse
=, eyre
2016-12-03 03:56:54 +03:00
=, mimes:html
=, html
2017-10-24 01:06:38 +03:00
=, format
2016-01-23 06:06:46 +03:00
|%
++ parse-url parse-url:interpolate
2016-04-08 22:44:34 +03:00
++ join
2016-02-19 23:33:56 +03:00
|= {a/cord b/(list cord)}
2016-01-23 06:06:46 +03:00
?~ b ''
2016-01-26 01:49:38 +03:00
(rap 3 |-([i.b ?~(t.b ~ [a $(b t.b)])]))
2016-01-23 06:06:46 +03:00
::
++ post-quay
|= {a/purl b/quay} ^- hiss
=. b (quay:hep-to-cab b)
2017-10-21 00:13:34 +03:00
=- [a %post - ?~(b ~ (some (as-octt +:(tail:en-purl b))))]
%^ my
:+ %accept
'application/json'
~
:+ %content-type
'application/x-www-form-urlencoded'
~
~
::
2016-03-05 01:33:28 +03:00
++ mean-wall !.
|= {a/term b/tape} ^+ !!
=- (mean (flop `tang`[>a< -]))
(turn (to-wain (crip b)) |=(c/cord leaf+(trip c)))
2016-03-05 01:33:28 +03:00
::
2016-02-19 23:33:56 +03:00
++ bad-response |=(a/@u ?:(=(2 (div a 100)) | ~&(bad-httr+a &)))
2016-01-23 06:06:46 +03:00
++ grab-json
2017-10-21 00:13:34 +03:00
|* {a/httr b/fist:dejs-soft:format}
2016-02-19 23:33:56 +03:00
~| bad-json+r.a
~| (de-json q:(need r.a))
(need (;~(biff de-json b) q:(need r.a)))
2016-01-23 06:06:46 +03:00
--
::
2017-10-24 01:06:38 +03:00
::::
::
:: XX belongs back in zuse
|%
++ pack :: light path encoding
|= {a/term b/path} ^- knot
%+ rap 3 :- (wack a)
(turn b |=(c/knot (cat 3 '_' (wack c))))
::
++ pick :: light path decoding
=+ fel=(most cab (sear wick urt:ab))
|=(a/knot `(unit {p/term q/path})`(rush a fel))
::
--
::
2016-01-23 06:06:46 +03:00
::::
::
|%
2016-02-24 06:49:17 +03:00
++ token ?($~ @t)
++ refresh {tok/token expiry/@da pending/_`?`|}
++ both-tokens {token refresh}
2016-02-19 23:33:56 +03:00
++ keys cord:{cid/@t cis/@t}
2016-01-23 06:06:46 +03:00
--
::
::::
::
=+ state-usr=|
2017-10-21 00:13:34 +03:00
|_ {(bale:eyre keys) tok/token}
2016-03-05 01:33:28 +03:00
++ client-id cid:decode-keys
++ client-secret cis:decode-keys
2017-10-21 00:13:34 +03:00
++ decode-keys :: XX from bale:eyre w/ typed %jael
2016-03-05 01:33:28 +03:00
^- {cid/@t cis/@t $~}
?. =(~ `@`key)
~| %oauth-bad-keys
((hard {cid/@t cis/@t $~}) (to-wain key))
2016-03-05 01:33:28 +03:00
%+ mean-wall %oauth-no-keys
"""
Run |init-oauth2 {<`path`dom>}
2016-03-05 01:33:28 +03:00
If necessary, obtain client keys configured for a redirect_uri of
{(trip redirect-uri)}
"""
2016-01-23 06:06:46 +03:00
::
++ auth-url
|= {scopes/(list @t) url/$@(@t purl)} ^- purl
2016-04-08 03:44:43 +03:00
~& [%oauth-warning "Make sure this urbit ".
2017-10-21 00:13:34 +03:00
"is running on {(en-purl our-host `~ ~)}"]
%+ add-query:interpolate url
%- quay:hep-to-cab
:~ state+?.(state-usr '' (pack usr /''))
client-id+client-id
redirect-uri+redirect-uri
scope+(join ' ' scopes)
2016-01-23 06:06:46 +03:00
==
::
2017-10-21 00:13:34 +03:00
:: XX duplicated from eyre
++ pack :: light path encoding
|= {a/term b/path} ^- knot
%+ rap 3 :- (wack a)
(turn b |=(c/knot (cat 3 '_' (wack c))))
::
++ our-host .^(hart %e /(scot %p our)/host/fake)
++ redirect-uri
2017-10-21 00:13:34 +03:00
%- crip %- en-purl
2016-04-27 04:15:51 +03:00
%^ into-url:interpolate 'https://our-host/~/ac/:domain/:user/in'
`our-host
:~ domain+(join '.' (flop dom))
user+?:(state-usr '_state' (scot %ta usr))
==
2016-01-23 06:06:46 +03:00
::
::
++ request-token
|= {a/$@(@t purl) grant-type/cord quy/quay} ^- hiss
%+ post-quay (parse-url a)
2016-04-07 23:07:21 +03:00
%- quay:hep-to-cab
2016-01-23 06:06:46 +03:00
%+ welp quy
2016-02-19 23:33:56 +03:00
:~ client-id+client-id
client-secret+client-secret
redirect-uri+redirect-uri
grant-type+grant-type
2016-01-23 06:06:46 +03:00
==
::
++ request-token-by-code
|=({a/$@(@t purl) b/@t} (request-token a 'authorization_code' code+b ~))
::
++ grab-token
2016-04-08 04:30:52 +03:00
|= a/httr ^- axs/@t
2017-10-21 00:13:34 +03:00
(grab-json a (ot 'access_token'^so ~):dejs-soft:format)
::
2016-04-08 04:30:52 +03:00
++ grab-expiring-token
|= a/httr ^- {axs/@t exp/@u}
2017-10-21 00:13:34 +03:00
(grab-json a (ot 'access_token'^so 'expires_in'^ni ~):dejs-soft:format)
::
++ grab-both-tokens
2016-04-08 04:30:52 +03:00
|= a/httr ^- {axs/@t exp/@u ref/@t}
2017-10-21 00:13:34 +03:00
%+ grab-json a
=, dejs-soft:format
(ot 'access_token'^so 'expires_in'^ni 'refresh_token'^so ~)
::
++ auth
?~ tok ~|(%no-bearer-token !!)
|%
++ header `cord`(cat 3 'Bearer ' `@t`tok)
++ query `cord`tok
--
::
++ add-auth-header
|= request/{url/purl meth hed/math (unit octs)}
^+ request
:: =. p.url.request [| `6.000 [%& /localhost]] :: for use with unix nc
2017-10-21 00:13:34 +03:00
~& add-auth-header+(en-purl url.request)
request(hed (~(add ja hed.request) %authorization header:auth))
::
++ add-auth-query
|= {token-name/cord request/{url/purl meth math (unit octs)}}
^+ request
:: =. p.url.request [| `6.000 [%& /localhost]] :: for use with unix nc
2017-10-21 00:13:34 +03:00
~& add-auth-query+(en-purl url.request)
request(r.url [[token-name query:auth] r.url.request])
2016-01-27 04:44:14 +03:00
::
++ re
|_ ref/refresh
++ needs-refresh ?~(tok.ref | is-expired)
++ is-expired (lth expiry.ref (add now ~m5))
++ update
|= exp/@u ^+ ref
ref(pending |, expiry (add now (mul ~s1 exp)))
::
++ update-if-needed
|= exchange-url/$@(@t purl)
^- {(unit hiss) refresh}
?~ tok.ref `ref
?. is-expired `ref
:_ ref(pending &)
`(request-token exchange-url 'refresh_token' refresh-token+tok.ref ~)
--
::
:: expected semantics, to be copied and modified if anything doesn't work
++ standard
2016-04-08 03:44:43 +03:00
|* {done/* save/$-(token *)}
|%
2016-04-08 03:44:43 +03:00
++ save ^-($-(token _done) ^save) :: shadow(type canary)
++ core-move $^({sec-move _done} sec-move) :: stateful
::
2016-04-08 22:44:34 +03:00
:: Insert token into query string. expects:
:: ++ in (in-code-to-token 'http://...') :: handle callback
++ out-add-query-param
|= {token-name/knot scopes/(list cord) dialog/$@(@t purl)}
::
|= a/hiss ^- $%({$send hiss} {$show purl})
?~ tok [%show (auth-url scopes dialog)]
[%send (add-auth-query token-name a)]
::
2016-04-08 22:44:34 +03:00
:: Add token as a header. expects:
:: ++ in (in-code-to-token 'http://...') :: handle callback
++ out-add-header
|= {scopes/(list cord) dialog/$@(@t purl)}
::
|= a/hiss ^- sec-move
?~ tok [%show (auth-url scopes dialog)]
[%send (add-auth-header a)]
::
2016-04-08 22:44:34 +03:00
:: Exchange code in query string for access token. expects:
:: ++ bak bak-save-token :: save access token
++ in-code-to-token
|= exchange-url/$@(@t purl)
::
|= a/quay ^- sec-move
=+ code=~|(%no-code (~(got by (malt a)) %code))
[%send (request-token-by-code exchange-url code)]
::
2016-04-08 22:44:34 +03:00
:: If an access token has been returned, save it
++ bak-save-token
|= a/httr ^- core-move
2016-04-08 22:44:34 +03:00
?: (bad-response p.a)
[%give a] :: [%redo ~] :: handle 4xx?
[[%redo ~] (save `token`(grab-token a))]
--
::
++ standard-refreshing
2016-04-08 03:44:43 +03:00
|* {done/* ref/refresh save/$-({token refresh} *)}
=+ s=(standard done |=(tok/token (save tok ref)))
|%
2016-04-08 03:44:43 +03:00
++ save ^-($-(both-tokens _done) ^save) :: shadow(type canary)
++ core-move $^({sec-move _done} sec-move) :: stateful
::
:: See ++out-add-query-param:standard
2016-04-08 22:44:34 +03:00
:: Refresh token if we have an expired one, ask for authentication if none is present,
:: insert auth token into the query string if it's valid. expects:
:: ++ in (in-code-to-token 'http://...') :: handle callback
:: ++ res res-save-after-refresh
++ out-refresh-or-add-query-param
|= {exchange/$@(@t purl) s-args/{knot (list cord) $@(@t purl)}}
::
|= a/hiss ^- core-move
=^ upd ref (~(update-if-needed re ref) exchange)
?^ upd [[%send u.upd] (save tok ref)]
%.(a (out-add-query-param.s s-args))
::
:: See ++out-add-header:standard
2016-04-08 22:44:34 +03:00
:: Refresh token if we have an expired one, ask for authentication if none is present,
:: add token as a header if it's valid. expects:
:: ++ in (in-code-to-token 'http://...') :: handle callback
:: ++ res res-save-after-refresh
++ out-refresh-or-add-header
|= {exchange/$@(@t purl) s-args/{(list cord) dialog/$@(@t purl)}}
::
|= a/hiss ^- core-move
=^ upd ref (~(update-if-needed re ref) exchange)
?^ upd [[%send u.upd] (save tok ref)]
%.(a (out-add-header.s s-args))
2016-01-27 04:44:14 +03:00
::
2016-04-08 22:44:34 +03:00
:: If the last request refreshed the access token, save it.
++ res-save-after-refresh
|= a/httr ^- core-move
?. pending.ref [%give a]
2016-04-08 04:30:52 +03:00
=+ `{axs/token exp/@u}`(grab-expiring-token a)
2016-04-08 06:00:58 +03:00
=. ref (~(update re ref) exp)
[[%redo ~] (save axs ref)]
2016-01-27 04:44:14 +03:00
::
2016-04-08 22:44:34 +03:00
:: Exchange code in query string for access and refresh tokens. expects:
:: ++ bak bak-save-both-tokens :: save access token
++ in-code-to-token in-code-to-token.s
2016-04-08 22:44:34 +03:00
::
:: If valid access and refresh tokens have been returned, save them
++ bak-save-both-tokens
|= a/httr ^- core-move
2016-04-08 04:30:52 +03:00
=+ `{axs/token exp/@u ref-new/token}`(grab-both-tokens a)
=. tok.ref ref-new
=. ref (~(update re ref) exp)
[[%redo ~] (save axs ref)]
2016-01-27 04:44:14 +03:00
--
2016-01-23 06:06:46 +03:00
--
2016-04-08 06:00:58 +03:00
::
:: XX move-me
::
::
:::: Example "standard" sec/ core:
::
::
2016-04-08 22:44:34 +03:00
:: ::
2016-04-08 06:00:58 +03:00
:: :::: /hoon/my-api/com/sec
:: ::
:: /+ oauth2
:: ::
:: ::::
:: ::
2017-10-21 00:13:34 +03:00
:: |_ {bal/(bale:eyre keys:oauth2) tok/token:oauth2}
2016-04-08 06:00:58 +03:00
:: ++ aut (~(standard oauth2 bal tok) . |=(tok/token:oauth2 +>(tok tok)))
:: ++ out
:: %+ out-add-header:aut scope=/full
:: oauth-dialog='https://my-api.com/authorize'
:: ::
:: ++ in
:: %- in-code-to-token:aut
:: exchange-url='https://my-api.com/access_token'
:: ::
:: ++ bak bak-save-token:aut
:: --
::
::
:::: Equivalent imperative code:
::
::
2016-04-08 22:44:34 +03:00
:: ::
2016-04-08 06:00:58 +03:00
:: :::: /hoon/my-api/com/sec
:: ::
:: /+ oauth2
:: ::
:: ::::
:: ::
2017-10-21 00:13:34 +03:00
:: |_ {bal/(bale:eyre keys:oauth2) tok/token:oauth2}
2016-04-08 06:00:58 +03:00
:: ++ aut ~(. oauth2 bal tok)
:: ++ out :: add header
:: =+ aut
:: |= req/hiss ^- $%({$send hiss} {$show purl})
:: ?~ tok
:: [%show (auth-url scope=/full 'https://my-api.com/authorize')]
:: [%send (add-auth-header req)]
:: ::
:: ++ in :: code to token
:: =+ aut
:: |= inp/quay ^- {$send hiss}
:: =+ code=~|(%no-code (~(got by (malt inp)) %code))
:: [%send (request-token-by-code 'https://my-api.com/access_token' code)]
:: ::
:: ++ bak :: save token
:: =+ aut
:: |= bak/httr ^- $%({{$redo $~} _..bak} {$give httr})
:: ?: (bad-response bak) [%give bak]
:: =. tok (grab-token bak)
:: [[%redo ~] ..bak]
:: --
::
::: :::
::::: ::
::: :::
::
:::: Example "standard-refreshing" sec/ core:
::
::
2016-04-08 22:44:34 +03:00
:: ::
2016-04-08 06:00:58 +03:00
:: :::: /hoon/my-api/com/sec
:: ::
:: /+ oauth2
:: ::
:: ::::
:: ::
2017-10-21 00:13:34 +03:00
:: |_ {bal/(bale:eyre keys:oauth2) tok/token:oauth2 ref/refresh:oauth2}
2016-04-08 06:00:58 +03:00
:: ++ aut
:: %^ ~(standard-refreshing oauth2 bal tok) . ref
:: |=({tok/token ref/refresh}:oauth2 +>(tok tok, ref ref))
:: ::
:: ++ exchange-url 'https://my-api.com/access_token'
:: ++ out
:: %^ out-refresh-or-add-header:aut exchange-url
:: scope=/full
:: oauth-dialog='https://my-api.com/authorize'
:: ::
:: ++ res res-save-after-refresh:aut
:: ++ in (in-code-to-token:aut exchange-url)
:: ++ bak bak-save-both-tokens:aut
:: --
::
::
:::: Equivalent imperative code:
::
::
2016-04-08 22:44:34 +03:00
:: ::
2016-04-08 06:00:58 +03:00
:: :::: /hoon/my-api/com/sec
:: ::
:: /+ oauth2
:: ::
:: ::::
:: ::
2017-10-21 00:13:34 +03:00
:: |_ {bal/(bale:eyre keys:oauth2) axs/token:oauth2 ref/refresh:oauth2}
2016-04-08 06:00:58 +03:00
:: ++ aut ~(. oauth2 bal axs)
:: ++ exchange-url 'https://my-api.com/access_token'
:: ++ out :: refresh or add header
:: =+ aut
:: |= req/hiss ^- $^({{$send hiss} _..out} $%({$send hiss} {$show purl}))
:: ?~ axs
:: [%show (auth-url scope=/full 'https://my-api.com/authorize')]
:: =^ upd ref (~(update-if-needed re ref) exchange-url)
:: ?^ upd [[%send u.upd] ..out]
:: [%send (add-auth-header req)]
:: ::
:: ++ res :: save after refresh
:: =+ aut
:: |= a/httr ^- $^({{$redo $~} _..res} {$give httr})
:: ?. pending.ref [%give a]
:: =+ `{axs/token exp/@u}`(grab-expiring-token a)
:: [[%redo ~] ..out(axs axs, ref (~(update re ref) exp))]
:: ::
:: ++ in :: exchange token
:: =+ aut
:: |= inp/quay ^- {$send hiss}
:: =+ code=~|(%no-code (~(got by (malt inp)) %code))
:: [%send (request-token-by-code exchange-url code)]
::
:: ++ bak :: save both tokens
:: =+ aut
:: |= a/httr ^- {{$redo $~} _..res}
:: =+ `{axs/token exp/@u ref-new/token}`(grab-both-tokens a)
:: =. tok.ref ref-new
:: [[%redo ~] ..bak(axs axs, ref (~(update re ref) exp))]
:: ::
:: ::
:: ++ bak
:: =+ aut
:: |= bak/httr ^- $%({{$redo $~} _..bak} {$give httr})
:: ?: (bad-response bak) [%give bak]
:: =. tok (grab-token bak)
:: [[%redo ~] ..bak]
:: --
::