Skip to content

Commit ad66d91

Browse files
committed
fixes: refresh token flow shall also honor client auth method
1 parent c747b6d commit ad66d91

File tree

4 files changed

+37
-47
lines changed

4 files changed

+37
-47
lines changed

hoauth2-demo/dev.org

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ curl https://api.linear.app/graphql \
2828
- ~AuthorizationGrantUserStore (MVar (Map.Map IdpName IdpAuthorizationCodeAppSessionData)~
2929
- User AppName would be better since it's already unique.
3030
- Just need to figure out how to match differ UI section
31+
** Refresh button shall only appear when there is refresh token

hoauth2/src/Network/OAuth2/Experiment/Flows/RefreshTokenRequest.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ conduitRefreshTokenRequest ::
5151
Manager ->
5252
OAuth2.RefreshToken ->
5353
ExceptT TokenResponseError m TokenResponse
54-
conduitRefreshTokenRequest IdpApplication {..} mgr rt =
55-
let tokenReq = mkRefreshTokenRequestParam application rt
54+
conduitRefreshTokenRequest ia mgr rt =
55+
let tokenReq = mkRefreshTokenRequestParam (application ia) rt
5656
body = unionMapsToQueryParams [toQueryParam tokenReq]
57-
in doJSONPostRequest mgr (mkOAuth2Key application) (idpTokenEndpoint idp) body
57+
in conduitTokenRequestInternal ia mgr body

hoauth2/src/Network/OAuth2/Experiment/Flows/TokenRequest.hs

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import Data.Aeson (FromJSON)
88
import Network.HTTP.Conduit
99
import Network.OAuth.OAuth2 (
1010
ClientAuthenticationMethod (..),
11-
OAuth2,
1211
PostBody,
1312
TokenResponse,
1413
uriToRequest,
@@ -23,7 +22,6 @@ import Network.OAuth.OAuth2.TokenRequest (
2322
import Network.OAuth2.Experiment.Pkce
2423
import Network.OAuth2.Experiment.Types
2524
import Network.OAuth2.Experiment.Utils
26-
import URI.ByteString (URI)
2725

2826
class HasTokenRequestClientAuthenticationMethod a where
2927
getClientAuthenticationMethod :: a -> ClientAuthenticationMethod
@@ -56,7 +54,12 @@ conduitTokenRequest ::
5654
ExchangeTokenInfo a ->
5755
ExceptT TokenResponseError m TokenResponse
5856
conduitTokenRequest idpApp mgr exchangeToken = do
59-
conduitTokenRequestInternal idpApp mgr (exchangeToken, Nothing)
57+
let req = mkTokenRequestParam (application idpApp) exchangeToken
58+
body =
59+
unionMapsToQueryParams
60+
[ toQueryParam req
61+
]
62+
in conduitTokenRequestInternal idpApp mgr body
6063

6164
-------------------------------------------------------------------------------
6265
-- PKCE Token Request --
@@ -70,59 +73,45 @@ conduitPkceTokenRequest ::
7073
(ExchangeTokenInfo a, CodeVerifier) ->
7174
ExceptT TokenResponseError m TokenResponse
7275
conduitPkceTokenRequest idpApp mgr (exchangeToken, codeVerifier) =
73-
conduitTokenRequestInternal idpApp mgr (exchangeToken, Just codeVerifier)
76+
let req = mkTokenRequestParam (application idpApp) exchangeToken
77+
body =
78+
unionMapsToQueryParams
79+
[ toQueryParam req
80+
, toQueryParam codeVerifier
81+
]
82+
in conduitTokenRequestInternal idpApp mgr body
7483

7584
-------------------------------------------------------------------------------
7685
-- Internal helpers --
7786
-------------------------------------------------------------------------------
7887

7988
conduitTokenRequestInternal ::
80-
(HasTokenRequest a, ToQueryParam (TokenRequest a), MonadIO m) =>
89+
( MonadIO m
90+
, HasOAuth2Key a
91+
, HasTokenRequestClientAuthenticationMethod a
92+
, FromJSON b
93+
) =>
8194
IdpApplication i a ->
82-
Manager ->
83-
(ExchangeTokenInfo a, Maybe CodeVerifier) ->
84-
ExceptT TokenResponseError m TokenResponse
85-
conduitTokenRequestInternal IdpApplication {..} mgr (exchangeToken, codeVerifier) =
86-
let req = mkTokenRequestParam application exchangeToken
87-
key = mkOAuth2Key application
88-
body =
89-
unionMapsToQueryParams
90-
[ toQueryParam req
91-
, toQueryParam codeVerifier
92-
]
93-
in doTokenRequestInternal
94-
(getClientAuthenticationMethod application)
95-
mgr
96-
key
97-
(idpTokenEndpoint idp)
98-
body
99-
100-
doTokenRequestInternal ::
101-
(MonadIO m, FromJSON a) =>
102-
ClientAuthenticationMethod ->
10395
-- | HTTP connection manager.
10496
Manager ->
105-
-- | OAuth options
106-
OAuth2 ->
107-
-- | URL
108-
URI ->
10997
-- | Request body.
11098
PostBody ->
11199
-- | Response as ByteString
112-
ExceptT TokenResponseError m a
113-
doTokenRequestInternal clientAuthMethod manager oa url body = do
100+
ExceptT TokenResponseError m b
101+
conduitTokenRequestInternal IdpApplication {..} manager body = do
102+
let oa = mkOAuth2Key application
103+
clientAuthMethod = getClientAuthenticationMethod application
104+
url = idpTokenEndpoint idp
105+
updateAuthHeader =
106+
case clientAuthMethod of
107+
ClientSecretBasic -> addBasicAuth oa
108+
ClientSecretPost -> id
109+
ClientAssertionJwt -> id
110+
go = do
111+
req <- uriToRequest url
112+
let req' = (updateAuthHeader . addDefaultRequestHeaders) req
113+
httpLbs (urlEncodedBody body req') manager
114114
resp <- ExceptT . liftIO $ fmap handleOAuth2TokenResponse go
115115
case parseResponseFlexible resp of
116116
Right obj -> return obj
117117
Left e -> throwE e
118-
where
119-
updateAuthHeader =
120-
case clientAuthMethod of
121-
ClientSecretBasic -> addBasicAuth oa
122-
ClientSecretPost -> id
123-
ClientAssertionJwt -> id
124-
125-
go = do
126-
req <- uriToRequest url
127-
let req' = (updateAuthHeader . addDefaultRequestHeaders) req
128-
httpLbs (urlEncodedBody body req') manager

hoauth2/test/Network/OAuth/OAuth2/TokenResponseSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Network.OAuth.OAuth2.TokenResponseSpec where
66
import Data.Aeson qualified as Aeson
77
import Data.Binary qualified as Binary
88
import Data.Maybe (fromJust)
9-
import Network.OAuth.OAuth2 (AccessToken (..), TokenResponse (..), RefreshToken (..))
9+
import Network.OAuth.OAuth2 (AccessToken (..), RefreshToken (..), TokenResponse (..))
1010
import Test.Hspec
1111

1212
spec :: Spec

0 commit comments

Comments
 (0)