mirror of
https://github.com/freckle/yesod-auth-oauth2.git
synced 2026-09-16 22:24:57 +02:00
Use CPP to get 2.7.0 to compile
This commit is contained in:
parent
17c778a47f
commit
cf43baac7a
@ -3,6 +3,8 @@
|
||||
module Network.OAuth.OAuth2.Compat
|
||||
( OAuth2(..)
|
||||
, OAuth2Result
|
||||
, Error
|
||||
, Errors
|
||||
, authorizationUrl
|
||||
, fetchAccessToken
|
||||
, fetchAccessToken2
|
||||
@ -15,6 +17,14 @@ module Network.OAuth.OAuth2.Compat
|
||||
import Data.ByteString.Lazy (ByteString)
|
||||
import Data.Text (Text)
|
||||
import Network.HTTP.Conduit (Manager)
|
||||
#if MIN_VERSION_hoauth2(2,7,0)
|
||||
import Network.OAuth.OAuth2
|
||||
( AccessToken(..)
|
||||
, ExchangeToken(..)
|
||||
, OAuth2Token(..)
|
||||
, RefreshToken(..)
|
||||
)
|
||||
#else
|
||||
import Network.OAuth.OAuth2
|
||||
( AccessToken(..)
|
||||
, ExchangeToken(..)
|
||||
@ -22,8 +32,13 @@ import Network.OAuth.OAuth2
|
||||
, OAuth2Token(..)
|
||||
, RefreshToken(..)
|
||||
)
|
||||
#endif
|
||||
import qualified Network.OAuth.OAuth2 as OAuth2
|
||||
#if MIN_VERSION_hoauth2(2,7,0)
|
||||
import Network.OAuth.OAuth2.TokenRequest (TokenRequestError)
|
||||
#else
|
||||
import Network.OAuth.OAuth2.TokenRequest (Errors)
|
||||
#endif
|
||||
import URI.ByteString
|
||||
|
||||
#if MIN_VERSION_hoauth2(2,2,0)
|
||||
@ -39,7 +54,17 @@ data OAuth2 = OAuth2
|
||||
, oauth2RedirectUri :: Maybe (URIRef Absolute)
|
||||
}
|
||||
|
||||
type OAuth2Result err a = Either (OAuth2Error err) a
|
||||
#if MIN_VERSION_hoauth2(2,7,0)
|
||||
type Errors = TokenRequestError
|
||||
#endif
|
||||
|
||||
#if MIN_VERSION_hoauth2(2,7,0)
|
||||
type Error = TokenRequestError
|
||||
#else
|
||||
type Error = OAuth2Error Errors
|
||||
#endif
|
||||
|
||||
type OAuth2Result a = Either Error a
|
||||
|
||||
authorizationUrl :: OAuth2 -> URI
|
||||
authorizationUrl = OAuth2.authorizationUrl . getOAuth2
|
||||
@ -48,14 +73,14 @@ fetchAccessToken
|
||||
:: Manager
|
||||
-> OAuth2
|
||||
-> ExchangeToken
|
||||
-> IO (OAuth2Result Errors OAuth2Token)
|
||||
-> IO (OAuth2Result OAuth2Token)
|
||||
fetchAccessToken = fetchAccessTokenBasic
|
||||
|
||||
fetchAccessToken2
|
||||
:: Manager
|
||||
-> OAuth2
|
||||
-> ExchangeToken
|
||||
-> IO (OAuth2Result Errors OAuth2Token)
|
||||
-> IO (OAuth2Result OAuth2Token)
|
||||
fetchAccessToken2 = fetchAccessTokenPost
|
||||
|
||||
authGetBS :: Manager -> AccessToken -> URI -> IO (Either ByteString ByteString)
|
||||
@ -131,7 +156,7 @@ fetchAccessTokenBasic
|
||||
:: Manager
|
||||
-> OAuth2
|
||||
-> ExchangeToken
|
||||
-> IO (OAuth2Result Errors OAuth2Token)
|
||||
-> IO (OAuth2Result OAuth2Token)
|
||||
fetchAccessTokenBasic m o e = runOAuth2 $ f m (getOAuth2 o) e
|
||||
where
|
||||
#if MIN_VERSION_hoauth2(2,6,0)
|
||||
@ -146,7 +171,7 @@ fetchAccessTokenPost
|
||||
:: Manager
|
||||
-> OAuth2
|
||||
-> ExchangeToken
|
||||
-> IO (OAuth2Result Errors OAuth2Token)
|
||||
-> IO (OAuth2Result OAuth2Token)
|
||||
fetchAccessTokenPost m o e = runOAuth2 $ f m (getOAuth2 o) e
|
||||
where
|
||||
#if MIN_VERSION_hoauth2(2, 6, 0)
|
||||
|
||||
@ -18,7 +18,6 @@ import qualified Data.Text as T
|
||||
import Data.Text.Encoding (encodeUtf8)
|
||||
import Network.HTTP.Conduit (Manager)
|
||||
import Network.OAuth.OAuth2.Compat
|
||||
import Network.OAuth.OAuth2.TokenRequest (Errors)
|
||||
import URI.ByteString.Extension
|
||||
import UnliftIO.Exception
|
||||
import Yesod.Auth hiding (ServerError)
|
||||
@ -32,7 +31,7 @@ import Yesod.Core hiding (ErrorResponse)
|
||||
-- This will be 'fetchAccessToken' or 'fetchAccessToken2'
|
||||
--
|
||||
type FetchToken
|
||||
= Manager -> OAuth2 -> ExchangeToken -> IO (OAuth2Result Errors OAuth2Token)
|
||||
= Manager -> OAuth2 -> ExchangeToken -> IO (OAuth2Result OAuth2Token)
|
||||
|
||||
-- | How to take an @'OAuth2Token'@ and retrieve user credentials
|
||||
type FetchCreds m = Manager -> OAuth2Token -> IO (Creds m)
|
||||
|
||||
@ -16,8 +16,7 @@ module Yesod.Auth.OAuth2.DispatchError
|
||||
|
||||
import Control.Monad.Except
|
||||
import Data.Text (Text, pack)
|
||||
import Network.OAuth.OAuth2
|
||||
import Network.OAuth.OAuth2.TokenRequest (Errors)
|
||||
import Network.OAuth.OAuth2.Compat (Error)
|
||||
import UnliftIO.Except ()
|
||||
import UnliftIO.Exception
|
||||
import Yesod.Auth hiding (ServerError)
|
||||
@ -31,7 +30,7 @@ data DispatchError
|
||||
| InvalidStateToken (Maybe Text) Text
|
||||
| InvalidCallbackUri Text
|
||||
| OAuth2HandshakeError ErrorResponse
|
||||
| OAuth2ResultError (OAuth2Error Errors)
|
||||
| OAuth2ResultError Error
|
||||
| FetchCredsIOException IOException
|
||||
| FetchCredsYesodOAuth2Exception YesodOAuth2Exception
|
||||
| OtherDispatchError Text
|
||||
@ -48,8 +47,8 @@ dispatchErrorMessage = \case
|
||||
MissingParameter name ->
|
||||
"Parameter '" <> name <> "' is required, but not present in the URL"
|
||||
InvalidStateToken{} -> "State token is invalid, please try again"
|
||||
InvalidCallbackUri{}
|
||||
-> "Callback URI was not valid, this server may be misconfigured (no approot)"
|
||||
InvalidCallbackUri{} ->
|
||||
"Callback URI was not valid, this server may be misconfigured (no approot)"
|
||||
OAuth2HandshakeError er -> "OAuth2 handshake failure: " <> erUserMessage er
|
||||
OAuth2ResultError{} -> "Login failed, please try again"
|
||||
FetchCredsIOException{} -> "Login failed, please try again"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user