servant-auth-token-acid: Acid-state backend for servant-auth-token server

[ bsd3, library, web ] [ Propose Tags ] [ Report a vulnerability ]

Please see README.md


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.4.0.0, 0.4.1.0, 0.4.1.1, 0.5.0.0, 0.5.1.0, 0.5.2.0, 0.5.3.0, 0.5.3.1, 0.5.4.0
Change log CHANGELOG.md
Dependencies acid-state (>=0.14 && <0.15), aeson-injector (>=1.0 && <1.2), base (>=4.7 && <5), bytestring (>=0.10 && <0.11), containers (>=0.5 && <0.6), ghc-prim (>=0.5 && <0.6), monad-control (>=1.0 && <1.1), mtl (>=2.2 && <2.3), safe (>=0.3 && <0.4), safecopy (>=0.9 && <0.10), servant-auth-token (>=0.5 && <0.6), servant-auth-token-api (>=0.5 && <0.6), servant-server (>=0.9 && <0.15), template-haskell (>=2.11 && <2.14), text (>=1.2 && <1.3), time (>=1.5 && <1.9), transformers (>=0.4 && <0.6), transformers-base (>=0.4 && <0.5), uuid (>=1.3 && <1.4) [details]
License BSD-3-Clause
Copyright 2016 Anton Gushcha
Author NCrashed
Maintainer ncrashed@gmail.com
Category Web
Home page https://github.com/ncrashed/servant-auth-token#readme
Source repo head: git clone https://github.com/ncrashed/servant-auth-token
Uploaded by NCrashed at 2018-09-13T17:41:44Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 4689 total (30 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-09-13 [all 1 reports]

Readme for servant-auth-token-acid-0.5.4.0

[back to package description]

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.