Skip to content
Permalink
Browse files

Switch to new Google login

  • Loading branch information...
snoyberg committed Feb 12, 2019
1 parent 6ac34e7 commit f77bba90d9684afb532639c68e64449523992535
Showing with 22 additions and 7 deletions.
  1. +2 −0 package.yaml
  2. +16 −6 src/Foundation.hs
  3. +3 −0 stack.yaml
  4. +1 −1 templates/login.hamlet
@@ -36,6 +36,7 @@ dependencies:
- yesod-core
- yesod-auth
- yesod-auth-fb
- yesod-auth-oauth2
- yesod-fb
- yesod-static
- yesod-form
@@ -79,6 +80,7 @@ dependencies:
- aeson
- cipher-aes
- yesod-gitrev
- unordered-containers

library:
source-dirs: src
@@ -33,12 +33,13 @@ import Yesod.Auth
import Yesod.Auth.Dummy
import Yesod.Auth.OpenId
import Yesod.Auth.Facebook.ServerSide
import qualified Yesod.Auth.GoogleEmail2 as Google
import Yesod.Auth.OAuth2 (oauth2Url)
import qualified Yesod.Auth.OAuth2.Google as Google
import Facebook (Credentials (Credentials))
import Yesod.Default.Config
import Yesod.Default.Util (addStaticContentExternal)
import Network.HTTP.Conduit (Manager)
import Control.Monad (unless)
import Control.Monad (unless, guard)
import Data.Char (isSpace)
import qualified Settings
import qualified Database.Persist
@@ -52,9 +53,10 @@ import Text.Hamlet
import Blaze.ByteString.Builder.Char.Utf8 (fromText)
import Network.HTTP.Types (encodePath, queryTextToQuery)
import Data.Text (Text)
import Data.Text.Encoding (encodeUtf8)
import qualified Data.Text as T
import qualified Data.Text.Read
import Data.Maybe (fromJust)
import Data.Maybe (fromJust, fromMaybe)
import Data.Time
import qualified Data.Set as Set
import Yesod.Form.Jquery
@@ -66,6 +68,8 @@ import Data.IORef (IORef)
import Yesod.Facebook
import Yesod.GitRev (GitRev)
import Network.Mail.Mime.SES (SES)
import qualified Data.HashMap.Strict as HM
import Data.Aeson (decodeStrict)

-- | The site argument for your application. This can be a good place to
-- keep settings and values requiring initialization before your application
@@ -374,7 +378,7 @@ instance YesodAuth App where
loginDest _ = ProfileR
logoutDest _ = RootR

getAuthId creds = do
getAuthId creds0 = do
muid <- maybeAuth
x <- liftHandler $ runDB $ do
x1 <- getBy $ UniqueIdent $ credsIdentClaimed creds
@@ -438,8 +442,14 @@ instance YesodAuth App where
setMessage "That identifier is already attached to an account. Please detach it from the other account first."
redirect ProfileR
where
creds = fromMaybe creds0 $ do
guard $ credsPlugin creds0 == "google"
ur <- lookup "userResponse" $ credsExtra creds0

This comment has been minimized.

@pbrisbin

pbrisbin Feb 12, 2019

Minor FYI: I would use getUserResponse here. The fact that the key is userResponse is not considered public API for the library and may change.

This comment has been minimized.

@snoyberg

snoyberg Feb 12, 2019

Author Owner

Good call, thanks! I've used getUserResponseJSON

Object o <- decodeStrict $ encodeUtf8 ur
String email <- HM.lookup "email" o
Just creds0 { credsIdent = email }
addBIDEmail uid
| credsPlugin creds `elem` ["googleemail2"] = do
| credsPlugin creds `elem` ["google"] = do
u <- get404 uid
unless (userVerifiedEmail u) $ update uid [UserEmail =. Just (credsIdent creds), UserVerifiedEmail =. True]
| otherwise = return ()
@@ -452,7 +462,7 @@ instance YesodAuth App where
authPlugins app =
[ authOpenId OPLocal []
, authFacebook []
, uncurry Google.authGoogleEmail (appGoogleEmailCreds app)
, uncurry (Google.oauth2GoogleScoped ["email", "profile"]) (appGoogleEmailCreds app)
] ++ extraAuthPlugins
where extraAuthPlugins =
-- Determine if authDummy login setting was enabled.
@@ -6,6 +6,9 @@ extra-deps:
sha256: 427f1b2baec1635d3ba93e91eafcce2bb62a949e033d7a499db3257b8b90d2be
size: 2983
- githash-0.1.0.1@sha256:fd8110389b1e4febf2bca0acfca6505130606233058a3cdab96bd66d7b359b84,1552
- yesod-auth-oauth2-0.6.1.0@sha256:8dcee69de8e2f61da5ce1007b585ba079de764815c449ae0a6df35540ebf3511
- hoauth2-1.8.3@sha256:32e1a6fcdc999d3eef522ee31c165805626e5c154aa828d603402767d02f2bc6
- uri-bytestring-aeson-0.1.0.7@sha256:33d83caa4704f07112564b98cb4b2e7b87f3f185e2e520c8d3444b95967d11f3

image:
container:
@@ -1,6 +1,6 @@
<div .login-options>
<h3>Login using
<a href=@{AuthR Google.forwardUrl} .button .google>
<a href=@{AuthR (oauth2Url "google")} .button .google>
<img src="@{StaticR google_gif}" alt="Login via Google">
<form method="get" action="@{AuthR forwardUrl}" .button .yahoo>
<input type="hidden" name="openid_identifier" value="http://me.yahoo.com">

0 comments on commit f77bba9

Please sign in to comment.
You can’t perform that action at this time.