@shoper/cli 0.2.1-2 → 0.2.1-4
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.
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import jwt from 'jsonwebtoken';
|
|
2
|
+
import differenceBy from 'lodash/differenceBy.js';
|
|
2
3
|
export class CLiAuthTokensUtils {
|
|
3
4
|
static getTokenPayload(token) {
|
|
4
5
|
try {
|
|
@@ -23,4 +24,36 @@ export class CLiAuthTokensUtils {
|
|
|
23
24
|
return true;
|
|
24
25
|
return tokenPayload.exp - Math.floor(Date.now() / 1000) < 0;
|
|
25
26
|
}
|
|
27
|
+
static isValidJWT(token) {
|
|
28
|
+
const parts = token.split('.');
|
|
29
|
+
if (parts.length !== 3) {
|
|
30
|
+
return { isValid: false, message: 'Invalid token format. JWT should have 3 parts.' };
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const decoded = jwt.decode(token, { complete: true });
|
|
34
|
+
if (!decoded) {
|
|
35
|
+
return { isValid: false, message: 'Failed to decode token.' };
|
|
36
|
+
}
|
|
37
|
+
const payload = decoded.payload;
|
|
38
|
+
const expectedPayloadKeys = ['exp', 'iat', 'iss', 'nbf', 'scope', 'id', 'name', 'key'];
|
|
39
|
+
const missingKeys = differenceBy(expectedPayloadKeys, Object.keys(payload));
|
|
40
|
+
if (missingKeys.length > 0) {
|
|
41
|
+
return { isValid: false, message: `Missing keys in payload: ${missingKeys.join(', ')}` };
|
|
42
|
+
}
|
|
43
|
+
const unexpectedKeys = differenceBy(Object.keys(payload), expectedPayloadKeys);
|
|
44
|
+
if (unexpectedKeys.length > 0) {
|
|
45
|
+
return { isValid: false, message: `Unexpected keys in payload: ${unexpectedKeys.join(', ')}` };
|
|
46
|
+
}
|
|
47
|
+
// Check if the token has expired
|
|
48
|
+
const now = Math.floor(Date.now() / 1000); // Current time in seconds
|
|
49
|
+
if (payload.exp < now) {
|
|
50
|
+
return { isValid: false, message: 'Token has expired.' };
|
|
51
|
+
}
|
|
52
|
+
// If all checks pass, consider the token valid (structure and keys checked)
|
|
53
|
+
return { isValid: true, message: 'Token is valid (structure and keys checked).' };
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return { isValid: false, message: 'Error decoding the token. Invalid format.' };
|
|
57
|
+
}
|
|
58
|
+
}
|
|
26
59
|
}
|
|
@@ -16,6 +16,13 @@ export class CliAuthAddTokenCommand extends BaseCliCommand {
|
|
|
16
16
|
const cliAuthTokensApi = this.getApi(CLI_AUTH_TOKENS_API_NAME);
|
|
17
17
|
try {
|
|
18
18
|
const { input: token } = await promptInput('Please paste your CLI token:');
|
|
19
|
+
const { isValid, message } = cliAuthTokensApi.validateToken(token);
|
|
20
|
+
if (!isValid) {
|
|
21
|
+
renderOnce(React.createElement(Error, { header: "Error: Failed to add token: " },
|
|
22
|
+
React.createElement(Box, null,
|
|
23
|
+
React.createElement(Text, null, message))));
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
19
26
|
cliAuthTokensApi.addToken(token);
|
|
20
27
|
cliAuthTokensApi.setDefaultToken(cliAuthTokensApi.getTokensCount());
|
|
21
28
|
renderOnce(React.createElement(Success, null,
|