BasicAuth should have a tag for config
Stop relying on the realm to be the tag.
This commit is contained in:
parent
95d994a5f5
commit
4865114330
3 changed files with 10 additions and 11 deletions
|
@ -42,7 +42,7 @@ type PrivateAPI = Get '[JSON] PrivateData
|
|||
|
||||
-- | our API
|
||||
type API = "public" :> PublicAPI
|
||||
:<|> "private" :> BasicAuth "foo-realm" User :> PrivateAPI
|
||||
:<|> "private" :> BasicAuth "foo-tag" "foo-realm" User :> PrivateAPI
|
||||
|
||||
-- | a value holding a proxy of our API type
|
||||
api :: Proxy API
|
||||
|
@ -63,10 +63,9 @@ authCheck =
|
|||
|
||||
-- | We need to supply our handlers with the right configuration. In this case,
|
||||
-- Basic Authentication requires a Config Entry with the 'BasicAuthCheck' value
|
||||
-- tagged with the realm that BasicAuth protects (in this case "foo-realm").
|
||||
-- This config is then supplied to 'server' and threaded to the BasicAuth HasServer
|
||||
-- handlers.
|
||||
serverConfig :: Config (ConfigEntry "foo-realm" (BasicAuthCheck User) ': '[])
|
||||
-- tagged with "foo-tag" This config is then supplied to 'server' and threaded
|
||||
-- to the BasicAuth HasServer handlers.
|
||||
serverConfig :: Config (ConfigEntry "foo-tag" (BasicAuthCheck User) ': '[])
|
||||
serverConfig = authCheck .: EmptyConfig
|
||||
|
||||
-- | an implementation of our server. Here is where we pass all the handlers to our endpoints.
|
||||
|
|
|
@ -484,16 +484,16 @@ instance HasServer api => HasServer (HttpVersion :> api) where
|
|||
|
||||
-- | Basic Authentication
|
||||
instance (KnownSymbol realm, HasServer api)
|
||||
=> HasServer (BasicAuth realm usr :> api) where
|
||||
type ServerT (BasicAuth realm usr :> api) m = usr -> ServerT api m
|
||||
type HasCfg (BasicAuth realm usr :> api) c
|
||||
= (HasConfigEntry c realm (BasicAuthCheck usr), HasCfg api c)
|
||||
=> HasServer (BasicAuth tag realm usr :> api) where
|
||||
type ServerT (BasicAuth tag realm usr :> api) m = usr -> ServerT api m
|
||||
type HasCfg (BasicAuth tag realm usr :> api) c
|
||||
= (HasConfigEntry c tag (BasicAuthCheck usr), HasCfg api c)
|
||||
|
||||
route Proxy cfg subserver = WithRequest $ \ request ->
|
||||
route (Proxy :: Proxy api) cfg (subserver `addAuthCheck` authCheck request)
|
||||
where
|
||||
realm = BC8.pack $ symbolVal (Proxy :: Proxy realm)
|
||||
baCfg = getConfigEntry (Proxy :: Proxy realm) cfg
|
||||
baCfg = getConfigEntry (Proxy :: Proxy tag) cfg
|
||||
authCheck req = runBasicAuth req realm baCfg
|
||||
|
||||
-- | General Authentication
|
||||
|
|
|
@ -17,7 +17,7 @@ import GHC.TypeLits (Symbol)
|
|||
-- In Basic Auth, username and password are base64-encoded and transmitted via
|
||||
-- the @Authorization@ header. Handshakes are not required, making it
|
||||
-- relatively efficient.
|
||||
data BasicAuth (realm :: Symbol) (usr :: *)
|
||||
data BasicAuth (tag :: k) (realm :: Symbol) (usr :: *)
|
||||
deriving (Typeable)
|
||||
|
||||
-- | A generalized Authentication combinator.
|
||||
|
|
Loading…
Reference in a new issue