@simpleview/sv-auth-client 4.4.1-0 → 4.4.3-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 +2 -2
- package/src/index.js +2 -0
- package/src/oauth2.js +123 -0
- package/src/prefixes/AuthPrefix.js +5 -11
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simpleview/sv-auth-client",
|
|
3
|
-
"version": "4.4.
|
|
3
|
+
"version": "4.4.3-0",
|
|
4
4
|
"description": "Client for communicating with sv-auth",
|
|
5
5
|
"author": "Owen Allen <owenallenaz@gmail.com>",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@simpleview/sv-graphql-client": "
|
|
7
|
+
"@simpleview/sv-graphql-client": ">=1.3.0",
|
|
8
8
|
"graphql": "14.2.1",
|
|
9
9
|
"graphql-tools": "4.0.4"
|
|
10
10
|
},
|
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,123 @@
|
|
|
1
|
+
const { URLSearchParams } = require("url");
|
|
2
|
+
const { request : axios } = require("axios");
|
|
3
|
+
const { createHash, randomBytes } = require("crypto");
|
|
4
|
+
|
|
5
|
+
async function oauth2AuthorizeCode({ authUrl, client_id, redirect_uri, code, code_verifier }) {
|
|
6
|
+
const params = new URLSearchParams({
|
|
7
|
+
grant_type: "authorization_code",
|
|
8
|
+
client_id,
|
|
9
|
+
redirect_uri,
|
|
10
|
+
code,
|
|
11
|
+
code_verifier
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const rtn = await axios({
|
|
15
|
+
url: `${authUrl}oauth2/token/`,
|
|
16
|
+
method: "POST",
|
|
17
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
18
|
+
data: params.toString()
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
token: rtn.data.access_token,
|
|
23
|
+
refresh_token: rtn.data.refresh_token
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function oauth2CreateKeyHash(key) {
|
|
28
|
+
return createHash("sha256").update(key).digest("base64");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function oauth2CreateLoginUrl({ authUrl, client_id, redirect_uri, redirectUrl, sv_auth_params, state, code_verifier }) {
|
|
32
|
+
const redirectParams = new URLSearchParams({ redirectUrl });
|
|
33
|
+
|
|
34
|
+
const params = new URLSearchParams({
|
|
35
|
+
response_type: "code",
|
|
36
|
+
code_challenge_method: "S256",
|
|
37
|
+
client_id,
|
|
38
|
+
redirect_uri: `${redirect_uri}?${redirectParams.toString()}`,
|
|
39
|
+
state,
|
|
40
|
+
code_challenge: oauth2CreateKeyHash(code_verifier),
|
|
41
|
+
sv_auth_params: JSON.stringify(sv_auth_params)
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return `${authUrl}oauth2/login/?${params.toString()}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function oauth2CreateRandomKey() {
|
|
48
|
+
return randomBytes(32).toString("hex");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function oauth2GetNewTokens({ authUrl, refresh_token }) {
|
|
52
|
+
const params = new URLSearchParams({
|
|
53
|
+
grant_type: "refresh_token",
|
|
54
|
+
refresh_token
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const rtn = await axios({
|
|
58
|
+
url: `${authUrl}oauth2/token/`,
|
|
59
|
+
method: "POST",
|
|
60
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
61
|
+
data: params.toString()
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
token: rtn.data.access_token,
|
|
66
|
+
refresh_token: rtn.data.refresh_token
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function oauth2GetToken({ authUrl, client_id, req, res }) {
|
|
71
|
+
const { state, code, redirectUrl } = req.query;
|
|
72
|
+
|
|
73
|
+
const { protocol, hostname, originalUrl } = req;
|
|
74
|
+
|
|
75
|
+
const redirect_uri = `${protocol}://${hostname}${originalUrl}`;
|
|
76
|
+
if (req.session.state !== state) {
|
|
77
|
+
throw new Error("State returned does not match stored state.");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const { token, refresh_token } = await oauth2AuthorizeCode({ authUrl, client_id, redirect_uri, code, code_verifier: req.session.code_verifier });
|
|
81
|
+
|
|
82
|
+
req.session.token = token;
|
|
83
|
+
req.session.refresh_token = refresh_token;
|
|
84
|
+
|
|
85
|
+
return res.redirect(302, redirectUrl);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function oauth2Login({ authUrl, client_id, redirect_uri, sv_auth_params, req, res }) {
|
|
89
|
+
req.session.state = oauth2CreateRandomKey();
|
|
90
|
+
|
|
91
|
+
req.session.code_verifier = oauth2CreateRandomKey();
|
|
92
|
+
|
|
93
|
+
const { protocol, hostname, originalUrl, session } = req;
|
|
94
|
+
|
|
95
|
+
const redirectUrl = `${protocol}://${hostname}${originalUrl}`;
|
|
96
|
+
|
|
97
|
+
const loginUrl = oauth2CreateLoginUrl({ authUrl, client_id, redirect_uri, redirectUrl, sv_auth_params, state: session.state, code_verifier: session.code_verifier });
|
|
98
|
+
|
|
99
|
+
return res.redirect(302, loginUrl);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function oauth2RefreshToken({ authUrl, req }) {
|
|
103
|
+
const { token, refresh_token } = await oauth2GetNewTokens({
|
|
104
|
+
authUrl,
|
|
105
|
+
refresh_token: req.session.refresh_token
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
req.session.token = token;
|
|
109
|
+
req.session.refresh_token = refresh_token;
|
|
110
|
+
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
module.exports = {
|
|
115
|
+
oauth2AuthorizeCode,
|
|
116
|
+
oauth2CreateKeyHash,
|
|
117
|
+
oauth2CreateLoginUrl,
|
|
118
|
+
oauth2CreateRandomKey,
|
|
119
|
+
oauth2GetToken,
|
|
120
|
+
oauth2GetNewTokens,
|
|
121
|
+
oauth2Login,
|
|
122
|
+
oauth2RefreshToken
|
|
123
|
+
}
|
|
@@ -85,10 +85,8 @@ class AuthPrefix {
|
|
|
85
85
|
|
|
86
86
|
return returnData;
|
|
87
87
|
}
|
|
88
|
-
async accounts_email_setup({ fields, context }) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const result = await query({
|
|
88
|
+
async accounts_email_setup({ fields, context = this._graphServer.context }) {
|
|
89
|
+
return query({
|
|
92
90
|
query : `
|
|
93
91
|
mutation {
|
|
94
92
|
auth {
|
|
@@ -99,14 +97,10 @@ class AuthPrefix {
|
|
|
99
97
|
}
|
|
100
98
|
`,
|
|
101
99
|
url : this._graphUrl,
|
|
102
|
-
token : context.token
|
|
100
|
+
token : context.token,
|
|
101
|
+
key : "auth.accounts_email_setup",
|
|
102
|
+
clean : true
|
|
103
103
|
});
|
|
104
|
-
|
|
105
|
-
const returnData = result.auth.accounts_email_setup;
|
|
106
|
-
|
|
107
|
-
nullToUndefined(returnData);
|
|
108
|
-
|
|
109
|
-
return returnData;
|
|
110
104
|
}
|
|
111
105
|
async oauth_clients(...args) {
|
|
112
106
|
return this._oauthClients.find(...args);
|