diff --git a/.env.example b/.env.example index caa935e..769e059 100644 --- a/.env.example +++ b/.env.example @@ -40,3 +40,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 diff --git a/example/Main.hs b/example/Main.hs index e371b2d..a4d158e 100644 --- a/example/Main.hs +++ b/example/Main.hs @@ -46,6 +46,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 @@ -146,6 +147,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 + }