{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
-- |
--
-- Generic OAuth2 plugin for Yesod
--
-- See @"Yesod.Auth.OAuth2.GitHub"@ for example usage.
--
module Yesod.Auth.OAuth2
  ( OAuth2(..)
  , FetchCreds
  , Manager
  , OAuth2Token(..)
  , Creds(..)
  , oauth2Url
  , authOAuth2
  , authOAuth2Widget

    -- * Alternatives that use 'fetchAccessToken2'
  , authOAuth2'
  , authOAuth2Widget'

    -- * Reading our @'credsExtra'@ keys
  , getAccessToken
  , getRefreshToken
  , getUserResponse
  , getUserResponseJSON
  ) where

import Control.Error.Util (note)
import Control.Monad ((<=<))
import Data.Aeson (FromJSON, eitherDecode)
import Data.ByteString.Lazy (ByteString, fromStrict)
import Data.Text (Text)
import Data.Text.Encoding (encodeUtf8)
import Network.HTTP.Conduit (Manager)
import Network.OAuth.OAuth2.Compat
import Yesod.Auth
import Yesod.Auth.OAuth2.Dispatch
import Yesod.Core.Widget

oauth2Url :: Text -> AuthRoute
oauth2Url :: Text -> AuthRoute
oauth2Url Text
name = Text -> Texts -> AuthRoute
PluginR Text
name [Text
"forward"]

-- | Create an @'AuthPlugin'@ for the given OAuth2 provider
--
-- Presents a generic @"Login via #{name}"@ link
--
authOAuth2 :: YesodAuth m => Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
authOAuth2 :: forall m.
YesodAuth m =>
Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
authOAuth2 Text
name = forall m.
YesodAuth m =>
WidgetFor m () -> Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
authOAuth2Widget [whamlet|Login via #{name}|] Text
name

-- | A version of 'authOAuth2' that uses 'fetchAccessToken2'
--
-- See <https://github.com/thoughtbot/yesod-auth-oauth2/pull/129>
--
authOAuth2' :: YesodAuth m => Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
authOAuth2' :: forall m.
YesodAuth m =>
Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
authOAuth2' Text
name = forall m.
YesodAuth m =>
WidgetFor m () -> Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
authOAuth2Widget' [whamlet|Login via #{name}|] Text
name

-- | Create an @'AuthPlugin'@ for the given OAuth2 provider
--
-- Allows passing a custom widget for the login link. See @'oauth2Eve'@ for an
-- example.
--
authOAuth2Widget
  :: YesodAuth m
  => WidgetFor m ()
  -> Text
  -> OAuth2
  -> FetchCreds m
  -> AuthPlugin m
authOAuth2Widget :: forall m.
YesodAuth m =>
WidgetFor m () -> Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
authOAuth2Widget = forall m.
YesodAuth m =>
FetchToken
-> WidgetFor m () -> Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
buildPlugin FetchToken
fetchAccessToken

-- | A version of 'authOAuth2Widget' that uses 'fetchAccessToken2'
--
-- See <https://github.com/thoughtbot/yesod-auth-oauth2/pull/129>
--
authOAuth2Widget'
  :: YesodAuth m
  => WidgetFor m ()
  -> Text
  -> OAuth2
  -> FetchCreds m
  -> AuthPlugin m
authOAuth2Widget' :: forall m.
YesodAuth m =>
WidgetFor m () -> Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
authOAuth2Widget' = forall m.
YesodAuth m =>
FetchToken
-> WidgetFor m () -> Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
buildPlugin FetchToken
fetchAccessToken2

buildPlugin
  :: YesodAuth m
  => FetchToken
  -> WidgetFor m ()
  -> Text
  -> OAuth2
  -> FetchCreds m
  -> AuthPlugin m
buildPlugin :: forall m.
YesodAuth m =>
FetchToken
-> WidgetFor m () -> Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
buildPlugin FetchToken
getToken WidgetFor m ()
widget Text
name OAuth2
oauth FetchCreds m
getCreds = forall master.
Text
-> (Text -> Texts -> AuthHandler master TypedContent)
-> ((AuthRoute -> Route master) -> WidgetFor master ())
-> AuthPlugin master
AuthPlugin
  Text
name
  (forall m.
Text
-> OAuth2
-> FetchToken
-> FetchCreds m
-> Text
-> Texts
-> AuthHandler m TypedContent
dispatchAuthRequest Text
name OAuth2
oauth FetchToken
getToken FetchCreds m
getCreds)
  (AuthRoute -> Route m) -> WidgetFor m ()
login
  where login :: (AuthRoute -> Route m) -> WidgetFor m ()
login AuthRoute -> Route m
tm = [whamlet|<a href=@{tm $ oauth2Url name}>^{widget}|]

-- | Read the @'AccessToken'@ from the values set via @'setExtra'@
getAccessToken :: Creds m -> Maybe AccessToken
getAccessToken :: forall m. Creds m -> Maybe AccessToken
getAccessToken = (Text -> AccessToken
AccessToken forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"accessToken" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall master. Creds master -> [(Text, Text)]
credsExtra

-- | Read the @'RefreshToken'@ from the values set via @'setExtra'@
--
-- N.B. not all providers supply this value.
--
getRefreshToken :: Creds m -> Maybe RefreshToken
getRefreshToken :: forall m. Creds m -> Maybe RefreshToken
getRefreshToken = (Text -> RefreshToken
RefreshToken forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"refreshToken" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall master. Creds master -> [(Text, Text)]
credsExtra

-- | Read the original profile response from the values set via @'setExtra'@
getUserResponse :: Creds m -> Maybe ByteString
getUserResponse :: forall m. Creds m -> Maybe ByteString
getUserResponse =
  (ByteString -> ByteString
fromStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
encodeUtf8 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"userResponse" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall master. Creds master -> [(Text, Text)]
credsExtra

-- | @'getUserResponse'@, and decode as JSON
getUserResponseJSON :: FromJSON a => Creds m -> Either String a
getUserResponseJSON :: forall a m. FromJSON a => Creds m -> Either String a
getUserResponseJSON =
  forall a. FromJSON a => ByteString -> Either String a
eitherDecode forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< forall a b. a -> Maybe b -> Either a b
note String
"userResponse key not present" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. Creds m -> Maybe ByteString
getUserResponse