async-refresh-tokens: Package implementing core logic for refreshing of expiring access tokens

[ bsd3, control, library ] [ Propose Tags ]

This package can be used for renewal of expiring access tokens according to user-provided actions. Tokens will be stored in a transactional variable (TVar).

[Skip to Readme]




Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Versions [RSS] 0.1.0,,,,
Dependencies async-refresh (>=, base (>=4.7 && <5), bytestring, formatting, microlens (>=0.4), microlens-th (>=0.4), monad-logger, safe-exceptions, text, unliftio, unliftio-core [details]
License BSD-3-Clause
Copyright (c) 2017, 2018 Moritz Clasmeier
Author Moritz Clasmeier
Category Control
Home page
Bug tracker
Source repo head: git clone
Uploaded by mtesseract at 2018-03-21T11:58:06Z
Distributions LTSHaskell:, NixOS:, Stackage:
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 4066 total (20 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-03-21 [all 1 reports]

Readme for async-refresh-tokens-

[back to package description]

async-refresh-tokens Hackage version Stackage version Build Status


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


  • 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).


{-# LANGUAGE OverloadedStrings   #-}

data TokenFoo

instance IsToken TokenFoo where
  tokenScopes _ = ["", "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