git.iko.soy/typeable/bloodhound
on master help sign in
Implement basic auth hook
febfae7Michael Xavier
src/Database/Bloodhound/Client.hs+13
@@ -57,6 +57,8 @@ module Database.Bloodhound.Client
57 , getStatus
57 , getStatus
58 , encodeBulkOperations
58 , encodeBulkOperations
59 , encodeBulkOperation
59 , encodeBulkOperation
60+ -- * Authentication
61+ , basicAuthHook
60 -- * Reply-handling tools
62 -- * Reply-handling tools
61 , isVersionConflict
63 , isVersionConflict
62 , isSuccess
64 , isSuccess
@@ -767,3 +769,14 @@ isCreated = statusCheck (== 201)
767
769
768statusCheck :: (Int -> Bool) -> Reply -> Bool
770statusCheck :: (Int -> Bool) -> Reply -> Bool
769statusCheck prd = prd . NHTS.statusCode . responseStatus
771statusCheck prd = prd . NHTS.statusCode . responseStatus
772+
773+-- | This is a hook that can be set via the 'bhRequestHook' function
774+-- that will authenticate all requests using an HTTP Basic
775+-- Authentication header. Note that it is *strongly* recommended that
776+-- this option only be used over an SSL connection.
777+--
778+-- >> (mkBHEnv myServer myManager) { bhRequestHook = basicAuthHook (EsUsername "myuser") (EsPassword "mypass") }
779+basicAuthHook :: EsUsername -> EsPassword -> Request -> IO Request
780+basicAuthHook (EsUsername u) (EsPassword p) = return . applyBasicAuth u' p'
781+ where u' = T.encodeUtf8 u
782+ p' = T.encodeUtf8 p
src/Database/Bloodhound/Types.hs+9
@@ -262,6 +262,9 @@ module Database.Bloodhound.Types
262 , TermsResult(..)
262 , TermsResult(..)
263 , DateHistogramResult(..)
263 , DateHistogramResult(..)
264 , DateRangeResult(..)
264 , DateRangeResult(..)
265+
266+ , EsUsername(..)
267+ , EsPassword(..)
265 ) where
268 ) where
266
269
267import Control.Applicative
270import Control.Applicative
@@ -3533,3 +3536,9 @@ instance Enum DocVersion where
3533 fromEnum = docVersionNumber
3536 fromEnum = docVersionNumber
3534 enumFrom = boundedEnumFrom
3537 enumFrom = boundedEnumFrom
3535 enumFromThen = boundedEnumFromThen
3538 enumFromThen = boundedEnumFromThen
3539+
3540+-- | Username type used for HTTP Basic authentication. See 'basicAuthHook'.
3541+newtype EsUsername = EsUsername { esUsername :: Text } deriving (Show, Eq)
3542+
3543+-- | Password type used for HTTP Basic authentication. See 'basicAuthHook'.
3544+newtype EsPassword = EsPassword { esPassword :: Text } deriving (Show, Eq)