-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathNativeBridgeManager.ts
More file actions
225 lines (208 loc) · 6.25 KB
/
Copy pathNativeBridgeManager.ts
File metadata and controls
225 lines (208 loc) · 6.25 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
import type { INativeBridge } from './INativeBridge';
import type {
ApiCredentials,
Credentials,
WebAuthorizeParameters,
ClearSessionParameters,
NativeClearSessionOptions,
DPoPHeadersParams,
SessionTransferCredentials,
} from '../../../types';
import {
SafariViewControllerPresentationStyle,
type LocalAuthenticationOptions,
type NativeAuthorizeOptions,
} from '../../../types/platform-specific';
import {
AuthError,
Credentials as CredentialsModel,
} from '../../../core/models';
import Auth0NativeModule from '../../../specs/NativeA0Auth0';
import type { NativeModuleError } from '../../../core/interfaces';
/**
* Manages the direct communication with the native Auth0 module.
* It implements the INativeBridge interface and is responsible for:
* - Calling the actual native methods.
*- Normalizing data and parameters between JS and Native.
* - Catching native errors and re-throwing them as structured AuthError objects.
*/
export class NativeBridgeManager implements INativeBridge {
private async a0_call<T>(
nativeMethod: (...args: any[]) => Promise<T>,
...args: any[]
): Promise<T> {
try {
return await nativeMethod(...args);
} catch (e) {
const { code, message } = e as NativeModuleError;
throw new AuthError(code, message, { code, json: e });
}
}
async hasValidInstance(clientId: string, domain: string): Promise<boolean> {
return this.a0_call(
Auth0NativeModule.hasValidAuth0InstanceWithConfiguration.bind(
Auth0NativeModule
),
clientId,
domain
);
}
async initialize(
clientId: string,
domain: string,
localAuthenticationOptions?: LocalAuthenticationOptions,
useDPoP: boolean = true
): Promise<void> {
// This is a new method we'd add to the native side to ensure the
// underlying Auth0.swift/Auth0.android SDKs are configured.
return this.a0_call(
Auth0NativeModule.initializeAuth0WithConfiguration.bind(
Auth0NativeModule
),
clientId,
domain,
localAuthenticationOptions,
useDPoP
);
}
getBundleIdentifier(): Promise<string> {
return this.a0_call(
Auth0NativeModule.getBundleIdentifier.bind(Auth0NativeModule)
);
}
async authorize(
parameters: WebAuthorizeParameters,
options: NativeAuthorizeOptions
): Promise<Credentials> {
let presentationStyle = options.useSFSafariViewController
? ((options.useSFSafariViewController as { presentationStyle: number })
?.presentationStyle ??
SafariViewControllerPresentationStyle.fullScreen)
: undefined;
const scheme =
parameters.redirectUrl?.split('://')[0] ?? options.customScheme;
const credential = await this.a0_call(
Auth0NativeModule.webAuth.bind(Auth0NativeModule),
scheme,
parameters.redirectUrl,
parameters.state,
parameters.nonce,
parameters.audience,
parameters.scope,
parameters.connection,
parameters.maxAge ?? 0,
parameters.organization,
parameters.invitationUrl,
options.leeway ?? 0,
options.ephemeralSession ?? false,
presentationStyle ?? 99, // Since we can't pass null to the native layer, and we need a value to represent this parameter is not set, we are using 99.
// //The native layer will check for this and ignore if the value is 99
parameters.additionalParameters ?? {}
);
return new CredentialsModel(credential);
}
async clearSession(
parameters: ClearSessionParameters,
options: NativeClearSessionOptions
): Promise<void> {
return this.a0_call(
Auth0NativeModule.webAuthLogout.bind(Auth0NativeModule),
options.customScheme,
parameters.federated ?? false,
parameters.returnToUrl
);
}
async cancelWebAuth(): Promise<void> {
return this.a0_call(
Auth0NativeModule.cancelWebAuth.bind(Auth0NativeModule)
);
}
async saveCredentials(credentials: Credentials): Promise<void> {
return this.a0_call(
Auth0NativeModule.saveCredentials.bind(Auth0NativeModule),
credentials
);
}
async getCredentials(
scope?: string,
minTtl?: number,
parameters?: Record<string, any>,
forceRefresh?: boolean
): Promise<Credentials> {
// Assuming the native side can take an empty object for parameters.
const params = parameters ?? {};
return this.a0_call(
Auth0NativeModule.getCredentials.bind(Auth0NativeModule),
scope,
minTtl ?? 0,
params,
forceRefresh ?? false
);
}
getApiCredentials(
audience: string,
scope?: string,
minTtl?: number,
parameters?: Record<string, any>
): Promise<ApiCredentials> {
const params = parameters ?? {};
return this.a0_call(
Auth0NativeModule.getApiCredentials.bind(Auth0NativeModule),
audience,
scope,
minTtl ?? 0,
params
);
}
clearApiCredentials(audience: string, scope?: string): Promise<void> {
return this.a0_call(
Auth0NativeModule.clearApiCredentials.bind(Auth0NativeModule),
audience,
scope
);
}
async hasValidCredentials(minTtl?: number): Promise<boolean> {
return this.a0_call(
Auth0NativeModule.hasValidCredentials.bind(Auth0NativeModule),
minTtl ?? 0
);
}
async clearCredentials(): Promise<void> {
return this.a0_call(
Auth0NativeModule.clearCredentials.bind(Auth0NativeModule)
);
}
async resumeWebAuth(url: string): Promise<void> {
return this.a0_call(
Auth0NativeModule.resumeWebAuth.bind(Auth0NativeModule),
url
);
}
async getDPoPHeaders(
params: DPoPHeadersParams
): Promise<Record<string, string>> {
return this.a0_call(
Auth0NativeModule.getDPoPHeaders.bind(Auth0NativeModule),
params.url,
params.method,
params.accessToken,
params.tokenType,
params.nonce
);
}
async clearDPoPKey(): Promise<void> {
return this.a0_call(Auth0NativeModule.clearDPoPKey.bind(Auth0NativeModule));
}
async getSSOCredentials(
parameters?: Record<string, any>,
headers?: Record<string, string>
): Promise<SessionTransferCredentials> {
const params = parameters ?? {};
const hdrs = headers ?? {};
return this.a0_call(
Auth0NativeModule.getSSOCredentials.bind(Auth0NativeModule),
params,
hdrs
);
}
}