oauth2.bend relies on unsafe/foreign
raw source on the hub · import bend-kit-oauth2@0.14.0.2/oauth2.bend as Oauth2
OAuth 2.0 client credentials, refresh, and authorization code with PKCE over Hairpin. Source: https://github.com/paymog/bend-kit/tree/main/oauth2
11 imports
import Base import bend-kit-crypto@0.2.2.2/crypto.bend as Crypto import bend-kit-hairpin@0.16.0.1/hairpin.bend as Hairpin import bend-kit-http@0.40.0.1/http.bend as Http import bend-kit-time@0.3.0.1/time.bend as Time import bend-kit-url@0.4.1.0/url.bend as Url import bend-kit-json@0.7.3.0/json.bend as Json import bend-kit-encoding@0.5.0.1/encoding.bend as Enc import bend-kit-bytes@0.3.3.1/bytes.bend as Bytes import ./pure.bend as Pure import bend-kit-time@0.3.0.2/pure.bend as PureTime
Types
type Config source · line 17 · raw
Data
RFC 6749 and RFC 7636. Text is a String of code points; form values go out as UTF-8. token: the token endpoint URL. secret: "" for a public client, which sends client_id in the body; a confidential client authenticates with HTTP Basic (RFC 6749 §2.3.1).
Config@token:String -> @id:String -> @secret:String -> Config
type Token source · line 22 · raw
Data
kind: token_type, as "Bearer". refresh: "" for none. scope: "" when the server did not say. expires: when the access token lapses, counted from just before the request; None without expires_in.
Token@access:String -> @kind:String -> @refresh:String -> @scope:String -> @expires:Maybe<&2, 0x0d2a146cd1cbaedf76cc532bcd3594b0/time.Instant> -> Token
type Err source · line 29 · raw
Data
ErrNet: the request did not complete. ErrOAuth: an RFC 6749 §5.2 error with its status, or a §4.1.2.1 error in a callback, status 0. ErrReply: not a Bearer token response, with its status and text. ErrState: the callback's state is not ours (§10.12). ErrCrypto: the random source or SHA-256 failed. ErrExpired: the token lapsed and has no refresh token.
ErrNet@e:0xd203401efed54307d469b1d5ad03842d/hairpin.Err -> Err
ErrOAuth@status:U32 -> @code:String -> @why:String -> Err
ErrReply@status:U32 -> @body:String -> Err
ErrStateErr
ErrCrypto@code:U32 -> @why:String -> Err
ErrExpiredErr
type Got source · line 37 · raw
Data
Got@access:String -> @kind:String -> @refresh:String -> @scope:String -> @expires:String -> @error:String -> @desc:String -> Got
Definitions
def instant.to.pure source · line 40 · raw
@v:0x0d2a146cd1cbaedf76cc532bcd3594b0/time.Instant -> 0xb11aca60dbb10f10d8f37cc3807477a2/pure.Instant
def instant.from.pure source · line 44 · raw
@v:0xb11aca60dbb10f10d8f37cc3807477a2/pure.Instant -> 0x0d2a146cd1cbaedf76cc532bcd3594b0/time.Instant
def expiry.to.pure source · line 48 · raw
@v:Maybe<&2, 0x0d2a146cd1cbaedf76cc532bcd3594b0/time.Instant> -> Maybe<&2, 0xb11aca60dbb10f10d8f37cc3807477a2/pure.Instant>
def expiry.from.pure source · line 55 · raw
@v:Maybe<&2, 0xb11aca60dbb10f10d8f37cc3807477a2/pure.Instant> -> Maybe<&2, 0x0d2a146cd1cbaedf76cc532bcd3594b0/time.Instant>
def config.to.pure source · line 62 · raw
@v:Config -> 0xcd3dd2c580e747a913a14883b4da826e/pure.Config
def token.to.pure source · line 66 · raw
@v:Token -> 0xcd3dd2c580e747a913a14883b4da826e/pure.Token
def token.from.pure source · line 70 · raw
@v:0xcd3dd2c580e747a913a14883b4da826e/pure.Token -> Token
def err.from.pure source · line 74 · raw
@v:0xcd3dd2c580e747a913a14883b4da826e/pure.Err -> Err
def result.token.from.pure source · line 83 · raw
@v:Result<&1, &1, 0xcd3dd2c580e747a913a14883b4da826e/pure.Err, 0xcd3dd2c580e747a913a14883b4da826e/pure.Token> -> Result<&1, &1, Err, Token>
def result.string.from.pure source · line 90 · raw
@v:Result<&1, &1, 0xcd3dd2c580e747a913a14883b4da826e/pure.Err, String> -> Result<&1, &1, Err, String>
def got.to.pure source · line 97 · raw
@v:Got -> 0xcd3dd2c580e747a913a14883b4da826e/pure.Got
def maybe.got.to.pure source · line 101 · raw
@v:Maybe<&2, Got> -> Maybe<&2, 0xcd3dd2c580e747a913a14883b4da826e/pure.Got>
def got.from.pure source · line 108 · raw
@v:0xcd3dd2c580e747a913a14883b4da826e/pure.Got -> Got
def maybe.got.from.pure source · line 112 · raw
@v:Maybe<&2, 0xcd3dd2c580e747a913a14883b4da826e/pure.Got> -> Maybe<&2, Got>
def b64url.ch source · line 122 · raw
@+c:U32 -> @t:String -> String
def b64url.go source · line 125 · raw
@s:String -> String
def b64url source · line 129 · raw
@b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> String
Base64url without padding (RFC 4648 §5, RFC 7636 Appendix A).
def utf8 source · line 134 · raw
@+s:String -> String
def field source · line 137 · raw
@m:Map<&2, String> -> @k:String -> @+v:String -> Map<&2, String>
def scoped source · line 140 · raw
@+scope:String -> @+m:Map<&2, String> -> Map<&2, String>
def form source · line 143 · raw
@+k:String -> @+v:String -> Map<&2, String>
def basic source · line 147 · raw
@+id:String -> @+secret:String -> String
RFC 6749 §2.3.1: id and secret are form-encoded before base64.
def auth.headers source · line 150 · raw
@+id:String -> @+secret:String -> Map<&2, List<&2, String>>
def auth.body source · line 154 · raw
@+id:String -> @+secret:String -> @+m:Map<&2, String> -> Map<&2, String>
def body source · line 158 · raw
@+id:String -> @+secret:String -> @m:Map<&2, String> -> String
The form body of a token request from cfg's client.
def got.put source · line 164 · raw
@g:Got -> @+k:String -> @+v:String -> Got
def got.text source · line 168 · raw
@v:0x419ec39802735c4664960ab4b9d962e5/json.Val -> String
A string's text, or a number's, as some servers send expires_in as "3600".
def got.go source · line 171 · raw
@kvs:List<&1, Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0x419ec39802735c4664960ab4b9d962e5/json.Val)> -> @g:Got -> Got
def got.obj source · line 174 · raw
@v:0x419ec39802735c4664960ab4b9d962e5/json.Val -> Maybe<&2, Got>
def got source · line 177 · raw
@m:Maybe<&1, 0x419ec39802735c4664960ab4b9d962e5/json.Val> -> Maybe<&2, Got>
def expiry source · line 180 · raw
@now:0x0d2a146cd1cbaedf76cc532bcd3594b0/time.Instant -> @m:Maybe<&2, U32> -> Maybe<&2, 0x0d2a146cd1cbaedf76cc532bcd3594b0/time.Instant>
def good source · line 184 · raw
@+status:U32 -> @g:Got -> Bool
A 2xx with an access token of type Bearer (RFC 6750 §4, case-insensitive), the only kind request sends.
def token.pick source · line 188 · raw
@ok:Bool -> @+status:U32 -> @+old:String -> @now:0x0d2a146cd1cbaedf76cc532bcd3594b0/time.Instant -> @b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @g:Got -> Result<&1, &1, Err, Token>
§6: a refresh reply without refresh_token keeps the old one. b: the body, decoded only for an error.
def token.of source · line 191 · raw
@+status:U32 -> @+old:String -> @now:0x0d2a146cd1cbaedf76cc532bcd3594b0/time.Instant -> @b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @m:Maybe<&2, Got> -> Result<&1, &1, Err, Token>
def parse.copy source · line 194 · raw
@+status:U32 -> @+old:String -> @now:0x0d2a146cd1cbaedf76cc532bcd3594b0/time.Instant -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes) -> Result<&1, &1, Err, Token>
def parse source · line 198 · raw
@+status:U32 -> @+old:String -> @now:0x0d2a146cd1cbaedf76cc532bcd3594b0/time.Instant -> @b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Result<&1, &1, Err, Token>
A token endpoint's reply at status, requested at now. old: the refresh token to keep when none comes back.
def reply source · line 201 · raw
@+old:String -> @now:0x0d2a146cd1cbaedf76cc532bcd3594b0/time.Instant -> @r:Result<&1, &1, 0xd203401efed54307d469b1d5ad03842d/hairpin.Err, 0x0a045079b0f843dd1eb3dfc9de50dbd6/http.Res> -> Result<&1, &1, Err, Token>
def grant.done source · line 209 · raw
@+old:String -> @now:0x0d2a146cd1cbaedf76cc532bcd3594b0/time.Instant -> @x:Pair(0xd203401efed54307d469b1d5ad03842d/hairpin.Client, Result<&1, &1, 0xd203401efed54307d469b1d5ad03842d/hairpin.Err, 0x0a045079b0f843dd1eb3dfc9de50dbd6/http.Res>) -> Pair(0xd203401efed54307d469b1d5ad03842d/hairpin.Client, Result<&1, &1, Err, Token>)
def grant source · line 215 · raw
@h:0xd203401efed54307d469b1d5ad03842d/hairpin.Client -> @cfg:Config -> @m:Map<&2, String> -> @+old:String -> IO(Pair(0xd203401efed54307d469b1d5ad03842d/hairpin.Client, Result<&1, &1, Err, Token>))
POST m to the token endpoint. Hairpin sends a POST once.
def client_credentials source · line 226 · raw
@h:0xd203401efed54307d469b1d5ad03842d/hairpin.Client -> @cfg:Config -> @+scope:String -> IO(Pair(0xd203401efed54307d469b1d5ad03842d/hairpin.Client, Result<&1, &1, Err, Token>))
RFC 6749 §4.4. scope: "" for the server's default.
def code source · line 230 · raw
@h:0xd203401efed54307d469b1d5ad03842d/hairpin.Client -> @cfg:Config -> @+code:String -> @+redirect:String -> @+verifier:String -> IO(Pair(0xd203401efed54307d469b1d5ad03842d/hairpin.Client, Result<&1, &1, Err, Token>))
RFC 6749 §4.1.3 with RFC 7636 §4.5: the code from callback, the same redirect_uri, and the verifier.
def refresh.go source · line 234 · raw
@none:Bool -> @h:0xd203401efed54307d469b1d5ad03842d/hairpin.Client -> @cfg:Config -> @+r:String -> @+scope:String -> IO(Pair(0xd203401efed54307d469b1d5ad03842d/hairpin.Client, Result<&1, &1, Err, Token>))
def refresh source · line 242 · raw
@h:0xd203401efed54307d469b1d5ad03842d/hairpin.Client -> @cfg:Config -> @tok:Token -> @+scope:String -> IO(Pair(0xd203401efed54307d469b1d5ad03842d/hairpin.Client, Result<&1, &1, Err, Token>))
RFC 6749 §6. scope: "" keeps the granted scope. ErrExpired when tok has no refresh token.
def expiring.at source · line 246 · raw
@e:Maybe<&2, 0x0d2a146cd1cbaedf76cc532bcd3594b0/time.Instant> -> @now:0x0d2a146cd1cbaedf76cc532bcd3594b0/time.Instant -> @+skew:U32 -> Bool
def expiring source · line 250 · raw
@tok:Token -> @now:0x0d2a146cd1cbaedf76cc532bcd3594b0/time.Instant -> @+skew:U32 -> Bool
Does tok lapse within skew seconds of now? A token without expires_in never does.
def fresh.go source · line 253 · raw
@due:Bool -> @h:0xd203401efed54307d469b1d5ad03842d/hairpin.Client -> @cfg:Config -> @+tok:Token -> IO(Pair(0xd203401efed54307d469b1d5ad03842d/hairpin.Client, Result<&1, &1, Err, Token>))
def fresh source · line 261 · raw
@h:0xd203401efed54307d469b1d5ad03842d/hairpin.Client -> @cfg:Config -> @+tok:Token -> @+skew:U32 -> IO(Pair(0xd203401efed54307d469b1d5ad03842d/hairpin.Client, Result<&1, &1, Err, Token>))
tok, or a refreshed one when it lapses within skew seconds.
def fresh.cc.go source · line 266 · raw
@due:Bool -> @h:0xd203401efed54307d469b1d5ad03842d/hairpin.Client -> @cfg:Config -> @+scope:String -> @+tok:Token -> IO(Pair(0xd203401efed54307d469b1d5ad03842d/hairpin.Client, Result<&1, &1, Err, Token>))
def fresh.cc source · line 274 · raw
@h:0xd203401efed54307d469b1d5ad03842d/hairpin.Client -> @cfg:Config -> @+scope:String -> @+tok:Token -> @+skew:U32 -> IO(Pair(0xd203401efed54307d469b1d5ad03842d/hairpin.Client, Result<&1, &1, Err, Token>))
tok, or a new client-credentials token when it lapses within skew seconds (§4.4.3: no refresh token).
def random.of source · line 281 · raw
@r:Result<&1, &1, Pair(U32, String), Pair(U32, Array<U32>)> -> Result<&1, &1, Err, String>
def random source · line 289 · raw
@+n:U32 -> IO(Result<&1, &1, Err, String>)
n octets from the OS secure random source, as base64url.
def verifier source · line 295 · raw
IO(Result<&1, &1, Err, String>)
§4.1: 32 random octets, 43 unreserved chars.
def state source · line 299 · raw
IO(Result<&1, &1, Err, String>)
An unguessable state for the authorization request (RFC 6749 §10.12).
def challenge.words source · line 302 · raw
@b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Result<&1, &1, Err, String>)
def challenge source · line 309 · raw
@+verifier:String -> IO(Result<&1, &1, Err, String>)
§4.2 S256: BASE64URL(SHA256(ASCII(verifier))).
def qmark source · line 312 · raw
@s:String -> Bool
def authorize.url source · line 316 · raw
@cfg:Config -> @+endpoint:String -> @+redirect:String -> @+scope:String -> @+state:String -> @+challenge:String -> String
RFC 6749 §4.1.1 with RFC 7636 §4.3: the URL to send the user to. endpoint may carry its own query.
def param.of source · line 319 · raw
@r:Pair(Map<&2, String>, String) -> String
def param source · line 322 · raw
@+m:Map<&2, String> -> @k:String -> String
def callback.pick source · line 325 · raw
@+err:String -> @desc:String -> @+got:String -> @+code:String -> @+state:String -> Result<&1, &1, Err, String>
def callback.m source · line 328 · raw
@+state:String -> @m:Maybe<&2, 0x1f2d80f53f971b16c6de6a65cb1918ae/url.Url> -> Result<&1, &1, Err, String>
def callback.frag source · line 331 · raw
@+state:String -> @r:Pair(String, String) -> Result<&1, &1, Err, String>
def callback.q source · line 334 · raw
@+state:String -> @r:Pair(String, String) -> Result<&1, &1, Err, String>
def callback source · line 338 · raw
@url:String -> @+state:String -> Result<&1, &1, Err, String>
§4.1.2: the code in the redirect to redirect_uri, once its state matches ours; the server's error if it sent one.
def bearer source · line 343 · raw
@tok:Token -> @h:Map<&2, List<&2, String>> -> Map<&2, List<&2, String>>
def request source · line 348 · raw
@c:0xd203401efed54307d469b1d5ad03842d/hairpin.Client -> @tok:Token -> @+method:String -> @url:String -> @headers:Map<&2, List<&2, String>> -> @body:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> IO(Pair(0xd203401efed54307d469b1d5ad03842d/hairpin.Client, Result<&1, &1, 0xd203401efed54307d469b1d5ad03842d/hairpin.Err, 0x0a045079b0f843dd1eb3dfc9de50dbd6/http.Res>))
Hairpin.request with Authorization: Bearer <access>, over any authorization header in headers.