diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2019-09-06 18:34:28 +0200 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2019-09-06 18:34:43 +0200 |
commit | 6df3894131699a3a81e5e4da1565268eb01639c6 (patch) | |
tree | f1bb612c6e71f5f75f2bc0ad123127d7edb54a09 /src/Sproxy/Application | |
parent | 5af03230208f355c53c3b8f763d9533f9e859411 (diff) | |
download | sproxy2-6df3894131699a3a81e5e4da1565268eb01639c6.tar.gz |
Reformat with modern hindent 5.2.7
Diffstat (limited to 'src/Sproxy/Application')
-rw-r--r-- | src/Sproxy/Application/Cookie.hs | 14 | ||||
-rw-r--r-- | src/Sproxy/Application/OAuth2/Google.hs | 100 | ||||
-rw-r--r-- | src/Sproxy/Application/OAuth2/LinkedIn.hs | 101 | ||||
-rw-r--r-- | src/Sproxy/Application/OAuth2/Yandex.hs | 88 |
4 files changed, 154 insertions, 149 deletions
diff --git a/src/Sproxy/Application/Cookie.hs b/src/Sproxy/Application/Cookie.hs index a9a8ad6..d027e19 100644 --- a/src/Sproxy/Application/Cookie.hs +++ b/src/Sproxy/Application/Cookie.hs @@ -42,9 +42,9 @@ instance DS.Serialize AuthCookie where (e, n, f, x) <- DS.get return AuthCookie - { acUser = AuthUser {auEmail = e, auGivenName = n, auFamilyName = f} - , acExpiry = CTime x - } + { acUser = AuthUser {auEmail = e, auGivenName = n, auFamilyName = f} + , acExpiry = CTime x + } cookieDecode :: ByteString -> ByteString -> Either String AuthCookie cookieDecode key d = State.decode key d >>= DS.decode @@ -67,10 +67,10 @@ getFamilyNameUtf8 = auFamilyName newUser :: Text -> AuthUser newUser email = AuthUser - { auEmail = encodeUtf8 . toLower . strip $ email - , auGivenName = "" - , auFamilyName = "" - } + { auEmail = encodeUtf8 . toLower . strip $ email + , auGivenName = "" + , auFamilyName = "" + } setGivenName :: Text -> AuthUser -> AuthUser setGivenName given au = au {auGivenName = encodeUtf8 . strip $ given} 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) diff --git a/src/Sproxy/Application/OAuth2/LinkedIn.hs b/src/Sproxy/Application/OAuth2/LinkedIn.hs index 3fdd7be..905aa56 100644 --- a/src/Sproxy/Application/OAuth2/LinkedIn.hs +++ b/src/Sproxy/Application/OAuth2/LinkedIn.hs @@ -7,8 +7,7 @@ module Sproxy.Application.OAuth2.LinkedIn 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) @@ -17,60 +16,62 @@ 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 = "LinkedIn" - , oauth2AuthorizeURL = - \state redirect_uri -> - "https://www.linkedin.com/oauth/v2/authorization" <> - "?scope=r_basicprofile%20r_emailaddress" <> - "&client_id=" <> - urlEncode True client_id <> - "&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.linkedin.com/oauth/v2/accessToken" - mgr <- H.newManager H.tlsManagerSettings - tresp <- H.httpLbs treq mgr - case decode $ H.responseBody tresp of - Nothing -> throwIO $ LinkedInException tresp - Just atResp -> do - let ureq = - (H.parseRequest_ - "https://api.linkedin.com/v1/people/\ + { oauth2Description = "LinkedIn" + , oauth2AuthorizeURL = + \state redirect_uri -> + "https://www.linkedin.com/oauth/v2/authorization" <> + "?scope=r_basicprofile%20r_emailaddress" <> + "&client_id=" <> + urlEncode True client_id <> + "&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.linkedin.com/oauth/v2/accessToken" + mgr <- H.newManager H.tlsManagerSettings + tresp <- H.httpLbs treq mgr + case decode $ H.responseBody tresp of + Nothing -> throwIO $ LinkedInException tresp + Just atResp -> do + let ureq = + (H.parseRequest_ + "https://api.linkedin.com/v1/people/\ \~:(email-address,first-name,last-name)?format=json") - { H.requestHeaders = - [ ( "Authorization" - , "Bearer " <> encodeUtf8 (accessToken atResp)) - ] - } - uresp <- H.httpLbs ureq mgr - case decode $ H.responseBody uresp of - Nothing -> throwIO $ LinkedInException uresp - Just u -> - return $ - setFamilyName (lastName u) $ - setGivenName (firstName u) $ newUser (emailAddress u) - } + { H.requestHeaders = + [ ( "Authorization" + , "Bearer " <> encodeUtf8 (accessToken atResp)) + ] + } + uresp <- H.httpLbs ureq mgr + case decode $ H.responseBody uresp of + Nothing -> throwIO $ LinkedInException uresp + Just u -> + return $ + setFamilyName (lastName u) $ + setGivenName (firstName u) $ newUser (emailAddress u) + } data LinkedInException = LinkedInException (H.Response ByteString) diff --git a/src/Sproxy/Application/OAuth2/Yandex.hs b/src/Sproxy/Application/OAuth2/Yandex.hs index e943a39..174cca9 100644 --- a/src/Sproxy/Application/OAuth2/Yandex.hs +++ b/src/Sproxy/Application/OAuth2/Yandex.hs @@ -7,8 +7,7 @@ module Sproxy.Application.OAuth2.Yandex 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) @@ -17,52 +16,55 @@ 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 = "Yandex" - , oauth2AuthorizeURL = - \state _redirect_uri -> - "https://oauth.yandex.ru/authorize" <> "?state=" <> urlEncode True state <> - "&client_id=" <> - urlEncode True client_id <> - "&response_type=code" <> - "&force_confirm=yes" - , oauth2Authenticate = - \code _redirect_uri -> do - let treq = - H.urlEncodedBody - [ ("grant_type", "authorization_code") - , ("client_id", client_id) - , ("client_secret", client_secret) - , ("code", code) - ] $ - H.parseRequest_ "POST https://oauth.yandex.ru/token" - mgr <- H.newManager H.tlsManagerSettings - tresp <- H.httpLbs treq mgr - case decode $ H.responseBody tresp of - Nothing -> throwIO $ YandexException tresp - Just atResp -> do - let ureq = - (H.parseRequest_ "https://login.yandex.ru/info?format=json") - { H.requestHeaders = - [ ( "Authorization" - , "OAuth " <> encodeUtf8 (accessToken atResp)) - ] - } - uresp <- H.httpLbs ureq mgr - case decode $ H.responseBody uresp of - Nothing -> throwIO $ YandexException uresp - Just u -> - return $ - setFamilyName (lastName u) $ - setGivenName (firstName u) $ newUser (defaultEmail u) - } + { oauth2Description = "Yandex" + , oauth2AuthorizeURL = + \state _redirect_uri -> + "https://oauth.yandex.ru/authorize" <> "?state=" <> + urlEncode True state <> + "&client_id=" <> + urlEncode True client_id <> + "&response_type=code" <> + "&force_confirm=yes" + , oauth2Authenticate = + \code _redirect_uri -> do + let treq = + H.urlEncodedBody + [ ("grant_type", "authorization_code") + , ("client_id", client_id) + , ("client_secret", client_secret) + , ("code", code) + ] $ + H.parseRequest_ "POST https://oauth.yandex.ru/token" + mgr <- H.newManager H.tlsManagerSettings + tresp <- H.httpLbs treq mgr + case decode $ H.responseBody tresp of + Nothing -> throwIO $ YandexException tresp + Just atResp -> do + let ureq = + (H.parseRequest_ "https://login.yandex.ru/info?format=json") + { H.requestHeaders = + [ ( "Authorization" + , "OAuth " <> encodeUtf8 (accessToken atResp)) + ] + } + uresp <- H.httpLbs ureq mgr + case decode $ H.responseBody uresp of + Nothing -> throwIO $ YandexException uresp + Just u -> + return $ + setFamilyName (lastName u) $ + setGivenName (firstName u) $ newUser (defaultEmail u) + } data YandexException = YandexException (H.Response ByteString) |