mirror of
https://github.com/freckle/yesod-auth-oauth2.git
synced 2026-02-03 06:40:24 +01:00
This adds support for `ghc-9.12` / `hoauth2-2.15` and drops support for
`ghc < 9.4` / `hoauth2 < 2.8`.
Since this would be a major version bump no matter what, I've changed
the interface we present to align with `hoauth2-2.15`. This means using
the newer `fetch` functions, and `TokenResponse{,Error}` type names.
I've maintained our own `OAuth2` type so that the redirect-uri can
remain a `Maybe` field. The way plugins are constructed, we need to
build an `OAuth2` value in a pure context without one, which is then
supplied later, when we have `MonadHandler` and so can render URLs.
51 lines
1.2 KiB
Haskell
51 lines
1.2 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module Yesod.Auth.OAuth2.WordPressDotCom
|
|
( oauth2WordPressDotCom
|
|
) 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
|
|
{ oauth2ClientId = clientId
|
|
, oauth2ClientSecret = clientSecret
|
|
, oauth2AuthorizeEndpoint =
|
|
"https://public-api.wordpress.com/oauth2/authorize"
|
|
`withQuery` [scopeParam "," ["auth"]]
|
|
, oauth2TokenEndpoint = "https://public-api.wordpress.com/oauth2/token"
|
|
, oauth2RedirectUri = Nothing
|
|
}
|