@openstax/ts-utils 1.15.5 → 1.16.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.
@@ -1,5 +1,5 @@
1
1
  import type { ConfigProviderForConfig } from '../../config';
2
- import { CookieAuthProvider } from '.';
2
+ import { AuthProvider, CookieAuthProvider } from '.';
3
3
  declare type Config = {
4
4
  cookieName: string;
5
5
  encryptionPrivateKey: string;
@@ -8,9 +8,12 @@ declare type Config = {
8
8
  interface Initializer<C> {
9
9
  configSpace?: C;
10
10
  }
11
+ export declare type DecryptionAuthProvider = CookieAuthProvider<AuthProvider & {
12
+ getTokenExpiration: () => Promise<number | undefined>;
13
+ }>;
11
14
  export declare const decryptionAuthProvider: <C extends string = "decryption">(initializer: Initializer<C>) => (configProvider: { [key in C]: {
12
15
  cookieName: import("../../config").ConfigValueProvider<string>;
13
16
  encryptionPrivateKey: import("../../config").ConfigValueProvider<string>;
14
17
  signaturePublicKey: import("../../config").ConfigValueProvider<string>;
15
- }; }) => CookieAuthProvider;
18
+ }; }) => DecryptionAuthProvider;
16
19
  export {};
@@ -18,12 +18,18 @@ export const decryptionAuthProvider = (initializer) => (configProvider) => {
18
18
  }
19
19
  return { headers };
20
20
  };
21
- const loadUser = async () => {
21
+ const getPayload = async () => {
22
22
  const [token] = getAuthTokenOrCookie(request, await cookieName());
23
23
  if (!token) {
24
24
  return undefined;
25
25
  }
26
- const result = decryptAndVerify(token, await encryptionPrivateKey(), await signaturePublicKey());
26
+ return decryptAndVerify(token, await encryptionPrivateKey(), await signaturePublicKey());
27
+ };
28
+ const loadUser = async () => {
29
+ const result = await getPayload();
30
+ if (!result) {
31
+ return undefined;
32
+ }
27
33
  if ('error' in result && result.error == 'expired token') {
28
34
  throw new SessionExpiredError();
29
35
  }
@@ -31,12 +37,16 @@ export const decryptionAuthProvider = (initializer) => (configProvider) => {
31
37
  };
32
38
  return {
33
39
  getAuthorizedFetchConfig,
40
+ getTokenExpiration: async () => {
41
+ const result = await getPayload();
42
+ return result ? result.exp : undefined;
43
+ },
34
44
  getUser: async () => {
35
45
  if (!user) {
36
46
  user = await loadUser();
37
47
  }
38
48
  return user;
39
- }
49
+ },
40
50
  };
41
51
  };
42
52
  };
@@ -45,9 +45,9 @@ export declare type CookieAuthProviderRequest = {
45
45
  headers: HttpHeaders;
46
46
  queryStringParameters?: QueryParams;
47
47
  };
48
- export declare type CookieAuthProvider = (inputs: {
48
+ export declare type CookieAuthProvider<T extends AuthProvider = AuthProvider> = (inputs: {
49
49
  request: CookieAuthProviderRequest;
50
- }) => AuthProvider;
50
+ }) => T;
51
51
  export declare type StubAuthProvider = (user: User | undefined) => AuthProvider;
52
52
  export declare const stubAuthProvider: (user?: User | undefined) => AuthProvider;
53
53
  export declare const getAuthTokenOrCookie: (request: CookieAuthProviderRequest, cookieName: string, queryKey?: string) => [string, {
@@ -17,11 +17,13 @@ export declare const verifyJws: (jws: string, signaturePublicKey: Buffer | strin
17
17
  * @param token the encrypted token
18
18
  * @param encryptionPrivateKey the private key used to encrypt the token
19
19
  * @param signaturePublicKey the public key used to verify the decrypted token
20
- * @returns {user: User} (success) or {error: string} (failure)
20
+ * @returns {user: User; exp: number} (success) or {error: string} (failure)
21
21
  */
22
22
  export declare const decryptAndVerify: (token: string, encryptionPrivateKey: string, signaturePublicKey: string) => {
23
23
  user: User;
24
+ exp: number;
24
25
  } | {
25
26
  error: string;
27
+ exp?: number;
26
28
  };
27
29
  export {};
@@ -57,7 +57,7 @@ export const verifyJws = (jws, signaturePublicKey) => {
57
57
  * @param token the encrypted token
58
58
  * @param encryptionPrivateKey the private key used to encrypt the token
59
59
  * @param signaturePublicKey the public key used to verify the decrypted token
60
- * @returns {user: User} (success) or {error: string} (failure)
60
+ * @returns {user: User; exp: number} (success) or {error: string} (failure)
61
61
  */
62
62
  export const decryptAndVerify = (token, encryptionPrivateKey, signaturePublicKey) => {
63
63
  const timestamp = Math.floor(Date.now() / 1000);
@@ -79,7 +79,7 @@ export const decryptAndVerify = (token, encryptionPrivateKey, signaturePublicKey
79
79
  return { error: 'invalid token' };
80
80
  }
81
81
  if (payload.exp < timestamp - clockTolerance) {
82
- return { error: 'expired token' };
82
+ return { error: 'expired token', exp: payload.exp };
83
83
  }
84
- return { user: payload.sub };
84
+ return { user: payload.sub, exp: payload.exp };
85
85
  };
@@ -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) => Promise<string>;
24
+ sign: (subject: string, maxExpiresIn?: string | number | undefined) => Promise<string>;
25
25
  };
26
26
  export declare type LaunchSigner = ReturnType<ReturnType<typeof createLaunchSigner>>;
27
27
  export {};
@@ -1,4 +1,5 @@
1
1
  import jwt from 'jsonwebtoken';
2
+ import ms from 'ms';
2
3
  import { JWK } from 'node-jose';
3
4
  import { once } from '../..';
4
5
  import { resolveConfigValue } from '../../config';
@@ -27,9 +28,17 @@ export const createLaunchSigner = ({ configSpace }) => (configProvider) => {
27
28
  return keystore;
28
29
  });
29
30
  const jwks = async () => (await getKeyStore()).toJSON(false);
30
- const sign = async (subject) => {
31
- const alg = await getAlg();
31
+ const getExpiresInWithMax = async (maxExpiresIn) => {
32
32
  const expiresIn = await getExpiresIn();
33
+ const maxExpiresInSeconds = typeof maxExpiresIn === 'number' ? maxExpiresIn :
34
+ typeof maxExpiresIn === 'string' ? Math.floor(ms(maxExpiresIn) / 1000) :
35
+ undefined;
36
+ return maxExpiresInSeconds ? Math.min(Math.floor(ms(expiresIn) / 1000), maxExpiresInSeconds) : expiresIn;
37
+ };
38
+ const sign = async (subject, maxExpiresIn) => {
39
+ const alg = await getAlg();
40
+ // expiresIn can be a number of seconds or a string like '1h' or '1d'
41
+ const expiresIn = await getExpiresInWithMax(maxExpiresIn);
33
42
  const iss = await getIss();
34
43
  const header = { alg, iss };
35
44
  return jwt.sign({}, await getPrivateKey(), { algorithm: alg, expiresIn, header, issuer: iss, subject });