aboutsummaryrefslogtreecommitdiff
path: root/src/Sproxy/Application/OAuth2/Google.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Sproxy/Application/OAuth2/Google.hs')
-rw-r--r--src/Sproxy/Application/OAuth2/Google.hs100
1 files changed, 51 insertions, 49 deletions
diff --git a/src/Sproxy/Application/OAuth2/Google.hs b/src/Sproxy/Application/OAuth2/Google.hs
index b2ea2c1..82c08c0 100644
--- a/src/Sproxy/Application/OAuth2/Google.hs
+++ b/src/Sproxy/Application/OAuth2/Google.hs
@@ -7,8 +7,7 @@ module Sproxy.Application.OAuth2.Google
import Control.Applicative (empty)
import Control.Exception (Exception, throwIO)
-import Data.Aeson
- (FromJSON, Value(Object), (.:), decode, parseJSON)
+import Data.Aeson (FromJSON, Value(Object), (.:), decode, parseJSON)
import Data.ByteString.Lazy (ByteString)
import Data.Monoid ((<>))
import Data.Text (Text, unpack)
@@ -16,58 +15,61 @@ import Data.Typeable (Typeable)
import qualified Network.HTTP.Conduit as H
import Network.HTTP.Types.URI (urlEncode)
-import Sproxy.Application.Cookie
- (newUser, setFamilyName, setGivenName)
+import Sproxy.Application.Cookie (newUser, setFamilyName, setGivenName)
import Sproxy.Application.OAuth2.Common
- (AccessTokenBody(accessToken), OAuth2Client(..), OAuth2Provider)
+ ( AccessTokenBody(accessToken)
+ , OAuth2Client(..)
+ , OAuth2Provider
+ )
provider :: OAuth2Provider
provider (client_id, client_secret) =
OAuth2Client
- { oauth2Description = "Google"
- , oauth2AuthorizeURL =
- \state redirect_uri ->
- "https://accounts.google.com/o/oauth2/v2/auth" <> "?scope=" <>
- urlEncode
- True
- "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile" <>
- "&client_id=" <>
- urlEncode True client_id <>
- "&prompt=select_account" <>
- "&redirect_uri=" <>
- urlEncode True redirect_uri <>
- "&response_type=code" <>
- "&state=" <>
- urlEncode True state
- , oauth2Authenticate =
- \code redirect_uri -> do
- let treq =
- H.urlEncodedBody
- [ ("client_id", client_id)
- , ("client_secret", client_secret)
- , ("code", code)
- , ("grant_type", "authorization_code")
- , ("redirect_uri", redirect_uri)
- ] $
- H.parseRequest_ "POST https://www.googleapis.com/oauth2/v4/token"
- mgr <- H.newManager H.tlsManagerSettings
- tresp <- H.httpLbs treq mgr
- case decode $ H.responseBody tresp of
- Nothing -> throwIO $ GoogleException tresp
- Just atResp -> do
- ureq <-
- H.parseRequest $
- unpack
- ("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" <>
- accessToken atResp)
- uresp <- H.httpLbs ureq mgr
- case decode $ H.responseBody uresp of
- Nothing -> throwIO $ GoogleException uresp
- Just u ->
- return $
- setFamilyName (familyName u) $
- setGivenName (givenName u) $ newUser (email u)
- }
+ { oauth2Description = "Google"
+ , oauth2AuthorizeURL =
+ \state redirect_uri ->
+ "https://accounts.google.com/o/oauth2/v2/auth" <> "?scope=" <>
+ urlEncode
+ True
+ "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile" <>
+ "&client_id=" <>
+ urlEncode True client_id <>
+ "&prompt=select_account" <>
+ "&redirect_uri=" <>
+ urlEncode True redirect_uri <>
+ "&response_type=code" <>
+ "&state=" <>
+ urlEncode True state
+ , oauth2Authenticate =
+ \code redirect_uri -> do
+ let treq =
+ H.urlEncodedBody
+ [ ("client_id", client_id)
+ , ("client_secret", client_secret)
+ , ("code", code)
+ , ("grant_type", "authorization_code")
+ , ("redirect_uri", redirect_uri)
+ ] $
+ H.parseRequest_
+ "POST https://www.googleapis.com/oauth2/v4/token"
+ mgr <- H.newManager H.tlsManagerSettings
+ tresp <- H.httpLbs treq mgr
+ case decode $ H.responseBody tresp of
+ Nothing -> throwIO $ GoogleException tresp
+ Just atResp -> do
+ ureq <-
+ H.parseRequest $
+ unpack
+ ("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" <>
+ accessToken atResp)
+ uresp <- H.httpLbs ureq mgr
+ case decode $ H.responseBody uresp of
+ Nothing -> throwIO $ GoogleException uresp
+ Just u ->
+ return $
+ setFamilyName (familyName u) $
+ setGivenName (givenName u) $ newUser (email u)
+ }
data GoogleException =
GoogleException (H.Response ByteString)