@cubist-labs/cubesigner-sdk 0.1.50 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -13
- package/dist/src/client.d.ts +434 -7
- package/dist/src/client.js +1022 -18
- package/dist/src/ethers/index.d.ts +2 -4
- package/dist/src/ethers/index.js +11 -9
- package/dist/src/fido.d.ts +76 -0
- package/dist/src/fido.js +148 -0
- package/dist/src/index.d.ts +102 -30
- package/dist/src/index.js +126 -72
- package/dist/src/key.d.ts +15 -45
- package/dist/src/key.js +31 -93
- package/dist/src/mfa.d.ts +85 -14
- package/dist/src/mfa.js +158 -40
- package/dist/src/org.d.ts +237 -123
- package/dist/src/org.js +108 -213
- package/dist/src/paginator.d.ts +76 -0
- package/dist/src/paginator.js +99 -0
- package/dist/src/role.d.ts +76 -74
- package/dist/src/role.js +79 -136
- package/dist/src/schema.d.ts +1672 -520
- package/dist/src/schema.js +1 -1
- package/dist/src/schema_types.d.ts +103 -0
- package/dist/src/schema_types.js +3 -0
- package/dist/src/session/session_manager.js +2 -2
- package/dist/src/session/session_storage.js +1 -1
- package/dist/src/session/signer_session_manager.d.ts +16 -29
- package/dist/src/session/signer_session_manager.js +27 -78
- package/dist/src/signer_session.d.ts +232 -125
- package/dist/src/signer_session.js +149 -250
- package/dist/src/util.d.ts +20 -0
- package/dist/src/util.js +31 -2
- package/package.json +13 -11
- package/src/client.ts +1217 -7
- package/src/ethers/index.ts +11 -18
- package/src/index.ts +149 -101
- package/src/key.ts +28 -121
- package/src/mfa.ts +202 -0
- package/src/org.ts +126 -275
- package/src/paginator.ts +122 -0
- package/src/role.ts +108 -181
- package/src/schema.ts +1673 -520
- package/src/schema_types.ts +103 -0
- package/src/session/session_manager.ts +2 -2
- package/src/session/session_storage.ts +1 -1
- package/src/session/signer_session_manager.ts +38 -108
- package/src/signer_session.ts +164 -323
- package/src/util.ts +41 -0
package/src/mfa.ts
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
ApiAddFidoChallenge,
|
|
5
|
+
ApiMfaFidoChallenge,
|
|
6
|
+
MfaRequestInfo,
|
|
7
|
+
PublicKeyCredential,
|
|
8
|
+
TotpInfo,
|
|
9
|
+
} from "./schema_types";
|
|
10
|
+
import { CubeSignerClient } from "./client";
|
|
11
|
+
import { decodeBase64Url, encodeToBase64Url } from "./util";
|
|
12
|
+
|
|
13
|
+
/** MFA receipt */
|
|
14
|
+
export interface MfaReceipt {
|
|
15
|
+
/** MFA request ID */
|
|
16
|
+
mfaId: string;
|
|
17
|
+
/** Corresponding org ID */
|
|
18
|
+
mfaOrgId: string;
|
|
19
|
+
/** MFA confirmation code */
|
|
20
|
+
mfaConf: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** TOTP challenge that must be answered before user's TOTP is updated */
|
|
24
|
+
export class TotpChallenge {
|
|
25
|
+
readonly #csc: CubeSignerClient;
|
|
26
|
+
readonly #totpInfo: TotpInfo;
|
|
27
|
+
|
|
28
|
+
/** The id of the challenge */
|
|
29
|
+
get totpId() {
|
|
30
|
+
return this.#totpInfo.totp_id;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The new TOTP configuration */
|
|
34
|
+
get totpUrl() {
|
|
35
|
+
return this.#totpInfo.totp_url;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {CubeSignerClient} csc Used when answering the challenge.
|
|
40
|
+
* @param {TotpInfo} totpInfo TOTP challenge information.
|
|
41
|
+
*/
|
|
42
|
+
constructor(csc: CubeSignerClient, totpInfo: TotpInfo) {
|
|
43
|
+
this.#csc = csc;
|
|
44
|
+
this.#totpInfo = totpInfo;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Answer the challenge with the code that corresponds to `this.totpUrl`.
|
|
49
|
+
* @param {string} code 6-digit code that corresponds to `this.totpUrl`.
|
|
50
|
+
*/
|
|
51
|
+
async answer(code: string) {
|
|
52
|
+
if (!/^\d{1,6}$/.test(code)) {
|
|
53
|
+
throw new Error(`Invalid TOTP code: ${code}; it must be a 6-digit string`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
await this.#csc.userResetTotpComplete(this.totpId, code);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Returned after creating a request to add a new FIDO device.
|
|
62
|
+
* Provides some helper methods for answering this challenge.
|
|
63
|
+
*/
|
|
64
|
+
export class AddFidoChallenge {
|
|
65
|
+
readonly #csc: CubeSignerClient;
|
|
66
|
+
readonly challengeId: string;
|
|
67
|
+
readonly options: any;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Constructor
|
|
71
|
+
* @param {CubeSignerClient} csc CubeSigner instance used to request to add a FIDO device
|
|
72
|
+
* @param {ApiAddFidoChallenge} challenge The challenge returned by the remote end.
|
|
73
|
+
*/
|
|
74
|
+
constructor(csc: CubeSignerClient, challenge: ApiAddFidoChallenge) {
|
|
75
|
+
this.#csc = csc;
|
|
76
|
+
this.challengeId = challenge.challenge_id;
|
|
77
|
+
|
|
78
|
+
// fix options returned from the server: rename fields and decode base64 fields to uint8[]
|
|
79
|
+
this.options = {
|
|
80
|
+
...challenge.options,
|
|
81
|
+
challenge: decodeBase64Url(challenge.options.challenge),
|
|
82
|
+
};
|
|
83
|
+
this.options.pubKeyCredParams ??= challenge.options.pub_key_cred_params;
|
|
84
|
+
this.options.excludeCredentials ??= challenge.options.exclude_credentials;
|
|
85
|
+
this.options.authenticatorSelection ??= challenge.options.authenticator_selection;
|
|
86
|
+
delete this.options.pub_key_cred_params;
|
|
87
|
+
delete this.options.exclude_credentials;
|
|
88
|
+
delete this.options.authenticator_selection;
|
|
89
|
+
|
|
90
|
+
if (challenge.options.user) {
|
|
91
|
+
this.options.user.id = decodeBase64Url(challenge.options.user.id);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
for (const credential of this.options.excludeCredentials ?? []) {
|
|
95
|
+
credential.id = decodeBase64Url(credential.id);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Answers this challenge by using the `CredentialsContainer` API to create a credential
|
|
101
|
+
* based on the the public key credential creation options from this challenge.
|
|
102
|
+
*/
|
|
103
|
+
async createCredentialAndAnswer() {
|
|
104
|
+
const cred = await navigator.credentials.create({ publicKey: this.options });
|
|
105
|
+
await this.answer(cred);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Answers this challenge using a given credential `cred`;
|
|
110
|
+
* the credential should be obtained by calling
|
|
111
|
+
*
|
|
112
|
+
* ```
|
|
113
|
+
* const cred = await navigator.credentials.create({ publicKey: this.options });
|
|
114
|
+
* ```
|
|
115
|
+
*
|
|
116
|
+
* @param {any} cred Credential created by calling the `CredentialContainer`'s `create` method
|
|
117
|
+
* based on the public key creation options from this challenge.
|
|
118
|
+
*/
|
|
119
|
+
async answer(cred: any) {
|
|
120
|
+
const answer = <PublicKeyCredential>{
|
|
121
|
+
id: cred.id,
|
|
122
|
+
response: {
|
|
123
|
+
clientDataJSON: encodeToBase64Url(cred.response.clientDataJSON),
|
|
124
|
+
attestationObject: encodeToBase64Url(cred.response.attestationObject),
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
await this.#csc.userRegisterFidoComplete(this.challengeId, answer);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Returned after initiating MFA approval using FIDO.
|
|
133
|
+
* Provides some helper methods for answering this challenge.
|
|
134
|
+
*/
|
|
135
|
+
export class MfaFidoChallenge {
|
|
136
|
+
readonly #csc: CubeSignerClient;
|
|
137
|
+
readonly mfaId: string;
|
|
138
|
+
readonly challengeId: string;
|
|
139
|
+
readonly options: any;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @param {CubeSignerClient} csc The session used to initiate MFA approval using FIDO
|
|
143
|
+
* @param {string} mfaId The MFA request id.
|
|
144
|
+
* @param {ApiMfaFidoChallenge} challenge The challenge returned by the remote end
|
|
145
|
+
*/
|
|
146
|
+
constructor(csc: CubeSignerClient, mfaId: string, challenge: ApiMfaFidoChallenge) {
|
|
147
|
+
this.#csc = csc;
|
|
148
|
+
this.mfaId = mfaId;
|
|
149
|
+
this.challengeId = challenge.challenge_id;
|
|
150
|
+
|
|
151
|
+
// fix options returned from the server: rename fields and decode base64 fields into uint8[]
|
|
152
|
+
this.options = {
|
|
153
|
+
...challenge.options,
|
|
154
|
+
challenge: decodeBase64Url(challenge.options.challenge),
|
|
155
|
+
};
|
|
156
|
+
this.options.rpId ??= challenge.options.rp_id;
|
|
157
|
+
this.options.allowCredentials ??= challenge.options.allow_credentials;
|
|
158
|
+
this.options.userVerification ??= challenge.options.user_verification;
|
|
159
|
+
delete this.options.rp_id;
|
|
160
|
+
delete this.options.allow_credentials;
|
|
161
|
+
delete this.options.user_verification;
|
|
162
|
+
|
|
163
|
+
for (const credential of this.options.allowCredentials ?? []) {
|
|
164
|
+
credential.id = decodeBase64Url(credential.id);
|
|
165
|
+
if (credential.transports === null) {
|
|
166
|
+
delete credential.transports;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Answers this challenge by using the `CredentialsContainer` API to get a credential
|
|
173
|
+
* based on the the public key credential request options from this challenge.
|
|
174
|
+
*/
|
|
175
|
+
async createCredentialAndAnswer(): Promise<MfaRequestInfo> {
|
|
176
|
+
const cred = await navigator.credentials.get({ publicKey: this.options });
|
|
177
|
+
return await this.answer(cred);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Answers this challenge using a given credential `cred`.
|
|
182
|
+
* To obtain this credential, for example, call
|
|
183
|
+
*
|
|
184
|
+
* ```
|
|
185
|
+
* const cred = await navigator.credentials.get({ publicKey: this.options });
|
|
186
|
+
* ```
|
|
187
|
+
*
|
|
188
|
+
* @param {any} cred Credential created by calling the `CredentialContainer`'s `get` method
|
|
189
|
+
* based on the public key credential request options from this challenge.
|
|
190
|
+
*/
|
|
191
|
+
async answer(cred: any): Promise<MfaRequestInfo> {
|
|
192
|
+
const answer = <PublicKeyCredential>{
|
|
193
|
+
id: cred.id,
|
|
194
|
+
response: {
|
|
195
|
+
clientDataJSON: encodeToBase64Url(cred.response.clientDataJSON),
|
|
196
|
+
authenticatorData: encodeToBase64Url(cred.response.authenticatorData),
|
|
197
|
+
signature: encodeToBase64Url(cred.response.signature),
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
return await this.#csc.mfaApproveFidoComplete(this.mfaId, this.challengeId, answer);
|
|
201
|
+
}
|
|
202
|
+
}
|