@simpleview/sv-auth-client 6.24.0 → 6.26.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 +6 -0
- package/src/Oauth2Express.js +22 -3
- package/src/index.js +1 -1
- package/src/oauth2.js +0 -0
- package/src/prefixes/AuthPrefix.js +20 -0
- package/src/utils.js +86 -0
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -110,6 +110,8 @@ const { GraphServer } = require("@simpleview/sv-graphql-client");
|
|
|
110
110
|
const graphServer = new GraphServer({ graphUrl : GRAPH_URL, prefixes : [AuthPrefix] });
|
|
111
111
|
```
|
|
112
112
|
|
|
113
|
+
`login_service_account({ input, fields })` logs in with a service-account email and private key. `login_service_account_id_token({ input, fields })` logs in with a Google ID token in `input.id_token`. Both return the auth result for the fields you request.
|
|
114
|
+
|
|
113
115
|
### canIds(perm, node_type, bindings)
|
|
114
116
|
|
|
115
117
|
This function is identical to `User.canIds` but requires you to pass in the `bindings` manually, it should only be used in cases where you cannot use `DirectiveCheckPerm`. See the documentation for [`User.canIds`](#User.canIds) for how it functions.
|
|
@@ -434,12 +436,16 @@ This method must be registered on the callback route. It takes the same paramete
|
|
|
434
436
|
|
|
435
437
|
It obtains a `token`, `refresh_token` and user from auth, stores them in the session and then redirects the user back to the original path.
|
|
436
438
|
|
|
439
|
+
`redirectUrl` is allowlisted to same-origin URLs and safe relative `/` paths. Unsafe values redirect to `/`. A missing `redirectUrl` still results in an error.
|
|
440
|
+
|
|
437
441
|
#### OAuth2Express.logout
|
|
438
442
|
|
|
439
443
|
This method must be registered on the logout route. It takes the same parameters as an Express route `(req, res)`.
|
|
440
444
|
|
|
441
445
|
It deletes the session entry and redirects the user to the auth logout route, which logs the user out from auth and then redirects to the auth login.
|
|
442
446
|
|
|
447
|
+
The same `redirectUrl` allowlist applies. Unsafe values fall back to the request origin. Relative paths are made absolute before being passed to Auth as `redirect_uri`.
|
|
448
|
+
|
|
443
449
|
#### OAuth2Express.applyMiddleware
|
|
444
450
|
|
|
445
451
|
This is a helper method which registers the `callback` and `logout` functions on the `callbackPath` and `logoutPath` respectively.
|
package/src/Oauth2Express.js
CHANGED
|
@@ -9,6 +9,10 @@ const {
|
|
|
9
9
|
oauth2GetTokens,
|
|
10
10
|
oauth2GetTokensFromRefresh
|
|
11
11
|
} = require("./oauth2");
|
|
12
|
+
const {
|
|
13
|
+
getRequestOrigin,
|
|
14
|
+
safeRedirectUrl
|
|
15
|
+
} = require("./utils");
|
|
12
16
|
|
|
13
17
|
class Oauth2Express {
|
|
14
18
|
/**
|
|
@@ -190,7 +194,7 @@ async function callback(req, res) {
|
|
|
190
194
|
}
|
|
191
195
|
session.oauth2 = newSession;
|
|
192
196
|
|
|
193
|
-
return res.redirect(302,
|
|
197
|
+
return res.redirect(302, allowedRedirect(req, redirectUrl, "/"));
|
|
194
198
|
}
|
|
195
199
|
|
|
196
200
|
/**
|
|
@@ -201,8 +205,9 @@ async function callback(req, res) {
|
|
|
201
205
|
function logout(req, res) {
|
|
202
206
|
delete req[this.sessionKey].oauth2;
|
|
203
207
|
|
|
204
|
-
const
|
|
205
|
-
const
|
|
208
|
+
const origin = getRequestOrigin(req);
|
|
209
|
+
const sanitized = allowedRedirect(req, req.query.redirectUrl, `${origin}/`);
|
|
210
|
+
const redirectUrl = sanitized.startsWith("/") ? `${origin}${sanitized}` : sanitized;
|
|
206
211
|
|
|
207
212
|
const logoutUrl = oauth2CreateLogoutUrl({
|
|
208
213
|
authUrl: this.authUrl,
|
|
@@ -213,4 +218,18 @@ function logout(req, res) {
|
|
|
213
218
|
return res.redirect(302, logoutUrl);
|
|
214
219
|
}
|
|
215
220
|
|
|
221
|
+
/**
|
|
222
|
+
* @param {import("express").Request} req
|
|
223
|
+
* @param {unknown} raw
|
|
224
|
+
* @param {string} fallback
|
|
225
|
+
* @returns {string}
|
|
226
|
+
*/
|
|
227
|
+
function allowedRedirect(req, raw, fallback) {
|
|
228
|
+
if (typeof raw !== "string") {
|
|
229
|
+
return fallback;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return safeRedirectUrl(raw, req) ?? fallback;
|
|
233
|
+
}
|
|
234
|
+
|
|
216
235
|
module.exports = Oauth2Express;
|
package/src/index.js
CHANGED
package/src/oauth2.js
CHANGED
|
File without changes
|
|
@@ -375,6 +375,26 @@ class AuthPrefix {
|
|
|
375
375
|
return result.auth.login_service_account;
|
|
376
376
|
}
|
|
377
377
|
|
|
378
|
+
async login_service_account_id_token({ input, fields }) {
|
|
379
|
+
const result = await query({
|
|
380
|
+
query : `
|
|
381
|
+
query($input : auth_login_service_account_id_token_input!) {
|
|
382
|
+
auth {
|
|
383
|
+
login_service_account_id_token(input: $input) {
|
|
384
|
+
${fields}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
`,
|
|
389
|
+
variables : {
|
|
390
|
+
input
|
|
391
|
+
},
|
|
392
|
+
url : this._graphUrl
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
return result.auth.login_service_account_id_token;
|
|
396
|
+
}
|
|
397
|
+
|
|
378
398
|
async login_sso_token({ input, fields }) {
|
|
379
399
|
const result = await query({
|
|
380
400
|
query : `
|
package/src/utils.js
CHANGED
|
@@ -29,5 +29,91 @@ const base64urlEncode = function (inputString) {
|
|
|
29
29
|
.replace(/\//g, "_");
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
const SAFE_RELATIVE_REDIRECT = /^\/(?!\/)[A-Za-z0-9._~!$&'()*+,;=:@/?%#-]*$/;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param {import("express").Request} req
|
|
36
|
+
* @returns {string | undefined}
|
|
37
|
+
*/
|
|
38
|
+
function getRequestOrigin(req) {
|
|
39
|
+
const protocol = String(req.protocol || "").replace(/:$/, "");
|
|
40
|
+
const hostname = String(req.hostname || "").toLowerCase();
|
|
41
|
+
|
|
42
|
+
// Match Oauth2Express middleware: protocol + hostname (no Host port).
|
|
43
|
+
if ((protocol === "http" || protocol === "https") && hostname) {
|
|
44
|
+
return `${protocol}://${hostname}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return hostname ? `https://${hostname}` : undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @param {string} value
|
|
52
|
+
* @param {string} [base]
|
|
53
|
+
* @returns {URL | undefined}
|
|
54
|
+
*/
|
|
55
|
+
function parseUrl(value, base) {
|
|
56
|
+
try {
|
|
57
|
+
return base ? new URL(value, base) : new URL(value);
|
|
58
|
+
} catch (err) {
|
|
59
|
+
if (err instanceof TypeError) {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
throw err;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Allow same-origin absolute URLs and safe relative `/` paths.
|
|
69
|
+
* Returns a normalized URL, or undefined when the value is unsafe.
|
|
70
|
+
* @param {string} redirectUrl
|
|
71
|
+
* @param {import("express").Request} req
|
|
72
|
+
* @returns {string | undefined}
|
|
73
|
+
*/
|
|
74
|
+
function safeRedirectUrl(redirectUrl, req) {
|
|
75
|
+
if (typeof redirectUrl !== "string" || redirectUrl.length === 0 || redirectUrl.includes("\\")) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const requestHost = String(req.hostname || "").toLowerCase();
|
|
80
|
+
const origin = getRequestOrigin(req);
|
|
81
|
+
/** @type {string | undefined} */
|
|
82
|
+
let result;
|
|
83
|
+
|
|
84
|
+
if (SAFE_RELATIVE_REDIRECT.test(redirectUrl)) {
|
|
85
|
+
if (/%(2f|5c)/i.test(redirectUrl) || !origin || !requestHost) {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const resolved = parseUrl(redirectUrl, origin);
|
|
90
|
+
if (!resolved || resolved.hostname.toLowerCase() !== requestHost || resolved.pathname.startsWith("//")) {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
result = `${resolved.pathname}${resolved.search}${resolved.hash}`;
|
|
95
|
+
} else {
|
|
96
|
+
const parsed = parseUrl(redirectUrl);
|
|
97
|
+
if (!parsed || (parsed.protocol !== "http:" && parsed.protocol !== "https:")) {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (parsed.username || parsed.password || parsed.pathname.startsWith("//")) {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const host = parsed.hostname.toLowerCase();
|
|
106
|
+
if (!requestHost || host !== requestHost || !origin) {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
result = `${origin}${parsed.pathname}${parsed.search}${parsed.hash}`;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
|
|
32
116
|
module.exports.canIds = canIds;
|
|
33
117
|
module.exports.base64urlEncode = base64urlEncode;
|
|
118
|
+
module.exports.getRequestOrigin = getRequestOrigin;
|
|
119
|
+
module.exports.safeRedirectUrl = safeRedirectUrl;
|