Skip to content

Commit a23d138

Browse files
committed
Add client registration for iOS
1 parent 9476e11 commit a23d138

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

ios/RNAppAuth.m

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,50 @@ - (dispatch_queue_t)methodQueue
3333
static NSUInteger const kCodeVerifierBytes = 32;
3434

3535
RCT_EXPORT_MODULE()
36+
37+
RCT_REMAP_METHOD(register,
38+
issuer: (NSString *) issuer
39+
redirectUrls: (NSArray *) redirectUrls
40+
responseTypes: (NSArray *) responseTypes
41+
grantTypes: (NSArray *) grantTypes
42+
subjectType: (NSString *) subjectType
43+
tokenEndpointAuthMethod: (NSString *) tokenEndpointAuthMethod
44+
additionalParameters: (NSDictionary *_Nullable) additionalParameters
45+
serviceConfiguration: (NSDictionary *_Nullable) serviceConfiguration
46+
resolve: (RCTPromiseResolveBlock) resolve
47+
reject: (RCTPromiseRejectBlock) reject)
48+
{
49+
// if we have manually provided configuration, we can use it and skip the OIDC well-known discovery endpoint call
50+
if (serviceConfiguration) {
51+
OIDServiceConfiguration *configuration = [self createServiceConfiguration:serviceConfiguration];
52+
[self registerWithConfiguration: configuration
53+
redirectUrls: redirectUrls
54+
responseTypes: responseTypes
55+
grantTypes: grantTypes
56+
subjectType: subjectType
57+
tokenEndpointAuthMethod: tokenEndpointAuthMethod
58+
additionalParameters: additionalParameters
59+
resolve: resolve
60+
reject: reject];
61+
} else {
62+
[OIDAuthorizationService discoverServiceConfigurationForIssuer:[NSURL URLWithString:issuer]
63+
completion:^(OIDServiceConfiguration *_Nullable configuration, NSError *_Nullable error) {
64+
if (!configuration) {
65+
reject(@"RNAppAuth Error", [error localizedDescription], error);
66+
return;
67+
}
68+
[self registerWithConfiguration: configuration
69+
redirectUrls: redirectUrls
70+
responseTypes: responseTypes
71+
grantTypes: grantTypes
72+
subjectType: subjectType
73+
tokenEndpointAuthMethod: tokenEndpointAuthMethod
74+
additionalParameters: additionalParameters
75+
resolve: resolve
76+
reject: reject];
77+
}];
78+
}
79+
} // end RCT_REMAP_METHOD(register,
3680

3781
RCT_REMAP_METHOD(authorize,
3882
issuer: (NSString *) issuer
@@ -163,6 +207,45 @@ + (nullable NSString *)codeChallengeS256ForVerifier:(NSString *)codeVerifier {
163207
return [OIDTokenUtilities encodeBase64urlNoPadding:sha256Verifier];
164208
}
165209

210+
211+
/*
212+
* Perform dynamic client registration with provided OIDServiceConfiguration
213+
*/
214+
- (void)registerWithConfiguration: (OIDServiceConfiguration *) configuration
215+
redirectUrls: (NSArray *) redirectUrlStrings
216+
responseTypes: (NSArray *) responseTypes
217+
grantTypes: (NSArray *) grantTypes
218+
subjectType: (NSString *) subjectType
219+
tokenEndpointAuthMethod: (NSString *) tokenEndpointAuthMethod
220+
additionalParameters: (NSDictionary *_Nullable) additionalParameters
221+
resolve: (RCTPromiseResolveBlock) resolve
222+
reject: (RCTPromiseRejectBlock) reject
223+
{
224+
NSMutableArray<NSURL *> *redirectUrls = [NSMutableArray arrayWithCapacity:[redirectUrlStrings count]];
225+
for (NSString *urlString in redirectUrlStrings) {
226+
[redirectUrls addObject:[NSURL URLWithString:urlString]];
227+
}
228+
229+
OIDRegistrationRequest *request =
230+
[[OIDRegistrationRequest alloc] initWithConfiguration:configuration
231+
redirectURIs:redirectUrls
232+
responseTypes:responseTypes
233+
grantTypes:grantTypes
234+
subjectType:subjectType
235+
tokenEndpointAuthMethod:tokenEndpointAuthMethod
236+
additionalParameters:additionalParameters];
237+
238+
[OIDAuthorizationService performRegistrationRequest:request
239+
completion:^(OIDRegistrationResponse *_Nullable response,
240+
NSError *_Nullable error) {
241+
if (response) {
242+
resolve([self formatRegistrationResponse:response]);
243+
} else {
244+
reject(@"RNAppAuth Error", [error localizedDescription], error);
245+
}
246+
}];
247+
}
248+
166249
/*
167250
* Authorize a user in exchange for a token with provided OIDServiceConfiguration
168251
*/
@@ -297,5 +380,22 @@ - (NSDictionary*)formatResponse: (OIDTokenResponse*) response
297380
@"scopes": authResponse.scope ? [authResponse.scope componentsSeparatedByString:@" "] : [NSArray new],
298381
};
299382
}
383+
384+
- (NSDictionary*)formatRegistrationResponse: (OIDRegistrationResponse*) response {
385+
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
386+
dateFormat.timeZone = [NSTimeZone timeZoneWithAbbreviation: @"UTC"];
387+
[dateFormat setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
388+
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
389+
390+
return @{@"clientId": response.clientID,
391+
@"additionalParameters": response.additionalParameters,
392+
@"clientIdIssuedAt": response.clientIDIssuedAt ? [dateFormat stringFromDate:response.clientIDIssuedAt] : @"",
393+
@"clientSecret": response.clientSecret ? response.clientSecret : @"",
394+
@"clientSecretExpiresAt": response.clientSecretExpiresAt ? [dateFormat stringFromDate:response.clientSecretExpiresAt] : @"",
395+
@"registrationAccessToken": response.registrationAccessToken ? response.registrationAccessToken : @"",
396+
@"registrationClientUri": response.registrationClientURI ? response.registrationClientURI : @"",
397+
@"tokenEndpointAuthMethod": response.tokenEndpointAuthenticationMethod ? response.tokenEndpointAuthenticationMethod : @"",
398+
};
399+
}
300400

301401
@end

0 commit comments

Comments
 (0)