Fix coding style in Battle plugin

This commit is contained in:
patrick brisbin 2018-01-23 08:50:19 -05:00
parent ed58922727
commit 8efe95773b

View File

@ -9,10 +9,9 @@
-- * Uses user's id as credentials identifier. -- * Uses user's id as credentials identifier.
-- * Returns user's battletag in extras. -- * Returns user's battletag in extras.
-- --
module Yesod.Auth.OAuth2.BattleNet module Yesod.Auth.OAuth2.BattleNet
( oAuth2BattleNet ) ( oAuth2BattleNet
where ) where
#if __GLASGOW_HASKELL__ < 710 #if __GLASGOW_HASKELL__ < 710
import Control.Applicative ((<$>), (<*>)) import Control.Applicative ((<$>), (<*>))
@ -20,59 +19,62 @@ import Control.Applicative ((<$>), (<*>))
import Control.Exception (throwIO) import Control.Exception (throwIO)
import Control.Monad (mzero) import Control.Monad (mzero)
import Yesod.Auth
import Yesod.Auth.OAuth2
import Data.Monoid ((<>))
import Network.HTTP.Conduit (Manager)
import Data.Aeson import Data.Aeson
import Data.Monoid ((<>))
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T (pack, toLower) import qualified Data.Text as T (pack, toLower)
import qualified Data.Text.Encoding as E (encodeUtf8) import qualified Data.Text.Encoding as E (encodeUtf8)
import Network.HTTP.Conduit (Manager)
import Prelude import Prelude
import Yesod.Auth
import Yesod.Auth.OAuth2
import Yesod.Core.Widget import Yesod.Core.Widget
data BattleNetUser = BattleNetUser data BattleNetUser = BattleNetUser
{ userId :: Int { userId :: Int
, battleTag :: Text , battleTag :: Text
} }
instance FromJSON BattleNetUser where instance FromJSON BattleNetUser where
parseJSON (Object o) = BattleNetUser parseJSON (Object o) = BattleNetUser
<$> o .: "id" <$> o .: "id"
<*> o .: "battletag" <*> o .: "battletag"
parseJSON _ = mzero parseJSON _ = mzero
oAuth2BattleNet :: YesodAuth m oAuth2BattleNet
=> Text -- ^ Client ID :: YesodAuth m
-> Text -- ^ Client Secret => Text -- ^ Client ID
-> Text -- ^ User region (e.g. "eu", "cn", "us") -> Text -- ^ Client Secret
-> WidgetT m IO () -- ^ Login widget -> Text -- ^ User region (e.g. "eu", "cn", "us")
-> AuthPlugin m -> WidgetT m IO () -- ^ Login widget
oAuth2BattleNet clientId clientSecret region widget = authOAuth2Widget widget "battle.net" oAuthData $ makeCredentials region -> AuthPlugin m
where oAuthData = OAuth2 { oauthClientId = clientId oAuth2BattleNet clientId clientSecret region widget =
, oauthClientSecret = clientSecret authOAuth2Widget widget "battle.net" oAuthData $ makeCredentials region
, oauthOAuthorizeEndpoint = fromRelative "https" host "/oauth/authorize" where
, oauthAccessTokenEndpoint = fromRelative "https" host "/oauth/token" oAuthData = OAuth2
, oauthCallback = Nothing { oauthClientId = clientId
} , oauthClientSecret = clientSecret
, oauthOAuthorizeEndpoint = fromRelative "https" host "/oauth/authorize"
, oauthAccessTokenEndpoint = fromRelative "https" host "/oauth/token"
, oauthCallback = Nothing
}
host = wwwHost $ T.toLower region host = wwwHost $ T.toLower region
makeCredentials :: Text -> Manager -> OAuth2Token -> IO (Creds m) makeCredentials :: Text -> Manager -> OAuth2Token -> IO (Creds m)
makeCredentials region manager token = do makeCredentials region manager token = do
userResult <- authGetJSON manager (accessToken token) userResult <- authGetJSON manager (accessToken token)
$ fromRelative "https" (apiHost $ T.toLower region) "/account/user" $ fromRelative "https" (apiHost $ T.toLower region) "/account/user"
case userResult of either
Left err -> throwIO $ invalidProfileResponse "battle.net" err (throwIO . invalidProfileResponse "battle.net")
Right user -> return Creds (\user ->
{ credsPlugin = "battle.net" return Creds
, credsIdent = T.pack $ show $ userId user { credsPlugin = "battle.net"
, credsExtra = [("battletag", battleTag user)] , 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"