@openstax/ts-utils 1.16.2 → 1.17.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.
@@ -21,7 +21,7 @@ export declare const createLaunchSigner: <C extends string = "launch">({ configS
21
21
  jwks: () => Promise<{
22
22
  keys: JWK.RawKey[];
23
23
  }>;
24
- sign: (subject: string, maxExpiresIn?: string | number | null | undefined) => Promise<string>;
24
+ sign: (subject: string, maxExp?: number | null | undefined) => Promise<string>;
25
25
  };
26
26
  export declare type LaunchSigner = ReturnType<ReturnType<typeof createLaunchSigner>>;
27
27
  export {};
@@ -34,17 +34,21 @@ const createLaunchSigner = ({ configSpace }) => (configProvider) => {
34
34
  return keystore;
35
35
  });
36
36
  const jwks = async () => (await getKeyStore()).toJSON(false);
37
- const getExpiresInWithMax = async (maxExpiresIn) => {
37
+ const getExpiresInWithMax = async (maxExp) => {
38
38
  const expiresIn = await getExpiresIn();
39
- const maxExpiresInSeconds = typeof maxExpiresIn === 'number' ? maxExpiresIn :
40
- typeof maxExpiresIn === 'string' ? Math.floor((0, ms_1.default)(maxExpiresIn) / 1000) :
41
- undefined;
42
- return maxExpiresInSeconds ? Math.min(Math.floor((0, ms_1.default)(expiresIn) / 1000), maxExpiresInSeconds) : expiresIn;
39
+ // The ms library used by jsonwebtoken can handle a value in seconds as well as a string like '1d'
40
+ if (!maxExp) {
41
+ return expiresIn;
42
+ }
43
+ // Convert both values to seconds for comparison
44
+ const expiresInSeconds = Math.floor((0, ms_1.default)(expiresIn) / 1000);
45
+ const maxExpSeconds = maxExp - Math.floor(Date.now() / 1000);
46
+ return Math.min(expiresInSeconds, maxExpSeconds);
43
47
  };
44
- const sign = async (subject, maxExpiresIn) => {
48
+ const sign = async (subject, maxExp) => {
45
49
  const alg = await getAlg();
46
50
  // expiresIn can be a number of seconds or a string like '1h' or '1d'
47
- const expiresIn = await getExpiresInWithMax(maxExpiresIn);
51
+ const expiresIn = await getExpiresInWithMax(maxExp);
48
52
  const iss = await getIss();
49
53
  const header = { alg, iss };
50
54
  return jsonwebtoken_1.default.sign({}, await getPrivateKey(), { algorithm: alg, expiresIn, header, issuer: iss, subject });