@simpleview/sv-auth-client 4.4.2 → 4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simpleview/sv-auth-client",
3
- "version": "4.4.2",
3
+ "version": "4.4.5-0",
4
4
  "description": "Client for communicating with sv-auth",
5
5
  "author": "Owen Allen <owenallenaz@gmail.com>",
6
6
  "dependencies": {
package/src/index.js CHANGED
@@ -4,10 +4,12 @@ const {
4
4
  } = require("./utils");
5
5
  const getDirectiveCheckPerm = require("./DirectiveCheckPerm");
6
6
  const getDirectiveGetUser = require("./DirectiveGetUser");
7
+ const oauth2 = require("./oauth2");
7
8
 
8
9
  module.exports = {
9
10
  ...browser,
10
11
  canIds,
11
12
  getDirectiveCheckPerm,
12
13
  getDirectiveGetUser,
14
+ ...oauth2,
13
15
  }
package/src/oauth2.js ADDED
@@ -0,0 +1,139 @@
1
+ const { URLSearchParams } = require("url");
2
+ const { request : axios } = require("axios");
3
+ const { createHash, randomBytes } = require("crypto");
4
+
5
+ const { validateAuthUrl } = require("../src/utils");
6
+
7
+ const liveAuthUrl = "https://auth.simpleviewinc.com/";
8
+
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
+ function oauth2CreateKeyHash(key) {
34
+ return createHash("sha256").update(key).digest("base64");
35
+ }
36
+
37
+ function oauth2CreateLoginUrl({ authUrl = liveAuthUrl, client_id, redirect_uri, redirectUrl, sv_auth_params, state, code_verifier }) {
38
+ validateAuthUrl(authUrl);
39
+
40
+ const redirectParams = new URLSearchParams({ redirectUrl });
41
+
42
+ const params = new URLSearchParams({
43
+ response_type: "code",
44
+ code_challenge_method: "S256",
45
+ client_id,
46
+ redirect_uri: `${redirect_uri}?${redirectParams.toString()}`,
47
+ state,
48
+ code_challenge: oauth2CreateKeyHash(code_verifier),
49
+ sv_auth_params: JSON.stringify(sv_auth_params)
50
+ });
51
+
52
+ return `${authUrl}oauth2/login/?${params.toString()}`;
53
+ }
54
+
55
+ function oauth2CreateRandomKey() {
56
+ return randomBytes(32).toString("hex");
57
+ }
58
+
59
+ async function oauth2GetNewTokens({ authUrl = liveAuthUrl, refresh_token }) {
60
+ validateAuthUrl(authUrl);
61
+
62
+ const params = new URLSearchParams({
63
+ grant_type: "refresh_token",
64
+ refresh_token
65
+ });
66
+
67
+ const rtn = await axios({
68
+ url: `${authUrl}oauth2/token/`,
69
+ method: "POST",
70
+ headers: { 'content-type': 'application/x-www-form-urlencoded' },
71
+ data: params.toString()
72
+ });
73
+
74
+ return {
75
+ token: rtn.data.access_token,
76
+ refresh_token: rtn.data.refresh_token
77
+ }
78
+ }
79
+
80
+ async function oauth2GetToken({ authUrl = liveAuthUrl, client_id, req, res }) {
81
+ validateAuthUrl(authUrl);
82
+
83
+ const { state, code, redirectUrl } = req.query;
84
+
85
+ const { protocol, hostname, originalUrl } = req;
86
+
87
+ const redirect_uri = `${protocol}://${hostname}${originalUrl}`;
88
+ if (req.session.state !== state) {
89
+ throw new Error("State returned does not match stored state.");
90
+ }
91
+
92
+ const { token, refresh_token } = await oauth2AuthorizeCode({ authUrl, client_id, redirect_uri, code, code_verifier: req.session.code_verifier });
93
+
94
+ req.session.token = token;
95
+ req.session.refresh_token = refresh_token;
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);
114
+ }
115
+
116
+ async function oauth2RefreshToken({ authUrl = liveAuthUrl, req }) {
117
+ validateAuthUrl(authUrl);
118
+
119
+ const { token, refresh_token } = await oauth2GetNewTokens({
120
+ authUrl,
121
+ refresh_token: req.session.refresh_token
122
+ });
123
+
124
+ req.session.token = token;
125
+ req.session.refresh_token = refresh_token;
126
+
127
+ return;
128
+ }
129
+
130
+ module.exports = {
131
+ oauth2AuthorizeCode,
132
+ oauth2CreateKeyHash,
133
+ oauth2CreateLoginUrl,
134
+ oauth2CreateRandomKey,
135
+ oauth2GetToken,
136
+ oauth2GetNewTokens,
137
+ oauth2Login,
138
+ oauth2RefreshToken
139
+ }
package/src/utils.js CHANGED
@@ -22,4 +22,20 @@ function canIds(perm, node_type, bindings = {}) {
22
22
  return nodeData;
23
23
  }
24
24
 
25
- module.exports.canIds = canIds;
25
+ function validateAuthUrl(authUrl) {
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 && !/http[^.]+.ui-service.default.svc.cluster.local/.test(authUrl)) {
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;