-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathindex.ts
127 lines (120 loc) · 4.14 KB
/
index.ts
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
import { NativeModules } from 'react-native';
import CredentialsManagerError from './credentialsManagerError';
import { Credentials } from '../types';
import { Auth0Module } from 'src/internal-types';
import { _ensureNativeModuleIsInitializedWithConfiguration } from '../utils/nativeHelper';
import LocalAuthenticationOptions from './localAuthenticationOptions';
class CredentialsManager {
private domain;
private clientId;
private Auth0Module: Auth0Module;
private localAuthenticationOptions?: LocalAuthenticationOptions;
/**
* @ignore
*/
constructor(
domain: string,
clientId: string,
localAuthenticationOptions?: LocalAuthenticationOptions
) {
this.domain = domain;
this.clientId = clientId;
this.localAuthenticationOptions = localAuthenticationOptions;
this.Auth0Module = NativeModules.A0Auth0;
}
/**
* Saves the provided credentials
*/
async saveCredentials(credentials: Credentials): Promise<void> {
const validateKeys = ['idToken', 'accessToken', 'tokenType', 'expiresAt'];
validateKeys.forEach((key) => {
if (!credentials[key]) {
const json = {
error: 'a0.credential_manager.invalid_input',
error_description: `${key} cannot be empty`,
invalid_parameter: key,
};
throw new CredentialsManagerError({ json, status: 0 });
}
});
try {
await _ensureNativeModuleIsInitializedWithConfiguration(
this.Auth0Module,
this.clientId,
this.domain,
this.localAuthenticationOptions
);
return await this.Auth0Module.saveCredentials(credentials);
} catch (e) {
const json = {
error: 'a0.credential_manager.invalid',
error_description: e.message,
};
throw new CredentialsManagerError({ json, status: 0 });
}
}
/**
* Gets the credentials that has already been saved
*
* @param scope The scope to request for the access token. If null is passed, the previous scope will be kept.
* @param minTtl The minimum time in seconds that the access token should last before expiration.
* @param parameters Additional parameters to send in the request to refresh expired credentials.
* @param forceRefresh Whether to force refresh the credentials. It will work only if the refresh token already exists. For iOS, doing forceRefresh will not send the scope. Since scope change already does force refresh, it is better to avoid force refresh if the scope is being changed.
* @returns A populated instance of {@link Credentials}.
*/
async getCredentials(
scope?: string,
minTtl: number = 0,
parameters: Record<string, unknown> = {},
forceRefresh: boolean = false
): Promise<Credentials> {
try {
await _ensureNativeModuleIsInitializedWithConfiguration(
this.Auth0Module,
this.clientId,
this.domain,
this.localAuthenticationOptions
);
return this.Auth0Module.getCredentials(
scope,
minTtl,
parameters,
forceRefresh
);
} catch (e) {
const json = {
error: 'a0.credential_manager.invalid',
error_description: e.message,
};
throw new CredentialsManagerError({ json, status: 0 });
}
}
/**
* Returns whether this manager contains a valid non-expired pair of credentials.
*
* @param minTtl The minimum time in seconds that the access token should last before expiration
* @returns `true` if a valid set of credentials are available, or `false` if there are no credentials to return.
*/
async hasValidCredentials(minTtl = 0): Promise<boolean> {
await _ensureNativeModuleIsInitializedWithConfiguration(
this.Auth0Module,
this.clientId,
this.domain,
this.localAuthenticationOptions
);
return await this.Auth0Module.hasValidCredentials(minTtl);
}
/**
* Delete the stored credentials
*/
async clearCredentials(): Promise<void> {
await _ensureNativeModuleIsInitializedWithConfiguration(
this.Auth0Module,
this.clientId,
this.domain,
this.localAuthenticationOptions
);
return this.Auth0Module.clearCredentials();
}
}
export default CredentialsManager;