-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-secret-mgmt.js
252 lines (226 loc) · 6.8 KB
/
api-secret-mgmt.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import axios from "axios";
/**
* Updates the API secret for a given API key.
* @param {string} api_key - The API key to update.
* @param {string} api_secret - The current API secret.
* @param {string} new_secret - The new API secret.
* @returns {Promise<boolean>} - A Promise that resolves to true if successful, false otherwise.
*/
export async function apiCreateSecret(
auth_api_key,
auth_api_secret,
api_key,
new_secret
) {
const url = `https://api.nexmo.com/accounts/${api_key}/secrets`;
let response = await axios.post(
url,
{
secret: new_secret,
},
{
auth: {
username: auth_api_key,
password: auth_api_secret,
},
}
);
return response;
}
export async function apiRetrieveAllSecrets(
auth_api_key,
auth_api_secret,
api_key
) {
const url = `https://api.nexmo.com/accounts/${api_key}/secrets`;
let response = await axios.get(url, {
auth: {
username: auth_api_key,
password: auth_api_secret,
},
});
return response;
}
export async function apiRevokeOneSecret(
auth_api_key,
auth_api_secret,
api_key,
secret_id
) {
const url = `https://api.nexmo.com/accounts/${api_key}/secrets/${secret_id}`;
let response = await axios.delete(url, {
auth: {
username: auth_api_key,
password: auth_api_secret,
},
});
return response;
}
export async function getFirstSecretId(data) {
// Check if _embedded and secrets array exist in the data object
if (
data._embedded &&
data._embedded.secrets &&
data._embedded.secrets.length > 0
) {
// Get the first secret in the secrets array
const firstSecret = data._embedded.secrets[0];
// Return the id of the first secret
return firstSecret.id;
}
// Return null if the secrets array is empty or does not exist
return null;
}
export async function getLastSecretId(data) {
// Check if _embedded and secrets array exist in the data object
if (
data._embedded &&
data._embedded.secrets &&
data._embedded.secrets.length > 0
) {
// Get the last secret in the secrets array
const lastSecret =
data._embedded.secrets[data._embedded.secrets.length - 1];
// Return the id of the last secret
// console.log("lastSecret.id", lastSecret.id);
return lastSecret.id;
}
// Return null if the secrets array is empty or does not exist
return null;
}
export function findSecretById(data, id) {
// Check if _embedded and secrets array exist in the data object
if (data._embedded && data._embedded.secrets) {
// Iterate through each secret in the secrets array
for (let secret of data._embedded.secrets) {
// Check if the secret's id matches the provided id
if (secret.id === id) {
return secret;
}
}
}
// Return null if no matching secret is found
return null;
}
// Used by DELETE route
export async function apiRetrieveAllSecretsRevokeOneSecret(
auth_api_key,
auth_api_secret,
sub_key
) {
try {
console.time("API Call apiRetrieveAllSecrets");
let apiRetrieveAllSecretsResp = await apiRetrieveAllSecrets(
auth_api_key,
auth_api_secret,
sub_key
);
console.log("apiRetrieveAllSecretsResp:", apiRetrieveAllSecretsResp.data);
console.timeEnd("API Call apiRetrieveAllSecrets");
// Check how many secrets, if 2 then delete first, create new secret, then modify subaccount to suspended: false.
if (apiRetrieveAllSecretsResp.data._embedded.secrets.length === 2) {
console.log("\n\n2 secrets");
console.time("\ngetFirstSecretId");
let firstSecretId = await getFirstSecretId(
apiRetrieveAllSecretsResp.data
);
console.timeEnd("\ngetFirstSecretId");
console.log("firstSecretId:", firstSecretId);
// If 2 secrets exists, then revoke first one via apiRevokeOneSecretResp
try {
console.time("API Call apiRevokeOneSecret");
let apiRevokeOneSecretResp = await apiRevokeOneSecret(
auth_api_key,
auth_api_secret,
sub_key,
firstSecretId
);
console.log(
"apiRevokeOneSecretResp.status:",
apiRevokeOneSecretResp.status
); // 204
console.timeEnd("API Call apiRevokeOneSecret");
return apiRevokeOneSecretResp;
} catch (error) {
console.log("apiRevokeOneSecretResp ERROR:", error);
if (error.response) {
console.error(
`apiRevokeOneSecretResp Error status code: ${error.response.status}`
);
if (error.response.status === 400) {
console.error(
"apiRevokeOneSecretResp Error details:",
error.response.data
);
}
return error.response.data;
} else if (error.request) {
console.error(
"apiRevokeOneSecretResp No response received:",
error.request
);
return "apiRevokeOneSecretResp No response received from the server";
} else {
console.error("apiRevokeOneSecretResp error:", error.message);
return error.message;
}
}
} else if (apiRetrieveAllSecretsResp.data._embedded.secrets.length === 1) {
console.log("\n\n1 secret");
return;
} else {
console.log("\n\nother secrets");
return;
}
} catch (error) {
console.log("apiRetrieveAllSecretsResp ERROR:", error);
if (error.response) {
console.error(
`apiRetrieveAllSecretsResp Error status code: ${error.response.status}`
);
if (error.response.status === 400) {
console.error(
"apiRetrieveAllSecretsResp Error details:",
error.response.data
);
}
return error.response.data;
} else if (error.request) {
console.error(
"apiRetrieveAllSecretsResp No response received:",
error.request
);
return "apiRetrieveAllSecretsResp No response received from the server";
} else {
console.error("apiRetrieveAllSecretsResp error:", error.message);
return error.message;
}
}
}
export async function validateSecret(secret) {
const errors = [];
// Check length
if (secret.length < 8) {
errors.push("The secret must be at least 8 characters long.");
}
if (secret.length > 25) {
errors.push("The secret must be no more than 25 characters long.");
}
// Check for at least one lowercase letter
if (!/[a-z]/.test(secret)) {
errors.push("The secret must contain at least one lowercase letter.");
}
// Check for at least one uppercase letter
if (!/[A-Z]/.test(secret)) {
errors.push("The secret must contain at least one uppercase letter.");
}
// Check for at least one digit
if (!/\d/.test(secret)) {
errors.push("The secret must contain at least one digit.");
}
if (errors.length > 0) {
return errors.join(" ");
} else {
return "valid secret";
}
}