{-# LANGUAGE OverloadedStrings    #-}
{-# LANGUAGE TypeFamilies         #-}
{-# LANGUAGE TypeSynonymInstances #-}

-- | Basic Authentication for clients
module Servant.Client.Core.BasicAuth (
    basicAuthReq,
    ) where

import           Data.ByteString.Base64
                 (encode)
import           Data.Text.Encoding
                 (decodeUtf8)
import           Servant.API.BasicAuth
                 (BasicAuthData (BasicAuthData))
import           Servant.Client.Core.Request
                 (Request, addHeader)

-- | Authenticate a request using Basic Authentication
basicAuthReq :: BasicAuthData -> Request -> Request
basicAuthReq :: BasicAuthData -> Request -> Request
basicAuthReq (BasicAuthData ByteString
user ByteString
pass) Request
req =
    let authText :: Text
authText = ByteString -> Text
decodeUtf8 (ByteString
"Basic " forall a. Semigroup a => a -> a -> a
<> ByteString -> ByteString
encode (ByteString
user forall a. Semigroup a => a -> a -> a
<> ByteString
":" forall a. Semigroup a => a -> a -> a
<> ByteString
pass))
    in forall a. ToHttpApiData a => HeaderName -> a -> Request -> Request
addHeader HeaderName
"Authorization" Text
authText Request
req