Helper functions for Jwt tokens.
This package provides functions for reading tokens, and for using them to make authenticated Http requests.
`Checks whether a token has expired, and returns True or False, or any error that occurred while decoding the token.
Note: This function assumes that the expiry was set in seconds and thus multiplies by 1000 to compare with Javascript time (in milliseconds). You may need to write a custom version if your server-side Jwt library works differently.
authenticate is a standard Http POST method containing login credentials, and using
a json decoder to extract the returned token.
submitCredentials : Model -> Cmd Msg
submitCredentials model =
E.object
[ ("username", E.string model.uname)
, ("password", E.string model.pword)
]
|> authenticate Auth "/sessions" tokenStringDecodercreateRequest creates a Http.Request with the token added to the headers, and
sets the withCredentials field to True.
get is a replacement for Http.get that returns a Http.Request with the token
attached the headers.
Task.perform
PostFail PostSucess
(get token dataDecoder "/api/data")get_ takes the same parameters as get, but returns a Task where a 401 Unauthorized error
due to token expiry has been converted into a TokenExpired.
update ... =
GetResult (Result.Err err) ->
case err of
Jwt.TokenExpired ->
{ model | msg = "Token expired" } ! []
_ ->
{ model | msg = toString err } ! []
[...]
tryToken : String -> Cmd Msg
tryToken token =
Jwt.get_ token "/api/data" dataDecoder
|> Task.perform GetResultpost is a replacement for Http.post that attaches a provided Jwt token
to the headers.
put provides Http.put with the token attached to the headers.
Examines a 401 Unauthorized reponse, and converts the error to TokenExpired when that is the case.
getAuth : Token -> String -> Json.Decoder a -> Task Never (Result JwtError a)
getAuth token url dec =
createRequest "GET" token url dec Http.emptyBody
|> toTask
|> Task.map Result.Ok
|> Task.onError (promote401 token)We distinguish between various errors
promote401,
decodeToken parses the token, checking that it meets the Jwt standards.
In the event of success,
decodeTokenreturns an Elm record structure using the JSON Decoder.