Skip to content

Commit

Permalink
Add ORCID OAuth provider
Browse files Browse the repository at this point in the history
  • Loading branch information
c-ding-math authored and pbrisbin committed Jul 8, 2024
1 parent 87a0231 commit 553e048
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 2 additions & 0 deletions example/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -149,6 +150,7 @@ mkFoundation = do
, loadPlugin (oauth2Spotify []) "SPOTIFY"
, loadPlugin oauth2Twitch "TWITCH"
, loadPlugin oauth2WordPressDotCom "WORDPRESS_DOT_COM"
, loadPlugin oauth2ORCID "ORCID"
, loadPlugin oauth2Upcase "UPCASE"
]

Expand Down
50 changes: 50 additions & 0 deletions src/Yesod/Auth/OAuth2/ORCID.hs
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion yesod-auth-oauth2.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cabal-version: 1.18
--
-- see: https://github.com/sol/hpack
--
-- hash: a9bf86bc3dabd56dd108a6550bbd114607294b7add72c38d081f7f58085af3e9
-- hash: e92a1426ba46aee8b456d3b2454bcee06ae044afd5e1bb7b0d50641568b576ac

name: yesod-auth-oauth2
version: 0.7.2.0
Expand Down Expand Up @@ -54,6 +54,7 @@ library
Yesod.Auth.OAuth2.GitLab
Yesod.Auth.OAuth2.Google
Yesod.Auth.OAuth2.Nylas
Yesod.Auth.OAuth2.ORCID
Yesod.Auth.OAuth2.Prelude
Yesod.Auth.OAuth2.Random
Yesod.Auth.OAuth2.Salesforce
Expand Down

0 comments on commit 553e048

Please sign in to comment.