Remove extra fields in Bitbucket provider

New keys:

- accessToken
- userResponseJSON

Removed keys:

- email
- login
- avatar_url
- access_token
- name
- location
This commit is contained in:
patrick brisbin 2018-01-27 09:17:30 -05:00
parent 734c9f464a
commit 391ef62813

View File

@ -5,7 +5,6 @@
-- --
-- * Authenticates against bitbucket -- * Authenticates against bitbucket
-- * Uses bitbucket uuid as credentials identifier -- * Uses bitbucket uuid as credentials identifier
-- * Returns email, username, full name, location and avatar as extras
-- --
module Yesod.Auth.OAuth2.Bitbucket module Yesod.Auth.OAuth2.Bitbucket
( oauth2Bitbucket ( oauth2Bitbucket
@ -14,74 +13,46 @@ module Yesod.Auth.OAuth2.Bitbucket
import Yesod.Auth.OAuth2.Prelude import Yesod.Auth.OAuth2.Prelude
import Data.List (find) import qualified Data.ByteString.Lazy as BL
import Data.Maybe (fromMaybe)
import qualified Data.Text as T import qualified Data.Text as T
data BitbucketUser = BitbucketUser newtype User = User Text
{ bitbucketUserId :: Text
, bitbucketUserName :: Maybe Text
, bitbucketUserLogin :: Text
, bitbucketUserLocation :: Maybe Text
, bitbucketUserLinks :: BitbucketUserLinks
}
instance FromJSON BitbucketUser where instance FromJSON User where
parseJSON = withObject "BitbucketUser" $ \o -> BitbucketUser parseJSON = withObject "User" $ \o -> User
<$> o .: "uuid" <$> o .: "uuid"
<*> o .:? "display_name"
<*> o .: "username"
<*> o .:? "location"
<*> o .: "links"
newtype BitbucketUserLinks = BitbucketUserLinks pluginName :: Text
{ bitbucketAvatarLink :: BitbucketLink pluginName = "bitbucket"
}
instance FromJSON BitbucketUserLinks where defaultScopes :: [Text]
parseJSON = withObject "BitbucketUserLinks" $ \o -> BitbucketUserLinks defaultScopes = ["account"]
<$> o .: "avatar"
newtype BitbucketLink = BitbucketLink oauth2Bitbucket :: YesodAuth m => Text -> Text -> AuthPlugin m
{ bitbucketLinkHref :: Text oauth2Bitbucket = oauth2BitbucketScoped defaultScopes
}
instance FromJSON BitbucketLink where oauth2BitbucketScoped :: YesodAuth m => [Text] -> Text -> Text -> AuthPlugin m
parseJSON = withObject "BitbucketLink" $ \o -> BitbucketLink oauth2BitbucketScoped scopes clientId clientSecret =
<$> o .: "href" authOAuth2 pluginName oauth2 $ \manager token -> do
(User userId, userResponseJSON) <-
authGetProfile pluginName manager token "https://api.bitbucket.com/2.0/user"
newtype BitbucketEmailSearchResults = BitbucketEmailSearchResults pure Creds
{ bitbucketEmails :: [BitbucketUserEmail] { credsPlugin = pluginName
} -- FIXME: Preserved bug. This should just be userId (it's already
-- a Text), but because this code was shipped, folks likely have
instance FromJSON BitbucketEmailSearchResults where -- Idents in their database like @"\"...\""@, and if we fixed this
parseJSON = withObject "BitbucketEmailSearchResults" $ \o -> BitbucketEmailSearchResults -- they would need migrating. We're keeping it for now as it's a
<$> o .: "values" -- minor wart. Breaking typed APIs is one thing, causing data to go
-- invalid is another.
data BitbucketUserEmail = BitbucketUserEmail , credsIdent = T.pack $ show userId
{ bitbucketUserEmailAddress :: Text , credsExtra =
, bitbucketUserEmailPrimary :: Bool [ ("accessToken", atoken $ accessToken token)
} , ("userResponseJSON", decodeUtf8 $ BL.toStrict userResponseJSON)
]
instance FromJSON BitbucketUserEmail where }
parseJSON = withObject "BitbucketUserEmail" $ \o -> BitbucketUserEmail
<$> o .: "email"
<*> o .: "is_primary"
oauth2Bitbucket :: YesodAuth m
=> Text -- ^ Client ID
-> Text -- ^ Client Secret
-> AuthPlugin m
oauth2Bitbucket clientId clientSecret = oauth2BitbucketScoped clientId clientSecret ["account"]
oauth2BitbucketScoped :: YesodAuth m
=> Text -- ^ Client ID
-> Text -- ^ Client Secret
-> [Text] -- ^ List of scopes to request
-> AuthPlugin m
oauth2BitbucketScoped clientId clientSecret scopes = authOAuth2 "bitbucket" oauth fetchBitbucketProfile
where where
oauth = OAuth2 oauth2 = OAuth2
{ oauthClientId = clientId { oauthClientId = clientId
, oauthClientSecret = clientSecret , oauthClientSecret = clientSecret
, oauthOAuthorizeEndpoint = "https://bitbucket.com/site/oauth2/authorize" `withQuery` , oauthOAuthorizeEndpoint = "https://bitbucket.com/site/oauth2/authorize" `withQuery`
@ -90,30 +61,3 @@ oauth2BitbucketScoped clientId clientSecret scopes = authOAuth2 "bitbucket" oaut
, oauthAccessTokenEndpoint = "https://bitbucket.com/site/oauth2/access_token" , oauthAccessTokenEndpoint = "https://bitbucket.com/site/oauth2/access_token"
, oauthCallback = Nothing , oauthCallback = Nothing
} }
fetchBitbucketProfile :: Manager -> OAuth2Token -> IO (Creds m)
fetchBitbucketProfile manager token = do
userResult <- authGetJSON manager (accessToken token) "https://api.bitbucket.com/2.0/user"
mailResult <- authGetJSON manager (accessToken token) "https://api.bitbucket.com/2.0/user/emails"
case (userResult, mailResult) of
(Right user, Right mails) -> return $ toCreds user (bitbucketEmails mails) token
(Left err, _) -> throwIO $ invalidProfileResponse "bitbucket" err
(_, Left err) -> throwIO $ invalidProfileResponse "bitbucket" err
toCreds :: BitbucketUser -> [BitbucketUserEmail] -> OAuth2Token -> Creds m
toCreds user userMails token = Creds
{ credsPlugin = "bitbucket"
, credsIdent = T.pack $ show $ bitbucketUserId user
, credsExtra =
[ ("email", bitbucketUserEmailAddress email)
, ("login", bitbucketUserLogin user)
, ("avatar_url", bitbucketLinkHref (bitbucketAvatarLink (bitbucketUserLinks user)))
, ("access_token", atoken $ accessToken token)
]
++ maybeExtra "name" (bitbucketUserName user)
++ maybeExtra "location" (bitbucketUserLocation user)
}
where
email = fromMaybe (head userMails) $ find bitbucketUserEmailPrimary userMails