From e1dc225b7a4aaadc3d70ea576ba7cc6420d21c45 Mon Sep 17 00:00:00 2001 From: nbloomf Date: Wed, 8 Jul 2020 00:46:05 -0500 Subject: [PATCH 1/2] Add WordPress.com as an auth provider Documentation at https://developer.wordpress.com/docs/wpcc/ --- example/Main.hs | 2 ++ src/Yesod/Auth/OAuth2/WordPressDotCom.hs | 44 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/Yesod/Auth/OAuth2/WordPressDotCom.hs diff --git a/example/Main.hs b/example/Main.hs index 9d48640..35bffc6 100644 --- a/example/Main.hs +++ b/example/Main.hs @@ -45,6 +45,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.WordPressDotCom import Yesod.Auth.OAuth2.Upcase data App = App @@ -143,6 +144,7 @@ mkFoundation = do , loadPlugin oauth2Salesforce "SALES_FORCE" , loadPlugin oauth2Slack "SLACK" , loadPlugin (oauth2Spotify []) "SPOTIFY" + , loadPlugin oauth2WordPressDotCom "WORDPRESS_DOT_COM" , loadPlugin oauth2Upcase "UPCASE" ] diff --git a/src/Yesod/Auth/OAuth2/WordPressDotCom.hs b/src/Yesod/Auth/OAuth2/WordPressDotCom.hs new file mode 100644 index 0000000..b953c5f --- /dev/null +++ b/src/Yesod/Auth/OAuth2/WordPressDotCom.hs @@ -0,0 +1,44 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Yesod.Auth.OAuth2.WordPressDotCom where + +import qualified Data.Text as T +import Yesod.Auth.OAuth2.Prelude + +pluginName :: Text +pluginName = "WordPress.com" + +newtype WpUser = WpUser Int + +instance FromJSON WpUser where + parseJSON = withObject "WpUser" $ \o -> WpUser + <$> o .: "ID" + +oauth2WordPressDotCom + :: ( YesodAuth m ) + => Text -- client ID + -> Text -- client secret + -> AuthPlugin m +oauth2WordPressDotCom clientId clientSecret = + authOAuth2 pluginName oauth2 $ \manager token -> do + (WpUser userId, userResponse) <- + authGetProfile pluginName manager token + "https://public-api.wordpress.com/rest/v1/me/" + + pure Creds + { credsPlugin = pluginName + , credsIdent = T.pack $ show userId + , credsExtra = setExtra token userResponse + } + + where + oauth2 = OAuth2 + { oauthClientId = clientId + , oauthClientSecret = clientSecret + , oauthOAuthorizeEndpoint = + "https://public-api.wordpress.com/oauth2/authorize" + `withQuery` [ scopeParam "," [ "auth" ] ] + , oauthAccessTokenEndpoint = + "https://public-api.wordpress.com/oauth2/token" + , oauthCallback = Nothing + } From 20fd72675f1946a35f22000f080fab659c81beb3 Mon Sep 17 00:00:00 2001 From: nbloomf Date: Wed, 8 Jul 2020 00:51:32 -0500 Subject: [PATCH 2/2] Add WordPress.com to .env.example --- .env.example | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.env.example b/.env.example index 24f3179..bfac8d0 100644 --- a/.env.example +++ b/.env.example @@ -34,3 +34,6 @@ SPOTIFY_CLIENT_SECRET=x UPCASE_CLIENT_ID=x UPCASE_CLIENT_SECRET=x + +WORDPRESS_DOT_COM_CLIENT_ID=x +WORDPRESS_DOT_COM_CLIENT_SECRET=x