module GitHub.Auth (
Auth (..),
AuthMethod,
endpoint,
setAuthRequest
) where
import GitHub.Internal.Prelude
import Prelude ()
import qualified Data.ByteString as BS
import qualified Network.HTTP.Client as HTTP
type Token = BS.ByteString
data Auth
= BasicAuth BS.ByteString BS.ByteString
| OAuth Token
| EnterpriseOAuth Text Token
deriving (Show, Data, Typeable, Eq, Ord, Generic)
instance NFData Auth where rnf = genericRnf
instance Binary Auth
instance Hashable Auth
class AuthMethod a where
endpoint :: a -> Maybe Text
setAuthRequest :: a -> HTTP.Request -> HTTP.Request
instance AuthMethod Auth where
endpoint (BasicAuth _ _) = Nothing
endpoint (OAuth _) = Nothing
endpoint (EnterpriseOAuth e _) = Just e
setAuthRequest (BasicAuth u p) = HTTP.applyBasicAuth u p
setAuthRequest (OAuth t) = setAuthHeader $ "token " <> t
setAuthRequest (EnterpriseOAuth _ t) = setAuthHeader $ "token " <> t
setAuthHeader :: BS.ByteString -> HTTP.Request -> HTTP.Request
setAuthHeader auth req =
req { HTTP.requestHeaders = ("Authorization", auth) : HTTP.requestHeaders req }