git.iko.soy/typeable/bloodhound
on master help sign in
Implement basic auth hook
commitfebfae7793c618c44edb9a850fd0c0fa388571d5 authorMichael Xavier <michael@michaelxavier.net> dateFri 4 Dec 2015, 09:18 (11 years ago) parent8801a3e
2 files changed+22
src/Database/Bloodhound/Client.hs+13 src/Database/Bloodhound/Types.hs+9
src/Database/Bloodhound/Client.hs+13
@@ -57,6 +57,8 @@ module Database.Bloodhound.Client
5757 , getStatus
5858 , encodeBulkOperations
5959 , encodeBulkOperation
60+ -- * Authentication
61+ , basicAuthHook
6062 -- * Reply-handling tools
6163 , isVersionConflict
6264 , isSuccess
@@ -767,3 +769,14 @@ isCreated = statusCheck (== 201)
767769
768770statusCheck :: (Int -> Bool) -> Reply -> Bool
769771statusCheck 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
262262 , TermsResult(..)
263263 , DateHistogramResult(..)
264264 , DateRangeResult(..)
265+
266+ , EsUsername(..)
267+ , EsPassword(..)
265268 ) where
266269
267270import Control.Applicative
@@ -3533,3 +3536,9 @@ instance Enum DocVersion where
35333536 fromEnum = docVersionNumber
35343537 enumFrom = boundedEnumFrom
35353538 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)