forked from reactioncommerce/example-storefront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateHydraClientIfNecessary.js
96 lines (84 loc) · 3.05 KB
/
createHydraClientIfNecessary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const fetch = require("isomorphic-unfetch");
const config = require("./config");
let baseUrl = config.CANONICAL_URL;
if (!baseUrl.endsWith("/")) baseUrl = `${baseUrl}/`;
const oauthRedirectUrl = `${baseUrl}callback`;
const oauthPostLogoutRedirectUrl = `${baseUrl}post-logout-callback`;
/* eslint-disable camelcase */
const storefrontHydraClient = {
client_id: config.OAUTH2_CLIENT_ID,
client_secret: config.OAUTH2_CLIENT_SECRET,
grant_types: [
"authorization_code",
"refresh_token"
],
post_logout_redirect_uris: [oauthPostLogoutRedirectUrl],
redirect_uris: [oauthRedirectUrl],
response_types: ["code", "id_token", "token"],
scope: "offline openid",
subject_type: "public",
token_endpoint_auth_method: "client_secret_post"
};
/* eslint-enable camelcase */
/**
* Creates a new Hydra client when on does not exist
*
* @returns {undefined}
*/
async function createHydraClientIfNecessary() {
let adminUrl = config.OAUTH2_ADMIN_URL;
if (!adminUrl.endsWith("/")) adminUrl = `${adminUrl}/`;
const getClientResponse = await fetch(`${adminUrl}clients/${config.OAUTH2_CLIENT_ID}`, {
method: "GET",
headers: { "Content-Type": "application/json" }
});
if (![200, 404].includes(getClientResponse.status)) {
// eslint-disable-next-line no-console
console.error(await getClientResponse.text());
throw new Error(`Could not get Hydra client [${getClientResponse.status}]`);
}
if (getClientResponse.status === 200) {
// Update the client to be sure it has the latest config
// eslint-disable-next-line no-console
console.info("Updating Hydra client...");
const updateClientResponse = await fetch(`${adminUrl}clients/${config.OAUTH2_CLIENT_ID}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(storefrontHydraClient)
});
if (updateClientResponse.status === 200) {
// eslint-disable-next-line no-console
console.info("OK: Hydra client updated");
} else {
// eslint-disable-next-line no-console
console.error(await updateClientResponse.text());
throw new Error(`Could not update Hydra client [${updateClientResponse.status}]`);
}
} else {
// eslint-disable-next-line no-console
console.info("Creating Hydra client...");
const response = await fetch(`${adminUrl}clients`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(storefrontHydraClient)
});
switch (response.status) {
case 200:
// intentional fallthrough!
// eslint-disable-line no-fallthrough
case 201:
// eslint-disable-next-line no-console
console.info("OK: Hydra client created");
break;
case 409:
// eslint-disable-next-line no-console
console.info("OK: Hydra client already exists");
break;
default:
// eslint-disable-next-line no-console
console.error(await response.text());
throw new Error(`Could not create Hydra client [${response.status}]`);
}
}
}
createHydraClientIfNecessary();