{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Yesod.Auth.OAuth2.GitHub
( oauth2GitHub
, oauth2GitHubWidget
, oauth2GitHubScoped
, oauth2GitHubScopedWidget
) where
import qualified Data.Text as T
import Yesod.Auth.OAuth2.Prelude
import Yesod.Core (WidgetFor, whamlet)
newtype User = User Int
instance FromJSON User where
parseJSON :: Value -> Parser User
parseJSON = String -> (Object -> Parser User) -> Value -> Parser User
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"User" ((Object -> Parser User) -> Value -> Parser User)
-> (Object -> Parser User) -> Value -> Parser User
forall a b. (a -> b) -> a -> b
$ \Object
o -> Int -> User
User (Int -> User) -> Parser Int -> Parser User
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser Int
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"
pluginName :: Text
pluginName :: Text
pluginName = Text
"github"
defaultScopes :: [Text]
defaultScopes :: [Text]
defaultScopes = [Text
"user:email"]
oauth2GitHub :: YesodAuth m => Text -> Text -> AuthPlugin m
oauth2GitHub :: forall m. YesodAuth m => Text -> Text -> AuthPlugin m
oauth2GitHub = [Text] -> Text -> Text -> AuthPlugin m
forall m. YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m
oauth2GitHubScoped [Text]
defaultScopes
oauth2GitHubWidget
:: YesodAuth m => WidgetFor m () -> Text -> Text -> AuthPlugin m
oauth2GitHubWidget :: forall m.
YesodAuth m =>
WidgetFor m () -> Text -> Text -> AuthPlugin m
oauth2GitHubWidget WidgetFor m ()
widget = WidgetFor m () -> [Text] -> Text -> Text -> AuthPlugin m
forall m.
YesodAuth m =>
WidgetFor m () -> [Text] -> Text -> Text -> AuthPlugin m
oauth2GitHubScopedWidget WidgetFor m ()
widget [Text]
defaultScopes
oauth2GitHubScoped :: YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m
oauth2GitHubScoped :: forall m. YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m
oauth2GitHubScoped =
WidgetFor m () -> [Text] -> Text -> Text -> AuthPlugin m
forall m.
YesodAuth m =>
WidgetFor m () -> [Text] -> Text -> Text -> AuthPlugin m
oauth2GitHubScopedWidget WidgetFor m ()
[whamlet|Login via #{pluginName}|]
oauth2GitHubScopedWidget
:: YesodAuth m => WidgetFor m () -> [Text] -> Text -> Text -> AuthPlugin m
oauth2GitHubScopedWidget :: forall m.
YesodAuth m =>
WidgetFor m () -> [Text] -> Text -> Text -> AuthPlugin m
oauth2GitHubScopedWidget WidgetFor m ()
widget [Text]
scopes Text
clientId Text
clientSecret =
WidgetFor m () -> Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
forall m.
YesodAuth m =>
WidgetFor m () -> Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
authOAuth2Widget WidgetFor m ()
widget Text
pluginName OAuth2
oauth2 (FetchCreds m -> AuthPlugin m) -> FetchCreds m -> AuthPlugin m
forall a b. (a -> b) -> a -> b
$ \Manager
manager OAuth2Token
token -> do
(User Int
userId, ByteString
userResponse) <-
Text -> Manager -> OAuth2Token -> URI -> IO (User, ByteString)
forall a.
FromJSON a =>
Text -> Manager -> OAuth2Token -> URI -> IO (a, ByteString)
authGetProfile
Text
pluginName
Manager
manager
OAuth2Token
token
URI
"https://api.github.com/user"
Creds m -> IO (Creds m)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
Creds
{ credsPlugin :: Text
credsPlugin = Text
pluginName
, credsIdent :: Text
credsIdent = String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show Int
userId
, credsExtra :: [(Text, Text)]
credsExtra = OAuth2Token -> ByteString -> [(Text, Text)]
setExtra OAuth2Token
token ByteString
userResponse
}
where
oauth2 :: OAuth2
oauth2 =
OAuth2
{ oauth2ClientId :: Text
oauth2ClientId = Text
clientId
, oauth2ClientSecret :: Maybe Text
oauth2ClientSecret = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
clientSecret
, oauth2AuthorizeEndpoint :: URI
oauth2AuthorizeEndpoint =
URI
"https://github.com/login/oauth/authorize"
URI -> [(ByteString, ByteString)] -> URI
forall a. URIRef a -> [(ByteString, ByteString)] -> URIRef a
`withQuery` [Text -> [Text] -> (ByteString, ByteString)
scopeParam Text
"," [Text]
scopes]
, oauth2TokenEndpoint :: URI
oauth2TokenEndpoint = URI
"https://github.com/login/oauth/access_token"
, oauth2RedirectUri :: Maybe URI
oauth2RedirectUri = Maybe URI
forall a. Maybe a
Nothing
}