changed image-display to widget-display

This commit is contained in:
Stefan Dresselhaus 2015-06-28 18:00:18 +02:00
parent abd11594d2
commit 77db081d6c
2 changed files with 37 additions and 35 deletions

View File

@ -11,7 +11,7 @@
-- --
module Yesod.Auth.OAuth2 module Yesod.Auth.OAuth2
( authOAuth2 ( authOAuth2
, authOAuth2Image , authOAuth2Widget
, oauth2Url , oauth2Url
, fromProfileURL , fromProfileURL
, YesodOAuth2Exception(..) , YesodOAuth2Exception(..)
@ -59,21 +59,21 @@ authOAuth2 :: YesodAuth m
-- --
-- See @'fromProfileURL'@ for an example. -- See @'fromProfileURL'@ for an example.
-> AuthPlugin m -> AuthPlugin m
authOAuth2 name oauth getCreds = authOAuth2Image name oauth getCreds Nothing authOAuth2 name oauth getCreds = authOAuth2Widget name oauth getCreds Nothing
authOAuth2Image :: YesodAuth m authOAuth2Widget :: YesodAuth m
=> Text -- ^ Service name => Text -- ^ Service name
-> OAuth2 -- ^ Service details -> OAuth2 -- ^ Service details
-> (Manager -> AccessToken -> IO (Creds m)) -> (Manager -> AccessToken -> IO (Creds m))
-- ^ This function defines how to take an @'AccessToken'@ and -- ^ This function defines how to take an @'AccessToken'@ and
-- retrieve additional information about the user, to be -- retrieve additional information about the user, to be
-- set in the session as @'Creds'@. Usually this means a -- set in the session as @'Creds'@. Usually this means a
-- second authorized request to @api/me.json@. -- second authorized request to @api/me.json@.
-- --
-- See @'fromProfileURL'@ for an example. -- See @'fromProfileURL'@ for an example.
-> Maybe Text -- ^ URL to image shown instead of "Login with xxx"-Text -> Maybe (WidgetT m IO ()) -- ^ Widget to be shown instead of "Login with xxx"-Text
-> AuthPlugin m -> AuthPlugin m
authOAuth2Image name oauth getCreds im = AuthPlugin name dispatch login authOAuth2Widget name oauth getCreds widget = AuthPlugin name dispatch login
where where
url = PluginR name ["callback"] url = PluginR name ["callback"]
@ -120,8 +120,8 @@ authOAuth2Image name oauth getCreds im = AuthPlugin name dispatch login
login tm = [whamlet| login tm = [whamlet|
<a href=@{tm $ oauth2Url name}> <a href=@{tm $ oauth2Url name}>
$maybe image <- im $maybe w <- widget
<img src=#{image}> ^{w}
$nothing $nothing
Login via #{name} Login via #{name}
|] |]

View File

@ -1,5 +1,6 @@
{-# LANGUAGE CPP #-} {-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
-- | -- |
-- --
-- OAuth2 plugin for http://eveonline.com -- OAuth2 plugin for http://eveonline.com
@ -10,9 +11,9 @@
-- --
module Yesod.Auth.OAuth2.EveOnline module Yesod.Auth.OAuth2.EveOnline
( oauth2Eve ( oauth2Eve
, oauth2EveImage , oauth2EveWidget
, oauth2EveScoped , oauth2EveScoped
, ImageType(..) , WidgetType(..)
, module Yesod.Auth.OAuth2 , module Yesod.Auth.OAuth2
) where ) where
@ -30,15 +31,16 @@ import Data.Text.Encoding (encodeUtf8, decodeUtf8)
import Network.HTTP.Conduit (Manager) import Network.HTTP.Conduit (Manager)
import Yesod.Auth import Yesod.Auth
import Yesod.Auth.OAuth2 import Yesod.Auth.OAuth2
import Yesod.Core.Widget
import qualified Data.Text as T import qualified Data.Text as T
data ImageType data YesodAuth m => WidgetType m
= BigWhite = BigWhite
| SmallWhite | SmallWhite
| BigBlack | BigBlack
| SmallBlack | SmallBlack
| Custom Text | Custom (WidgetT m IO ())
data EveUser = EveUser data EveUser = EveUser
{ eveUserId :: Int { eveUserId :: Int
@ -66,27 +68,27 @@ oauth2Eve :: YesodAuth m
-> AuthPlugin m -> AuthPlugin m
oauth2Eve clientId clientSecret = oauth2EveScoped clientId clientSecret ["publicData"] Nothing oauth2Eve clientId clientSecret = oauth2EveScoped clientId clientSecret ["publicData"] Nothing
oauth2EveImage :: YesodAuth m oauth2EveWidget :: YesodAuth m
=> Text -- ^ Client ID => Text -- ^ Client ID
-> Text -- ^ Client Secret -> Text -- ^ Client Secret
-> ImageType -> WidgetType m
-> AuthPlugin m -> AuthPlugin m
oauth2EveImage clientId clientSecret im = oauth2EveScoped clientId clientSecret ["publicData"] (Just . toURI $ im) oauth2EveWidget clientId clientSecret w = oauth2EveScoped clientId clientSecret ["publicData"] (Just . toWidget $ w)
where where
toURI :: ImageType -> Text toWidget :: YesodAuth m => WidgetType m -> WidgetT m IO ()
toURI BigWhite = "https://images.contentful.com/idjq7aai9ylm/4PTzeiAshqiM8osU2giO0Y/5cc4cb60bac52422da2e45db87b6819c/EVE_SSO_Login_Buttons_Large_White.png?w=270&h=45" toWidget BigWhite = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/4PTzeiAshqiM8osU2giO0Y/5cc4cb60bac52422da2e45db87b6819c/EVE_SSO_Login_Buttons_Large_White.png?w=270&h=45">|]
toURI BigBlack = "https://images.contentful.com/idjq7aai9ylm/4fSjj56uD6CYwYyus4KmES/4f6385c91e6de56274d99496e6adebab/EVE_SSO_Login_Buttons_Large_Black.png?w=270&h=45" toWidget BigBlack = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/4fSjj56uD6CYwYyus4KmES/4f6385c91e6de56274d99496e6adebab/EVE_SSO_Login_Buttons_Large_Black.png?w=270&h=45">|]
toURI SmallWhite = "https://images.contentful.com/idjq7aai9ylm/18BxKSXCymyqY4QKo8KwKe/c2bdded6118472dd587c8107f24104d7/EVE_SSO_Login_Buttons_Small_White.png?w=195&h=30" toWidget SmallWhite = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/18BxKSXCymyqY4QKo8KwKe/c2bdded6118472dd587c8107f24104d7/EVE_SSO_Login_Buttons_Small_White.png?w=195&h=30">|]
toURI SmallBlack = "https://images.contentful.com/idjq7aai9ylm/12vrPsIMBQi28QwCGOAqGk/33234da7672c6b0cdca394fc8e0b1c2b/EVE_SSO_Login_Buttons_Small_Black.png?w=195&h=30" toWidget SmallBlack = [whamlet|<img src="https://images.contentful.com/idjq7aai9ylm/12vrPsIMBQi28QwCGOAqGk/33234da7672c6b0cdca394fc8e0b1c2b/EVE_SSO_Login_Buttons_Small_Black.png?w=195&h=30">|]
toURI (Custom a) = a toWidget (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 Text -- ^ Login-Image -> Maybe (WidgetT m IO ()) -- ^ Login-Widget
-> AuthPlugin m -> AuthPlugin m
oauth2EveScoped clientId clientSecret scopes = authOAuth2Image "eveonline" oauth fetchEveProfile oauth2EveScoped clientId clientSecret scopes = authOAuth2Widget "eveonline" oauth fetchEveProfile
where where
oauth = OAuth2 oauth = OAuth2
{ oauthClientId = encodeUtf8 clientId { oauthClientId = encodeUtf8 clientId