some formatting and refactoring

This commit is contained in:
Sönke Hahn 2016-04-06 10:59:49 +08:00
parent 933a2c4445
commit 4224c20bff
4 changed files with 25 additions and 19 deletions

View File

@ -1,7 +1,7 @@
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
module Servant.Server.Internal.BasicAuth where module Servant.Server.Internal.BasicAuth where
@ -15,9 +15,9 @@ import GHC.Generics
import Network.HTTP.Types (Header) import Network.HTTP.Types (Header)
import Network.Wai (Request, requestHeaders) import Network.Wai (Request, requestHeaders)
import Servant.API.BasicAuth (BasicAuthData(BasicAuthData)) import Servant.API.BasicAuth (BasicAuthData(BasicAuthData))
import Servant.Server.Internal.RoutingApplication import Servant.Server.Internal.RoutingApplication
import Servant.Server.Internal.ServantErr import Servant.Server.Internal.ServantErr
-- * Basic Auth -- * Basic Auth

View File

@ -546,16 +546,16 @@ type BasicAuthAPI = BasicAuth "foo" () :> "basic" :> Get '[JSON] Animal
basicAuthApi :: Proxy BasicAuthAPI basicAuthApi :: Proxy BasicAuthAPI
basicAuthApi = Proxy basicAuthApi = Proxy
basicAuthServer :: Server BasicAuthAPI basicAuthServer :: Server BasicAuthAPI
basicAuthServer = const (return jerry) basicAuthServer = const (return jerry)
basicAuthContext :: Context '[ BasicAuthCheck () ] basicAuthContext :: Context '[ BasicAuthCheck () ]
basicAuthContext = basicAuthContext =
let basicHandler = BasicAuthCheck $ (\(BasicAuthData usr pass) -> let basicHandler = BasicAuthCheck $ \(BasicAuthData usr pass) ->
if usr == "servant" && pass == "server" if usr == "servant" && pass == "server"
then return (Authorized ()) then return (Authorized ())
else return Unauthorized else return Unauthorized
)
in basicHandler :. EmptyContext in basicHandler :. EmptyContext
basicAuthSpec :: Spec basicAuthSpec :: Spec
@ -564,10 +564,13 @@ basicAuthSpec = do
with (return (serveWithContext basicAuthApi basicAuthContext basicAuthServer)) $ do with (return (serveWithContext basicAuthApi basicAuthContext basicAuthServer)) $ do
context "Basic Authentication" $ do context "Basic Authentication" $ do
it "returns with 401 with bad password" $ do it "returns 401 with bad password" $ do
get "/basic" `shouldRespondWith` 401 get "/basic" `shouldRespondWith` 401
it "returns 200 with the right password" $ do it "returns 200 with the right password" $ do
THW.request methodGet "/basic" [("Authorization","Basic c2VydmFudDpzZXJ2ZXI=")] "" `shouldRespondWith` 200 let validCredentials = [("Authorization", "Basic c2VydmFudDpzZXJ2ZXI=")]
THW.request methodGet "/basic" validCredentials ""
`shouldRespondWith` 200
-- }}} -- }}}
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
@ -575,14 +578,16 @@ basicAuthSpec = do
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
type GenAuthAPI = AuthProtect "auth" :> "auth" :> Get '[JSON] Animal type GenAuthAPI = AuthProtect "auth" :> "auth" :> Get '[JSON] Animal
authApi :: Proxy GenAuthAPI authApi :: Proxy GenAuthAPI
authApi = Proxy authApi = Proxy
authServer :: Server GenAuthAPI authServer :: Server GenAuthAPI
authServer = const (return tweety) authServer = const (return tweety)
type instance AuthServerData (AuthProtect "auth") = () type instance AuthServerData (AuthProtect "auth") = ()
genAuthContext :: Context '[ AuthHandler Request () ] genAuthContext :: Context '[AuthHandler Request ()]
genAuthContext = genAuthContext =
let authHandler = (\req -> let authHandler = (\req ->
if elem ("Auth", "secret") (requestHeaders req) if elem ("Auth", "secret") (requestHeaders req)
@ -599,6 +604,7 @@ genAuthSpec = do
context "Custom Auth Protection" $ do context "Custom Auth Protection" $ do
it "returns 401 when missing headers" $ do it "returns 401 when missing headers" $ do
get "/auth" `shouldRespondWith` 401 get "/auth" `shouldRespondWith` 401
it "returns 200 with the right header" $ do it "returns 200 with the right header" $ do
THW.request methodGet "/auth" [("Auth","secret")] "" `shouldRespondWith` 200 THW.request methodGet "/auth" [("Auth","secret")] "" `shouldRespondWith` 200

View File

@ -1,12 +1,13 @@
{-# LANGUAGE DataKinds #-} {-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE KindSignatures #-} {-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-} {-# LANGUAGE PolyKinds #-}
module Servant.API.BasicAuth where module Servant.API.BasicAuth where
import Data.ByteString (ByteString) import Data.ByteString (ByteString)
import Data.Typeable (Typeable) import Data.Typeable (Typeable)
import GHC.TypeLits (Symbol) import GHC.TypeLits (Symbol)
-- | Combinator for <https://tools.ietf.org/html/rfc2617#section-2 Basic Access Authentication>. -- | Combinator for <https://tools.ietf.org/html/rfc2617#section-2 Basic Access Authentication>.

View File

@ -1,7 +1,7 @@
{-# LANGUAGE DataKinds #-} {-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE KindSignatures #-} {-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-} {-# LANGUAGE PolyKinds #-}
module Servant.API.Experimental.Auth where module Servant.API.Experimental.Auth where
import Data.Typeable (Typeable) import Data.Typeable (Typeable)
@ -11,4 +11,3 @@ import Data.Typeable (Typeable)
-- --
-- NOTE: THIS API IS EXPERIMENTAL AND SUBJECT TO CHANGE. -- NOTE: THIS API IS EXPERIMENTAL AND SUBJECT TO CHANGE.
data AuthProtect (tag :: k) deriving (Typeable) data AuthProtect (tag :: k) deriving (Typeable)