{-# LANGUAGE OverloadedStrings #-}
module Yesod.Auth.OAuth2.WordPressDotCom
( oauth2WordPressDotCom
)
where
import qualified Data.Text as T
import Yesod.Auth.OAuth2.Prelude
pluginName :: Text
pluginName :: Text
pluginName = Text
"WordPress.com"
newtype WpUser = WpUser Int
instance FromJSON WpUser where
parseJSON :: Value -> Parser WpUser
parseJSON = String -> (Object -> Parser WpUser) -> Value -> Parser WpUser
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"WpUser" ((Object -> Parser WpUser) -> Value -> Parser WpUser)
-> (Object -> Parser WpUser) -> Value -> Parser WpUser
forall a b. (a -> b) -> a -> b
$ \Object
o -> Int -> WpUser
WpUser (Int -> WpUser) -> Parser Int -> Parser WpUser
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Text -> Parser Int
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"ID"
oauth2WordPressDotCom
:: (YesodAuth m)
=> Text
-> Text
-> AuthPlugin m
oauth2WordPressDotCom :: Text -> Text -> AuthPlugin m
oauth2WordPressDotCom Text
clientId Text
clientSecret =
Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
forall m.
YesodAuth m =>
Text -> OAuth2 -> FetchCreds m -> AuthPlugin m
authOAuth2 Text
pluginName OAuth2
oauth2 (FetchCreds m -> AuthPlugin m) -> FetchCreds m -> AuthPlugin m
forall a b. (a -> b) -> a -> b
$ \Manager
manager OAuth2Token
token -> do
(WpUser Int
userId, ByteString
userResponse) <- Text -> Manager -> OAuth2Token -> URI -> IO (WpUser, ByteString)
forall a.
FromJSON a =>
Text -> Manager -> OAuth2Token -> URI -> IO (a, ByteString)
authGetProfile
Text
pluginName
Manager
manager
OAuth2Token
token
URI
"https://public-api.wordpress.com/rest/v1/me/"
Creds m -> IO (Creds m)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Creds :: forall master. Text -> Text -> [(Text, Text)] -> Creds master
Creds
{ credsPlugin :: Text
credsPlugin = Text
pluginName
, credsIdent :: Text
credsIdent = String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show Int
userId
, credsExtra :: [(Text, Text)]
credsExtra = OAuth2Token -> ByteString -> [(Text, Text)]
setExtra OAuth2Token
token ByteString
userResponse
}
where
oauth2 :: OAuth2
oauth2 = OAuth2 :: Text -> Maybe Text -> URI -> URI -> Maybe URI -> OAuth2
OAuth2
{ oauthClientId :: Text
oauthClientId = Text
clientId
, oauthClientSecret :: Maybe Text
oauthClientSecret = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
clientSecret
, oauthOAuthorizeEndpoint :: URI
oauthOAuthorizeEndpoint =
URI
"https://public-api.wordpress.com/oauth2/authorize"
URI -> [(ByteString, ByteString)] -> URI
forall a. URIRef a -> [(ByteString, ByteString)] -> URIRef a
`withQuery` [Text -> [Text] -> (ByteString, ByteString)
scopeParam Text
"," [Text
"auth"]]
, oauthAccessTokenEndpoint :: URI
oauthAccessTokenEndpoint =
URI
"https://public-api.wordpress.com/oauth2/token"
, oauthCallback :: Maybe URI
oauthCallback = Maybe URI
forall a. Maybe a
Nothing
}