Package implementing core logic for refreshing of expiring access tokens.
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).
async-refresh-tokens

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
IsTokentype classes by defining thetokenScopesmethod and (optionally)tokenName(a human readable label for this token).Use
newEmptyTokenStoreto create a new token stores (token stores are basicallyTVars containing the tokens wrapped inEither SomeException).Create a new configuration by adjusting
defaultTokenConfusing the functionstokenConfAddRequestandtokenConfSetFactor. The functiontokenConfAddRequestexpects values of typeRequestToken— these values encapsulate the token stores together with a token-refreshing action.Use
newTokenRefresherto initiate token refreshing for each registered token refreshing request.To use the current token, extract it from the
TVarusingreadTVar(and pattern matching onRight).
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