Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow custom nonce in OIDAuthorizationRequest #788

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Source/AppAuthCore/OIDAuthorizationRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,29 @@ extern NSString *const OIDOAuthorizationRequestCodeChallengeMethodS256;
responseType:(NSString *)responseType
additionalParameters:(nullable NSDictionary<NSString *, NSString *> *)additionalParameters;

/*! @brief Creates an authorization request with custom nonce, a secure @c state,
and PKCE with S256 as the @c code_challenge_method.
@param configuration The service's configuration.
@param clientID The client identifier.
@param scopes An array of scopes to combine into a single scope string per the OAuth2 spec.
@param redirectURL The client's redirect URI.
@param responseType The expected response type.
@param nonce String value used to associate a Client session with an ID Token. Can be set to nil
mdmathias marked this conversation as resolved.
Show resolved Hide resolved
if not using OpenID Connect, although pure OAuth servers should ignore params they don't
understand anyway.
@param additionalParameters The client's additional authorization parameters.
@remarks This convenience initializer generates a state parameter and PKCE challenges
automatically.
*/
- (instancetype)
initWithConfiguration:(OIDServiceConfiguration *)configuration
clientId:(NSString *)clientID
scopes:(nullable NSArray<NSString *> *)scopes
redirectURL:(NSURL *)redirectURL
responseType:(NSString *)responseType
nonce:(nullable NSString *)nonce
additionalParameters:(nullable NSDictionary<NSString *, NSString *> *)additionalParameters;

/*! @brief Creates an authorization request with opinionated defaults (a secure @c state, @c nonce,
and PKCE with S256 as the @c code_challenge_method).
@param configuration The service's configuration.
Expand Down
26 changes: 26 additions & 0 deletions Source/AppAuthCore/OIDAuthorizationRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,32 @@ - (instancetype)initWithConfiguration:(OIDServiceConfiguration *)configuration
additionalParameters:additionalParameters];
}

- (instancetype)
initWithConfiguration:(OIDServiceConfiguration *)configuration
clientId:(NSString *)clientID
scopes:(nullable NSArray<NSString *> *)scopes
redirectURL:(NSURL *)redirectURL
responseType:(NSString *)responseType
nonce:(nullable NSString *)nonce
additionalParameters:(nullable NSDictionary<NSString *, NSString *> *)additionalParameters {
// generates PKCE code verifier and challenge
NSString *codeVerifier = [[self class] generateCodeVerifier];
NSString *codeChallenge = [[self class] codeChallengeS256ForVerifier:codeVerifier];

return [self initWithConfiguration:configuration
clientId:clientID
clientSecret:nil
scope:[OIDScopeUtilities scopesWithArray:scopes]
redirectURL:redirectURL
responseType:responseType
state:[[self class] generateState]
nonce:nonce
codeVerifier:codeVerifier
codeChallenge:codeChallenge
codeChallengeMethod:OIDOAuthorizationRequestCodeChallengeMethodS256
additionalParameters:additionalParameters];
}

#pragma mark - NSCopying

- (instancetype)copyWithZone:(nullable NSZone *)zone {
Expand Down
23 changes: 23 additions & 0 deletions UnitTests/OIDAuthorizationRequestTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,29 @@ - (void)testScopeInitializerWithManyScopesAndNoClientSecret {
kTestAdditionalParameterValue, @"");
}


/*! @brief Tests the initializer which takes a nonce
*/
- (void)testNonceInitializer {
OIDServiceConfiguration *configuration = [OIDServiceConfigurationTests testInstance];
OIDAuthorizationRequest *request =
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
clientId:kTestClientID
scopes:@[]
redirectURL:[NSURL URLWithString:kTestRedirectURL]
responseType:OIDResponseTypeCode
nonce:kTestNonce
additionalParameters:nil];

XCTAssertEqualObjects(request.nonce, kTestNonce);
XCTAssertEqualObjects(request.responseType, @"code");
XCTAssertEqualObjects(request.scope, @"");
XCTAssertEqualObjects(request.clientID, kTestClientID);
XCTAssertNil(request.clientSecret);
XCTAssertEqualObjects(request.redirectURL, [NSURL URLWithString:kTestRedirectURL]);
XCTAssertEqualObjects(@(request.additionalParameters.count), @0);
}

- (void)testScopeInitializerWithManyScopesAndClientSecret {
NSDictionary *additionalParameters =
@{ kTestAdditionalParameterKey : kTestAdditionalParameterValue };
Expand Down
Loading