Merge branch 'add/wordpress-dot-com' into merge/test-all-providers

This commit is contained in:
nbloomf 2020-07-08 01:40:56 -05:00
commit 53e113d2dc
3 changed files with 49 additions and 0 deletions

View File

@ -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

View File

@ -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"
]

View File

@ -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
}