mirror of
https://github.com/freckle/yesod-auth-oauth2.git
synced 2026-09-02 01:43:51 +02:00
Remove extra information from EveOnline provider
Removed keys: - charName - charId - tokenType - expires All can be recovered by re-parsing userResponseJSON.
This commit is contained in:
parent
391ef62813
commit
98b9f1108d
@ -6,7 +6,6 @@
|
|||||||
--
|
--
|
||||||
-- * Authenticates against eveonline
|
-- * Authenticates against eveonline
|
||||||
-- * Uses EVEs unique account-user-char-hash as credentials identifier
|
-- * Uses EVEs unique account-user-char-hash as credentials identifier
|
||||||
-- * Returns charName, charId, tokenType, accessToken and expires as extras
|
|
||||||
--
|
--
|
||||||
module Yesod.Auth.OAuth2.EveOnline
|
module Yesod.Auth.OAuth2.EveOnline
|
||||||
( oauth2Eve
|
( oauth2Eve
|
||||||
@ -16,9 +15,16 @@ module Yesod.Auth.OAuth2.EveOnline
|
|||||||
|
|
||||||
import Yesod.Auth.OAuth2.Prelude
|
import Yesod.Auth.OAuth2.Prelude
|
||||||
|
|
||||||
|
import qualified Data.ByteString.Lazy as BL
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Yesod.Core.Widget
|
import Yesod.Core.Widget
|
||||||
|
|
||||||
|
newtype User = User Text
|
||||||
|
|
||||||
|
instance FromJSON User where
|
||||||
|
parseJSON = withObject "User" $ \o -> User
|
||||||
|
<$> o .: "CharacterOwnerHash"
|
||||||
|
|
||||||
data WidgetType m
|
data WidgetType m
|
||||||
= Plain -- ^ Simple "Login via eveonline" text
|
= Plain -- ^ Simple "Login via eveonline" text
|
||||||
| BigWhite
|
| BigWhite
|
||||||
@ -27,49 +33,40 @@ data WidgetType m
|
|||||||
| SmallBlack
|
| SmallBlack
|
||||||
| Custom (WidgetT m IO ())
|
| Custom (WidgetT m IO ())
|
||||||
|
|
||||||
data EveUser = EveUser
|
asWidget :: YesodAuth m => WidgetType m -> WidgetT m IO ()
|
||||||
{ eveUserName :: Text
|
asWidget Plain = [whamlet|Login via eveonline|]
|
||||||
, eveUserExpire :: Text
|
asWidget BigWhite = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/4PTzeiAshqiM8osU2giO0Y/5cc4cb60bac52422da2e45db87b6819c/EVE_SSO_Login_Buttons_Large_White.png?w=270&h=45">|]
|
||||||
, eveTokenType :: Text
|
asWidget BigBlack = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/4fSjj56uD6CYwYyus4KmES/4f6385c91e6de56274d99496e6adebab/EVE_SSO_Login_Buttons_Large_Black.png?w=270&h=45">|]
|
||||||
, eveCharOwnerHash :: Text
|
asWidget SmallWhite = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/18BxKSXCymyqY4QKo8KwKe/c2bdded6118472dd587c8107f24104d7/EVE_SSO_Login_Buttons_Small_White.png?w=195&h=30">|]
|
||||||
, eveCharId :: Integer
|
asWidget SmallBlack = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/12vrPsIMBQi28QwCGOAqGk/33234da7672c6b0cdca394fc8e0b1c2b/EVE_SSO_Login_Buttons_Small_Black.png?w=195&h=30">|]
|
||||||
}
|
asWidget (Custom a) = a
|
||||||
|
|
||||||
instance FromJSON EveUser where
|
pluginName :: Text
|
||||||
parseJSON = withObject "EveUser" $ \o -> EveUser
|
pluginName = "eveonline"
|
||||||
<$> o .: "CharacterName"
|
|
||||||
<*> o .: "ExpiresOn"
|
|
||||||
<*> o .: "TokenType"
|
|
||||||
<*> o .: "CharacterOwnerHash"
|
|
||||||
<*> o .: "CharacterID"
|
|
||||||
|
|
||||||
oauth2Eve :: YesodAuth m
|
defaultScopes :: [Text]
|
||||||
=> Text -- ^ Client ID
|
defaultScopes = ["publicData"]
|
||||||
-> Text -- ^ Client Secret
|
|
||||||
-> WidgetType m
|
|
||||||
-> AuthPlugin m
|
|
||||||
oauth2Eve clientId clientSecret = oauth2EveScoped clientId clientSecret ["publicData"] . asWidget
|
|
||||||
|
|
||||||
|
oauth2Eve :: YesodAuth m => WidgetType m -> Text -> Text -> AuthPlugin m
|
||||||
|
oauth2Eve = oauth2EveScoped defaultScopes
|
||||||
|
|
||||||
|
oauth2EveScoped :: YesodAuth m => [Text] -> WidgetType m -> Text -> Text -> AuthPlugin m
|
||||||
|
oauth2EveScoped scopes widgetType clientId clientSecret =
|
||||||
|
authOAuth2Widget (asWidget widgetType) pluginName oauth2 $ \manager token -> do
|
||||||
|
(User userId, userResponseJSON) <-
|
||||||
|
authGetProfile pluginName manager token "https://login.eveonline.com/oauth/verify"
|
||||||
|
|
||||||
|
pure Creds
|
||||||
|
{ credsPlugin = "eveonline"
|
||||||
|
-- FIXME: Preserved bug. See similar comment in Bitbucket provider.
|
||||||
|
, credsIdent = T.pack $ show userId
|
||||||
|
, credsExtra =
|
||||||
|
[ ("accessToken", atoken $ accessToken token)
|
||||||
|
, ("userResponseJSON", decodeUtf8 $ BL.toStrict userResponseJSON)
|
||||||
|
]
|
||||||
|
}
|
||||||
where
|
where
|
||||||
asWidget :: YesodAuth m => WidgetType m -> WidgetT m IO ()
|
oauth2 = OAuth2
|
||||||
asWidget Plain = [whamlet|Login via eveonline|]
|
|
||||||
asWidget BigWhite = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/4PTzeiAshqiM8osU2giO0Y/5cc4cb60bac52422da2e45db87b6819c/EVE_SSO_Login_Buttons_Large_White.png?w=270&h=45">|]
|
|
||||||
asWidget BigBlack = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/4fSjj56uD6CYwYyus4KmES/4f6385c91e6de56274d99496e6adebab/EVE_SSO_Login_Buttons_Large_Black.png?w=270&h=45">|]
|
|
||||||
asWidget SmallWhite = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/18BxKSXCymyqY4QKo8KwKe/c2bdded6118472dd587c8107f24104d7/EVE_SSO_Login_Buttons_Small_White.png?w=195&h=30">|]
|
|
||||||
asWidget SmallBlack = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/12vrPsIMBQi28QwCGOAqGk/33234da7672c6b0cdca394fc8e0b1c2b/EVE_SSO_Login_Buttons_Small_Black.png?w=195&h=30">|]
|
|
||||||
asWidget (Custom a) = a
|
|
||||||
|
|
||||||
oauth2EveScoped :: YesodAuth m
|
|
||||||
=> Text -- ^ Client ID
|
|
||||||
-> Text -- ^ Client Secret
|
|
||||||
-> [Text] -- ^ List of scopes to request
|
|
||||||
-> WidgetT m IO () -- ^ Login widget
|
|
||||||
-> AuthPlugin m
|
|
||||||
oauth2EveScoped clientId clientSecret scopes widget =
|
|
||||||
authOAuth2Widget widget "eveonline" oauth fetchEveProfile
|
|
||||||
|
|
||||||
where
|
|
||||||
oauth = OAuth2
|
|
||||||
{ oauthClientId = clientId
|
{ oauthClientId = clientId
|
||||||
, oauthClientSecret = clientSecret
|
, oauthClientSecret = clientSecret
|
||||||
, oauthOAuthorizeEndpoint = "https://login.eveonline.com/oauth/authorize" `withQuery`
|
, oauthOAuthorizeEndpoint = "https://login.eveonline.com/oauth/authorize" `withQuery`
|
||||||
@ -79,24 +76,3 @@ oauth2EveScoped clientId clientSecret scopes widget =
|
|||||||
, oauthAccessTokenEndpoint = "https://login.eveonline.com/oauth/token"
|
, oauthAccessTokenEndpoint = "https://login.eveonline.com/oauth/token"
|
||||||
, oauthCallback = Nothing
|
, oauthCallback = Nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchEveProfile :: Manager -> OAuth2Token -> IO (Creds m)
|
|
||||||
fetchEveProfile manager token = do
|
|
||||||
userResult <- authGetJSON manager (accessToken token) "https://login.eveonline.com/oauth/verify"
|
|
||||||
|
|
||||||
case userResult of
|
|
||||||
Right user -> return $ toCreds user token
|
|
||||||
Left err-> throwIO $ invalidProfileResponse "eveonline" err
|
|
||||||
|
|
||||||
toCreds :: EveUser -> OAuth2Token -> Creds m
|
|
||||||
toCreds user token = Creds
|
|
||||||
{ credsPlugin = "eveonline"
|
|
||||||
, credsIdent = T.pack $ show $ eveCharOwnerHash user
|
|
||||||
, credsExtra =
|
|
||||||
[ ("charName", eveUserName user)
|
|
||||||
, ("charId", T.pack . show . eveCharId $ user)
|
|
||||||
, ("tokenType", eveTokenType user)
|
|
||||||
, ("expires", eveUserExpire user)
|
|
||||||
, ("accessToken", atoken $ accessToken token)
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user