From 3c15ecd871a5b10f98123059b97e973221ff7395 Mon Sep 17 00:00:00 2001 From: Michael Gilliland Date: Wed, 1 Feb 2023 14:20:08 -0500 Subject: [PATCH] Fix hoauth2 compat for 2.7.0 (#165) Use CPP to get 2.7.0 to compile Resolves #164 --- .github/workflows/ci.yml | 1 + CHANGELOG.md | 7 ++- package.yaml | 2 +- src/Network/OAuth/OAuth2/Compat.hs | 23 ++++++++-- src/Yesod/Auth/OAuth2/Dispatch.hs | 1 - src/Yesod/Auth/OAuth2/DispatchError.hs | 63 +++++++++++++------------- stack-hoauth2-2.6.yaml | 11 +++++ stack-hoauth2-2.6.yaml.lock | 19 ++++++++ stack-nightly.yaml | 13 +++--- stack-nightly.yaml.lock | 44 ++++-------------- yesod-auth-oauth2.cabal | 6 +-- 11 files changed, 106 insertions(+), 84 deletions(-) create mode 100644 stack-hoauth2-2.6.yaml create mode 100644 stack-hoauth2-2.6.yaml.lock diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ee6cce0..9f9f593 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,6 +20,7 @@ jobs: - stack-hoauth2-2.0.yaml - stack-hoauth2-2.2.yaml - stack-hoauth2-2.3.yaml + - stack-hoauth2-2.6.yaml fail-fast: false steps: diff --git a/CHANGELOG.md b/CHANGELOG.md index 35c93d8..69e53da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,9 @@ -## [_Unreleased_](https://github.com/thoughtbot/yesod-auth-oauth2/compare/v0.7.0.2...main) +## [_Unreleased_](https://github.com/thoughtbot/yesod-auth-oauth2/compare/v0.7.0.3...main) + +## [v0.7.0.3](https://github.com/thoughtbot/yesod-auth-oauth2/compare/v0.7.0.2...v0.7.0.3) + +- Support `hoauth-2.7`. This change is only breaking in the unlikely case of users + using something other than `fetchAccessToken` or `fetchAccessToken2` ## [v0.7.0.2](https://github.com/thoughtbot/yesod-auth-oauth2/compare/v0.7.0.1...v0.7.0.2) diff --git a/package.yaml b/package.yaml index 3da2790..6a03c03 100644 --- a/package.yaml +++ b/package.yaml @@ -1,6 +1,6 @@ --- name: yesod-auth-oauth2 -version: 0.7.0.2 +version: 0.7.0.3 synopsis: OAuth 2.0 authentication plugins description: Library to authenticate with OAuth 2.0 for Yesod web applications. category: Web diff --git a/src/Network/OAuth/OAuth2/Compat.hs b/src/Network/OAuth/OAuth2/Compat.hs index 05af0da..3b386c1 100644 --- a/src/Network/OAuth/OAuth2/Compat.hs +++ b/src/Network/OAuth/OAuth2/Compat.hs @@ -3,6 +3,7 @@ module Network.OAuth.OAuth2.Compat ( OAuth2(..) , OAuth2Result + , Errors , authorizationUrl , fetchAccessToken , fetchAccessToken2 @@ -15,6 +16,16 @@ module Network.OAuth.OAuth2.Compat import Data.ByteString.Lazy (ByteString) import Data.Text (Text) import Network.HTTP.Conduit (Manager) +import qualified Network.OAuth.OAuth2 as OAuth2 +#if MIN_VERSION_hoauth2(2,7,0) +import Network.OAuth.OAuth2 + ( AccessToken(..) + , ExchangeToken(..) + , OAuth2Token(..) + , RefreshToken(..) + ) +import Network.OAuth.OAuth2.TokenRequest (TokenRequestError) +#else import Network.OAuth.OAuth2 ( AccessToken(..) , ExchangeToken(..) @@ -22,8 +33,8 @@ import Network.OAuth.OAuth2 , OAuth2Token(..) , RefreshToken(..) ) -import qualified Network.OAuth.OAuth2 as OAuth2 -import Network.OAuth.OAuth2.TokenRequest (Errors) +import qualified Network.OAuth.OAuth2.TokenRequest as LegacyTokenRequest +#endif import URI.ByteString #if MIN_VERSION_hoauth2(2,2,0) @@ -39,7 +50,13 @@ 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 +#else +type Errors = OAuth2Error LegacyTokenRequest.Errors +#endif + +type OAuth2Result err a = Either err a authorizationUrl :: OAuth2 -> URI authorizationUrl = OAuth2.authorizationUrl . getOAuth2 diff --git a/src/Yesod/Auth/OAuth2/Dispatch.hs b/src/Yesod/Auth/OAuth2/Dispatch.hs index dfeb598..7b795b4 100644 --- a/src/Yesod/Auth/OAuth2/Dispatch.hs +++ b/src/Yesod/Auth/OAuth2/Dispatch.hs @@ -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) diff --git a/src/Yesod/Auth/OAuth2/DispatchError.hs b/src/Yesod/Auth/OAuth2/DispatchError.hs index bdfc483..434294b 100644 --- a/src/Yesod/Auth/OAuth2/DispatchError.hs +++ b/src/Yesod/Auth/OAuth2/DispatchError.hs @@ -9,15 +9,14 @@ {-# LANGUAGE TypeFamilies #-} module Yesod.Auth.OAuth2.DispatchError - ( DispatchError(..) - , handleDispatchError - , onDispatchError - ) where + ( DispatchError(..) + , handleDispatchError + , onDispatchError + ) where import Control.Monad.Except import Data.Text (Text, pack) -import Network.OAuth.OAuth2 -import Network.OAuth.OAuth2.TokenRequest (Errors) +import Network.OAuth.OAuth2.Compat (Errors) 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 Errors | FetchCredsIOException IOException | FetchCredsYesodOAuth2Exception YesodOAuth2Exception | OtherDispatchError Text @@ -45,37 +44,37 @@ data DispatchError -- dispatchErrorMessage :: DispatchError -> Text 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)" - OAuth2HandshakeError er -> "OAuth2 handshake failure: " <> erUserMessage er - OAuth2ResultError{} -> "Login failed, please try again" - FetchCredsIOException{} -> "Login failed, please try again" - FetchCredsYesodOAuth2Exception{} -> "Login failed, please try again" - OtherDispatchError{} -> "Login failed, please try again" + 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)" + OAuth2HandshakeError er -> "OAuth2 handshake failure: " <> erUserMessage er + OAuth2ResultError{} -> "Login failed, please try again" + FetchCredsIOException{} -> "Login failed, please try again" + FetchCredsYesodOAuth2Exception{} -> "Login failed, please try again" + OtherDispatchError{} -> "Login failed, please try again" handleDispatchError - :: MonadAuthHandler site m - => ExceptT DispatchError m TypedContent - -> m TypedContent + :: MonadAuthHandler site m + => ExceptT DispatchError m TypedContent + -> m TypedContent handleDispatchError f = do - result <- runExceptT f - either onDispatchError pure result + result <- runExceptT f + either onDispatchError pure result onDispatchError :: MonadAuthHandler site m => DispatchError -> m TypedContent onDispatchError err = do - errorId <- liftIO $ randomText 16 - let suffix = " [errorId=" <> errorId <> "]" - $(logError) $ pack (displayException err) <> suffix + errorId <- liftIO $ randomText 16 + let suffix = " [errorId=" <> errorId <> "]" + $(logError) $ pack (displayException err) <> suffix - let message = dispatchErrorMessage err <> suffix - messageValue = - object ["error" .= object ["id" .= errorId, "message" .= message]] + let message = dispatchErrorMessage err <> suffix + messageValue = + object ["error" .= object ["id" .= errorId, "message" .= message]] - loginR <- ($ LoginR) <$> getRouteToParent + loginR <- ($ LoginR) <$> getRouteToParent - selectRep $ do - provideRep @_ @Html $ onErrorHtml loginR message - provideRep @_ @Value $ pure messageValue + selectRep $ do + provideRep @_ @Html $ onErrorHtml loginR message + provideRep @_ @Value $ pure messageValue diff --git a/stack-hoauth2-2.6.yaml b/stack-hoauth2-2.6.yaml new file mode 100644 index 0000000..b3bc7fd --- /dev/null +++ b/stack-hoauth2-2.6.yaml @@ -0,0 +1,11 @@ +resolver: nightly-2022-12-09 + +extra-deps: + - hoauth2-2.6.0 + +# hoauth2 needs upper-bounds relaxed for +# +# - memory-0.18.0 +# - text-2.0.1 +# +allow-newer: true diff --git a/stack-hoauth2-2.6.yaml.lock b/stack-hoauth2-2.6.yaml.lock new file mode 100644 index 0000000..3dbaac6 --- /dev/null +++ b/stack-hoauth2-2.6.yaml.lock @@ -0,0 +1,19 @@ +# This file was autogenerated by Stack. +# You should not edit this file by hand. +# For more information, please see the documentation at: +# https://docs.haskellstack.org/en/stable/lock_files + +packages: +- completed: + hackage: hoauth2-2.6.0@sha256:168321df73bf75dc7cdda8e72725e9f3f624a9776b1fe59ae46c29c45029dc5d,2262 + pantry-tree: + sha256: 5d39759b171cfaaf5842069a5548c2c1a41c0978645d018da7df713a186192b5 + size: 859 + original: + hackage: hoauth2-2.6.0 +snapshots: +- completed: + sha256: b77e2c2b7988ed34cd317c01eb1958a8b8c234c3cc17e44077616c212959bed0 + size: 558756 + url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/nightly/2022/12/9.yaml + original: nightly-2022-12-09 diff --git a/stack-nightly.yaml b/stack-nightly.yaml index b3bc7fd..f461ebd 100644 --- a/stack-nightly.yaml +++ b/stack-nightly.yaml @@ -1,11 +1,10 @@ -resolver: nightly-2022-12-09 +resolver: nightly-2023-01-30 extra-deps: - - hoauth2-2.6.0 + - hoauth2-2.7.0 +allow-newer-deps: + - hoauth2 # hoauth2 needs upper-bounds relaxed for -# -# - memory-0.18.0 -# - text-2.0.1 -# -allow-newer: true + - memory + - text diff --git a/stack-nightly.yaml.lock b/stack-nightly.yaml.lock index 44908c9..0fcf432 100644 --- a/stack-nightly.yaml.lock +++ b/stack-nightly.yaml.lock @@ -5,43 +5,15 @@ packages: - completed: - hackage: hoauth2-2.4.0@sha256:c2609ee4744dee10640b9d7d85d271bd8ef643d38b0bddd006cdbe2997582481,2814 + hackage: hoauth2-2.7.0@sha256:34c5b82d49a814cfa214b65f5422fda876e38c1275504b574355cf5413e37138,2833 pantry-tree: - size: 594 - sha256: 5e035c54476fbc89f11b8cd13dcd91f2cf2501d3b4276a1b39d1531d1487ade1 + sha256: f575d77978c823bedba13c67f7e31195da6c5543fbd9b35b9def225a8ace6833 + size: 996 original: - hackage: hoauth2-2.4.0 -- completed: - hackage: yesod-auth-1.6.11@sha256:602c0db6cc85cb7b4ad82970379e77e3dee650ab6f05d8d955c6fec7934c5f31,3054 - pantry-tree: - size: 1013 - sha256: 3c6076aa68d31b1f2c40f327bb11c7c7adf7c3ec0f8288ade092185b241c2451 - original: - hackage: yesod-auth-1.6.11 -- completed: - hackage: yesod-core-1.6.22.0@sha256:116c0c2013e81a5cd426ddeb94e07242f4b849a6e9d8a4aa02a2dae826c58086,8124 - pantry-tree: - size: 5367 - sha256: 61bbff374230df6c88f873bf24072760e9fbc2c5c1000870d20120d8d1028bba - original: - hackage: yesod-core-1.6.22.0 -- completed: - hackage: yesod-form-1.7.0@sha256:fd857fb9ea4f5af8500ec8613aa026e3a478c874b93da9d8ab8f17f329ec8c9e,3387 - pantry-tree: - size: 1729 - sha256: 228cbf75994b694a05ecd413935fd3bd8c32d4a7f1b0b486777fd913a440cdbe - original: - hackage: yesod-form-1.7.0 -- completed: - hackage: yesod-persistent-1.6.0.7@sha256:7ece60b1a1e0c9f56ec2f1cf67dd9d0c3962ccabc878b975bef7f743709d267d,1732 - pantry-tree: - size: 497 - sha256: 3778ef2964e1a3890afc22cc9124eacb40e64b62bed4983a85d3b99897f54c5c - original: - hackage: yesod-persistent-1.6.0.7 + hackage: hoauth2-2.7.0 snapshots: - completed: - size: 520054 - url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/nightly/2022/3/25.yaml - sha256: 2c84a2124de70525171f9f6dc92544658c5353e42084e08635b8513d1fa4e6fc - original: nightly-2022-03-25 + sha256: 09ac166bef7e4db4adb4830cf87b3ef4160477702c3da0765d9f7a8c2cffd446 + size: 596592 + url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/nightly/2023/1/30.yaml + original: nightly-2023-01-30 diff --git a/yesod-auth-oauth2.cabal b/yesod-auth-oauth2.cabal index 1c42526..5912497 100644 --- a/yesod-auth-oauth2.cabal +++ b/yesod-auth-oauth2.cabal @@ -1,13 +1,13 @@ cabal-version: 1.18 --- This file has been generated from package.yaml by hpack version 0.35.0. +-- This file has been generated from package.yaml by hpack version 0.35.1. -- -- see: https://github.com/sol/hpack -- --- hash: 6ff32214308580349bea075f2eb01f97fc72598ea3e9a23f77a3e678d9c2c23a +-- hash: 2aa7c7881c27a03f22715aa6db358fc5fe466f5967a09741265bd0f7a999e2b1 name: yesod-auth-oauth2 -version: 0.7.0.2 +version: 0.7.0.3 synopsis: OAuth 2.0 authentication plugins description: Library to authenticate with OAuth 2.0 for Yesod web applications. category: Web