mirror of
https://github.com/freckle/yesod-auth-oauth2.git
synced 2026-01-25 10:21:57 +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.
65 lines
1.9 KiB
Haskell
65 lines
1.9 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
-- |
|
|
--
|
|
-- OAuth2 plugin for http://bitbucket.com
|
|
--
|
|
-- * Authenticates against bitbucket
|
|
-- * Uses bitbucket uuid as credentials identifier
|
|
module Yesod.Auth.OAuth2.Bitbucket
|
|
( oauth2Bitbucket
|
|
, oauth2BitbucketScoped
|
|
) where
|
|
|
|
import Yesod.Auth.OAuth2.Prelude
|
|
|
|
import qualified Data.Text as T
|
|
|
|
newtype User = User Text
|
|
|
|
instance FromJSON User where
|
|
parseJSON = withObject "User" $ \o -> User <$> o .: "uuid"
|
|
|
|
pluginName :: Text
|
|
pluginName = "bitbucket"
|
|
|
|
defaultScopes :: [Text]
|
|
defaultScopes = ["account"]
|
|
|
|
oauth2Bitbucket :: YesodAuth m => Text -> Text -> AuthPlugin m
|
|
oauth2Bitbucket = oauth2BitbucketScoped defaultScopes
|
|
|
|
oauth2BitbucketScoped :: YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m
|
|
oauth2BitbucketScoped scopes clientId clientSecret =
|
|
authOAuth2 pluginName oauth2 $ \manager token -> do
|
|
(User userId, userResponse) <-
|
|
authGetProfile
|
|
pluginName
|
|
manager
|
|
token
|
|
"https://api.bitbucket.com/2.0/user"
|
|
|
|
pure
|
|
Creds
|
|
{ credsPlugin = pluginName
|
|
, -- FIXME: Preserved bug. This should just be userId (it's already
|
|
-- a Text), but because this code was shipped, folks likely have
|
|
-- Idents in their database like @"\"...\""@, and if we fixed this
|
|
-- they would need migrating. We're keeping it for now as it's a
|
|
-- minor wart. Breaking typed APIs is one thing, causing data to go
|
|
-- invalid is another.
|
|
credsIdent = T.pack $ show userId
|
|
, credsExtra = setExtra token userResponse
|
|
}
|
|
where
|
|
oauth2 =
|
|
OAuth2
|
|
{ oauth2ClientId = clientId
|
|
, oauth2ClientSecret = clientSecret
|
|
, oauth2AuthorizeEndpoint =
|
|
"https://bitbucket.com/site/oauth2/authorize"
|
|
`withQuery` [scopeParam "," scopes]
|
|
, oauth2TokenEndpoint = "https://bitbucket.com/site/oauth2/access_token"
|
|
, oauth2RedirectUri = Nothing
|
|
}
|