Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow JWT as an authentication method, fixes #489 #497

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/GitHub/Auth.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module GitHub.Auth (
Auth (..),
Token,
JWTToken,
AuthMethod,
endpoint,
setAuthRequest
Expand All @@ -9,14 +11,17 @@ import GitHub.Internal.Prelude
import Prelude ()

import qualified Data.ByteString as BS
import qualified Data.Text.Encoding as TE
import qualified Network.HTTP.Client as HTTP

type Token = BS.ByteString
type JWTToken = Text

-- | The Github auth data type
data Auth
= BasicAuth BS.ByteString BS.ByteString -- ^ Username and password
| OAuth Token -- ^ OAuth token
| JWT JWTToken -- ^ JWT Token
| EnterpriseOAuth Text Token -- ^ Custom endpoint and OAuth token
deriving (Show, Data, Typeable, Eq, Ord, Generic)

Expand All @@ -40,10 +45,12 @@ instance AuthMethod () where
instance AuthMethod Auth where
endpoint (BasicAuth _ _) = Nothing
endpoint (OAuth _) = Nothing
endpoint (JWT _) = Nothing
endpoint (EnterpriseOAuth e _) = Just e

setAuthRequest (BasicAuth u p) = HTTP.applyBasicAuth u p
setAuthRequest (OAuth t) = setAuthHeader $ "token " <> t
setAuthRequest (JWT t) = setAuthHeader $ "Bearer " <> TE.encodeUtf8 t
setAuthRequest (EnterpriseOAuth _ t) = setAuthHeader $ "token " <> t

setAuthHeader :: BS.ByteString -> HTTP.Request -> HTTP.Request
Expand Down