-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathNativeCredentialsManager.ts
More file actions
74 lines (65 loc) · 2.37 KB
/
Copy pathNativeCredentialsManager.ts
File metadata and controls
74 lines (65 loc) · 2.37 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
import type { ICredentialsManager } from '../../../core/interfaces';
import { ApiCredentials, AuthError } from '../../../core/models';
import { CredentialsManagerError } from '../../../core/models';
import type {
ApiCredentials as IApiCredentials,
Credentials,
SessionTransferCredentials,
} from '../../../types';
import type { INativeBridge } from '../bridge';
/**
* A native platform-specific implementation of the ICredentialsManager.
* It delegates all credential storage, retrieval, and management logic to the
* underlying native bridge, which uses secure native storage.
*/
export class NativeCredentialsManager implements ICredentialsManager {
constructor(private bridge: INativeBridge) {}
private async handleError<T>(promise: Promise<T>): Promise<T> {
try {
return await promise;
} catch (e) {
// Assume the bridge only throws AuthError.
throw new CredentialsManagerError(e as AuthError);
}
}
saveCredentials(credentials: Credentials): Promise<void> {
return this.handleError(this.bridge.saveCredentials(credentials));
}
getCredentials(
scope?: string,
minTtl?: number,
parameters?: Record<string, any>,
forceRefresh?: boolean
): Promise<Credentials> {
return this.handleError(
this.bridge.getCredentials(scope, minTtl, parameters, forceRefresh)
);
}
async clearCredentials(): Promise<void> {
return this.handleError(this.bridge.clearCredentials());
}
async hasValidCredentials(minTtl?: number): Promise<boolean> {
return this.handleError(this.bridge.hasValidCredentials(minTtl));
}
async getApiCredentials(
audience: string,
scope?: string,
minTtl?: number,
parameters?: Record<string, any>
): Promise<ApiCredentials> {
const nativeCredentials = await this.handleError(
this.bridge.getApiCredentials(audience, scope, minTtl ?? 0, parameters)
);
// Convert plain object from native to class instance
return new ApiCredentials(nativeCredentials as IApiCredentials);
}
async clearApiCredentials(audience: string, scope?: string): Promise<void> {
return this.handleError(this.bridge.clearApiCredentials(audience, scope));
}
getSSOCredentials(
parameters?: Record<string, any>,
headers?: Record<string, string>
): Promise<SessionTransferCredentials> {
return this.handleError(this.bridge.getSSOCredentials(parameters, headers));
}
}