The async-refresh-tokens package

[Tags:bsd3, library, test]

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]

Properties

Versions 0.1.0, 0.2.0.0, 0.3.0.0
Dependencies async-refresh (>=0.2.0.2), base (>=4.7 && <5), bytestring, formatting, lifted-async, microlens (>=0.4), microlens-th (>=0.4), monad-control, monad-logger, safe-exceptions, stm, text [details]
License BSD3
Copyright (c) 2017 Moritz Schulte
Author Moritz Schulte
Maintainer mtesseract@silverratio.net
Category Control
Home page https://github.com/mtesseract/async-refresh-tokens#readme
Bug tracker https://github.com/mtesseract/async-refresh-tokens/issues
Source repository head: git clone https://github.com/mtesseract/async-refresh-tokens
Uploaded Sun Jul 23 09:39:18 UTC 2017 by mtesseract
Distributions LTSHaskell:0.3.0.0, NixOS:0.3.0.0, Stackage:0.3.0.0, Tumbleweed:0.1.0
Downloads 164 total (59 in the last 30 days)
Votes
0 []
Status Docs available [build log]
Last success reported on 2017-07-23 [all 1 reports]
Hackage Matrix CI

Modules

[Index]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees

Readme for async-refresh-tokens

Readme for async-refresh-tokens-0.3.0.0

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