all repos

onasty @ f6a62ba

a one-time notes service

onasty/web/src/JwtUtil.elm(view raw)

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
module JwtUtil exposing (isExpired)

import Jwt
import Time


{-| Checks if a JWT token is expired or about to expire.
-}
isExpired : Time.Posix -> String -> Bool
isExpired now token =
    let
        expirationThreshold : number
        expirationThreshold =
            40 * 1000

        timeDiff : Int
        timeDiff =
            getTokenExpiration token
                |> (\expiration -> expiration - Time.posixToMillis now)
    in
    timeDiff <= expirationThreshold


{-| Extracts the expiration time (in millis) from a JWT token.
Returns 0 if cannot parse token.
-}
getTokenExpiration : String -> Int
getTokenExpiration token =
    Jwt.getTokenExpirationMillis token
        |> Result.withDefault 0