mirror of
https://github.com/freckle/yesod-auth-oauth2.git
synced 2026-01-30 04:40:25 +01:00
- Update to ghc-8.8 / lts-16.0 - Update to hoauth2 >= 1.11.0 - authGetBS has pre-encoded errors a v1.9 - oauthClientSecret is Maybe at v1.11 - Tweak non-default Resolvers as required
50 lines
1.3 KiB
Haskell
50 lines
1.3 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
-- |
|
|
--
|
|
-- OAuth2 plugin for http://upcase.com
|
|
--
|
|
-- * Authenticates against upcase
|
|
-- * Uses upcase user id as credentials identifier
|
|
--
|
|
module Yesod.Auth.OAuth2.Upcase
|
|
( oauth2Upcase
|
|
)
|
|
where
|
|
|
|
import Yesod.Auth.OAuth2.Prelude
|
|
|
|
import qualified Data.Text as T
|
|
|
|
newtype User = User Int
|
|
|
|
instance FromJSON User where
|
|
parseJSON = withObject "User" $ \root -> do
|
|
o <- root .: "user"
|
|
User <$> o .: "id"
|
|
|
|
pluginName :: Text
|
|
pluginName = "upcase"
|
|
|
|
oauth2Upcase :: YesodAuth m => Text -> Text -> AuthPlugin m
|
|
oauth2Upcase clientId clientSecret =
|
|
authOAuth2 pluginName oauth2 $ \manager token -> do
|
|
(User userId, userResponse) <- authGetProfile
|
|
pluginName
|
|
manager
|
|
token
|
|
"http://upcase.com/api/v1/me.json"
|
|
|
|
pure Creds
|
|
{ credsPlugin = pluginName
|
|
, credsIdent = T.pack $ show userId
|
|
, credsExtra = setExtra token userResponse
|
|
}
|
|
where
|
|
oauth2 = OAuth2
|
|
{ oauthClientId = clientId
|
|
, oauthClientSecret = Just clientSecret
|
|
, oauthOAuthorizeEndpoint = "http://upcase.com/oauth/authorize"
|
|
, oauthAccessTokenEndpoint = "http://upcase.com/oauth/token"
|
|
, oauthCallback = Nothing
|
|
}
|