@labdigital/commercetools-mock 2.11.0 → 2.12.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/dist/index.js CHANGED
@@ -1262,7 +1262,8 @@ var OAuth2Store = class {
1262
1262
  access_token: randomBytes(16).toString("base64"),
1263
1263
  token_type: "Bearer",
1264
1264
  expires_in: 172800,
1265
- scope: scope || "todo"
1265
+ scope: scope || "todo",
1266
+ refresh_token: `my-project-${randomBytes(16).toString("base64")}`
1266
1267
  };
1267
1268
  this.tokens.push(token);
1268
1269
  return token;
@@ -1275,7 +1276,8 @@ var OAuth2Store = class {
1275
1276
  access_token: randomBytes(16).toString("base64"),
1276
1277
  token_type: "Bearer",
1277
1278
  expires_in: 172800,
1278
- scope: scope ? `${scope} anonymous_id:${anonymousId}` : `anonymous_id:${anonymousId}`
1279
+ scope: scope ? `${scope} anonymous_id:${anonymousId}` : `anonymous_id:${anonymousId}`,
1280
+ refresh_token: `my-project-${randomBytes(16).toString("base64")}`
1279
1281
  };
1280
1282
  this.tokens.push(token);
1281
1283
  return token;
@@ -1285,11 +1287,29 @@ var OAuth2Store = class {
1285
1287
  access_token: randomBytes(16).toString("base64"),
1286
1288
  token_type: "Bearer",
1287
1289
  expires_in: 172800,
1288
- scope: scope ? `${scope} customer_id:${customerId}` : `customer_id:${customerId}`
1290
+ scope: scope ? `${scope} customer_id:${customerId}` : `customer_id:${customerId}`,
1291
+ refresh_token: `my-project-${randomBytes(16).toString("base64")}`
1289
1292
  };
1290
1293
  this.tokens.push(token);
1291
1294
  return token;
1292
1295
  }
1296
+ refreshToken(clientId, clientSecret, refreshToken) {
1297
+ const existing = this.tokens.find((t) => t.refresh_token === refreshToken);
1298
+ if (!existing) {
1299
+ return void 0;
1300
+ }
1301
+ const token = {
1302
+ ...existing,
1303
+ access_token: randomBytes(16).toString("base64")
1304
+ };
1305
+ this.tokens.push(token);
1306
+ return {
1307
+ access_token: token.access_token,
1308
+ token_type: token.token_type,
1309
+ expires_in: token.expires_in,
1310
+ scope: token.scope
1311
+ };
1312
+ }
1293
1313
  validateToken(token) {
1294
1314
  if (!this.validate)
1295
1315
  return true;
@@ -1429,11 +1449,36 @@ var OAuth2Server = class {
1429
1449
  );
1430
1450
  return response.status(200).send(token);
1431
1451
  } else if (grantType === "refresh_token") {
1432
- const token = this.store.getClientToken(
1452
+ const refreshToken = request.query.refresh_token?.toString();
1453
+ if (!refreshToken) {
1454
+ return next(
1455
+ new CommercetoolsError(
1456
+ {
1457
+ code: "invalid_request",
1458
+ message: "Missing required parameter: refresh_token."
1459
+ },
1460
+ 400
1461
+ )
1462
+ );
1463
+ }
1464
+ const token = this.store.refreshToken(
1433
1465
  request.credentials.clientId,
1434
1466
  request.credentials.clientSecret,
1435
- request.query.scope?.toString()
1467
+ refreshToken
1436
1468
  );
1469
+ if (!token) {
1470
+ return next(
1471
+ new CommercetoolsError(
1472
+ {
1473
+ statusCode: 400,
1474
+ message: "The refresh token was not found. It may have expired.",
1475
+ error: "invalid_grant",
1476
+ error_description: "The refresh token was not found. It may have expired."
1477
+ },
1478
+ 400
1479
+ )
1480
+ );
1481
+ }
1437
1482
  return response.status(200).send(token);
1438
1483
  } else {
1439
1484
  return next(