-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathNativeCredentialsManager.ts
More file actions
46 lines (40 loc) · 1.47 KB
/
NativeCredentialsManager.ts
File metadata and controls
46 lines (40 loc) · 1.47 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
import type { ICredentialsManager } from '../../../core/interfaces';
import { AuthError } from '../../../core/models';
import { CredentialsManagerError } from '../../../core/models';
import type { Credentials } 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)
);
}
hasValidCredentials(minTtl?: number): Promise<boolean> {
return this.handleError(this.bridge.hasValidCredentials(minTtl));
}
clearCredentials(): Promise<void> {
return this.handleError(this.bridge.clearCredentials());
}
}