@labdigital/commercetools-mock 2.11.0 → 2.12.1

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.cjs CHANGED
@@ -1294,14 +1294,18 @@ var OAuth2Store = class {
1294
1294
  constructor(validate = true) {
1295
1295
  this.validate = validate;
1296
1296
  }
1297
+ addToken(token) {
1298
+ this.tokens.push(token);
1299
+ }
1297
1300
  getClientToken(clientId, clientSecret, scope) {
1298
1301
  const token = {
1299
1302
  access_token: (0, import_crypto.randomBytes)(16).toString("base64"),
1300
1303
  token_type: "Bearer",
1301
1304
  expires_in: 172800,
1302
- scope: scope || "todo"
1305
+ scope: scope || "todo",
1306
+ refresh_token: `my-project-${(0, import_crypto.randomBytes)(16).toString("base64")}`
1303
1307
  };
1304
- this.tokens.push(token);
1308
+ this.addToken(token);
1305
1309
  return token;
1306
1310
  }
1307
1311
  getAnonymousToken(scope, anonymousId) {
@@ -1312,9 +1316,10 @@ var OAuth2Store = class {
1312
1316
  access_token: (0, import_crypto.randomBytes)(16).toString("base64"),
1313
1317
  token_type: "Bearer",
1314
1318
  expires_in: 172800,
1315
- scope: scope ? `${scope} anonymous_id:${anonymousId}` : `anonymous_id:${anonymousId}`
1319
+ scope: scope ? `${scope} anonymous_id:${anonymousId}` : `anonymous_id:${anonymousId}`,
1320
+ refresh_token: `my-project-${(0, import_crypto.randomBytes)(16).toString("base64")}`
1316
1321
  };
1317
- this.tokens.push(token);
1322
+ this.addToken(token);
1318
1323
  return token;
1319
1324
  }
1320
1325
  getCustomerToken(scope, customerId) {
@@ -1322,11 +1327,29 @@ var OAuth2Store = class {
1322
1327
  access_token: (0, import_crypto.randomBytes)(16).toString("base64"),
1323
1328
  token_type: "Bearer",
1324
1329
  expires_in: 172800,
1325
- scope: scope ? `${scope} customer_id:${customerId}` : `customer_id:${customerId}`
1330
+ scope: scope ? `${scope} customer_id:${customerId}` : `customer_id:${customerId}`,
1331
+ refresh_token: `my-project-${(0, import_crypto.randomBytes)(16).toString("base64")}`
1326
1332
  };
1327
- this.tokens.push(token);
1333
+ this.addToken(token);
1328
1334
  return token;
1329
1335
  }
1336
+ refreshToken(clientId, clientSecret, refreshToken) {
1337
+ const existing = this.tokens.find((t) => t.refresh_token === refreshToken);
1338
+ if (!existing) {
1339
+ return void 0;
1340
+ }
1341
+ const token = {
1342
+ ...existing,
1343
+ access_token: (0, import_crypto.randomBytes)(16).toString("base64")
1344
+ };
1345
+ this.addToken(token);
1346
+ return {
1347
+ access_token: token.access_token,
1348
+ token_type: token.token_type,
1349
+ expires_in: token.expires_in,
1350
+ scope: token.scope
1351
+ };
1352
+ }
1330
1353
  validateToken(token) {
1331
1354
  if (!this.validate)
1332
1355
  return true;
@@ -1466,11 +1489,36 @@ var OAuth2Server = class {
1466
1489
  );
1467
1490
  return response.status(200).send(token);
1468
1491
  } else if (grantType === "refresh_token") {
1469
- const token = this.store.getClientToken(
1492
+ const refreshToken = request.query.refresh_token?.toString();
1493
+ if (!refreshToken) {
1494
+ return next(
1495
+ new CommercetoolsError(
1496
+ {
1497
+ code: "invalid_request",
1498
+ message: "Missing required parameter: refresh_token."
1499
+ },
1500
+ 400
1501
+ )
1502
+ );
1503
+ }
1504
+ const token = this.store.refreshToken(
1470
1505
  request.credentials.clientId,
1471
1506
  request.credentials.clientSecret,
1472
- request.query.scope?.toString()
1507
+ refreshToken
1473
1508
  );
1509
+ if (!token) {
1510
+ return next(
1511
+ new CommercetoolsError(
1512
+ {
1513
+ statusCode: 400,
1514
+ message: "The refresh token was not found. It may have expired.",
1515
+ error: "invalid_grant",
1516
+ error_description: "The refresh token was not found. It may have expired."
1517
+ },
1518
+ 400
1519
+ )
1520
+ );
1521
+ }
1474
1522
  return response.status(200).send(token);
1475
1523
  } else {
1476
1524
  return next(
@@ -7276,6 +7324,9 @@ var CommercetoolsMock = class {
7276
7324
  this._storage
7277
7325
  );
7278
7326
  }
7327
+ authStore() {
7328
+ return this._oauth2.store;
7329
+ }
7279
7330
  runServer(port = 3e3, options) {
7280
7331
  const server = this.app.listen(port, () => {
7281
7332
  console.info(`Mock server listening at http://localhost:${port}`);