mirror of
https://github.com/freckle/yesod-auth-oauth2.git
synced 2026-04-23 19:27:44 +02:00
Remove extra information from BattleNet provider
New keys: - accessToken - userResponseJSON Removed keys: - battleTag
This commit is contained in:
parent
3d4ff8da39
commit
734c9f464a
@ -14,18 +14,18 @@ module Yesod.Auth.OAuth2.BattleNet
|
|||||||
|
|
||||||
import Yesod.Auth.OAuth2.Prelude
|
import Yesod.Auth.OAuth2.Prelude
|
||||||
|
|
||||||
|
import qualified Data.ByteString.Lazy as BL
|
||||||
import qualified Data.Text as T (pack, toLower)
|
import qualified Data.Text as T (pack, toLower)
|
||||||
import Yesod.Core.Widget
|
import Yesod.Core.Widget
|
||||||
|
|
||||||
data BattleNetUser = BattleNetUser
|
newtype User = User Int
|
||||||
{ userId :: Int
|
|
||||||
, battleTag :: Text
|
|
||||||
}
|
|
||||||
|
|
||||||
instance FromJSON BattleNetUser where
|
instance FromJSON User where
|
||||||
parseJSON = withObject "BattleNetUser" $ \o -> BattleNetUser
|
parseJSON = withObject "User" $ \o -> User
|
||||||
<$> o .: "id"
|
<$> o .: "id"
|
||||||
<*> o .: "battletag"
|
|
||||||
|
pluginName :: Text
|
||||||
|
pluginName = "battle.net"
|
||||||
|
|
||||||
oAuth2BattleNet
|
oAuth2BattleNet
|
||||||
:: YesodAuth m
|
:: YesodAuth m
|
||||||
@ -35,9 +35,22 @@ oAuth2BattleNet
|
|||||||
-> WidgetT m IO () -- ^ Login widget
|
-> WidgetT m IO () -- ^ Login widget
|
||||||
-> AuthPlugin m
|
-> AuthPlugin m
|
||||||
oAuth2BattleNet clientId clientSecret region widget =
|
oAuth2BattleNet clientId clientSecret region widget =
|
||||||
authOAuth2Widget widget "battle.net" oAuthData $ makeCredentials region
|
authOAuth2Widget widget pluginName oauth2 $ \manager token -> do
|
||||||
|
(User userId, userResponseJSON) <-
|
||||||
|
authGetProfile pluginName manager token
|
||||||
|
$ fromRelative "https" (apiHost $ T.toLower region) "/account/user"
|
||||||
|
|
||||||
|
pure Creds
|
||||||
|
{ credsPlugin = pluginName
|
||||||
|
, credsIdent = T.pack $ show userId
|
||||||
|
, credsExtra =
|
||||||
|
[ ("accessToken", atoken $ accessToken token)
|
||||||
|
, ("userResponseJSON", decodeUtf8 $ BL.toStrict userResponseJSON)
|
||||||
|
]
|
||||||
|
}
|
||||||
where
|
where
|
||||||
oAuthData = OAuth2
|
host = wwwHost $ T.toLower region
|
||||||
|
oauth2 = OAuth2
|
||||||
{ oauthClientId = clientId
|
{ oauthClientId = clientId
|
||||||
, oauthClientSecret = clientSecret
|
, oauthClientSecret = clientSecret
|
||||||
, oauthOAuthorizeEndpoint = fromRelative "https" host "/oauth/authorize"
|
, oauthOAuthorizeEndpoint = fromRelative "https" host "/oauth/authorize"
|
||||||
@ -45,22 +58,6 @@ oAuth2BattleNet clientId clientSecret region widget =
|
|||||||
, oauthCallback = Nothing
|
, oauthCallback = Nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
host = wwwHost $ T.toLower region
|
|
||||||
|
|
||||||
makeCredentials :: Text -> Manager -> OAuth2Token -> IO (Creds m)
|
|
||||||
makeCredentials region manager token = do
|
|
||||||
userResult <- authGetJSON manager (accessToken token)
|
|
||||||
$ fromRelative "https" (apiHost $ T.toLower region) "/account/user"
|
|
||||||
|
|
||||||
either
|
|
||||||
(throwIO . invalidProfileResponse "battle.net")
|
|
||||||
(\user ->
|
|
||||||
return Creds
|
|
||||||
{ credsPlugin = "battle.net"
|
|
||||||
, credsIdent = T.pack $ show $ userId user
|
|
||||||
, credsExtra = [("battletag", battleTag user)]
|
|
||||||
}
|
|
||||||
) userResult
|
|
||||||
|
|
||||||
apiHost :: Text -> Host
|
apiHost :: Text -> Host
|
||||||
apiHost "cn" = "api.battlenet.com.cn"
|
apiHost "cn" = "api.battlenet.com.cn"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user