Skip to content

Commit

Permalink
Throws an error when an unsupported response_type is used.
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamDenniss committed Mar 25, 2017
1 parent 98c8fbf commit 270b506
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
14 changes: 14 additions & 0 deletions Source/OIDAuthorizationRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#import "OIDAuthorizationRequest.h"

#import "OIDDefines.h"
#import "OIDError.h"
#import "OIDScopeUtilities.h"
#import "OIDServiceConfiguration.h"
#import "OIDTokenUtilities.h"
Expand Down Expand Up @@ -80,6 +81,13 @@
*/
static NSUInteger const kCodeVerifierBytes = 32;

/*! @brief Exception text for unsupported response types.
*/
static NSString *const OIDOAuthExceptionUnsupportedResponseTypeMessage =
@"The response_type \"%@\" isn't supported. AppAuth only supports the \"code\" response_type.";

/*! @brief Code challenge request method.
*/
NSString *const OIDOAuthorizationRequestCodeChallengeMethodS256 = @"S256";

@implementation OIDAuthorizationRequest
Expand Down Expand Up @@ -114,6 +122,12 @@ - (instancetype)initWithConfiguration:(OIDServiceConfiguration *)configuration
_scope = [scope copy];
_redirectURL = [redirectURL copy];
_responseType = [responseType copy];
if (![_responseType isEqualToString:OIDResponseTypeCode]) {
// AppAuth only supports the `code` response type.
// Discussion: https://github.com/openid/AppAuth-iOS/issues/98
[NSException raise:OIDOAuthExceptionUnsupportedResponseType
format:OIDOAuthExceptionUnsupportedResponseTypeMessage, _responseType, nil];
}
_state = [state copy];
_codeVerifier = [codeVerifier copy];
_codeChallenge = [codeChallenge copy];
Expand Down
4 changes: 4 additions & 0 deletions Source/OIDError.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,8 @@ typedef NS_ENUM(NSInteger, OIDErrorCodeOAuthRegistration) {
*/
extern NSString *const OIDOAuthExceptionInvalidAuthorizationFlow;

/*! @brief Exception for unsupported response types.
*/
extern NSString *const OIDOAuthExceptionUnsupportedResponseType;

NS_ASSUME_NONNULL_END
2 changes: 2 additions & 0 deletions Source/OIDError.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
NSString *const OIDOAuthExceptionInvalidAuthorizationFlow = @"An OAuth redirect was sent to a "
"OIDAuthorizationFlowSession after it already completed.";

NSString *const OIDOAuthExceptionUnsupportedResponseType = @"Unsupported response type";

NSString *const OIDOAuthErrorResponseErrorKey = @"OIDOAuthErrorResponseErrorKey";

NSString *const OIDOAuthErrorFieldError = @"error";
Expand Down
53 changes: 52 additions & 1 deletion UnitTests/OIDAuthorizationRequestTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

/*! @brief Test value for the @c responseType property.
*/
static NSString *const kTestResponseType = @"ResponseType";
static NSString *const kTestResponseType = @"code";

/*! @brief Test value for the @c clientID property.
*/
Expand Down Expand Up @@ -403,4 +403,55 @@ - (void)testPKCEVerifierRecommendations {
@"The spec RECOMMENDS a '43-octet URL safe string'");
}

- (void)testSupportedResponseTypes {
NSDictionary *additionalParameters =
@{ kTestAdditionalParameterKey : kTestAdditionalParameterValue };
OIDServiceConfiguration *configuration = [OIDServiceConfigurationTests testInstance];

NSString *scope = [OIDScopeUtilities scopesWithArray:@[ kTestScope, kTestScopeA ]];

XCTAssertThrows(
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
clientId:kTestClientID
clientSecret:kTestClientSecret
scope:scope
redirectURL:[NSURL URLWithString:kTestRedirectURL]
responseType:@"code id_token"
state:kTestState
codeVerifier:kTestCodeVerifier
codeChallenge:[[self class] codeChallenge]
codeChallengeMethod:[[self class] codeChallengeMethod]
additionalParameters:additionalParameters]
);

XCTAssertThrows(
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
clientId:kTestClientID
clientSecret:kTestClientSecret
scope:scope
redirectURL:[NSURL URLWithString:kTestRedirectURL]
responseType:@"code token id_token"
state:kTestState
codeVerifier:kTestCodeVerifier
codeChallenge:[[self class] codeChallenge]
codeChallengeMethod:[[self class] codeChallengeMethod]
additionalParameters:additionalParameters]
);

XCTAssertNoThrow(
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
clientId:kTestClientID
clientSecret:kTestClientSecret
scope:scope
redirectURL:[NSURL URLWithString:kTestRedirectURL]
responseType:@"code"
state:kTestState
codeVerifier:kTestCodeVerifier
codeChallenge:[[self class] codeChallenge]
codeChallengeMethod:[[self class] codeChallengeMethod]
additionalParameters:additionalParameters]
);

}

@end

0 comments on commit 270b506

Please sign in to comment.