From a0a996aed20ffd2a688833161c60c82ec6142904 Mon Sep 17 00:00:00 2001 From: Ding Date: Fri, 24 May 2024 22:34:48 +0800 Subject: [PATCH 1/3] orcid auth plugin --- example/Main.hs | 2 ++ src/Yesod/Auth/OAuth2/ORCID.hs | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/Yesod/Auth/OAuth2/ORCID.hs diff --git a/example/Main.hs b/example/Main.hs index 9c8d836..3dd7d0a 100644 --- a/example/Main.hs +++ b/example/Main.hs @@ -40,6 +40,7 @@ import Yesod.Auth.OAuth2.Spotify import Yesod.Auth.OAuth2.Twitch import Yesod.Auth.OAuth2.Upcase import Yesod.Auth.OAuth2.WordPressDotCom +import Yesod.Auth.OAuth2.ORCID data App = App { appHttpManager :: Manager @@ -149,6 +150,7 @@ mkFoundation = do , loadPlugin (oauth2Spotify []) "SPOTIFY" , loadPlugin oauth2Twitch "TWITCH" , loadPlugin oauth2WordPressDotCom "WORDPRESS_DOT_COM" + , loadPlugin oauth2Orcid "ORCID" , loadPlugin oauth2Upcase "UPCASE" ] diff --git a/src/Yesod/Auth/OAuth2/ORCID.hs b/src/Yesod/Auth/OAuth2/ORCID.hs new file mode 100644 index 0000000..46dd87c --- /dev/null +++ b/src/Yesod/Auth/OAuth2/ORCID.hs @@ -0,0 +1,50 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Yesod.Auth.OAuth2.Orcid + ( oauth2Orcid + ) where + +import qualified Data.Text as T +import Yesod.Auth.OAuth2.Prelude + +pluginName :: Text +pluginName = "orcid" + +newtype User = User Text + +instance FromJSON User where + parseJSON = withObject "User" $ \o -> User <$> o .: "sub" + +oauth2Orcid + :: YesodAuth m + => Text + -- ^ Client Id + -> Text + -- ^ Client Secret + -> AuthPlugin m +oauth2Orcid clientId clientSecret = + authOAuth2 pluginName oauth2 $ \manager token -> do + (User userId, userResponse) <- + authGetProfile + pluginName + manager + token + "https://orcid.org/oauth/userinfo" + + pure + Creds + { credsPlugin = pluginName + , credsIdent = T.pack $ show userId + , credsExtra = setExtra token userResponse + } + where + oauth2 = + OAuth2 + { oauth2ClientId = clientId + , oauth2ClientSecret = Just clientSecret + , oauth2AuthorizeEndpoint = + "https://orcid.org/oauth/authorize" + `withQuery` [scopeParam " " ["openid"]] + , oauth2TokenEndpoint = "https://orcid.org/oauth/token" + , oauth2RedirectUri = Nothing + } From 2c892977ccd68da4f58a996bd359ec32e49dc2ff Mon Sep 17 00:00:00 2001 From: Ding Date: Fri, 24 May 2024 23:11:24 +0800 Subject: [PATCH 2/3] correct moudle name --- src/Yesod/Auth/OAuth2/ORCID.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Yesod/Auth/OAuth2/ORCID.hs b/src/Yesod/Auth/OAuth2/ORCID.hs index 46dd87c..306ff86 100644 --- a/src/Yesod/Auth/OAuth2/ORCID.hs +++ b/src/Yesod/Auth/OAuth2/ORCID.hs @@ -1,6 +1,6 @@ {-# LANGUAGE OverloadedStrings #-} -module Yesod.Auth.OAuth2.Orcid +module Yesod.Auth.OAuth2.ORCID ( oauth2Orcid ) where From 6c1f627067b98ebd37ff42773098c35816f55ae9 Mon Sep 17 00:00:00 2001 From: Ding Date: Fri, 24 May 2024 23:57:38 +0800 Subject: [PATCH 3/3] uppercase everywhere --- example/Main.hs | 2 +- src/Yesod/Auth/OAuth2/ORCID.hs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/example/Main.hs b/example/Main.hs index 3dd7d0a..562bf31 100644 --- a/example/Main.hs +++ b/example/Main.hs @@ -150,7 +150,7 @@ mkFoundation = do , loadPlugin (oauth2Spotify []) "SPOTIFY" , loadPlugin oauth2Twitch "TWITCH" , loadPlugin oauth2WordPressDotCom "WORDPRESS_DOT_COM" - , loadPlugin oauth2Orcid "ORCID" + , loadPlugin oauth2ORCID "ORCID" , loadPlugin oauth2Upcase "UPCASE" ] diff --git a/src/Yesod/Auth/OAuth2/ORCID.hs b/src/Yesod/Auth/OAuth2/ORCID.hs index 306ff86..a60f394 100644 --- a/src/Yesod/Auth/OAuth2/ORCID.hs +++ b/src/Yesod/Auth/OAuth2/ORCID.hs @@ -1,7 +1,7 @@ {-# LANGUAGE OverloadedStrings #-} module Yesod.Auth.OAuth2.ORCID - ( oauth2Orcid + ( oauth2ORCID ) where import qualified Data.Text as T @@ -15,14 +15,14 @@ newtype User = User Text instance FromJSON User where parseJSON = withObject "User" $ \o -> User <$> o .: "sub" -oauth2Orcid +oauth2ORCID :: YesodAuth m => Text -- ^ Client Id -> Text -- ^ Client Secret -> AuthPlugin m -oauth2Orcid clientId clientSecret = +oauth2ORCID clientId clientSecret = authOAuth2 pluginName oauth2 $ \manager token -> do (User userId, userResponse) <- authGetProfile