{-# 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 :: Value -> Parser User
parseJSON = String -> (Object -> Parser User) -> Value -> Parser User
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"User" ((Object -> Parser User) -> Value -> Parser User)
-> (Object -> Parser User) -> Value -> Parser User
forall a b. (a -> b) -> a -> b
$ \Object
o -> Text -> User
User (Text -> User) -> Parser Text -> Parser User
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"uuid"

pluginName :: Text
pluginName :: Text
pluginName = Text
"bitbucket"

defaultScopes :: [Text]
defaultScopes :: [Text]
defaultScopes = [Text
"account"]

oauth2Bitbucket :: YesodAuth m => Text -> Text -> AuthPlugin m
oauth2Bitbucket :: Text -> Text -> AuthPlugin m
oauth2Bitbucket = [Text] -> Text -> Text -> AuthPlugin m
forall m. YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m
oauth2BitbucketScoped [Text]
defaultScopes

oauth2BitbucketScoped :: YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m
oauth2BitbucketScoped :: [Text] -> Text -> Text -> AuthPlugin m
oauth2BitbucketScoped [Text]
scopes 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
        (User Text
userId, ByteString
userResponse) <- Text -> Manager -> OAuth2Token -> URI -> IO (User, ByteString)
forall a.
FromJSON a =>
Text -> Manager -> OAuth2Token -> URI -> IO (a, ByteString)
authGetProfile
            Text
pluginName
            Manager
manager
            OAuth2Token
token
            URI
"https://api.bitbucket.com/2.0/user"

        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
            -- 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 :: Text
credsIdent = String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Text -> String
forall a. Show a => a -> String
show Text
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://bitbucket.com/site/oauth2/authorize"
                URI -> [(ByteString, ByteString)] -> URI
forall a. URIRef a -> [(ByteString, ByteString)] -> URIRef a
`withQuery` [Text -> [Text] -> (ByteString, ByteString)
scopeParam Text
"," [Text]
scopes]
        , oauthAccessTokenEndpoint :: URI
oauthAccessTokenEndpoint =
            URI
"https://bitbucket.com/site/oauth2/access_token"
        , oauthCallback :: Maybe URI
oauthCallback = Maybe URI
forall a. Maybe a
Nothing
        }