Skip to content

Commit 6708788

Browse files
Bertin PhilippeWilliamDenniss
Bertin Philippe
authored andcommitted
Fix state parameter mismatch error message.
In case of “state” parameter mismatch between request and response, the generated error was not populated with the right information. Also, the OAuth 2 response is “valid” regardless of the state parameter so it makes sense to create the response object before checking for the state parameter.
1 parent 32dadff commit 6708788

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

Source/OIDAuthorizationService.m

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737
*/
3838
static NSString *const kOpenIDConfigurationWellKnownPath = @".well-known/openid-configuration";
3939

40-
/*! @brief The state authorization parameter.
41-
*/
42-
static NSString *const kStateParameter = @"state";
4340

4441
NS_ASSUME_NONNULL_BEGIN
4542

@@ -127,25 +124,25 @@ - (BOOL)resumeAuthorizationFlowWithURL:(NSURL *)URL {
127124
underlyingError:nil];
128125
}
129126

130-
// verifies that the state in the response matches the state in the request, or both are nil
131-
if (!OIDIsEqualIncludingNil(_request.state, query.dictionaryValue[kStateParameter])) {
132-
NSMutableDictionary *userInfo = [query.dictionaryValue mutableCopy];
133-
userInfo[NSLocalizedDescriptionKey] =
134-
[NSString stringWithFormat:@"State mismatch, expecting %@ but got %@ in authorization "
135-
"response %@",
136-
_request.state,
137-
response.state,
138-
response];
139-
response = nil;
140-
error = [NSError errorWithDomain:OIDOAuthAuthorizationErrorDomain
141-
code:OIDErrorCodeOAuthAuthorizationClientError
142-
userInfo:userInfo];
143-
}
144-
145127
// no error, should be a valid OAuth 2.0 response
146128
if (!error) {
147129
response = [[OIDAuthorizationResponse alloc] initWithRequest:_request
148130
parameters:query.dictionaryValue];
131+
132+
// verifies that the state in the response matches the state in the request, or both are nil
133+
if (!OIDIsEqualIncludingNil(_request.state, response.state)) {
134+
NSMutableDictionary *userInfo = [query.dictionaryValue mutableCopy];
135+
userInfo[NSLocalizedDescriptionKey] =
136+
[NSString stringWithFormat:@"State mismatch, expecting %@ but got %@ in authorization "
137+
"response %@",
138+
_request.state,
139+
response.state,
140+
response];
141+
response = nil;
142+
error = [NSError errorWithDomain:OIDOAuthAuthorizationErrorDomain
143+
code:OIDErrorCodeOAuthAuthorizationClientError
144+
userInfo:userInfo];
145+
}
149146
}
150147

151148
[_UICoordinator dismissAuthorizationAnimated:YES

0 commit comments

Comments
 (0)