async-refresh-tokens

Package implementing core logic for refreshing of expiring access tokens https://github.com/mtesseract/async-refresh-tokens#readme

Version on this page:0.3.0.1
LTS Haskell 15.15:0.4.0.0
Stackage Nightly 2020-06-05:0.4.0.0
Latest on Hackage:0.4.0.0

See all snapshots async-refresh-tokens appears in

BSD-3-Clause licensed by Moritz Schulte
Maintained by [email protected]

Module documentation for 0.3.0.1

This version can be pinned in stack with:[email protected]:9cce86b4d2e972b25d1052c464d656e20f7b3933d416f9a275db05fc89dd99d0,2102

async-refresh-tokens Hackage version Stackage version Build Status

About

This is Haskell library built on top of the async-refresh package implementing the logic for refreshing of expiring access tokens.

Usage

  • Create new token types.

  • Make the tokens be instances of the IsToken type classes by defining the tokenScopes method and (optionally) tokenName (a human readable label for this token).

  • Use newEmptyTokenStore to create a new token stores (token stores are basically TVars containing the tokens wrapped in Either SomeException).

  • Create a new configuration by adjusting defaultTokenConf using the functions tokenConfAddRequest and tokenConfSetFactor. The function tokenConfAddRequest expects values of type RequestToken — these values encapsulate the token stores together with a token-refreshing action.

  • Use newTokenRefresher to initiate token refreshing for each registered token refreshing request.

  • To use the current token, extract it from the TVar using readTVar (and pattern matching on Right).

Example

{-# LANGUAGE OverloadedStrings   #-}

data TokenFoo

instance IsToken TokenFoo where
  tokenScopes _ = ["foo.read", "foo.write"]

createTokenStoreFoo :: LoggingT IO (TokenStore TokenFoo)
createTokenStoreFoo = do
  tokenFoo <- newEmptyTokenStore (Proxy :: Proxy TokenFoo)
  let conf = defaultTokenConf
             & tokenConfAddRequest (RequestToken tokenFoo actionFoo)
  _ <- newTokenRefresher conf
  return tokenFoo

  where actionFoo :: (MonadIO m, IsToken t) => m (RefreshResult (Token t))
        actionFoo =
          return $ RefreshResult (Token "secret-foo-token") Nothing