From 77a66fa6e8cfef7a749e70345dab706586828584 Mon Sep 17 00:00:00 2001 From: Barry Moore Date: Fri, 18 Mar 2022 10:08:18 -0400 Subject: [PATCH] Add twitch.tv plugin --- .env.example | 3 ++ example/Main.hs | 2 ++ src/Yesod/Auth/OAuth2/Twitch.hs | 56 +++++++++++++++++++++++++++++++++ yesod-auth-oauth2.cabal | 3 +- 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/Yesod/Auth/OAuth2/Twitch.hs diff --git a/.env.example b/.env.example index 24118bb..325618d 100644 --- a/.env.example +++ b/.env.example @@ -41,6 +41,9 @@ SLACK_CLIENT_SECRET=x SPOTIFY_CLIENT_ID=x SPOTIFY_CLIENT_SECRET=x +TWITCH_CLIENT_ID=x +TWITCH_CLIENT_SECRET=x + UPCASE_CLIENT_ID=x UPCASE_CLIENT_SECRET=x diff --git a/example/Main.hs b/example/Main.hs index 18ffd3c..91a8cb7 100644 --- a/example/Main.hs +++ b/example/Main.hs @@ -47,6 +47,7 @@ import Yesod.Auth.OAuth2.Nylas import Yesod.Auth.OAuth2.Salesforce import Yesod.Auth.OAuth2.Slack import Yesod.Auth.OAuth2.Spotify +import Yesod.Auth.OAuth2.Twitch import Yesod.Auth.OAuth2.Upcase import Yesod.Auth.OAuth2.WordPressDotCom @@ -147,6 +148,7 @@ mkFoundation = do , loadPlugin oauth2Salesforce "SALES_FORCE" , loadPlugin oauth2Slack "SLACK" , loadPlugin (oauth2Spotify []) "SPOTIFY" + , loadPlugin oauth2Twitch "TWITCH" , loadPlugin oauth2WordPressDotCom "WORDPRESS_DOT_COM" , loadPlugin oauth2Upcase "UPCASE" ] diff --git a/src/Yesod/Auth/OAuth2/Twitch.hs b/src/Yesod/Auth/OAuth2/Twitch.hs new file mode 100644 index 0000000..4fcc56f --- /dev/null +++ b/src/Yesod/Auth/OAuth2/Twitch.hs @@ -0,0 +1,56 @@ +{-# 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 = withObject "User" $ \o -> User <$> o .: "user_id" + +pluginName :: Text +pluginName = "twitch" + +defaultScopes :: [Text] +defaultScopes = ["user:read:email"] + +oauth2Twitch :: YesodAuth m => Text -> Text -> AuthPlugin m +oauth2Twitch = oauth2TwitchScoped defaultScopes + +oauth2TwitchScoped :: YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m +oauth2TwitchScoped scopes clientId clientSecret = + authOAuth2 pluginName oauth2 $ \manager token -> do + (User userId, userResponse) <- authGetProfile + pluginName + manager + token + "https://id.twitch.tv/oauth2/validate" + + pure Creds { credsPlugin = pluginName + , credsIdent = userId + , credsExtra = setExtra token userResponse + } + where + oauth2 = OAuth2 + { oauth2ClientId = clientId + , oauth2ClientSecret = Just clientSecret + , oauth2AuthorizeEndpoint = "https://id.twitch.tv/oauth2/authorize" + `withQuery` [scopeParam " " scopes] + , oauth2TokenEndpoint = "https://id.twitch.tv/oauth2/token" + `withQuery` [ ("client_id", T.encodeUtf8 clientId) + , ("client_secret", T.encodeUtf8 clientSecret) + ] + , oauth2RedirectUri = Nothing + } diff --git a/yesod-auth-oauth2.cabal b/yesod-auth-oauth2.cabal index b4da805..a508d4c 100644 --- a/yesod-auth-oauth2.cabal +++ b/yesod-auth-oauth2.cabal @@ -4,7 +4,7 @@ cabal-version: 1.12 -- -- see: https://github.com/sol/hpack -- --- hash: d3ba666a53fb63a2f059501ac04eca709a905d0dd1f806ea5a7cd64e06ef890d +-- hash: 233909874fdbdbd71fa70c49f5a4223b4150b85d9415dbbed7fde2fff9e5ebcf name: yesod-auth-oauth2 version: 0.7.0.1 @@ -57,6 +57,7 @@ library Yesod.Auth.OAuth2.Salesforce Yesod.Auth.OAuth2.Slack Yesod.Auth.OAuth2.Spotify + Yesod.Auth.OAuth2.Twitch Yesod.Auth.OAuth2.Upcase Yesod.Auth.OAuth2.WordPressDotCom other-modules: