react-native-credentials-manager 0.8.2 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,25 @@
3
3
  #import <React/RCTLog.h>
4
4
  #import <AuthenticationServices/AuthenticationServices.h>
5
5
 
6
+ static NSArray<NSString *> *RNCMNSArrayFromLazyStringVector(const facebook::react::LazyVector<NSString *> &vec)
7
+ {
8
+ NSMutableArray<NSString *> *result = [NSMutableArray arrayWithCapacity:vec.size()];
9
+ for (size_t i = 0, n = vec.size(); i < n; i++) {
10
+ NSString *value = vec.at(i);
11
+ if (value != nil) {
12
+ [result addObject:value];
13
+ }
14
+ }
15
+ return result;
16
+ }
17
+
18
+ @interface CredentialsManager ()
19
+ - (NSArray<ASAuthorizationScope> *)appleAuthorizationScopesFromJSScopes:(NSArray<NSString *> *)scopes;
20
+ - (void)configureAppleIDRequest:(ASAuthorizationAppleIDRequest *)request
21
+ nonce:(NSString *)nonce
22
+ requestedScopes:(NSArray<NSString *> *)requestedScopes;
23
+ @end
24
+
6
25
  @implementation CredentialsManager
7
26
 
8
27
  RCT_EXPORT_MODULE()
@@ -119,6 +138,14 @@ preferImmediatelyAvailableCredentials:(BOOL)preferImmediatelyAvailableCredential
119
138
  reject:(RCTPromiseRejectBlock)reject {
120
139
 
121
140
  id passkeyParams = params.passkeys();
141
+
142
+ NSString *appleNonce = nil;
143
+ NSArray<NSString *> *appleRequestedScopes = nil;
144
+ std::optional<JS::NativeCredentialsManager::AppleSignInParams> appleSignIn = params.appleSignIn();
145
+ if (appleSignIn.has_value()) {
146
+ appleNonce = appleSignIn->nonce();
147
+ appleRequestedScopes = RNCMNSArrayFromLazyStringVector(appleSignIn->requestedScopes());
148
+ }
122
149
 
123
150
  dispatch_async(dispatch_get_main_queue(), ^{
124
151
  self.currentResolve = resolve;
@@ -169,9 +196,7 @@ preferImmediatelyAvailableCredentials:(BOOL)preferImmediatelyAvailableCredential
169
196
  } else if ([option isEqualToString:@"apple-signin"]) {
170
197
  ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
171
198
  ASAuthorizationAppleIDRequest *appleIDRequest = [appleIDProvider createRequest];
172
-
173
- NSArray<ASAuthorizationScope> *defaultScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
174
- appleIDRequest.requestedScopes = defaultScopes;
199
+ [self configureAppleIDRequest:appleIDRequest nonce:appleNonce requestedScopes:appleRequestedScopes];
175
200
 
176
201
  [authRequests addObject:appleIDRequest];
177
202
  } else if ([option isEqualToString:@"google-signin"]) {
@@ -206,6 +231,8 @@ preferImmediatelyAvailableCredentials:(BOOL)preferImmediatelyAvailableCredential
206
231
  - (void)signUpWithApple:(JS::NativeCredentialsManager::AppleSignInParams &)params
207
232
  resolve:(RCTPromiseResolveBlock)resolve
208
233
  reject:(RCTPromiseRejectBlock)reject {
234
+ NSString *nonce = params.nonce();
235
+ NSArray<NSString *> *requestedScopes = RNCMNSArrayFromLazyStringVector(params.requestedScopes());
209
236
 
210
237
  dispatch_async(dispatch_get_main_queue(), ^{
211
238
  self.currentResolve = resolve;
@@ -213,8 +240,7 @@ preferImmediatelyAvailableCredentials:(BOOL)preferImmediatelyAvailableCredential
213
240
 
214
241
  ASAuthorizationAppleIDProvider *appleIDProvider = [[ASAuthorizationAppleIDProvider alloc] init];
215
242
  ASAuthorizationAppleIDRequest *appleIDRequest = [appleIDProvider createRequest];
216
-
217
- appleIDRequest.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
243
+ [self configureAppleIDRequest:appleIDRequest nonce:nonce requestedScopes:requestedScopes];
218
244
 
219
245
  ASAuthorizationController *authController = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[appleIDRequest]];
220
246
  authController.delegate = self;
@@ -378,6 +404,32 @@ preferImmediatelyAvailableCredentials:(BOOL)preferImmediatelyAvailableCredential
378
404
  return keyWindow ?: [[UIApplication sharedApplication] windows].firstObject;
379
405
  }
380
406
 
407
+ #pragma mark - Apple Sign In Helpers
408
+
409
+ - (NSArray<ASAuthorizationScope> *)appleAuthorizationScopesFromJSScopes:(NSArray<NSString *> *)scopes {
410
+ NSMutableArray<ASAuthorizationScope> *mappedScopes = [NSMutableArray array];
411
+ for (NSString *scope in scopes) {
412
+ if ([scope isEqualToString:@"fullName"]) {
413
+ [mappedScopes addObject:ASAuthorizationScopeFullName];
414
+ } else if ([scope isEqualToString:@"email"]) {
415
+ [mappedScopes addObject:ASAuthorizationScopeEmail];
416
+ }
417
+ }
418
+
419
+ return mappedScopes.count > 0
420
+ ? [mappedScopes copy]
421
+ : @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
422
+ }
423
+
424
+ - (void)configureAppleIDRequest:(ASAuthorizationAppleIDRequest *)request
425
+ nonce:(NSString *)nonce
426
+ requestedScopes:(NSArray<NSString *> *)requestedScopes {
427
+ if (nonce.length > 0) {
428
+ request.nonce = nonce;
429
+ }
430
+ request.requestedScopes = [self appleAuthorizationScopesFromJSScopes:requestedScopes];
431
+ }
432
+
381
433
  #pragma mark - Helper Methods
382
434
 
383
435
  - (NSString *)createAuthenticationResponseJSON:(ASAuthorizationPlatformPublicKeyCredentialAssertion *)assertion {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-credentials-manager",
3
- "version": "0.8.2",
3
+ "version": "0.9.0",
4
4
  "description": "A React Native library that implements the Credential Manager API for Android. This library allows you to manage passwords and passkeys in your React Native applications.",
5
5
  "source": "./src/index.tsx",
6
6
  "main": "./lib/commonjs/index.js",