-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathRNAppleAuthASAuthorizationDelegates.m
112 lines (95 loc) · 4.01 KB
/
RNAppleAuthASAuthorizationDelegates.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#import "RNAppleAuthASAuthorizationDelegates.h"
@implementation RNAppleAuthASAuthorizationDelegates
- (instancetype)initWithCompletion:(void (^)(NSError *error, NSDictionary *authorizationCredential))completion andNonce:(NSString *)nonce {
if (self = [super init]) {
_completion = completion;
_nonce = nonce;
}
return self;
}
#pragma mark - ASAuthorizationControllerPresentationContextProviding Methods
- (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller {
#if TARGET_OS_OSX
return [[NSApplication sharedApplication] keyWindow];
#else
return [[UIApplication sharedApplication] keyWindow];
#endif
}
#pragma mark - ASAuthorizationControllerDelegate Methods
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization {
NSLog(@"RNAppleAuth -> didCompleteWithAuthorization");
ASAuthorizationAppleIDCredential *appleIdCredential = authorization.credential;
_completion(nil, [self buildDictionaryFromAppleIdCredential:appleIdCredential]);
_completion = nil;
}
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error {
NSLog(@"RNAppleAuth -> didCompleteWithError");
NSLog(error.localizedDescription);
_completion(error, nil);
_completion = nil;
}
#pragma mark - ASAuthorizationController Methods
- (void)performRequestsForAuthorizationController:(ASAuthorizationController *)authorizationController {
authorizationController.delegate = self;
authorizationController.presentationContextProvider = self;
[authorizationController performRequests];
}
#pragma mark - ASAuthorizationController Methods
- (NSDictionary *)buildDictionaryFromAppleIdCredential:(ASAuthorizationAppleIDCredential *)appleIdCredential {
NSString *identityToken;
if ([appleIdCredential valueForKey:@"identityToken"] != nil) {
identityToken = [
[NSString alloc] initWithData:[appleIdCredential valueForKey:@"identityToken"] encoding:NSUTF8StringEncoding
];
}
NSString *authorizationCode;
if ([appleIdCredential valueForKey:@"authorizationCode"] != nil) {
authorizationCode = [
[NSString alloc] initWithData:[appleIdCredential valueForKey:@"authorizationCode"] encoding:NSUTF8StringEncoding
];
}
NSMutableDictionary *fullName;
if ([appleIdCredential valueForKey:@"fullName"] != nil) {
fullName = [[appleIdCredential.fullName dictionaryWithValuesForKeys:@[
@"namePrefix",
@"givenName",
@"middleName",
@"familyName",
@"nameSuffix",
@"nickname",
]] mutableCopy];
[fullName enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if (obj == nil) {
fullName[key] = [NSNull null];
}
}];
}
return @{
@"nonce": _nonce ? _nonce : (id) [NSNull null],
@"user": appleIdCredential.user,
@"fullName": fullName ? fullName : (id) [NSNull null],
@"realUserStatus": @(appleIdCredential.realUserStatus),
@"authorizedScopes": appleIdCredential.authorizedScopes,
@"identityToken": identityToken ? identityToken : (id) [NSNull null],
@"email": appleIdCredential.email ? appleIdCredential.email : (id) [NSNull null],
@"state": appleIdCredential.state ? appleIdCredential.state : (id) [NSNull null],
@"authorizationCode": authorizationCode ? authorizationCode : (id) [NSNull null],
};
}
@end