@simpleview/sv-auth-client 4.4.4-0 → 4.5.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/.vscode/settings.json +5 -0
- package/package.json +9 -3
- package/src/DirectiveCheckPerm.js +3 -3
- package/src/DirectiveGetUser.js +2 -2
- package/src/Oauth2Express.js +219 -0
- package/src/index.js +2 -0
- package/src/oauth2.js +111 -100
- package/src/types.ts +30 -1
- package/src/utils.js +1 -17
package/package.json
CHANGED
|
@@ -1,17 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simpleview/sv-auth-client",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.5.0",
|
|
4
4
|
"description": "Client for communicating with sv-auth",
|
|
5
5
|
"author": "Owen Allen <owenallenaz@gmail.com>",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@simpleview/sv-graphql-client": ">=1.3.0",
|
|
8
|
+
"@simpleview/express-async-handler": "1.0.2",
|
|
8
9
|
"graphql": "14.2.1",
|
|
9
|
-
"graphql-tools": "4.0.4"
|
|
10
|
+
"graphql-tools": "4.0.4",
|
|
11
|
+
"jsonwebtoken": "8.5.1"
|
|
10
12
|
},
|
|
11
13
|
"devDependencies": {
|
|
12
14
|
"@simpleview/mochalib": "1.0.1",
|
|
15
|
+
"@simpleview/assertlib": "1.2.0",
|
|
16
|
+
"apollo-server": "2.21.2",
|
|
17
|
+
"cookie": "0.5.0",
|
|
18
|
+
"cookie-session": "2.0.0",
|
|
13
19
|
"mocha": "5.2.0",
|
|
14
|
-
"
|
|
20
|
+
"supertest": "6.2.4"
|
|
15
21
|
},
|
|
16
22
|
"main": "src/index.js",
|
|
17
23
|
"browser": "src/browser.js",
|
|
@@ -18,7 +18,7 @@ function getDirectiveCheckPerm({ name, graphUrl }) {
|
|
|
18
18
|
if (directiveArgs.sv === true && context.user.sv !== true) {
|
|
19
19
|
throw new Error("User is not authorized to access this resource (ERR: 1005).");
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
// check if the resolver requires specific permissions
|
|
23
23
|
if (directiveArgs.perms !== undefined) {
|
|
24
24
|
const allowed = context.user.can(directiveArgs.perms);
|
|
@@ -58,7 +58,7 @@ function getDirectiveCheckPerm({ name, graphUrl }) {
|
|
|
58
58
|
|
|
59
59
|
context.user.permissionObjBindings = bindings.permissionObj;
|
|
60
60
|
}
|
|
61
|
-
|
|
61
|
+
|
|
62
62
|
return await resolve.call(this, parent, args, context, info);
|
|
63
63
|
}
|
|
64
64
|
}
|
|
@@ -83,4 +83,4 @@ function getDirectiveCheckPerm({ name, graphUrl }) {
|
|
|
83
83
|
};
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
module.exports = getDirectiveCheckPerm;
|
|
86
|
+
module.exports = getDirectiveCheckPerm;
|
package/src/DirectiveGetUser.js
CHANGED
|
@@ -6,7 +6,7 @@ const AuthClient = require("./AuthClient");
|
|
|
6
6
|
|
|
7
7
|
function getDirectiveGetUser({ name, graphUrl }){
|
|
8
8
|
const authClient = new AuthClient({ graphUrl });
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
class DirectiveGetUser extends SchemaDirectiveVisitor {
|
|
11
11
|
visitFieldDefinition(field) {
|
|
12
12
|
const { resolve = defaultFieldResolver } = field;
|
|
@@ -50,4 +50,4 @@ function getDirectiveGetUser({ name, graphUrl }){
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
module.exports = getDirectiveGetUser;
|
|
53
|
+
module.exports = getDirectiveGetUser;
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
//@ts-check
|
|
2
|
+
const { decode } = require("jsonwebtoken");
|
|
3
|
+
const { asyncWrap } = require("@simpleview/express-async-handler");
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
oauth2CreateLoginUrl,
|
|
7
|
+
oauth2CreateLogoutUrl,
|
|
8
|
+
oauth2CreateRandomKey,
|
|
9
|
+
oauth2GetTokens,
|
|
10
|
+
oauth2GetTokensFromRefresh
|
|
11
|
+
} = require("../src/oauth2");
|
|
12
|
+
|
|
13
|
+
class Oauth2Express {
|
|
14
|
+
/**
|
|
15
|
+
* @param {object} args
|
|
16
|
+
* @param {string} args.authUrl - URL to use for Auth
|
|
17
|
+
* @param {string} args.client_id - Your registered client_id declared at the same Auth.
|
|
18
|
+
* @param {string} [args.callbackPath] - The callback path on your express app that will handle the callback from Auth.
|
|
19
|
+
* @param {string} [args.logoutPath] - The logout path on your express app that will handle logging the user out and sending them back to the login screen.
|
|
20
|
+
* @param {import("./types").CreateParams} [args.createParams] - Transforms a request object into an object with "product", "account_id", and/or "account_name" for using within auth.
|
|
21
|
+
* @param {string} [args.sessionKey] - Key to store the declared session
|
|
22
|
+
*/
|
|
23
|
+
constructor({
|
|
24
|
+
authUrl = "https://auth.simpleviewinc.com/",
|
|
25
|
+
client_id,
|
|
26
|
+
callbackPath = "/oauth2/callback/",
|
|
27
|
+
logoutPath = "/logout/",
|
|
28
|
+
createParams,
|
|
29
|
+
sessionKey = "session"
|
|
30
|
+
}) {
|
|
31
|
+
const validUrls = [
|
|
32
|
+
"https://auth.simpleviewinc.com/",
|
|
33
|
+
"https://auth.dev.simpleviewinc.com/",
|
|
34
|
+
"https://auth.qa.simpleviewinc.com/",
|
|
35
|
+
"https://auth.kube.simpleview.io/"
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
if (validUrls.indexOf(authUrl) === -1 && !/http[^.]+.ui-service.default.svc.cluster.local/.test(authUrl)) {
|
|
39
|
+
throw new Error("authUrl must be one of " + validUrls.join(", "));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!client_id) {
|
|
43
|
+
throw new Error("client_id is required.")
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
this.authUrl = authUrl;
|
|
47
|
+
this.client_id = client_id;
|
|
48
|
+
this.callbackPath = callbackPath;
|
|
49
|
+
this.logoutPath = logoutPath;
|
|
50
|
+
this.createParams = createParams;
|
|
51
|
+
this.sessionKey = sessionKey;
|
|
52
|
+
|
|
53
|
+
this.middleware = asyncWrap(middleware.bind(this));
|
|
54
|
+
this.callback = asyncWrap(callback.bind(this));
|
|
55
|
+
this.logout = asyncWrap(logout.bind(this));
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Apply callback and logout middleware to the application
|
|
59
|
+
* @param {import("express").Application} app
|
|
60
|
+
*/
|
|
61
|
+
applyMiddleware(app) {
|
|
62
|
+
app.get(this.callbackPath, this.callback);
|
|
63
|
+
app.get(this.logoutPath, this.logout);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @this {Oauth2Express}
|
|
69
|
+
* @param {import("express").Request} req
|
|
70
|
+
* @param {import("express").Response} res
|
|
71
|
+
* @param {import("express").NextFunction} next
|
|
72
|
+
*/
|
|
73
|
+
async function middleware(req, res, next) {
|
|
74
|
+
const session = req[this.sessionKey];
|
|
75
|
+
/**
|
|
76
|
+
* @type {import("./types").UnknownSession}
|
|
77
|
+
*/
|
|
78
|
+
const oauth2 = session?.oauth2;
|
|
79
|
+
|
|
80
|
+
// If we don't have a token or a refresh_token than we need to redirect to Auth to log the user in
|
|
81
|
+
if (!oauth2) {
|
|
82
|
+
if (req.method !== "GET") {
|
|
83
|
+
throw new Error("User is not authorized to access this resource.");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** @type {import("./types").InitialSession} */
|
|
87
|
+
const newAuth = {
|
|
88
|
+
type: "initial",
|
|
89
|
+
state: oauth2CreateRandomKey(),
|
|
90
|
+
code_verifier: oauth2CreateRandomKey()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
req[this.sessionKey].oauth2 = newAuth;
|
|
94
|
+
|
|
95
|
+
const { protocol, hostname, originalUrl } = req;
|
|
96
|
+
|
|
97
|
+
const redirect_uri = new URL(`${protocol}://${hostname}${this.callbackPath}`);
|
|
98
|
+
redirect_uri.searchParams.set("redirectUrl", `${protocol}://${hostname}${originalUrl}`);
|
|
99
|
+
|
|
100
|
+
const loginUrl = oauth2CreateLoginUrl({
|
|
101
|
+
authUrl: this.authUrl,
|
|
102
|
+
client_id: this.client_id,
|
|
103
|
+
redirect_uri: redirect_uri.toString(),
|
|
104
|
+
sv_auth_params: this.createParams && this.createParams(req),
|
|
105
|
+
state: newAuth.state,
|
|
106
|
+
code_verifier: newAuth.code_verifier
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
return res.redirect(302, loginUrl);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (oauth2.type === "initial") {
|
|
113
|
+
throw new Error("Invalid session state.");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (oauth2.type === "logged_in") {
|
|
117
|
+
// If the token is older than 1 day we refresh
|
|
118
|
+
if (Date.now() - oauth2.token_created > 1000 * 60 * 60 * 24) {
|
|
119
|
+
const result = await oauth2GetTokensFromRefresh({
|
|
120
|
+
authUrl: this.authUrl,
|
|
121
|
+
refresh_token: oauth2.refresh_token
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
oauth2.token = result.token;
|
|
125
|
+
oauth2.refresh_token = result.refresh_token;
|
|
126
|
+
oauth2.token_created = Date.now();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return next();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @this {Oauth2Express}
|
|
135
|
+
* @param {import("express").Request} req
|
|
136
|
+
* @param {import("express").Response} res
|
|
137
|
+
*/
|
|
138
|
+
async function callback(req, res) {
|
|
139
|
+
const {
|
|
140
|
+
state,
|
|
141
|
+
code,
|
|
142
|
+
redirectUrl,
|
|
143
|
+
error,
|
|
144
|
+
error_description
|
|
145
|
+
} = req.query;
|
|
146
|
+
|
|
147
|
+
if (error) {
|
|
148
|
+
throw new Error(`${error}${error_description ? ": " + error_description : ""}`);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const session = req[this.sessionKey];
|
|
152
|
+
/**
|
|
153
|
+
* @type {import("./types").InitialSession}
|
|
154
|
+
*/
|
|
155
|
+
const oauth2 = session?.oauth2;
|
|
156
|
+
|
|
157
|
+
if (!oauth2 || !oauth2.code_verifier || !oauth2.state) {
|
|
158
|
+
throw new Error("Session does not exist.");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (session.oauth2.state !== state) {
|
|
162
|
+
throw new Error("State returned does not match stored state.");
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (!code) {
|
|
166
|
+
throw new Error("No code is present on the query string.");
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (!redirectUrl) {
|
|
170
|
+
throw new Error("No redirectUrl is present on the query string.");
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const { protocol, hostname, originalUrl } = req;
|
|
174
|
+
|
|
175
|
+
const redirect_uri = `${protocol}://${hostname}${originalUrl}`;
|
|
176
|
+
|
|
177
|
+
const { token, refresh_token } = await oauth2GetTokens({
|
|
178
|
+
authUrl: this.authUrl,
|
|
179
|
+
client_id: this.client_id,
|
|
180
|
+
redirect_uri,
|
|
181
|
+
code,
|
|
182
|
+
code_verifier: session.oauth2.code_verifier
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
/** @type {import("./types").LoggedInSession} */
|
|
186
|
+
const newSession = {
|
|
187
|
+
type: "logged_in",
|
|
188
|
+
created: Date.now(),
|
|
189
|
+
token_created: Date.now(),
|
|
190
|
+
token,
|
|
191
|
+
refresh_token,
|
|
192
|
+
email: /** @type {object} */ (decode(token)).email
|
|
193
|
+
}
|
|
194
|
+
session.oauth2 = newSession;
|
|
195
|
+
|
|
196
|
+
return res.redirect(302, /** @type {string} */ (redirectUrl));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* @this {Oauth2Express}
|
|
201
|
+
* @param {import("express").Request} req
|
|
202
|
+
* @param {import("express").Response} res
|
|
203
|
+
*/
|
|
204
|
+
function logout(req, res) {
|
|
205
|
+
delete req[this.sessionKey].oauth2;
|
|
206
|
+
|
|
207
|
+
const { protocol, hostname, query } = req;
|
|
208
|
+
const redirectUrl = /** @type {string} */ (query.redirectUrl) ?? `${protocol}://${hostname}/`
|
|
209
|
+
|
|
210
|
+
const logoutUrl = oauth2CreateLogoutUrl({
|
|
211
|
+
authUrl: this.authUrl,
|
|
212
|
+
redirect_uri: redirectUrl,
|
|
213
|
+
sv_auth_params: this.createParams && this.createParams(req),
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
return res.redirect(302, logoutUrl);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
module.exports = Oauth2Express;
|
package/src/index.js
CHANGED
|
@@ -5,6 +5,7 @@ const {
|
|
|
5
5
|
const getDirectiveCheckPerm = require("./DirectiveCheckPerm");
|
|
6
6
|
const getDirectiveGetUser = require("./DirectiveGetUser");
|
|
7
7
|
const oauth2 = require("./oauth2");
|
|
8
|
+
const Oauth2Express = require("./Oauth2Express");
|
|
8
9
|
|
|
9
10
|
module.exports = {
|
|
10
11
|
...browser,
|
|
@@ -12,4 +13,5 @@ module.exports = {
|
|
|
12
13
|
getDirectiveCheckPerm,
|
|
13
14
|
getDirectiveGetUser,
|
|
14
15
|
...oauth2,
|
|
16
|
+
Oauth2Express
|
|
15
17
|
}
|
package/src/oauth2.js
CHANGED
|
@@ -1,75 +1,106 @@
|
|
|
1
|
-
|
|
1
|
+
//@ts-check
|
|
2
2
|
const { request : axios } = require("axios");
|
|
3
3
|
const { createHash, randomBytes } = require("crypto");
|
|
4
4
|
|
|
5
|
-
const { validateAuthUrl } = require("../src/utils");
|
|
6
|
-
|
|
7
5
|
const liveAuthUrl = "https://auth.simpleviewinc.com/";
|
|
8
6
|
|
|
9
|
-
async function oauth2AuthorizeCode({ authUrl = liveAuthUrl, client_id, redirect_uri, code, code_verifier }) {
|
|
10
|
-
validateAuthUrl(authUrl);
|
|
11
|
-
|
|
12
|
-
const params = new URLSearchParams({
|
|
13
|
-
grant_type: "authorization_code",
|
|
14
|
-
client_id,
|
|
15
|
-
redirect_uri,
|
|
16
|
-
code,
|
|
17
|
-
code_verifier
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
const rtn = await axios({
|
|
21
|
-
url: `${authUrl}oauth2/token/`,
|
|
22
|
-
method: "POST",
|
|
23
|
-
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
24
|
-
data: params.toString()
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
return {
|
|
28
|
-
token: rtn.data.access_token,
|
|
29
|
-
refresh_token: rtn.data.refresh_token
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
7
|
function oauth2CreateKeyHash(key) {
|
|
34
8
|
return createHash("sha256").update(key).digest("base64");
|
|
35
9
|
}
|
|
36
10
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Generate a login URL
|
|
13
|
+
* @param {object} args
|
|
14
|
+
* @param {string} args.authUrl - URL of Auth
|
|
15
|
+
* @param {string} args.client_id - Registered client_id
|
|
16
|
+
* @param {string} args.redirect_uri - The URL auth will redirect to to handle oauth2 verification
|
|
17
|
+
* @param {object} args.sv_auth_params - Params to append to the login request such as product, account_name, account_id
|
|
18
|
+
* @param {string} args.state - Unique state key to identify this transaction
|
|
19
|
+
* @param {string} args.code_verifier - Code verifier used to generate code_challenge
|
|
20
|
+
* @returns {string}
|
|
21
|
+
*/
|
|
22
|
+
function oauth2CreateLoginUrl({
|
|
23
|
+
authUrl = liveAuthUrl,
|
|
24
|
+
client_id,
|
|
25
|
+
redirect_uri,
|
|
26
|
+
sv_auth_params,
|
|
27
|
+
state,
|
|
28
|
+
code_verifier
|
|
29
|
+
}) {
|
|
42
30
|
const params = new URLSearchParams({
|
|
43
31
|
response_type: "code",
|
|
44
32
|
code_challenge_method: "S256",
|
|
45
33
|
client_id,
|
|
46
|
-
redirect_uri
|
|
34
|
+
redirect_uri,
|
|
47
35
|
state,
|
|
48
|
-
code_challenge: oauth2CreateKeyHash(code_verifier)
|
|
49
|
-
sv_auth_params: JSON.stringify(sv_auth_params)
|
|
36
|
+
code_challenge: oauth2CreateKeyHash(code_verifier)
|
|
50
37
|
});
|
|
51
38
|
|
|
39
|
+
if (sv_auth_params) {
|
|
40
|
+
params.set("sv_auth_params", JSON.stringify(sv_auth_params));
|
|
41
|
+
}
|
|
42
|
+
|
|
52
43
|
return `${authUrl}oauth2/login/?${params.toString()}`;
|
|
53
44
|
}
|
|
54
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Generates a logout URL
|
|
48
|
+
* @param {object} args
|
|
49
|
+
* @param {string} args.authUrl - URL of Auth
|
|
50
|
+
* @param {string} args.redirect_uri - The URL auth will redirect after login
|
|
51
|
+
* @param {object} args.sv_auth_params - Params to append to the login request such as product, account_name, account_id
|
|
52
|
+
* @returns {string}
|
|
53
|
+
*/
|
|
54
|
+
function oauth2CreateLogoutUrl({
|
|
55
|
+
authUrl = liveAuthUrl,
|
|
56
|
+
redirect_uri,
|
|
57
|
+
sv_auth_params = {}
|
|
58
|
+
}) {
|
|
59
|
+
const url = new URL(`${authUrl}logout/`);
|
|
60
|
+
url.searchParams.set("redirectUrl", redirect_uri);
|
|
61
|
+
|
|
62
|
+
for (const [key, value] of Object.entries(sv_auth_params)) {
|
|
63
|
+
url.searchParams.set(key, value);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return url.toString();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Generate an oauth2 random identifier
|
|
71
|
+
*/
|
|
55
72
|
function oauth2CreateRandomKey() {
|
|
56
73
|
return randomBytes(32).toString("hex");
|
|
57
74
|
}
|
|
58
75
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
76
|
+
/**
|
|
77
|
+
* Internal function to retreive tokens from the server
|
|
78
|
+
* @param {object} args
|
|
79
|
+
* @param {string} args.authUrl - URL of Auth
|
|
80
|
+
* @param {object} args.params - Params to add to the request
|
|
81
|
+
* @returns {Promise<{ token: string, refresh_token: string }>}
|
|
82
|
+
*/
|
|
83
|
+
async function _oauth2GetTokens({
|
|
84
|
+
authUrl = liveAuthUrl,
|
|
85
|
+
params
|
|
86
|
+
}) {
|
|
87
|
+
let rtn;
|
|
88
|
+
try {
|
|
89
|
+
rtn = await axios({
|
|
90
|
+
url: `${authUrl}oauth2/token/`,
|
|
91
|
+
method: "POST",
|
|
92
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
93
|
+
data: new URLSearchParams(params).toString()
|
|
94
|
+
});
|
|
95
|
+
} catch (e) {
|
|
96
|
+
if (e.response?.data?.error) {
|
|
97
|
+
const errData = e.response.data;
|
|
98
|
+
const terms = [errData.error, errData.error_description];
|
|
99
|
+
throw new Error(`Auth Error: ${terms.filter(val => val).join(", ")}`);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
throw new Error(`Auth Error: ${e.message}`);
|
|
103
|
+
}
|
|
73
104
|
|
|
74
105
|
return {
|
|
75
106
|
token: rtn.data.access_token,
|
|
@@ -77,63 +108,43 @@ async function oauth2GetNewTokens({ authUrl = liveAuthUrl, refresh_token }) {
|
|
|
77
108
|
}
|
|
78
109
|
}
|
|
79
110
|
|
|
80
|
-
async function
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
return res.redirect(302, redirectUrl);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function oauth2Login({ authUrl = liveAuthUrl, client_id, redirect_uri, sv_auth_params, req, res }) {
|
|
101
|
-
validateAuthUrl(authUrl);
|
|
102
|
-
|
|
103
|
-
req.session.state = oauth2CreateRandomKey();
|
|
104
|
-
|
|
105
|
-
req.session.code_verifier = oauth2CreateRandomKey();
|
|
106
|
-
|
|
107
|
-
const { protocol, hostname, originalUrl, session } = req;
|
|
108
|
-
|
|
109
|
-
const redirectUrl = `${protocol}://${hostname}${originalUrl}`;
|
|
110
|
-
|
|
111
|
-
const loginUrl = oauth2CreateLoginUrl({ authUrl, client_id, redirect_uri, redirectUrl, sv_auth_params, state: session.state, code_verifier: session.code_verifier });
|
|
112
|
-
|
|
113
|
-
return res.redirect(302, loginUrl);
|
|
111
|
+
async function oauth2GetTokens({
|
|
112
|
+
authUrl,
|
|
113
|
+
client_id,
|
|
114
|
+
redirect_uri,
|
|
115
|
+
code,
|
|
116
|
+
code_verifier
|
|
117
|
+
}) {
|
|
118
|
+
return _oauth2GetTokens({
|
|
119
|
+
authUrl,
|
|
120
|
+
params: {
|
|
121
|
+
client_id,
|
|
122
|
+
grant_type: "authorization_code",
|
|
123
|
+
redirect_uri,
|
|
124
|
+
code,
|
|
125
|
+
code_verifier
|
|
126
|
+
}
|
|
127
|
+
})
|
|
114
128
|
}
|
|
115
129
|
|
|
116
|
-
async function
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
130
|
+
async function oauth2GetTokensFromRefresh({
|
|
131
|
+
authUrl,
|
|
132
|
+
refresh_token
|
|
133
|
+
}) {
|
|
134
|
+
return _oauth2GetTokens({
|
|
120
135
|
authUrl,
|
|
121
|
-
|
|
136
|
+
params: {
|
|
137
|
+
grant_type: "refresh_token",
|
|
138
|
+
refresh_token
|
|
139
|
+
}
|
|
122
140
|
});
|
|
123
|
-
|
|
124
|
-
req.session.token = token;
|
|
125
|
-
req.session.refresh_token = refresh_token;
|
|
126
|
-
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
141
|
+
}
|
|
129
142
|
|
|
130
143
|
module.exports = {
|
|
131
|
-
oauth2AuthorizeCode,
|
|
132
144
|
oauth2CreateKeyHash,
|
|
133
145
|
oauth2CreateLoginUrl,
|
|
146
|
+
oauth2CreateLogoutUrl,
|
|
134
147
|
oauth2CreateRandomKey,
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
oauth2RefreshToken
|
|
139
|
-
}
|
|
148
|
+
oauth2GetTokens,
|
|
149
|
+
oauth2GetTokensFromRefresh
|
|
150
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,7 +1,36 @@
|
|
|
1
|
+
import { Request } from "express";
|
|
2
|
+
|
|
1
3
|
export interface ObjectBindingsPermissionObj {
|
|
2
4
|
[key: string]: true | ObjectBindingsPermissionObjPerm
|
|
3
5
|
}
|
|
4
6
|
|
|
5
7
|
export interface ObjectBindingsPermissionObjPerm {
|
|
6
8
|
[key: string]: string[]
|
|
7
|
-
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface CreateParams {
|
|
12
|
+
(req: Request): {
|
|
13
|
+
product?: string
|
|
14
|
+
account_name?: string
|
|
15
|
+
acct_id?: string
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type UnknownSession = InitialSession | LoggedInSession | undefined
|
|
20
|
+
|
|
21
|
+
export interface InitialSession {
|
|
22
|
+
type: "initial"
|
|
23
|
+
code_verifier: string
|
|
24
|
+
state: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface LoggedInSession {
|
|
28
|
+
type: "logged_in"
|
|
29
|
+
/** The time the session was created */
|
|
30
|
+
created: number
|
|
31
|
+
/** The time the token was created */
|
|
32
|
+
token_created: number
|
|
33
|
+
token: string
|
|
34
|
+
refresh_token: string
|
|
35
|
+
email: string
|
|
36
|
+
}
|
package/src/utils.js
CHANGED
|
@@ -22,20 +22,4 @@ function canIds(perm, node_type, bindings = {}) {
|
|
|
22
22
|
return nodeData;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
const validUrls = [
|
|
27
|
-
"https://auth.simpleviewinc.com/",
|
|
28
|
-
"https://auth.dev.simpleviewinc.com/",
|
|
29
|
-
"https://auth.qa.simpleviewinc.com/",
|
|
30
|
-
"https://auth.kube.simpleview.io/"
|
|
31
|
-
]
|
|
32
|
-
|
|
33
|
-
if (validUrls.indexOf(authUrl) === -1) {
|
|
34
|
-
throw new Error("authUrl must be one of " + validUrls.join(", "));
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
module.exports.canIds = canIds;
|
|
41
|
-
module.exports.validateAuthUrl = validateAuthUrl;
|
|
25
|
+
module.exports.canIds = canIds;
|