mirror of
https://github.com/freckle/yesod-auth-oauth2.git
synced 2026-04-10 05:09:03 +02:00
Merge pull request #34 from thoughtbot/pb-fixes
Prep for release after Eve Online merge
This commit is contained in:
commit
a4f9dc91f5
@ -48,6 +48,10 @@ instance Exception YesodOAuth2Exception
|
|||||||
oauth2Url :: Text -> AuthRoute
|
oauth2Url :: Text -> AuthRoute
|
||||||
oauth2Url name = PluginR name ["forward"]
|
oauth2Url name = PluginR name ["forward"]
|
||||||
|
|
||||||
|
-- | Create an @'AuthPlugin'@ for the given OAuth2 provider
|
||||||
|
--
|
||||||
|
-- Presents a generic @"Login via name"@ link
|
||||||
|
--
|
||||||
authOAuth2 :: YesodAuth m
|
authOAuth2 :: YesodAuth m
|
||||||
=> Text -- ^ Service name
|
=> Text -- ^ Service name
|
||||||
-> OAuth2 -- ^ Service details
|
-> OAuth2 -- ^ Service details
|
||||||
@ -59,21 +63,20 @@ authOAuth2 :: YesodAuth m
|
|||||||
--
|
--
|
||||||
-- See @'fromProfileURL'@ for an example.
|
-- See @'fromProfileURL'@ for an example.
|
||||||
-> AuthPlugin m
|
-> AuthPlugin m
|
||||||
authOAuth2 name oauth getCreds = authOAuth2Widget name oauth getCreds [whamlet|Login via #{name}|]
|
authOAuth2 name = authOAuth2Widget [whamlet|Login via #{name}|] name
|
||||||
|
|
||||||
|
-- | Create an @'AuthPlugin'@ for the given OAuth2 provider
|
||||||
|
--
|
||||||
|
-- Allows passing a custom widget for the login link. See @'oauth2Eve'@ for an
|
||||||
|
-- example.
|
||||||
|
--
|
||||||
authOAuth2Widget :: YesodAuth m
|
authOAuth2Widget :: YesodAuth m
|
||||||
=> Text -- ^ Service name
|
=> WidgetT m IO ()
|
||||||
-> OAuth2 -- ^ Service details
|
-> Text
|
||||||
|
-> OAuth2
|
||||||
-> (Manager -> AccessToken -> IO (Creds m))
|
-> (Manager -> AccessToken -> IO (Creds m))
|
||||||
-- ^ This function defines how to take an @'AccessToken'@ and
|
|
||||||
-- retrieve additional information about the user, to be
|
|
||||||
-- set in the session as @'Creds'@. Usually this means a
|
|
||||||
-- second authorized request to @api/me.json@.
|
|
||||||
--
|
|
||||||
-- See @'fromProfileURL'@ for an example.
|
|
||||||
-> WidgetT m IO () -- ^ Widget to be shown instead of "Login with xxx"-Text
|
|
||||||
-> AuthPlugin m
|
-> AuthPlugin m
|
||||||
authOAuth2Widget name oauth getCreds widget = AuthPlugin name dispatch login
|
authOAuth2Widget widget name oauth getCreds = AuthPlugin name dispatch login
|
||||||
|
|
||||||
where
|
where
|
||||||
url = PluginR name ["callback"]
|
url = PluginR name ["callback"]
|
||||||
@ -118,12 +121,9 @@ authOAuth2Widget name oauth getCreds widget = AuthPlugin name dispatch login
|
|||||||
tokenSessionKey :: Text
|
tokenSessionKey :: Text
|
||||||
tokenSessionKey = "_yesod_oauth2_" <> name
|
tokenSessionKey = "_yesod_oauth2_" <> name
|
||||||
|
|
||||||
login tm = [whamlet|
|
login tm = [whamlet|<a href=@{tm $ oauth2Url name}>^{widget}|]
|
||||||
<a href=@{tm $ oauth2Url name}>
|
|
||||||
^{widget}
|
|
||||||
|]
|
|
||||||
|
|
||||||
-- | Handle the common case of fetching Profile information a JSON endpoint
|
-- | Handle the common case of fetching Profile information from a JSON endpoint
|
||||||
--
|
--
|
||||||
-- Throws @'InvalidProfileResponse'@ if JSON parsing fails
|
-- Throws @'InvalidProfileResponse'@ if JSON parsing fails
|
||||||
--
|
--
|
||||||
|
|||||||
@ -7,11 +7,10 @@
|
|||||||
--
|
--
|
||||||
-- * 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, tokenType and expires as extras
|
-- * Returns charName, charId, tokenType, accessToken and expires as extras
|
||||||
--
|
--
|
||||||
module Yesod.Auth.OAuth2.EveOnline
|
module Yesod.Auth.OAuth2.EveOnline
|
||||||
( oauth2Eve
|
( oauth2Eve
|
||||||
, oauth2EveWidget
|
|
||||||
, oauth2EveScoped
|
, oauth2EveScoped
|
||||||
, WidgetType(..)
|
, WidgetType(..)
|
||||||
, module Yesod.Auth.OAuth2
|
, module Yesod.Auth.OAuth2
|
||||||
@ -25,8 +24,7 @@ import Control.Exception.Lifted
|
|||||||
import Control.Monad (mzero)
|
import Control.Monad (mzero)
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
import Data.Monoid ((<>))
|
import Data.Monoid ((<>))
|
||||||
import Data.Text as T (Text,unwords)
|
import Data.Text (Text)
|
||||||
import Data.ByteString as B (ByteString)
|
|
||||||
import Data.Text.Encoding (encodeUtf8, decodeUtf8)
|
import Data.Text.Encoding (encodeUtf8, decodeUtf8)
|
||||||
import Network.HTTP.Conduit (Manager)
|
import Network.HTTP.Conduit (Manager)
|
||||||
import Yesod.Auth
|
import Yesod.Auth
|
||||||
@ -35,63 +33,57 @@ import Yesod.Core.Widget
|
|||||||
|
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
|
|
||||||
data YesodAuth m => WidgetType m
|
data WidgetType m
|
||||||
= BigWhite
|
= Plain -- ^ Simple "Login via eveonline" text
|
||||||
|
| BigWhite
|
||||||
| SmallWhite
|
| SmallWhite
|
||||||
| BigBlack
|
| BigBlack
|
||||||
| SmallBlack
|
| SmallBlack
|
||||||
| Custom (WidgetT m IO ())
|
| Custom (WidgetT m IO ())
|
||||||
|
|
||||||
data EveUser = EveUser
|
data EveUser = EveUser
|
||||||
{ eveUserId :: Int
|
{ eveUserName :: Text
|
||||||
, eveUserName :: Text
|
|
||||||
, eveUserExpire :: Text
|
, eveUserExpire :: Text
|
||||||
, eveScopes :: [Text]
|
|
||||||
, eveTokenType :: Text
|
, eveTokenType :: Text
|
||||||
, eveCharOwnerHash :: Text
|
, eveCharOwnerHash :: Text
|
||||||
|
, eveCharId :: Integer
|
||||||
}
|
}
|
||||||
|
|
||||||
instance FromJSON EveUser where
|
instance FromJSON EveUser where
|
||||||
parseJSON (Object o) = EveUser
|
parseJSON (Object o) = EveUser
|
||||||
<$> o .: "CharacterID"
|
<$> o .: "CharacterName"
|
||||||
<*> o .: "CharacterName"
|
|
||||||
<*> o .: "ExpiresOn"
|
<*> o .: "ExpiresOn"
|
||||||
<*> (T.words <$> o .: "Scopes")
|
|
||||||
<*> o .: "TokenType"
|
<*> o .: "TokenType"
|
||||||
<*> o .: "CharacterOwnerHash"
|
<*> o .: "CharacterOwnerHash"
|
||||||
|
<*> o .: "CharacterID"
|
||||||
|
|
||||||
parseJSON _ = mzero
|
parseJSON _ = mzero
|
||||||
|
|
||||||
oauth2Eve :: YesodAuth m
|
oauth2Eve :: YesodAuth m
|
||||||
=> Text -- ^ Client ID
|
=> Text -- ^ Client ID
|
||||||
-> Text -- ^ Client Secret
|
-> Text -- ^ Client Secret
|
||||||
|
-> WidgetType m
|
||||||
-> AuthPlugin m
|
-> AuthPlugin m
|
||||||
oauth2Eve clientId clientSecret = oauth2EveScoped clientId clientSecret ["publicData"] Nothing
|
oauth2Eve clientId clientSecret = oauth2EveScoped clientId clientSecret ["publicData"] . asWidget
|
||||||
|
|
||||||
oauth2EveWidget :: YesodAuth m
|
|
||||||
=> Text -- ^ Client ID
|
|
||||||
-> Text -- ^ Client Secret
|
|
||||||
-> WidgetType m
|
|
||||||
-> AuthPlugin m
|
|
||||||
oauth2EveWidget clientId clientSecret w = oauth2EveScoped clientId clientSecret ["publicData"] (Just . toWidget $ w)
|
|
||||||
where
|
where
|
||||||
toWidget :: YesodAuth m => WidgetType m -> WidgetT m IO ()
|
asWidget :: YesodAuth m => WidgetType m -> WidgetT m IO ()
|
||||||
toWidget BigWhite = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/4PTzeiAshqiM8osU2giO0Y/5cc4cb60bac52422da2e45db87b6819c/EVE_SSO_Login_Buttons_Large_White.png?w=270&h=45">|]
|
asWidget Plain = [whamlet|Login via eveonline|]
|
||||||
toWidget BigBlack = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/4fSjj56uD6CYwYyus4KmES/4f6385c91e6de56274d99496e6adebab/EVE_SSO_Login_Buttons_Large_Black.png?w=270&h=45">|]
|
asWidget BigWhite = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/4PTzeiAshqiM8osU2giO0Y/5cc4cb60bac52422da2e45db87b6819c/EVE_SSO_Login_Buttons_Large_White.png?w=270&h=45">|]
|
||||||
toWidget SmallWhite = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/18BxKSXCymyqY4QKo8KwKe/c2bdded6118472dd587c8107f24104d7/EVE_SSO_Login_Buttons_Small_White.png?w=195&h=30">|]
|
asWidget BigBlack = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/4fSjj56uD6CYwYyus4KmES/4f6385c91e6de56274d99496e6adebab/EVE_SSO_Login_Buttons_Large_Black.png?w=270&h=45">|]
|
||||||
toWidget SmallBlack = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/12vrPsIMBQi28QwCGOAqGk/33234da7672c6b0cdca394fc8e0b1c2b/EVE_SSO_Login_Buttons_Small_Black.png?w=195&h=30">|]
|
asWidget SmallWhite = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/18BxKSXCymyqY4QKo8KwKe/c2bdded6118472dd587c8107f24104d7/EVE_SSO_Login_Buttons_Small_White.png?w=195&h=30">|]
|
||||||
toWidget (Custom a) = a
|
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
|
oauth2EveScoped :: YesodAuth m
|
||||||
=> Text -- ^ Client ID
|
=> Text -- ^ Client ID
|
||||||
-> Text -- ^ Client Secret
|
-> Text -- ^ Client Secret
|
||||||
-> [Text] -- ^ List of scopes to request
|
-> [Text] -- ^ List of scopes to request
|
||||||
-> Maybe (WidgetT m IO ()) -- ^ Login-Widget
|
-> WidgetT m IO () -- ^ Login widget
|
||||||
-> AuthPlugin m
|
-> AuthPlugin m
|
||||||
oauth2EveScoped clientId clientSecret scopes widget =
|
oauth2EveScoped clientId clientSecret scopes widget =
|
||||||
case widget of
|
authOAuth2Widget widget "eveonline" oauth fetchEveProfile
|
||||||
Just w -> authOAuth2Widget "eveonline" oauth fetchEveProfile w
|
|
||||||
Nothing -> authOAuth2 "eveonline" oauth fetchEveProfile
|
|
||||||
where
|
where
|
||||||
oauth = OAuth2
|
oauth = OAuth2
|
||||||
{ oauthClientId = encodeUtf8 clientId
|
{ oauthClientId = encodeUtf8 clientId
|
||||||
@ -115,7 +107,9 @@ toCreds user token = Creds
|
|||||||
, credsIdent = T.pack $ show $ eveCharOwnerHash user
|
, credsIdent = T.pack $ show $ eveCharOwnerHash user
|
||||||
, credsExtra =
|
, credsExtra =
|
||||||
[ ("charName", eveUserName user)
|
[ ("charName", eveUserName user)
|
||||||
|
, ("charId", T.pack . show . eveCharId $ user)
|
||||||
, ("tokenType", eveTokenType user)
|
, ("tokenType", eveTokenType user)
|
||||||
, ("expires", eveUserExpire user)
|
, ("expires", eveUserExpire user)
|
||||||
|
, ("accessToken", decodeUtf8 . accessToken $ token)
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user