-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathNativeBridgeManager.ts
More file actions
150 lines (137 loc) · 4.25 KB
/
NativeBridgeManager.ts
File metadata and controls
150 lines (137 loc) · 4.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
import type { INativeBridge } from './INativeBridge';
import type {
Credentials,
WebAuthorizeParameters,
ClearSessionParameters,
NativeClearSessionOptions,
} 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';
type NativeModuleError = {
code: string;
message: string;
};
/**
* 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 });
}
}
async hasValidInstance(clientId: string, domain: string): Promise<boolean> {
return this.a0_call(
Auth0NativeModule.hasValidAuth0InstanceWithConfiguration,
clientId,
domain
);
}
async initialize(
clientId: string,
domain: string,
localAuthenticationOptions?: LocalAuthenticationOptions
): 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,
clientId,
domain,
localAuthenticationOptions
);
}
getBundleIdentifier(): Promise<string> {
return this.a0_call(Auth0NativeModule.getBundleIdentifier);
}
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,
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,
parameters.additionalParameters ?? {}
);
return new CredentialsModel(credential);
}
async clearSession(
parameters: ClearSessionParameters,
options: NativeClearSessionOptions
): Promise<void> {
return this.a0_call(
Auth0NativeModule.webAuthLogout,
options.customScheme,
parameters.federated ?? false,
parameters.returnToUrl
);
}
async cancelWebAuth(): Promise<void> {
return this.a0_call(Auth0NativeModule.cancelWebAuth);
}
async saveCredentials(credentials: Credentials): Promise<void> {
return this.a0_call(Auth0NativeModule.saveCredentials, 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,
scope,
minTtl ?? 0,
params,
forceRefresh ?? false
);
}
async hasValidCredentials(minTtl?: number): Promise<boolean> {
return this.a0_call(Auth0NativeModule.hasValidCredentials, minTtl ?? 0);
}
async clearCredentials(): Promise<void> {
return this.a0_call(Auth0NativeModule.clearCredentials);
}
async resumeWebAuth(url: string): Promise<void> {
return this.a0_call(Auth0NativeModule.resumeWebAuth, url);
}
}