@simpleview/sv-auth-client 4.4.3-0 → 4.4.4-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.3-0",
3
+ "version": "4.4.4-0",
4
4
  "description": "Client for communicating with sv-auth",
5
5
  "author": "Owen Allen <owenallenaz@gmail.com>",
6
6
  "dependencies": {
package/src/oauth2.js CHANGED
@@ -2,7 +2,13 @@ const { URLSearchParams } = require("url");
2
2
  const { request : axios } = require("axios");
3
3
  const { createHash, randomBytes } = require("crypto");
4
4
 
5
- async function oauth2AuthorizeCode({ authUrl, client_id, redirect_uri, code, code_verifier }) {
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
+
6
12
  const params = new URLSearchParams({
7
13
  grant_type: "authorization_code",
8
14
  client_id,
@@ -28,7 +34,9 @@ function oauth2CreateKeyHash(key) {
28
34
  return createHash("sha256").update(key).digest("base64");
29
35
  }
30
36
 
31
- function oauth2CreateLoginUrl({ authUrl, client_id, redirect_uri, redirectUrl, sv_auth_params, state, code_verifier }) {
37
+ function oauth2CreateLoginUrl({ authUrl = liveAuthUrl, client_id, redirect_uri, redirectUrl, sv_auth_params, state, code_verifier }) {
38
+ validateAuthUrl(authUrl);
39
+
32
40
  const redirectParams = new URLSearchParams({ redirectUrl });
33
41
 
34
42
  const params = new URLSearchParams({
@@ -48,7 +56,9 @@ function oauth2CreateRandomKey() {
48
56
  return randomBytes(32).toString("hex");
49
57
  }
50
58
 
51
- async function oauth2GetNewTokens({ authUrl, refresh_token }) {
59
+ async function oauth2GetNewTokens({ authUrl = liveAuthUrl, refresh_token }) {
60
+ validateAuthUrl(authUrl);
61
+
52
62
  const params = new URLSearchParams({
53
63
  grant_type: "refresh_token",
54
64
  refresh_token
@@ -67,7 +77,9 @@ async function oauth2GetNewTokens({ authUrl, refresh_token }) {
67
77
  }
68
78
  }
69
79
 
70
- async function oauth2GetToken({ authUrl, client_id, req, res }) {
80
+ async function oauth2GetToken({ authUrl = liveAuthUrl, client_id, req, res }) {
81
+ validateAuthUrl(authUrl);
82
+
71
83
  const { state, code, redirectUrl } = req.query;
72
84
 
73
85
  const { protocol, hostname, originalUrl } = req;
@@ -85,7 +97,9 @@ async function oauth2GetToken({ authUrl, client_id, req, res }) {
85
97
  return res.redirect(302, redirectUrl);
86
98
  }
87
99
 
88
- function oauth2Login({ authUrl, client_id, redirect_uri, sv_auth_params, req, res }) {
100
+ function oauth2Login({ authUrl = liveAuthUrl, client_id, redirect_uri, sv_auth_params, req, res }) {
101
+ validateAuthUrl(authUrl);
102
+
89
103
  req.session.state = oauth2CreateRandomKey();
90
104
 
91
105
  req.session.code_verifier = oauth2CreateRandomKey();
@@ -99,7 +113,9 @@ function oauth2Login({ authUrl, client_id, redirect_uri, sv_auth_params, req, re
99
113
  return res.redirect(302, loginUrl);
100
114
  }
101
115
 
102
- async function oauth2RefreshToken({ authUrl, req }) {
116
+ async function oauth2RefreshToken({ authUrl = liveAuthUrl, req }) {
117
+ validateAuthUrl(authUrl);
118
+
103
119
  const { token, refresh_token } = await oauth2GetNewTokens({
104
120
  authUrl,
105
121
  refresh_token: req.session.refresh_token
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) {
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;