@@ -8,7 +8,6 @@ import Data.Aeson (FromJSON)
8
8
import Network.HTTP.Conduit
9
9
import Network.OAuth.OAuth2 (
10
10
ClientAuthenticationMethod (.. ),
11
- OAuth2 ,
12
11
PostBody ,
13
12
TokenResponse ,
14
13
uriToRequest ,
@@ -23,7 +22,6 @@ import Network.OAuth.OAuth2.TokenRequest (
23
22
import Network.OAuth2.Experiment.Pkce
24
23
import Network.OAuth2.Experiment.Types
25
24
import Network.OAuth2.Experiment.Utils
26
- import URI.ByteString (URI )
27
25
28
26
class HasTokenRequestClientAuthenticationMethod a where
29
27
getClientAuthenticationMethod :: a -> ClientAuthenticationMethod
@@ -56,7 +54,12 @@ conduitTokenRequest ::
56
54
ExchangeTokenInfo a ->
57
55
ExceptT TokenResponseError m TokenResponse
58
56
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
60
63
61
64
-------------------------------------------------------------------------------
62
65
-- PKCE Token Request --
@@ -70,59 +73,45 @@ conduitPkceTokenRequest ::
70
73
(ExchangeTokenInfo a , CodeVerifier ) ->
71
74
ExceptT TokenResponseError m TokenResponse
72
75
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
74
83
75
84
-------------------------------------------------------------------------------
76
85
-- Internal helpers --
77
86
-------------------------------------------------------------------------------
78
87
79
88
conduitTokenRequestInternal ::
80
- (HasTokenRequest a , ToQueryParam (TokenRequest a ), MonadIO m ) =>
89
+ ( MonadIO m
90
+ , HasOAuth2Key a
91
+ , HasTokenRequestClientAuthenticationMethod a
92
+ , FromJSON b
93
+ ) =>
81
94
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 ->
103
95
-- | HTTP connection manager.
104
96
Manager ->
105
- -- | OAuth options
106
- OAuth2 ->
107
- -- | URL
108
- URI ->
109
97
-- | Request body.
110
98
PostBody ->
111
99
-- | 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
114
114
resp <- ExceptT . liftIO $ fmap handleOAuth2TokenResponse go
115
115
case parseResponseFlexible resp of
116
116
Right obj -> return obj
117
117
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
0 commit comments