The servant-auth-token-acid package

[Tags:bsd3, library]

Please see README.md


[Skip to Readme]

Properties

Versions 0.4.0.0, 0.4.1.0, 0.4.1.1
Change log CHANGELOG.md
Dependencies acid-state (==0.14.*), aeson-injector (==1.0.*), base (>=4.7 && <5), bytestring (==0.10.*), containers (==0.5.*), ghc-prim (==0.5.*), monad-control (==1.0.*), mtl (==2.2.*), safe (==0.3.*), safecopy (==0.9.*), servant-auth-token (==0.4.*), servant-auth-token-api (==0.4.*), servant-server (>=0.9 && <0.12), template-haskell (==2.11.*), text (==1.2.*), time (>=1.5 && <1.7), transformers (>=0.4 && <0.6), transformers-base (==0.4.*), uuid (==1.3.*) [details]
License BSD3
Copyright 2016 Anton Gushcha
Author NCrashed
Maintainer ncrashed@gmail.com
Category Web
Home page https://github.com/ncrashed/servant-auth-token#readme
Source repository head: git clone https://github.com/ncrashed/servant-auth-token
Uploaded Fri Jun 23 09:58:07 UTC 2017 by NCrashed
Distributions NixOS:0.4.1.1
Downloads 100 total (41 in the last 30 days)
Votes
0 []
Status Docs not available [build log]
Last success reported on 2017-06-23 [all 3 reports]
Hackage Matrix CI

Modules

  • Servant
    • Server
      • Auth
        • Token
          • Servant.Server.Auth.Token.Acid
            • Servant.Server.Auth.Token.Acid.Schema

Downloads

Maintainer's Corner

For package maintainers and hackage trustees

Readme for servant-auth-token-acid

Readme for servant-auth-token-acid-0.4.1.1

servant-auth-token-acid

Storage backend on acid for servant-auth-token server.

As acid-state is bad at composing states, the integration of the library in your project requires a massive TH mixins. The authentification state's queries are simply copied to your app.

First, define you own global acid-state type:

import Data.SafeCopy
import Servant.Server.Auth.Token.Acid.Schema as A

-- | Application global state for acid-state
data DB = DB {
  dbAuth :: A.Model -- ^ Storage for Auth state
, dbCustom :: () -- ^ Demo of custom state
}

-- | Generation of inital state
newDB :: DB
newDB = DB {
    dbAuth = A.newModel
  , dbCustom = ()
  }

-- | Extraction of Auth model from global state
instance HasModelRead DB where
  askModel = dbAuth

-- | Extraction of Auth model from global state
instance HasModelWrite DB where
  putModel db m = db { dbAuth = m }

deriveSafeCopy 0 'base ''DB

-- Mixin auth state queries and derive acid-state instances for them
A.deriveQueries ''DB
A.makeModelAcidic ''DB

Next, define your monad stack for the authorization actions:

-- Derive HasStorage for 'AcidBackendT' with your 'DB'.
-- It is important that it is come before the below newtype
deriveAcidHasStorage ''DB

-- | Special monad for authorisation actions
newtype AuthM a = AuthM { unAuthM :: AcidBackendT DB IO a }
  deriving (Functor, Applicative, Monad, MonadIO, MonadError ServantErr, HasAuthConfig, HasStorage)

-- | Execution of authorisation actions that require 'AuthHandler' context
runAuth :: AuthM a -> ServerM a
runAuth m = do
  cfg <- asks envAuthConfig
  db <- asks envDB
  liftHandler $ ExceptT $ runAcidBackendT cfg db $ unAuthM m

See a full example in servant-auth-token-example-acid.