{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}

-- |
--
-- OAuth2 plugin for http://www.google.com
--
-- * Authenticates against Google
-- * Uses Google user id as credentials identifier
--
-- If you were previously relying on email as the creds identifier, you can
-- still do that (and more) by overriding it in the creds returned by the plugin
-- with any value read out of the new @userResponse@ key in @'credsExtra'@.
--
-- For example:
--
-- > data User = User { userEmail :: Text }
-- >
-- > instance FromJSON User where -- you know...
-- >
-- > authenticate creds = do
-- >     -- 'getUserResponseJSON' provided by "Yesod.Auth.OAuth" module
-- >     let Right email = userEmail <$> getUserResponseJSON creds
-- >         updatedCreds = creds { credsIdent = email }
-- >
-- >     -- continue normally with updatedCreds
module Yesod.Auth.OAuth2.Google
  ( oauth2Google
  , oauth2GoogleWidget
  , oauth2GoogleScoped
  , oauth2GoogleScopedWidget
  ) where

import Yesod.Auth.OAuth2.Prelude
import Yesod.Core (WidgetFor, whamlet)

newtype User = User Text

instance FromJSON User where
  parseJSON :: Value -> Parser User
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"User" forall a b. (a -> b) -> a -> b
$ \Object
o ->
      Text -> User
User
        -- Required for data backwards-compatibility
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Text
"google-uid:" forall a. Semigroup a => a -> a -> a
<>) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"sub")

pluginName :: Text
pluginName :: Text
pluginName = Text
"google"

defaultScopes :: [Text]
defaultScopes :: [Text]
defaultScopes = [Text
"openid", Text
"email"]

oauth2Google :: YesodAuth m => Text -> Text -> AuthPlugin m
oauth2Google :: forall m. YesodAuth m => Text -> Text -> AuthPlugin m
oauth2Google = forall m. YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m
oauth2GoogleScoped [Text]
defaultScopes

oauth2GoogleWidget
  :: YesodAuth m => WidgetFor m () -> Text -> Text -> AuthPlugin m
oauth2GoogleWidget :: forall m.
YesodAuth m =>
WidgetFor m () -> Text -> Text -> AuthPlugin m
oauth2GoogleWidget WidgetFor m ()
widget = forall m.
YesodAuth m =>
WidgetFor m () -> [Text] -> Text -> Text -> AuthPlugin m
oauth2GoogleScopedWidget WidgetFor m ()
widget [Text]
defaultScopes

oauth2GoogleScoped :: YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m
oauth2GoogleScoped :: forall m. YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m
oauth2GoogleScoped =
  forall m.
YesodAuth m =>
WidgetFor m () -> [Text] -> Text -> Text -> AuthPlugin m
oauth2GoogleScopedWidget [whamlet|Login via #{pluginName}|]

oauth2GoogleScopedWidget
  :: YesodAuth m => WidgetFor m () -> [Text] -> Text -> Text -> AuthPlugin m
oauth2GoogleScopedWidget :: forall m.
YesodAuth m =>
WidgetFor m () -> [Text] -> Text -> Text -> AuthPlugin m
oauth2GoogleScopedWidget WidgetFor m ()
widget [Text]
scopes Text
clientId Text
clientSecret =
  forall m.
YesodAuth m =>
WidgetFor m () -> Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
authOAuth2Widget WidgetFor m ()
widget Text
pluginName OAuth2
oauth2 forall a b. (a -> b) -> a -> b
$ \Manager
manager OAuth2Token
token -> do
    (User Text
userId, ByteString
userResponse) <-
      forall a.
FromJSON a =>
Text -> Manager -> OAuth2Token -> URI -> IO (a, ByteString)
authGetProfile
        Text
pluginName
        Manager
manager
        OAuth2Token
token
        URI
"https://www.googleapis.com/oauth2/v3/userinfo"

    forall (f :: * -> *) a. Applicative f => a -> f a
pure
      Creds
        { credsPlugin :: Text
credsPlugin = Text
pluginName
        , credsIdent :: Text
credsIdent = Text
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 = forall a. a -> Maybe a
Just Text
clientSecret
      , oauth2AuthorizeEndpoint :: URI
oauth2AuthorizeEndpoint =
          URI
"https://accounts.google.com/o/oauth2/auth"
            forall a. URIRef a -> [(ByteString, ByteString)] -> URIRef a
`withQuery` [Text -> [Text] -> (ByteString, ByteString)
scopeParam Text
" " [Text]
scopes]
      , oauth2TokenEndpoint :: URI
oauth2TokenEndpoint = URI
"https://www.googleapis.com/oauth2/v3/token"
      , oauth2RedirectUri :: Maybe URI
oauth2RedirectUri = forall a. Maybe a
Nothing
      }