Haskell JWT generator quickstart
Create a token with your public API key in the sub claim, a near-term Unix timestamp in exp, and an HS256 signature made with your private API key.
Sapling JWTs use HS256 with a
sub claim containing your public API key and an exp claim containing a Unix timestamp. The examples use a one-hour lifetime; shorten it further when your application can refresh tokens easily.
Haskell
HS256 · sub + exp
This example uses the jwt package's Web.JWT module.
{-# LANGUAGE OverloadedStrings #-}
import Data.Time.Clock.POSIX (getPOSIXTime)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import System.Environment (getEnv)
import Web.JWT
main :: IO ()
main = do
publicKey <- getEnv "SAPLING_PUBLIC_KEY"
privateKey <- getEnv "SAPLING_PRIVATE_KEY"
now <- getPOSIXTime
let expiresAt = (round now :: Integer) + 3600
claims = mempty
{ sub = stringOrURI (T.pack publicKey)
, exp = numericDate (fromIntegral expiresAt)
}
token = encodeSigned
(hmacSecret (T.pack privateKey))
mempty
claims
T.putStrLn token
Generated token
text/plain
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.<base64url-claims>.<signature>
Return the compact token to the browser and pass it to Sapling.init as the key. Issue a fresh token after it expires; never send the private key.
About Haskell
Haskell is a popular programming language in industry and academia. It is purely functional, declarative, and statically typed.