From 20ff7feaaceca30da6b9dcdcf21cd31bf5461acf Mon Sep 17 00:00:00 2001 From: patrick brisbin Date: Tue, 5 Jan 2021 13:48:50 -0500 Subject: [PATCH] Add ClassLink plugin --- .env.example | 3 ++ example/Main.hs | 2 ++ src/Yesod/Auth/OAuth2/ClassLink.hs | 51 ++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/Yesod/Auth/OAuth2/ClassLink.hs diff --git a/.env.example b/.env.example index 769e059..24118bb 100644 --- a/.env.example +++ b/.env.example @@ -14,6 +14,9 @@ BATTLE_NET_CLIENT_SECRET=x BITBUCKET_CLIENT_ID=x BITBUCKET_CLIENT_SECRET=x +CLASSLINK_CLIENT_ID=x +CLASSLINK_CLIENT_SECRET=x + EVE_ONLINE_CLIENT_ID=x EVE_ONLINE_CLIENT_SECRET=x diff --git a/example/Main.hs b/example/Main.hs index 1667bd6..18ffd3c 100644 --- a/example/Main.hs +++ b/example/Main.hs @@ -38,6 +38,7 @@ import Yesod.Auth import Yesod.Auth.OAuth2.AzureAD import Yesod.Auth.OAuth2.BattleNet import Yesod.Auth.OAuth2.Bitbucket +import Yesod.Auth.OAuth2.ClassLink import Yesod.Auth.OAuth2.EveOnline import Yesod.Auth.OAuth2.GitHub import Yesod.Auth.OAuth2.GitLab @@ -137,6 +138,7 @@ mkFoundation = do [ loadPlugin oauth2AzureAD "AZURE_AD" , loadPlugin (oauth2BattleNet [whamlet|TODO|] "en") "BATTLE_NET" , loadPlugin oauth2Bitbucket "BITBUCKET" + , loadPlugin oauth2ClassLink "CLASSLINK" , loadPlugin (oauth2Eve Plain) "EVE_ONLINE" , loadPlugin oauth2GitHub "GITHUB" , loadPlugin oauth2GitLab "GITLAB" diff --git a/src/Yesod/Auth/OAuth2/ClassLink.hs b/src/Yesod/Auth/OAuth2/ClassLink.hs new file mode 100644 index 0000000..03c51ad --- /dev/null +++ b/src/Yesod/Auth/OAuth2/ClassLink.hs @@ -0,0 +1,51 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Yesod.Auth.OAuth2.ClassLink + ( oauth2ClassLink + , oauth2ClassLinkScoped + ) +where + +import Yesod.Auth.OAuth2.Prelude + +import qualified Data.Text as T + +newtype User = User Int + +instance FromJSON User where + parseJSON = withObject "User" $ \o -> User <$> o .: "UserId" + +pluginName :: Text +pluginName = "classlink" + +defaultScopes :: [Text] +defaultScopes = ["profile", "oneroster"] + +oauth2ClassLink :: YesodAuth m => Text -> Text -> AuthPlugin m +oauth2ClassLink = oauth2ClassLinkScoped defaultScopes + +oauth2ClassLinkScoped :: YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m +oauth2ClassLinkScoped scopes clientId clientSecret = + authOAuth2 pluginName oauth2 $ \manager token -> do + (User userId, userResponse) <- authGetProfile + pluginName + manager + token + "https://nodeapi.classlink.com/v2/my/info" + + pure Creds + { credsPlugin = pluginName + , credsIdent = T.pack $ show userId + , credsExtra = setExtra token userResponse + } + where + oauth2 = OAuth2 + { oauthClientId = clientId + , oauthClientSecret = Just clientSecret + , oauthOAuthorizeEndpoint = + "https://launchpad.classlink.com/oauth2/v2/auth" + `withQuery` [scopeParam "," scopes] + , oauthAccessTokenEndpoint = + "https://launchpad.classlink.com/oauth2/v2/token" + , oauthCallback = Nothing + }