@peektravel/app-utilities 0.2.2 → 0.2.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.
package/dist/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var crypto = require('crypto');
4
3
  var jwt = require('jsonwebtoken');
4
+ var crypto = require('crypto');
5
5
 
6
6
  function _interopNamespace(e) {
7
7
  if (e && e.__esModule) return e;
@@ -23,6 +23,8 @@ function _interopNamespace(e) {
23
23
 
24
24
  var jwt__namespace = /*#__PURE__*/_interopNamespace(jwt);
25
25
 
26
+ // src/peek-access-service.ts
27
+
26
28
  // src/internal/gateway-endpoints.ts
27
29
  var SALES_ENDPOINT = "sales";
28
30
  var V2_EXTENDABLE_SLUG = "peek_backoffice_api-v1";
@@ -2918,9 +2920,11 @@ var DEFAULT_V2_BASE_URL = "https://app-registry.peeklabs.com/installations-api";
2918
2920
  var DEFAULT_TOKEN_TTL_SECONDS = 3600;
2919
2921
  var DEFAULT_TOKEN_REFRESH_LEEWAY_SECONDS = 60;
2920
2922
  var DEFAULT_RETRY_DELAYS_MS = [1e3, 2e3, 4e3];
2923
+ var PEEK_TOKEN_ISSUER = "app_registry_v2";
2921
2924
  var PeekAccessService = class {
2922
2925
  client;
2923
2926
  productServiceOptions;
2927
+ jwtSecret;
2924
2928
  productService;
2925
2929
  accountUserService;
2926
2930
  resourcePoolService;
@@ -2939,6 +2943,7 @@ var PeekAccessService = class {
2939
2943
  requireNonEmpty(config.issuer, "issuer");
2940
2944
  requireNonEmpty(config.appId, "appId");
2941
2945
  if (!isV2) requireNonEmpty(config.gatewayKey ?? "", "gatewayKey");
2946
+ this.jwtSecret = config.jwtSecret;
2942
2947
  const logger = config.logger ?? noopLogger;
2943
2948
  const tokens = new TokenManager({
2944
2949
  secret: config.jwtSecret,
@@ -2962,6 +2967,41 @@ var PeekAccessService = class {
2962
2967
  itemOptionsPageSize: config.itemOptionsPageSize
2963
2968
  };
2964
2969
  }
2970
+ /**
2971
+ * Verifies a Peek auth token issued by the app registry and returns the
2972
+ * decoded claims.
2973
+ *
2974
+ * Validates the HMAC signature (using this service's `jwtSecret`), the token
2975
+ * expiry, the `"app_registry_v2"` issuer, and the `"Joken"` audience. Throws
2976
+ * from the `jsonwebtoken` library on any failure — callers should catch to
2977
+ * distinguish error kinds:
2978
+ *
2979
+ * - `JsonWebTokenError` — signature invalid, wrong issuer/audience, or token
2980
+ * malformed
2981
+ * - `TokenExpiredError` — past `exp`
2982
+ * - `NotBeforeError` — before `nbf`
2983
+ *
2984
+ * @throws {JsonWebTokenError} signature invalid or token malformed
2985
+ * @throws {TokenExpiredError} token has expired
2986
+ * @throws {NotBeforeError} token not yet valid
2987
+ */
2988
+ verifyPeekAuthToken(token) {
2989
+ const payload = jwt__namespace.verify(token, this.jwtSecret, {
2990
+ issuer: PEEK_TOKEN_ISSUER
2991
+ });
2992
+ const { user: u } = payload;
2993
+ return {
2994
+ installId: payload.sub,
2995
+ displayVersion: payload.display_version,
2996
+ user: {
2997
+ email: u.email,
2998
+ id: u.id,
2999
+ isAdmin: u.is_admin,
3000
+ locale: u.locale,
3001
+ name: u.name
3002
+ }
3003
+ };
3004
+ }
2965
3005
  /**
2966
3006
  * Returns the {@link ProductService} for this install, bound to the shared
2967
3007
  * authenticated transport. The instance is created lazily and reused.