Auth

parses and builds HTTP authentication headers (RFC 7235): the Authorization and Proxy-Authorization credentials a client sends, and the WWW-Authenticate and Proxy-Authenticate challenges a server answers a 401 or 407 with. The Basic (RFC 7617) and Bearer (RFC 6750) schemes have dedicated helpers.

(match (Auth.parse "Basic YWxhZGRpbjpvcGVuIHNlc2FtZQ==")
  (Result.Success c)
    (match (Auth.basic-credentials &c)
      (Result.Success up) (println* (Pair.a &up) ":" (Pair.b &up))
      (Result.Error e) (IO.errorln &e))
  (Result.Error e) (IO.errorln &e))

basic

defn

(Fn [(Ref String a), b] (Result String String))

                        (basic user password)
                    

builds an RFC 7617 Basic Authorization value from user and password. Fails when user contains a colon, which the scheme cannot represent; a password may contain any number of them.

basic-challenge

defn

(Fn [(Ref String a)] String)

                        (basic-challenge realm)
                    

builds a Basic WWW-Authenticate challenge for realm.

basic-credentials

defn

(Fn [(Ref Credentials a)] (Result (Pair String String) String))

                        (basic-credentials c)
                    

decodes RFC 7617 Basic credentials into a user and password Pair, splitting on the first colon. Fails when the scheme is not Basic, the base64 does not decode, or the decoded value has no colon.

bearer

defn

(Fn [a] String)

                        (bearer token)
                    

builds an RFC 6750 Bearer Authorization value from token.

bearer-challenge

defn

(Fn [(Ref String a)] String)

                        (bearer-challenge realm)
                    

builds a Bearer WWW-Authenticate challenge for realm.

bearer-token

defn

(Fn [(Ref Credentials a)] (Result String String))

                        (bearer-token c)
                    

the token of RFC 6750 Bearer credentials. Fails when the scheme is not Bearer, or when the token is empty.

parse

defn

(Fn [(Ref String a)] (Result Credentials String))

                        (parse s)
                    

parses an Authorization or Proxy-Authorization value into the one set of credentials it may carry. Fails when there is no scheme, or when more than one scheme is present.

parse-challenges

defn

(Fn [(Ref String a)] (Array Credentials))

                        (parse-challenges s)
                    

parses a WWW-Authenticate or Proxy-Authenticate value into its challenges, in header order. One header value may carry several challenges, and a challenge may carry no params at all. Bytes that fit the grammar nowhere are dropped rather than rejected, so this never fails.