@cubist-labs/cubesigner-sdk 0.1.26 → 0.1.77
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 +94 -33
- package/dist/src/ethers/index.d.ts +25 -5
- package/dist/src/ethers/index.js +58 -16
- package/dist/src/fido.d.ts +76 -0
- package/dist/src/fido.js +148 -0
- package/dist/src/index.d.ts +148 -35
- package/dist/src/index.js +320 -53
- package/dist/src/key.d.ts +64 -8
- package/dist/src/key.js +91 -19
- package/dist/src/org.d.ts +98 -9
- package/dist/src/org.js +144 -29
- package/dist/src/paginator.d.ts +76 -0
- package/dist/src/paginator.js +99 -0
- package/dist/src/role.d.ts +20 -8
- package/dist/src/role.js +7 -5
- package/dist/src/schema.d.ts +2395 -393
- package/dist/src/schema.js +1 -1
- package/dist/src/session/cognito_manager.d.ts +59 -0
- package/dist/src/session/cognito_manager.js +111 -0
- package/dist/src/session/session_manager.d.ts +15 -0
- package/dist/src/session/session_manager.js +21 -2
- package/dist/src/session/session_storage.js +1 -1
- package/dist/src/session/signer_session_manager.d.ts +24 -12
- package/dist/src/session/signer_session_manager.js +45 -20
- package/dist/src/signer_session.d.ts +136 -38
- package/dist/src/signer_session.js +187 -80
- package/dist/src/util.d.ts +20 -0
- package/dist/src/util.js +31 -2
- package/package.json +12 -7
- package/src/ethers/index.ts +88 -16
- package/src/fido.ts +166 -0
- package/src/index.ts +366 -77
- package/src/key.ts +112 -16
- package/src/org.ts +200 -37
- package/src/paginator.ts +122 -0
- package/src/role.ts +24 -11
- package/src/schema.ts +2458 -449
- package/src/session/{management_session_manager.ts → cognito_manager.ts} +13 -15
- package/src/session/session_manager.ts +25 -1
- package/src/session/session_storage.ts +1 -1
- package/src/session/signer_session_manager.ts +57 -27
- package/src/signer_session.ts +266 -89
- package/src/util.ts +41 -0
- package/src/session/oidc_session_manager.ts +0 -193
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import { paths, Client } from "../client";
|
|
2
|
-
import { EnvInterface } from "..";
|
|
3
|
-
import { assertOk } from "../util";
|
|
4
|
-
import { OrgSessionManager } from "./session_manager";
|
|
5
|
-
import { SessionStorage } from "./session_storage";
|
|
6
|
-
import createClient from "openapi-fetch";
|
|
7
|
-
|
|
8
|
-
// An token obtained from an OIDC token is valid for 5 minutes
|
|
9
|
-
const OIDC_TOKEN_EXP_SECS = 300;
|
|
10
|
-
|
|
11
|
-
type OidcAuthResponse =
|
|
12
|
-
paths["/v0/org/{org_id}/oidc"]["post"]["responses"]["200"]["content"]["application/json"];
|
|
13
|
-
|
|
14
|
-
/** JSON representation of the OIDC token */
|
|
15
|
-
export interface OidcSessionData {
|
|
16
|
-
/** The environment that this token is for */
|
|
17
|
-
env: EnvInterface;
|
|
18
|
-
/** The organization ID */
|
|
19
|
-
org_id: string;
|
|
20
|
-
/** The OIDC token that this session was created from */
|
|
21
|
-
oidc_token: string;
|
|
22
|
-
/** The token to include in Authorization header */
|
|
23
|
-
token: string;
|
|
24
|
-
/** Token expiration timestamp */
|
|
25
|
-
token_exp: number;
|
|
26
|
-
/** The scopes of the token */
|
|
27
|
-
scopes: Array<string>;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** Type of storage required for OIDC sessions */
|
|
31
|
-
export type OidcSessionStorage = SessionStorage<OidcSessionData>;
|
|
32
|
-
|
|
33
|
-
/** Manager for OIDC sessions. */
|
|
34
|
-
export class OidcSessionManager extends OrgSessionManager<OidcSessionData> {
|
|
35
|
-
#client: Client;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* @return {string} The current auth token.
|
|
39
|
-
* @internal
|
|
40
|
-
*/
|
|
41
|
-
async token(): Promise<string> {
|
|
42
|
-
const session = await this.storage.retrieve();
|
|
43
|
-
return session.token;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Returns a client with the current session and refreshes the current
|
|
48
|
-
* session. May **UPDATE/MUTATE** self.
|
|
49
|
-
*/
|
|
50
|
-
async client(): Promise<Client> {
|
|
51
|
-
await this.refreshIfNeeded();
|
|
52
|
-
return this.#client;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** Revokes the session. */
|
|
56
|
-
async revoke(): Promise<void> {
|
|
57
|
-
this.unsupported("revoke");
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Refreshes the session and **UPDATES/MUTATES** self.
|
|
62
|
-
*/
|
|
63
|
-
async refresh(): Promise<void> {
|
|
64
|
-
const session = await this.storage.retrieve();
|
|
65
|
-
const [token, tokenExp] = await OidcSessionManager.#exchangeToken(
|
|
66
|
-
session.env,
|
|
67
|
-
session.oidc_token,
|
|
68
|
-
session.org_id,
|
|
69
|
-
session.scopes,
|
|
70
|
-
);
|
|
71
|
-
await this.storage.save(<OidcSessionData>{
|
|
72
|
-
...session,
|
|
73
|
-
token: token,
|
|
74
|
-
token_exp: tokenExp,
|
|
75
|
-
});
|
|
76
|
-
this.#client = this.createClient(token);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Returns whether it's time to refresh this token.
|
|
81
|
-
* @return {boolean} Whether it's time to refresh this token.
|
|
82
|
-
* @internal
|
|
83
|
-
*/
|
|
84
|
-
async isStale(): Promise<boolean> {
|
|
85
|
-
const session = await this.storage.retrieve();
|
|
86
|
-
return this.hasExpired(session.token_exp);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Refreshes the session if it is about to expire.
|
|
91
|
-
* @return {boolean} Whether the session token was refreshed.
|
|
92
|
-
* @internal
|
|
93
|
-
*/
|
|
94
|
-
async refreshIfNeeded(): Promise<boolean> {
|
|
95
|
-
if (await this.isStale()) {
|
|
96
|
-
await this.refresh();
|
|
97
|
-
return true;
|
|
98
|
-
}
|
|
99
|
-
return false;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Authenticate an OIDC user and create a new session for them.
|
|
104
|
-
* @param {EnvInterface} env The environment of the session
|
|
105
|
-
* @param {SessionStorage<SignerSessionObject>} storage The signer session storage
|
|
106
|
-
* @param {string} oidcToken The OIDC token
|
|
107
|
-
* @param {string} orgId The id of the organization that the user is in
|
|
108
|
-
* @param {List<string>} scopes The scopes of the resulting session
|
|
109
|
-
* @return {Promise<OidcSessionManager>} The signer session
|
|
110
|
-
*/
|
|
111
|
-
static async create(
|
|
112
|
-
env: EnvInterface,
|
|
113
|
-
storage: SessionStorage<OidcSessionData>,
|
|
114
|
-
oidcToken: string,
|
|
115
|
-
orgId: string,
|
|
116
|
-
scopes: Array<string>,
|
|
117
|
-
): Promise<OidcSessionManager> {
|
|
118
|
-
const [token, tokenExp] = await OidcSessionManager.#exchangeToken(
|
|
119
|
-
env,
|
|
120
|
-
oidcToken,
|
|
121
|
-
orgId,
|
|
122
|
-
scopes,
|
|
123
|
-
);
|
|
124
|
-
await storage.save(<OidcSessionData>{
|
|
125
|
-
env,
|
|
126
|
-
org_id: orgId,
|
|
127
|
-
oidc_token: oidcToken,
|
|
128
|
-
token,
|
|
129
|
-
token_exp: tokenExp,
|
|
130
|
-
scopes,
|
|
131
|
-
});
|
|
132
|
-
return new OidcSessionManager(env, orgId, token, storage);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Load from storage
|
|
137
|
-
* @param {OidcSessionStorage} storage The storage to load from
|
|
138
|
-
* @return {Promise<OidcSessionManager>} New OIDC session manager
|
|
139
|
-
*/
|
|
140
|
-
static async loadFromStorage(storage: OidcSessionStorage): Promise<OidcSessionManager> {
|
|
141
|
-
const info = await storage.retrieve();
|
|
142
|
-
return new OidcSessionManager(info.env, info.org_id, info.token, storage);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Constructor.
|
|
147
|
-
* @param {EnvInterface} env The environment of the session
|
|
148
|
-
* @param {string} orgId The id of the org associated with this session
|
|
149
|
-
* @param {string} token The authorization token to use
|
|
150
|
-
* @param {SessionStorage<U>} storage The storage back end to use for storing
|
|
151
|
-
* session information
|
|
152
|
-
*/
|
|
153
|
-
private constructor(
|
|
154
|
-
env: EnvInterface,
|
|
155
|
-
orgId: string,
|
|
156
|
-
token: string,
|
|
157
|
-
storage: SessionStorage<OidcSessionData>,
|
|
158
|
-
) {
|
|
159
|
-
super(env, orgId, storage);
|
|
160
|
-
this.#client = this.createClient(token);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Exchange an OIDC token for a CubeSigner session token.
|
|
165
|
-
* @param {EnvInterface} env The CubeSigner environment
|
|
166
|
-
* @param {string} oidcToken The OIDC token
|
|
167
|
-
* @param {string} orgId The id of the organization that the user is in
|
|
168
|
-
* @param {List<string>} scopes The scopes of the resulting session
|
|
169
|
-
* @return {Promise<[string, number]>} The session token and its expiration time
|
|
170
|
-
*/
|
|
171
|
-
static async #exchangeToken(
|
|
172
|
-
env: EnvInterface,
|
|
173
|
-
oidcToken: string,
|
|
174
|
-
orgId: string,
|
|
175
|
-
scopes: Array<string>,
|
|
176
|
-
): Promise<[string, number]> {
|
|
177
|
-
const client = createClient<paths>({
|
|
178
|
-
baseUrl: env.SignerApiRoot,
|
|
179
|
-
headers: {
|
|
180
|
-
Authorization: oidcToken,
|
|
181
|
-
},
|
|
182
|
-
});
|
|
183
|
-
const resp = await client.post("/v0/org/{org_id}/oidc", {
|
|
184
|
-
params: { path: { org_id: orgId } },
|
|
185
|
-
body: {
|
|
186
|
-
scopes,
|
|
187
|
-
},
|
|
188
|
-
parseAs: "json",
|
|
189
|
-
});
|
|
190
|
-
const data = assertOk(resp) as OidcAuthResponse;
|
|
191
|
-
return [data.token, new Date().getTime() / 1000 + OIDC_TOKEN_EXP_SECS];
|
|
192
|
-
}
|
|
193
|
-
}
|