{-# LANGUAGE OverloadedStrings #-}

-- |
--
-- OAuth2 plugin for http://twitch.tv
--
-- * Authenticates against twitch
-- * Uses twitch user id as credentials identifier
module Yesod.Auth.OAuth2.Twitch
  ( oauth2Twitch
  , oauth2TwitchScoped
  ) where

import Yesod.Auth.OAuth2.Prelude

import qualified Data.Text.Encoding as T

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 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"user_id"

pluginName :: Text
pluginName :: Text
pluginName = Text
"twitch"

defaultScopes :: [Text]
defaultScopes :: [Text]
defaultScopes = [Text
"user:read:email"]

oauth2Twitch :: YesodAuth m => Text -> Text -> AuthPlugin m
oauth2Twitch :: forall m. YesodAuth m => Text -> Text -> AuthPlugin m
oauth2Twitch = forall m. YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m
oauth2TwitchScoped [Text]
defaultScopes

oauth2TwitchScoped :: YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m
oauth2TwitchScoped :: forall m. YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m
oauth2TwitchScoped [Text]
scopes Text
clientId Text
clientSecret =
  forall m.
YesodAuth m =>
Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
authOAuth2 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://id.twitch.tv/oauth2/validate"

    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://id.twitch.tv/oauth2/authorize"
            forall a. URIRef a -> [(ByteString, ByteString)] -> URIRef a
`withQuery` [Text -> [Text] -> (ByteString, ByteString)
scopeParam Text
" " [Text]
scopes]
      , oauth2TokenEndpoint :: URI
oauth2TokenEndpoint =
          URI
"https://id.twitch.tv/oauth2/token"
            forall a. URIRef a -> [(ByteString, ByteString)] -> URIRef a
`withQuery` [ (ByteString
"client_id", Text -> ByteString
T.encodeUtf8 Text
clientId)
                        , (ByteString
"client_secret", Text -> ByteString
T.encodeUtf8 Text
clientSecret)
                        ]
      , oauth2RedirectUri :: Maybe URI
oauth2RedirectUri = forall a. Maybe a
Nothing
      }