@shoper/cli 0.2.1-1 → 0.2.1-3

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.
@@ -40,4 +40,7 @@ export class CliAuthTokensApi extends FeatureApi {
40
40
  getTokensCount() {
41
41
  return this.#service.getTokensCount();
42
42
  }
43
+ validateToken(token) {
44
+ return this.#service.validateToken(token);
45
+ }
43
46
  }
@@ -23,4 +23,34 @@ export class CLiAuthTokensUtils {
23
23
  return true;
24
24
  return tokenPayload.exp - Math.floor(Date.now() / 1000) < 0;
25
25
  }
26
+ static isValidJWT(token) {
27
+ const parts = token.split('.');
28
+ if (parts.length !== 3) {
29
+ return { isValid: false, message: 'Invalid token format. JWT should have 3 parts.' };
30
+ }
31
+ try {
32
+ const decoded = jwt.decode(token, { complete: true });
33
+ if (!decoded) {
34
+ return { isValid: false, message: 'Failed to decode token.' };
35
+ }
36
+ const { header } = decoded;
37
+ const payload = decoded.payload;
38
+ const expectedPayloadKeys = ['sub', 'exp', 'iat', 'iss', 'nbf', 'scope', 'id', 'name', 'key'];
39
+ for (const key of Object.keys(payload)) {
40
+ if (!expectedPayloadKeys.includes(key)) {
41
+ return { isValid: false, message: `Unexpected key in payload: ${key}` };
42
+ }
43
+ }
44
+ // Check if the token has expired
45
+ const now = Math.floor(Date.now() / 1000); // Current time in seconds
46
+ if (payload.exp < now) {
47
+ return { isValid: false, message: 'Token has expired.' };
48
+ }
49
+ // If all checks pass, consider the token valid (structure and keys checked)
50
+ return { isValid: true, message: 'Token is valid (structure and keys checked).' };
51
+ }
52
+ catch {
53
+ return { isValid: false, message: 'Error decoding the token. Invalid format.' };
54
+ }
55
+ }
26
56
  }
@@ -16,6 +16,9 @@ export class CLiAuthTokensService {
16
16
  tokens.push(tokeItem);
17
17
  this._setTokens(tokens);
18
18
  }
19
+ validateToken(token) {
20
+ return CLiAuthTokensUtils.isValidJWT(token);
21
+ }
19
22
  hasToken(index) {
20
23
  return index <= this.getTokensCount();
21
24
  }
@@ -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,
@@ -72,8 +72,7 @@ export class ThemePushService {
72
72
  const { uploadedImageData, rejectedImageData } = await themeFilesUploadApi.uploadFiles(filesToUpload);
73
73
  if (uploadedImageData.length)
74
74
  await ThemeImagesUtils.removeUploadedOriginalFiles(themeRootDir, uploadedImageData);
75
- if (rejectedImageData.length)
76
- await this._removeUploadedThemeFiles(rejectedImageData, themeRootDir);
75
+ // if (rejectedImageData.length) await this._removeUploadedThemeFiles(rejectedImageData, themeRootDir);
77
76
  return {
78
77
  localFileNameToUploaded: uploadedImageData.reduce((acc, { originalFilename, uploadedFilename }) => {
79
78
  return {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@shoper/cli",
3
3
  "packageManager": "yarn@3.2.0",
4
4
  "sideEffects": false,
5
- "version": "0.2.1-1",
5
+ "version": "0.2.1-3",
6
6
  "description": "CLI tool for Shoper",
7
7
  "author": "Joanna Firek",
8
8
  "license": "MIT",