-
-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathindex.ios.ts
More file actions
270 lines (224 loc) · 8.95 KB
/
index.ios.ts
File metadata and controls
270 lines (224 loc) · 8.95 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { Device, Utils, View } from '@nativescript/core';
import { SignInOptions, User, UserFullName } from '.';
import { CredentialState, UNSUPPORTED, UserDetectionStatus } from './common';
export class SignInError extends Error {
#native: NSError;
static fromNative(native: NSError, message?: string) {
const error = new SignInError(message || native?.localizedDescription);
error.#native = native;
return error;
}
get native() {
return this.#native;
}
}
function randomNonce(length: number) {
let remainingLength = length;
let result = '';
let error = false;
let errorValue;
const characterSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._";
while (remainingLength > 0) {
const randoms = [];
for (let i = 0; i < 16; i++) {
if (error) {
break;
}
const random = interop.alloc(1);
const errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, random);
if (errorCode !== errSecSuccess) {
errorValue = errorCode;
error = true;
break;
}
randoms.push(random);
}
if (error) {
break;
}
randoms.forEach((value) => {
if (remainingLength === 0) {
return;
}
const ref = new interop.Reference(interop.types.uint8, value);
if (ref.value < characterSet.length) {
const character = characterSet[ref.value];
result += character;
remainingLength--;
}
})
}
return result;
}
function sha256Hash(input: string): string {
const sha256buffer = CC_SHA256(interop.handleof(NSString.stringWithString(input).UTF8String), input.length, interop.alloc(input.length));
let inputHashed = '';
for (let i = 0; i < input.length; i++) {
inputHashed += parseInt(sha256buffer[i], 10).toString(16).padStart(2, '0');
}
return inputHashed;
}
export class SignIn {
static #controller: ASAuthorizationController;
static #delegate: ASAuthorizationControllerDelegate;
public static signIn(
options?: SignInOptions
) {
return new Promise<User>((resolve, reject) => {
if (!SignIn.isSupported()) {
reject(UNSUPPORTED);
}
ensureClass();
this.#delegate = ASAuthorizationControllerDelegateImpl.initWithCallback(resolve, reject);
(this.#delegate as any)._options = options;
const provider = ASAuthorizationAppleIDProvider.new();
const request = provider.createRequest();
if (options?.user) {
request.user = options.user;
}
if (options?.scopes) {
request.requestedScopes = options.scopes.map(item => {
switch (item) {
case 'EMAIL':
return ASAuthorizationScopeEmail;
case 'FULL_NAME':
return ASAuthorizationScopeFullName;
default:
return null
}
}).filter(item => item !== null) as any
}
if (options?.useNonce) {
if (options.nonce) {
request.nonce = sha256Hash(options.nonce);
} else {
const nonce = randomNonce(32);
request.nonce = sha256Hash(nonce);
(this.#delegate as any)._options.nonce = nonce;
}
}
this.#controller = ASAuthorizationController.alloc().initWithAuthorizationRequests([
request
]);
this.#controller.delegate = this.#delegate;
this.#controller.performRequests();
});
}
public static isSupported() {
return parseFloat(Device.sdkVersion) >= 13;
}
public static getState(userID: string) {
return new Promise<CredentialState>((resolve, reject) => {
if (!SignIn.isSupported()) {
reject(UNSUPPORTED);
}
const provider = ASAuthorizationAppleIDProvider.new();
provider.getCredentialStateForUserIDCompletion(userID, (state, error) => {
if (error) {
reject(SignInError.fromNative(error));
} else {
resolve(getCredentialState(state));
}
});
});
}
}
export class SignInButton extends View {
createNativeView(): Object {
return ASAuthorizationAppleIDButton.new()
}
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number) {
const nativeView = this.nativeView;
if (nativeView) {
const width = Utils.layout.getMeasureSpecSize(widthMeasureSpec);
const height = Utils.layout.getMeasureSpecSize(heightMeasureSpec);
this.setMeasuredDimension(width, height);
}
}
}
function getCredentialState(state: ASAuthorizationAppleIDProviderCredentialState): CredentialState {
switch (state) {
case ASAuthorizationAppleIDProviderCredentialState.Authorized:
return CredentialState.Authorized;
case ASAuthorizationAppleIDProviderCredentialState.Revoked:
return CredentialState.Revoked;
case ASAuthorizationAppleIDProviderCredentialState.Transferred:
return CredentialState.Transferred;
default:
if (state !== ASAuthorizationAppleIDProviderCredentialState.NotFound) {
console.info("Invalid state for getState: " + state + ", please report an issue at the plugin repo!");
}
return CredentialState.NotFound;
}
}
function getRealUserStatus(status: ASUserDetectionStatus): UserDetectionStatus {
switch (status) {
case ASUserDetectionStatus.LikelyReal:
return UserDetectionStatus.LikelyReal;
case ASUserDetectionStatus.Unsupported:
return UserDetectionStatus.Unsupported;
default:
return UserDetectionStatus.Unknown
}
}
let ASAuthorizationControllerDelegateImpl;
function ensureClass() {
if (!SignIn.isSupported()) {
return;
}
if (ASAuthorizationControllerDelegateImpl) {
return;
}
@NativeClass()
@ObjCClass(ASAuthorizationControllerDelegate)
class ASAuthorizationControllerDelegateExt extends NSObject implements ASAuthorizationControllerDelegate {
_resolve;
_reject;
_options: SignInOptions;
static initWithCallback(resolve, reject) {
const delegate = <ASAuthorizationControllerDelegateExt>ASAuthorizationControllerDelegateExt.new();
delegate._resolve = resolve;
delegate._reject = reject;
return delegate;
}
authorizationControllerDidCompleteWithAuthorization(controller: ASAuthorizationController, authorization: ASAuthorization): void {
const credential = authorization.credential as ASAuthorizationAppleIDCredential;
let identityToken = null;
const identityTokenData = credential.valueForKey('identityToken');
if (identityTokenData) {
identityToken = NSString.alloc().initWithDataEncoding(identityTokenData, NSUTF8StringEncoding).toString();
}
let authorizationCode = null;
const authorizationCodeData = credential.valueForKey('authorizationCode');
if (authorizationCodeData) {
authorizationCode = NSString.alloc().initWithDataEncoding(authorizationCodeData, NSUTF8StringEncoding).toString();
}
let fullName: UserFullName = null;
const fullNameData = credential.fullName;
if (fullNameData) {
fullName = {};
fullName.namePrefix = fullNameData.namePrefix;
fullName.givenName = fullNameData.givenName;
fullName.middleName = fullNameData.middleName;
fullName.familyName = fullNameData.familyName;
fullName.nameSuffix = fullNameData.nameSuffix;
fullName.nickname = fullNameData.nickname;
}
this._resolve({
nonce: this._options?.nonce || null,
user: credential.user,
fullName,
realUserStatus: getRealUserStatus(credential.realUserStatus),
authorizedScopes: credential.authorizedScopes,
identityToken,
email: credential.email || null,
state: credential.state,
authorizationCode
});
}
authorizationControllerDidCompleteWithError(controller: ASAuthorizationController, error: NSError): void {
this._reject?.(SignInError.fromNative(error));
}
}
ASAuthorizationControllerDelegateImpl = ASAuthorizationControllerDelegateExt;
}