@simpleview/sv-auth-client 6.23.2 → 6.24.0
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/package.json +1 -1
- package/readme.md +36 -0
- package/src/oauth2.js +88 -6
- package/src/prefixes/AuthPrefix.js +23 -0
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -370,6 +370,42 @@ Returns a token and refresh_token.
|
|
|
370
370
|
* `authUrl` - optional string - The URL of the auth system the website should utilise. Defaults to the live auth system. If you need to reference another system you can pass it here. Usually only used when testing in non-production environments.
|
|
371
371
|
* `refresh_token` - string - This should be a valid refresh token previously obtained from auth.
|
|
372
372
|
|
|
373
|
+
#### Embed handoff (token-exchange)
|
|
374
|
+
|
|
375
|
+
For iframe / CHIPS SSO, a parent BFF exchanges a fresh access token for a short-lived handoff JWT, delivers it to the embed (e.g. `postMessage`), and the embed BFF redeems it for user tokens. Both calls hit `POST /oauth2/token/` with `grant_type=urn:ietf:params:oauth:grant-type:token-exchange`.
|
|
376
|
+
|
|
377
|
+
Integration contract for iframe `postMessage`, origins, and cookies: [embed-iframe-sso.md](../../embed-iframe-sso.md).
|
|
378
|
+
|
|
379
|
+
* Parent should refresh its access token before hop 1 if it is near expiry.
|
|
380
|
+
* Parent `oauth_client` must list the target `audience` in `token_exchange_audiences`.
|
|
381
|
+
* Hop 1 returns a signed `embed_handoff` JWT (~60s). Mongo stores only the JWT `jti` for single-use replay protection.
|
|
382
|
+
* Hop 2 redeems that JWT once for `token` + `refresh_token`.
|
|
383
|
+
* Export: `EMBED_HANDOFF_TOKEN_TYPE` — `urn:simpleview:params:oauth:token-type:embed_handoff` (hop 2 `subject_token_type`).
|
|
384
|
+
|
|
385
|
+
#### oauth2ExchangeToken
|
|
386
|
+
|
|
387
|
+
Hop 1 — exchange a parent access JWT for an embed handoff JWT.
|
|
388
|
+
|
|
389
|
+
Returns `{ handoff_token, expires_in }`.
|
|
390
|
+
|
|
391
|
+
* args
|
|
392
|
+
* `authUrl` - optional string - The URL of the auth system. Defaults to live auth.
|
|
393
|
+
* `client_id` - string - Parent oauth client id (e.g. `crm3`).
|
|
394
|
+
* `subject_token` - string - Fresh parent access JWT.
|
|
395
|
+
* `audience` - string - Target audience (e.g. `apex`).
|
|
396
|
+
|
|
397
|
+
#### oauth2RedeemHandoffToken
|
|
398
|
+
|
|
399
|
+
Hop 2 — redeem an embed handoff JWT for user access + refresh tokens.
|
|
400
|
+
|
|
401
|
+
Returns `{ token, refresh_token }`.
|
|
402
|
+
|
|
403
|
+
* args
|
|
404
|
+
* `authUrl` - optional string - The URL of the auth system. Defaults to live auth.
|
|
405
|
+
* `client_id` - string - Same parent oauth client id used in hop 1.
|
|
406
|
+
* `subject_token` - string - The `handoff_token` from hop 1.
|
|
407
|
+
* `audience` - string - Same audience used in hop 1.
|
|
408
|
+
|
|
373
409
|
### OAuth2Express
|
|
374
410
|
|
|
375
411
|
The OAuth2Express class provides a simple interface to allow products using Express to easily add OAuth 2.0 authentication with auth. See the [sv-auth](https://github.com/simpleviewinc/sv-auth) readme for sample usage.
|
package/src/oauth2.js
CHANGED
|
@@ -5,6 +5,10 @@ const { base64urlEncode } = require("./utils");
|
|
|
5
5
|
|
|
6
6
|
const liveAuthUrl = "https://auth.simpleviewinc.com/";
|
|
7
7
|
|
|
8
|
+
const TOKEN_EXCHANGE_GRANT = "urn:ietf:params:oauth:grant-type:token-exchange";
|
|
9
|
+
const SUBJECT_TOKEN_TYPE_ACCESS_TOKEN = "urn:ietf:params:oauth:token-type:access_token";
|
|
10
|
+
const EMBED_HANDOFF_TOKEN_TYPE = "urn:simpleview:params:oauth:token-type:embed_handoff";
|
|
11
|
+
|
|
8
12
|
function oauth2CreateKeyHash(key) {
|
|
9
13
|
return base64urlEncode(createHash("sha256").update(key).digest("base64"));
|
|
10
14
|
}
|
|
@@ -75,13 +79,13 @@ function oauth2CreateRandomKey() {
|
|
|
75
79
|
}
|
|
76
80
|
|
|
77
81
|
/**
|
|
78
|
-
* Internal
|
|
82
|
+
* Internal POST to /oauth2/token/
|
|
79
83
|
* @param {object} args
|
|
80
84
|
* @param {string} args.authUrl - URL of Auth
|
|
81
85
|
* @param {object} args.params - Params to add to the request
|
|
82
|
-
* @returns {Promise<
|
|
86
|
+
* @returns {Promise<object>}
|
|
83
87
|
*/
|
|
84
|
-
async function
|
|
88
|
+
async function _oauth2PostToken({
|
|
85
89
|
authUrl = liveAuthUrl,
|
|
86
90
|
params
|
|
87
91
|
}) {
|
|
@@ -103,9 +107,25 @@ async function _oauth2GetTokens({
|
|
|
103
107
|
throw new Error(`Auth Error: ${e.message}`);
|
|
104
108
|
}
|
|
105
109
|
|
|
110
|
+
return rtn.data;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Internal function to retreive tokens from the server
|
|
115
|
+
* @param {object} args
|
|
116
|
+
* @param {string} args.authUrl - URL of Auth
|
|
117
|
+
* @param {object} args.params - Params to add to the request
|
|
118
|
+
* @returns {Promise<{ token: string, refresh_token: string }>}
|
|
119
|
+
*/
|
|
120
|
+
async function _oauth2GetTokens({
|
|
121
|
+
authUrl = liveAuthUrl,
|
|
122
|
+
params
|
|
123
|
+
}) {
|
|
124
|
+
const data = await _oauth2PostToken({ authUrl, params });
|
|
125
|
+
|
|
106
126
|
return {
|
|
107
|
-
token:
|
|
108
|
-
refresh_token:
|
|
127
|
+
token: data.access_token,
|
|
128
|
+
refresh_token: data.refresh_token
|
|
109
129
|
}
|
|
110
130
|
}
|
|
111
131
|
|
|
@@ -141,11 +161,73 @@ async function oauth2GetTokensFromRefresh({
|
|
|
141
161
|
});
|
|
142
162
|
}
|
|
143
163
|
|
|
164
|
+
/**
|
|
165
|
+
* Exchange a parent access token for an embed handoff JWT (token-exchange hop 1).
|
|
166
|
+
* @param {object} args
|
|
167
|
+
* @param {string} args.authUrl - URL of Auth
|
|
168
|
+
* @param {string} args.client_id - Parent oauth client_id
|
|
169
|
+
* @param {string} args.subject_token - Fresh parent access JWT
|
|
170
|
+
* @param {string} args.audience - Target audience string (e.g. apex)
|
|
171
|
+
* @returns {Promise<{ handoff_token: string, expires_in: number }>}
|
|
172
|
+
*/
|
|
173
|
+
async function oauth2ExchangeToken({
|
|
174
|
+
authUrl,
|
|
175
|
+
client_id,
|
|
176
|
+
subject_token,
|
|
177
|
+
audience
|
|
178
|
+
}) {
|
|
179
|
+
const data = await _oauth2PostToken({
|
|
180
|
+
authUrl,
|
|
181
|
+
params: {
|
|
182
|
+
grant_type: TOKEN_EXCHANGE_GRANT,
|
|
183
|
+
client_id,
|
|
184
|
+
subject_token,
|
|
185
|
+
subject_token_type: SUBJECT_TOKEN_TYPE_ACCESS_TOKEN,
|
|
186
|
+
audience
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
return {
|
|
191
|
+
handoff_token: data.access_token,
|
|
192
|
+
expires_in: data.expires_in
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Redeem an embed handoff JWT for user access + refresh tokens (token-exchange hop 2).
|
|
198
|
+
* @param {object} args
|
|
199
|
+
* @param {string} args.authUrl - URL of Auth
|
|
200
|
+
* @param {string} args.client_id - Parent oauth client_id
|
|
201
|
+
* @param {string} args.subject_token - embed_handoff JWT from hop 1
|
|
202
|
+
* @param {string} args.audience - Target audience string (must match hop 1)
|
|
203
|
+
* @returns {Promise<{ token: string, refresh_token: string }>}
|
|
204
|
+
*/
|
|
205
|
+
async function oauth2RedeemHandoffToken({
|
|
206
|
+
authUrl,
|
|
207
|
+
client_id,
|
|
208
|
+
subject_token,
|
|
209
|
+
audience
|
|
210
|
+
}) {
|
|
211
|
+
return _oauth2GetTokens({
|
|
212
|
+
authUrl,
|
|
213
|
+
params: {
|
|
214
|
+
grant_type: TOKEN_EXCHANGE_GRANT,
|
|
215
|
+
client_id,
|
|
216
|
+
subject_token,
|
|
217
|
+
subject_token_type: EMBED_HANDOFF_TOKEN_TYPE,
|
|
218
|
+
audience
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
144
223
|
module.exports = {
|
|
145
224
|
oauth2CreateKeyHash,
|
|
146
225
|
oauth2CreateLoginUrl,
|
|
147
226
|
oauth2CreateLogoutUrl,
|
|
148
227
|
oauth2CreateRandomKey,
|
|
149
228
|
oauth2GetTokens,
|
|
150
|
-
oauth2GetTokensFromRefresh
|
|
229
|
+
oauth2GetTokensFromRefresh,
|
|
230
|
+
oauth2ExchangeToken,
|
|
231
|
+
oauth2RedeemHandoffToken,
|
|
232
|
+
EMBED_HANDOFF_TOKEN_TYPE
|
|
151
233
|
}
|
|
@@ -744,6 +744,29 @@ class AuthPrefix {
|
|
|
744
744
|
return returnData;
|
|
745
745
|
}
|
|
746
746
|
|
|
747
|
+
async verify_password({ input, fields, context }) {
|
|
748
|
+
context = context || this._graphServer.context;
|
|
749
|
+
|
|
750
|
+
return query({
|
|
751
|
+
query: `
|
|
752
|
+
query($input: auth_verify_password_input!) {
|
|
753
|
+
auth {
|
|
754
|
+
verify_password(input: $input) {
|
|
755
|
+
${fields}
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
`,
|
|
760
|
+
variables: {
|
|
761
|
+
input
|
|
762
|
+
},
|
|
763
|
+
url: this._graphUrl,
|
|
764
|
+
token: context.token,
|
|
765
|
+
key: "auth.verify_password",
|
|
766
|
+
clean: true
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
|
|
747
770
|
async update_totp({ input, fields, context }) {
|
|
748
771
|
context = context || this._graphServer.context;
|
|
749
772
|
|