HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleservant-auth-server-0.4.9.0Haskell2010

Servant.Auth.Server

  • 15 types
  • 6 classes
  • 20 values

This package provides implementations for some common authentication methods. Authentication yields a trustworthy (because generated by the server) value of an some arbitrary type:

type MyApi = Protected

type Protected = Auth '[JWT, Cookie] User :> Get '[JSON] UserAccountDetails

server :: Server Protected
server (Authenticated usr) = ... -- here we know the client really is
                                 -- who she claims to be
server _ = throwAll err401

Additional configuration happens via Context.

Example for Custom Handler

To use a custom Servant.Server.Handler it is necessary to use Servant.Server.hoistServerWithContext instead of hoistServer and specify the Context.

Below is an example of passing CookieSettings and JWTSettings in the Context to create a specialized function equivalent to hoistServer for an API that includes cookie authentication.

hoistServerWithAuth
  :: HasServer api '[CookieSettings, JWTSettings]
  => Proxy api
  -> (forall x. m x -> n x)
  -> ServerT api m
  -> ServerT api n
hoistServerWithAuth api =
  hoistServerWithContext api (Proxy :: Proxy '[CookieSettings, JWTSettings])

Auth

3 declarations

Basic types

datadata Auth (auths :: [Type]) val
#

Auth [auth1, auth2] val :> api represents an API protected *either* by auth1 or auth2

Instances4HasLink, HasServer, MkLink, ServerT
datadata AuthResult val
#

The result of an authentication attempt.

Constructors

  • BadPassword
  • NoSuchUser
  • Authenticated val

    Authentication succeeded.

  • Indefinite

    If an authentication procedure cannot be carried out - if for example it expects a password and username in a header that is not present - Indefinite is returned. This indicates that other authentication methods should be tried.

Instances15Monad, Functor, Applicative, Foldable, Traversable, Alternative, …
newtypenewtype AuthCheck val
#

An AuthCheck is the function used to decide the authentication status (the AuthResult) of a request. Different AuthChecks may be combined as a Monoid or Alternative; the semantics of this is that the *first* non-Indefinite result from left to right is used and the rest are ignored.

Constructors

Instances13Monad, Functor, MonadFail, Applicative, Alternative, MonadPlus, …

JWT

0 declarations

JSON Web Tokens (JWT) are a compact and secure way of transferring information between parties. In this library, they are signed by the server (or by some other party posessing the relevant key), and used to indicate the bearer's identity or authorization.

Arbitrary information can be encoded - just declare instances for the FromJWT and ToJWT classes. Don't go overboard though - be aware that usually you'll be trasmitting this information on each request (and response!).

Note that, while the tokens are signed, they are not encrypted. Do not put any information you do not wish the client to know in them!

Combinator

Re-exported from 'servant-auth'

datadata JWT
#

A JSON Web Token (JWT) in the Authorization header:

Authorization: Bearer <token>

Note that while the token is signed, it is not encrypted. Therefore do not keep in it any information you would not like the client to know.

JWTs are described in IETF's RFC 7519

Instances2IsAuth, AuthArgs
  • FromJWT usr => IsAuth JWT usrDefined in servant-auth-server-0.4.9.0 · Servant.Auth.Server.Internal.Class
  • type AuthArgs JWT = '[JWTSettings]Defined in servant-auth-server-0.4.9.0 · Servant.Auth.Server.Internal.Class

Classes

classclass FromJWT a where
#

How to decode data from a JWT.

The default implementation assumes the data is stored in the unregistered dat claim, and uses the FromJSON instance to decode value from there.

classclass ToJWT a where
#

How to encode data from a JWT.

The default implementation stores data in the unregistered dat claim, and uses the type's ToJSON instance to encode the data.

Methods

Related types

datadata IsMatch
#
Instances6Eq, Ord, Read, Show, Generic, Rep
  • Eq IsMatchDefined in servant-auth-server-0.4.9.0 · Servant.Auth.Server.Internal.ConfigTypes
  • Ord IsMatchDefined in servant-auth-server-0.4.9.0 · Servant.Auth.Server.Internal.ConfigTypes
  • Read IsMatchDefined in servant-auth-server-0.4.9.0 · Servant.Auth.Server.Internal.ConfigTypes
  • Show IsMatchDefined in servant-auth-server-0.4.9.0 · Servant.Auth.Server.Internal.ConfigTypes
  • Generic IsMatchDefined in servant-auth-server-0.4.9.0 · Servant.Auth.Server.Internal.ConfigTypes
  • type Rep IsMatch = D1 ('MetaData "IsMatch" "Servant.Auth.Server.Internal.ConfigTypes" "servant-auth-server-0.4.9.0-CzaN2g7P12r5uHKHmTCqeh" 'False) (C1 ('MetaCons "Matches" 'PrefixI 'False) U1 :+: C1 ('MetaCons "DoesNotMatch" 'PrefixI 'False) U1)Defined in servant-auth-server-0.4.9.0 · Servant.Auth.Server.Internal.ConfigTypes

Settings

datadata JWTSettings
#

JWTSettings are used to generate cookies, and to verify JWTs.

Constructors

Instances2Generic, Rep

Create check

Cookie

0 declarations

Cookies are also a method of identifying and authenticating a user. They are particular common when the client is a browser

Combinator

Re-exported from 'servant-auth'

datadata Cookie
#

A cookie. The content cookie itself is a JWT. Another cookie is also used, the contents of which are expected to be send back to the server in a header, for XSRF protection.

Instances2IsAuth, AuthArgs

Settings

datadata CookieSettings
#

The policies to use when generating cookies.

If *both* cookieMaxAge and cookieExpires are Nothing, browsers will treat the cookie as a *session cookie*. These will be deleted when the browser is closed.

Note that having the setting Secure may cause testing failures if you are not testing over HTTPS.

Constructors

Instances5Eq, Show, Generic, Default, Rep
datadata XsrfCookieSettings
#

The policies to use when generating and verifying XSRF cookies

Constructors

Instances5Eq, Show, Generic, Default, Rep
valueacceptLogin
  1. :: (ToJWT session, AddHeader mods "Set-Cookie" SetCookie response withOneCookie, AddHeader mods "Set-Cookie" SetCookie withOneCookie withTwoCookies)
  2. => CookieSettings
  3. -> JWTSettings
  4. -> session
  5. -> IO (Maybe (response -> withTwoCookies))
#

For a JWT-serializable session, returns a function that decorates a provided response object with XSRF and session cookies. This should be used when a user successfully authenticates with credentials.

Related types

datadata IsSecure
#

Was this request made over an SSL connection?

Note that this value will not tell you if the client originally made this request over SSL, but rather whether the current connection is SSL. The distinction lies with reverse proxies. In many cases, the client will connect to a load balancer over SSL, but connect to the WAI handler without SSL. In such a case, the handlers would get NotSecure, but from a user perspective, there is a secure connection.

Constructors

  • Secure

    the connection to the server is secure (HTTPS)

  • NotSecure

    the connection to the server is not secure (HTTP)

Instances10Eq, Ord, Read, Show, Generic, HasLink, …
datadata SameSite
#
Instances6Eq, Ord, Read, Show, Generic, Rep
  • Eq SameSiteDefined in servant-auth-server-0.4.9.0 · Servant.Auth.Server.Internal.ConfigTypes
  • Ord SameSiteDefined in servant-auth-server-0.4.9.0 · Servant.Auth.Server.Internal.ConfigTypes
  • Read SameSiteDefined in servant-auth-server-0.4.9.0 · Servant.Auth.Server.Internal.ConfigTypes
  • Show SameSiteDefined in servant-auth-server-0.4.9.0 · Servant.Auth.Server.Internal.ConfigTypes
  • Generic SameSiteDefined in servant-auth-server-0.4.9.0 · Servant.Auth.Server.Internal.ConfigTypes
  • type Rep SameSite = D1 ('MetaData "SameSite" "Servant.Auth.Server.Internal.ConfigTypes" "servant-auth-server-0.4.9.0-CzaN2g7P12r5uHKHmTCqeh" 'False) (C1 ('MetaCons "AnySite" 'PrefixI 'False) U1 :+: (C1 ('MetaCons "SameSiteStrict" 'PrefixI 'False) U1 :+: C1 ('MetaCons "SameSiteLax" 'PrefixI 'False) U1))Defined in servant-auth-server-0.4.9.0 · Servant.Auth.Server.Internal.ConfigTypes

BasicAuth

0 declarations

Combinator

Re-exported from 'servant-auth'

Classes

classclass FromBasicAuthData a where
#

Methods

  • fromBasicAuthData :: BasicAuthData -> BasicAuthCfg -> IO (AuthResult a)

    Whether the username exists and the password is correct. Note that, rather than passing a Pass to the function, we pass a function that checks an EncryptedPass. This is to make sure you don't accidentally do something untoward with the password, like store it.

Settings

familytype family BasicAuthCfg
#

A type holding the configuration for Basic Authentication. It is defined as a type family with no arguments, so that it can be instantiated to whatever type you need to authenticate your users (use type instance BasicAuthCfg = ...).

Note that the instantiation is application-wide, i.e. there can be only one instance. As a consequence, it should not be instantiated in a library.

Basic Authentication expects an element of type BasicAuthCfg to be in the Context; that element is then passed automatically to the instance of FromBasicAuthData together with the authentication data obtained from the client.

If you do not need a configuration for Basic Authentication, you can use just BasicAuthCfg = (), and recall to also add () to the Context. A basic but more interesting example is to take as BasicAuthCfg a list of authorised username/password pairs:

deriving instance Eq BasicAuthData
type instance BasicAuthCfg = [BasicAuthData]
instance FromBasicAuthData User where
  fromBasicAuthData authData authCfg =
    if elem authData authCfg then ...

Related types

datadata IsPasswordCorrect
#
Instances6Eq, Ord, Read, Show, Generic, Rep

Authentication request

Utilies

8 declarations
classclass ThrowAll a where
#

Methods

  • throwAll :: ServerError -> a

    throwAll is a convenience function to throw errors across an entire sub-API

    throwAll err400 :: Handler a :<|> Handler b :<|> Handler c
       == throwError err400 :<|> throwError err400 :<|> err400
Instances6ThrowAll
valuewriteKey :: FilePath -> IO ()
#

Writes a secret to a file. Can for instance be used from the REPL to persist a key to a file, which can then be included with the application. Restore the key using readKey.

Re-exports

classclass Default a where
#

A class for types with a default value.

Methods

  • def :: a

    The default value for this type.

Instances102Default, …
datadata SetCookie
#

Data type representing the key-value pair to use for a cookie, as well as configuration options for it.

Creating a SetCookie

SetCookie does not export a constructor; instead, use defaultSetCookie and override values (see http://www.yesodweb.com/book/settings-types for details):

import Web.Cookie
:set -XOverloadedStrings
let cookie = defaultSetCookie { setCookieName = "cookieName", setCookieValue = "cookieValue" }
Cookie Configuration

Cookies have several configuration options; a brief summary of each option is given below. For more information, see RFC 6265 or Wikipedia.

Instances6Eq, Show, NFData, Default, FromHttpApiData, ToHttpApiData
  • Eq SetCookieDefined in cookie-0.5.1 · Web.Cookie
  • Show SetCookieDefined in cookie-0.5.1 · Web.Cookie
  • NFData SetCookieDefined in cookie-0.5.1 · Web.Cookie
  • Default SetCookieDefined in cookie-0.5.1 · Web.Cookie
  • FromHttpApiData SetCookieDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData

    Note: this instance works correctly for alphanumeric name and value

    Example1 expression
    parseUrlPiece "SESSID=r2t5uvjq435r4q7ib3vtdjq120" :: Either Text SetCookieRight (SetCookie {setCookieName = "SESSID", setCookieValue = "r2t5uvjq435r4q7ib3vtdjq120", setCookiePath = Nothing, setCookieExpires = Nothing, setCookieMaxAge = Nothing, setCookieDomain = Nothing, setCookieHttpOnly = False, setCookieSecure = False, setCookieSameSite = Nothing})
  • ToHttpApiData SetCookieDefined in http-api-data-0.6.1 · Web.Internal.HttpApiData

    Note: this instance works correctly for alphanumeric name and value

    Example2 expressions
    let Right c = parseUrlPiece "SESSID=r2t5uvjq435r4q7ib3vtdjq120" :: Either Text SetCookietoUrlPiece c"SESSID=r2t5uvjq435r4q7ib3vtdjq120"
    Example1 expression
    toHeader c"SESSID=r2t5uvjq435r4q7ib3vtdjq120"