basic-auth: config -> context
This commit is contained in:
parent
1e703be15f
commit
546adc391a
5 changed files with 23 additions and 23 deletions
|
@ -166,11 +166,11 @@ basicAuthHandler =
|
||||||
else return Unauthorized
|
else return Unauthorized
|
||||||
in BasicAuthCheck check
|
in BasicAuthCheck check
|
||||||
|
|
||||||
serverConfig :: Config '[ BasicAuthCheck () ]
|
serverContext :: Context '[ BasicAuthCheck () ]
|
||||||
serverConfig = basicAuthHandler :. EmptyConfig
|
serverContext = basicAuthHandler :. EmptyContext
|
||||||
|
|
||||||
basicAuthServer :: Application
|
basicAuthServer :: Application
|
||||||
basicAuthServer = serveWithConfig basicAuthAPI serverConfig (const (return alice))
|
basicAuthServer = serveWithContext basicAuthAPI serverContext (const (return alice))
|
||||||
|
|
||||||
{-# NOINLINE manager #-}
|
{-# NOINLINE manager #-}
|
||||||
manager :: C.Manager
|
manager :: C.Manager
|
||||||
|
|
|
@ -18,8 +18,8 @@ import Servant.Server (BasicAuthCheck (BasicAuthCheck),
|
||||||
BasicAuthResult( Authorized
|
BasicAuthResult( Authorized
|
||||||
, Unauthorized
|
, Unauthorized
|
||||||
),
|
),
|
||||||
Config ((:.), EmptyConfig), Server,
|
Context ((:.), EmptyContext), Server,
|
||||||
serveWithConfig)
|
serveWithContext)
|
||||||
|
|
||||||
-- | let's define some types that our API returns.
|
-- | let's define some types that our API returns.
|
||||||
|
|
||||||
|
@ -62,12 +62,12 @@ authCheck =
|
||||||
else return Unauthorized
|
else return Unauthorized
|
||||||
in BasicAuthCheck check
|
in BasicAuthCheck check
|
||||||
|
|
||||||
-- | We need to supply our handlers with the right configuration. In this case,
|
-- | We need to supply our handlers with the right Context. In this case,
|
||||||
-- Basic Authentication requires a Config Entry with the 'BasicAuthCheck' value
|
-- Basic Authentication requires a Context Entry with the 'BasicAuthCheck' value
|
||||||
-- tagged with "foo-tag" This config is then supplied to 'server' and threaded
|
-- tagged with "foo-tag" This context is then supplied to 'server' and threaded
|
||||||
-- to the BasicAuth HasServer handlers.
|
-- to the BasicAuth HasServer handlers.
|
||||||
serverConfig :: Config (BasicAuthCheck User ': '[])
|
serverContext :: Context (BasicAuthCheck User ': '[])
|
||||||
serverConfig = authCheck :. EmptyConfig
|
serverContext = authCheck :. EmptyContext
|
||||||
|
|
||||||
-- | an implementation of our server. Here is where we pass all the handlers to our endpoints.
|
-- | an implementation of our server. Here is where we pass all the handlers to our endpoints.
|
||||||
-- In particular, for the BasicAuth protected handler, we need to supply a function
|
-- In particular, for the BasicAuth protected handler, we need to supply a function
|
||||||
|
@ -80,7 +80,7 @@ server =
|
||||||
|
|
||||||
-- | hello, server!
|
-- | hello, server!
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = run 8080 (serveWithConfig api serverConfig server)
|
main = run 8080 (serveWithContext api serverContext server)
|
||||||
|
|
||||||
{- Sample session
|
{- Sample session
|
||||||
|
|
||||||
|
|
|
@ -457,19 +457,19 @@ instance HasServer api context => HasServer (HttpVersion :> api) context where
|
||||||
|
|
||||||
-- | Basic Authentication
|
-- | Basic Authentication
|
||||||
instance ( KnownSymbol realm
|
instance ( KnownSymbol realm
|
||||||
, HasServer api config
|
, HasServer api context
|
||||||
, HasConfigEntry config (BasicAuthCheck usr)
|
, HasContextEntry context (BasicAuthCheck usr)
|
||||||
)
|
)
|
||||||
=> HasServer (BasicAuth realm usr :> api) config where
|
=> HasServer (BasicAuth realm usr :> api) context where
|
||||||
|
|
||||||
type ServerT (BasicAuth realm usr :> api) m = usr -> ServerT api m
|
type ServerT (BasicAuth realm usr :> api) m = usr -> ServerT api m
|
||||||
|
|
||||||
route Proxy config subserver = WithRequest $ \ request ->
|
route Proxy context subserver = WithRequest $ \ request ->
|
||||||
route (Proxy :: Proxy api) config (subserver `addAuthCheck` authCheck request)
|
route (Proxy :: Proxy api) context (subserver `addAuthCheck` authCheck request)
|
||||||
where
|
where
|
||||||
realm = BC8.pack $ symbolVal (Proxy :: Proxy realm)
|
realm = BC8.pack $ symbolVal (Proxy :: Proxy realm)
|
||||||
basicAuthConfig = getConfigEntry config
|
basicAuthContext = getContextEntry context
|
||||||
authCheck req = runBasicAuth req realm basicAuthConfig
|
authCheck req = runBasicAuth req realm basicAuthContext
|
||||||
|
|
||||||
-- * helpers
|
-- * helpers
|
||||||
|
|
||||||
|
|
|
@ -56,8 +56,8 @@ errorOrderServer = \_ _ _ -> throwE err402
|
||||||
errorOrderSpec :: Spec
|
errorOrderSpec :: Spec
|
||||||
errorOrderSpec =
|
errorOrderSpec =
|
||||||
describe "HTTP error order" $
|
describe "HTTP error order" $
|
||||||
with (return $ serveWithConfig errorOrderApi
|
with (return $ serveWithContext errorOrderApi
|
||||||
(errorOrderAuthCheck :. EmptyConfig)
|
(errorOrderAuthCheck :. EmptyContext)
|
||||||
errorOrderServer
|
errorOrderServer
|
||||||
) $ do
|
) $ do
|
||||||
let badContentType = (hContentType, "text/plain")
|
let badContentType = (hContentType, "text/plain")
|
||||||
|
@ -183,8 +183,8 @@ errorRetryServer
|
||||||
errorRetrySpec :: Spec
|
errorRetrySpec :: Spec
|
||||||
errorRetrySpec =
|
errorRetrySpec =
|
||||||
describe "Handler search" $
|
describe "Handler search" $
|
||||||
with (return $ serveWithConfig errorRetryApi
|
with (return $ serveWithContext errorRetryApi
|
||||||
(errorOrderAuthCheck :. EmptyConfig)
|
(errorOrderAuthCheck :. EmptyContext)
|
||||||
errorRetryServer
|
errorRetryServer
|
||||||
) $ do
|
) $ do
|
||||||
|
|
||||||
|
|
|
@ -65,7 +65,7 @@ import Servant.Server.Internal.Router
|
||||||
(tweakResponse, runRouter,
|
(tweakResponse, runRouter,
|
||||||
Router, Router'(LeafRouter))
|
Router, Router'(LeafRouter))
|
||||||
import Servant.Server.Internal.Context
|
import Servant.Server.Internal.Context
|
||||||
(Context(..), NamedContext(..))
|
(NamedContext(..))
|
||||||
|
|
||||||
-- * comprehensive api test
|
-- * comprehensive api test
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue