Skip to content

Commit 8b437c4

Browse files
authored
feat: allow custom nonce in OIDAuthorizationRequest (#788)
1 parent b376a87 commit 8b437c4

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

Source/AppAuthCore/OIDAuthorizationRequest.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,29 @@ extern NSString *const OIDOAuthorizationRequestCodeChallengeMethodS256;
159159
responseType:(NSString *)responseType
160160
additionalParameters:(nullable NSDictionary<NSString *, NSString *> *)additionalParameters;
161161

162+
/*! @brief Creates an authorization request with custom nonce, a secure @c state,
163+
and PKCE with S256 as the @c code_challenge_method.
164+
@param configuration The service's configuration.
165+
@param clientID The client identifier.
166+
@param scopes An array of scopes to combine into a single scope string per the OAuth2 spec.
167+
@param redirectURL The client's redirect URI.
168+
@param responseType The expected response type.
169+
@param nonce String value used to associate a Client session with an ID Token. Can be set to nil
170+
if not using OpenID Connect, although pure OAuth servers should ignore params they don't
171+
understand anyway.
172+
@param additionalParameters The client's additional authorization parameters.
173+
@remarks This convenience initializer generates a state parameter and PKCE challenges
174+
automatically.
175+
*/
176+
- (instancetype)
177+
initWithConfiguration:(OIDServiceConfiguration *)configuration
178+
clientId:(NSString *)clientID
179+
scopes:(nullable NSArray<NSString *> *)scopes
180+
redirectURL:(NSURL *)redirectURL
181+
responseType:(NSString *)responseType
182+
nonce:(nullable NSString *)nonce
183+
additionalParameters:(nullable NSDictionary<NSString *, NSString *> *)additionalParameters;
184+
162185
/*! @brief Creates an authorization request with opinionated defaults (a secure @c state, @c nonce,
163186
and PKCE with S256 as the @c code_challenge_method).
164187
@param configuration The service's configuration.

Source/AppAuthCore/OIDAuthorizationRequest.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,32 @@ - (instancetype)initWithConfiguration:(OIDServiceConfiguration *)configuration
202202
additionalParameters:additionalParameters];
203203
}
204204

205+
- (instancetype)
206+
initWithConfiguration:(OIDServiceConfiguration *)configuration
207+
clientId:(NSString *)clientID
208+
scopes:(nullable NSArray<NSString *> *)scopes
209+
redirectURL:(NSURL *)redirectURL
210+
responseType:(NSString *)responseType
211+
nonce:(nullable NSString *)nonce
212+
additionalParameters:(nullable NSDictionary<NSString *, NSString *> *)additionalParameters {
213+
// generates PKCE code verifier and challenge
214+
NSString *codeVerifier = [[self class] generateCodeVerifier];
215+
NSString *codeChallenge = [[self class] codeChallengeS256ForVerifier:codeVerifier];
216+
217+
return [self initWithConfiguration:configuration
218+
clientId:clientID
219+
clientSecret:nil
220+
scope:[OIDScopeUtilities scopesWithArray:scopes]
221+
redirectURL:redirectURL
222+
responseType:responseType
223+
state:[[self class] generateState]
224+
nonce:nonce
225+
codeVerifier:codeVerifier
226+
codeChallenge:codeChallenge
227+
codeChallengeMethod:OIDOAuthorizationRequestCodeChallengeMethodS256
228+
additionalParameters:additionalParameters];
229+
}
230+
205231
#pragma mark - NSCopying
206232

207233
- (instancetype)copyWithZone:(nullable NSZone *)zone {

UnitTests/OIDAuthorizationRequestTests.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,29 @@ - (void)testScopeInitializerWithManyScopesAndNoClientSecret {
223223
kTestAdditionalParameterValue, @"");
224224
}
225225

226+
227+
/*! @brief Tests the initializer which takes a nonce
228+
*/
229+
- (void)testNonceInitializer {
230+
OIDServiceConfiguration *configuration = [OIDServiceConfigurationTests testInstance];
231+
OIDAuthorizationRequest *request =
232+
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
233+
clientId:kTestClientID
234+
scopes:@[]
235+
redirectURL:[NSURL URLWithString:kTestRedirectURL]
236+
responseType:OIDResponseTypeCode
237+
nonce:kTestNonce
238+
additionalParameters:nil];
239+
240+
XCTAssertEqualObjects(request.nonce, kTestNonce);
241+
XCTAssertEqualObjects(request.responseType, @"code");
242+
XCTAssertEqualObjects(request.scope, @"");
243+
XCTAssertEqualObjects(request.clientID, kTestClientID);
244+
XCTAssertNil(request.clientSecret);
245+
XCTAssertEqualObjects(request.redirectURL, [NSURL URLWithString:kTestRedirectURL]);
246+
XCTAssertEqualObjects(@(request.additionalParameters.count), @0);
247+
}
248+
226249
- (void)testScopeInitializerWithManyScopesAndClientSecret {
227250
NSDictionary *additionalParameters =
228251
@{ kTestAdditionalParameterKey : kTestAdditionalParameterValue };

0 commit comments

Comments
 (0)