@opengovsg/refx-ts-sdk 0.0.0-develop-alpha-1763450943 → 0.0.0-develop-alpha-1763535866

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.
@@ -3,6 +3,7 @@ export declare namespace ReferralExchangeJwtClient {
3
3
  interface Options extends Omit<ReferralExchangeClient.Options, "fetcher" | "apiKey"> {
4
4
  privateKey: string;
5
5
  apiKeyName: string;
6
+ subject?: string;
6
7
  tokenCache?: TokenCacheOptions;
7
8
  }
8
9
  interface TokenCacheOptions {
@@ -30,17 +30,18 @@ const Fetcher_1 = require("../core/fetcher/Fetcher");
30
30
  const JWT_TTL_SECONDS = 15;
31
31
  class ReferralExchangeJwtClient extends Client_1.ReferralExchangeClient {
32
32
  constructor(options) {
33
- const { privateKey, apiKeyName, tokenCache } = options, baseOptions = __rest(options, ["privateKey", "apiKeyName", "tokenCache"]);
34
- const signer = new JwtSigner({ privateKey, issuer: apiKeyName, tokenCache });
33
+ const { privateKey, apiKeyName, subject, tokenCache } = options, baseOptions = __rest(options, ["privateKey", "apiKeyName", "subject", "tokenCache"]);
34
+ const signer = new JwtSigner({ privateKey, issuer: apiKeyName, subject, tokenCache });
35
35
  const fetcher = createJwtFetcher(signer);
36
36
  super(Object.assign(Object.assign({}, baseOptions), { fetcher }));
37
37
  }
38
38
  }
39
39
  exports.ReferralExchangeJwtClient = ReferralExchangeJwtClient;
40
40
  class JwtSigner {
41
- constructor({ privateKey, issuer, tokenCache }) {
41
+ constructor({ privateKey, issuer, subject, tokenCache }) {
42
42
  this.privateKey = privateKey;
43
43
  this.issuer = issuer;
44
+ this.subject = subject;
44
45
  const cacheEnabled = tokenCache != null;
45
46
  this.cacheEnabled = cacheEnabled;
46
47
  this.refreshBufferSeconds = cacheEnabled ? clampRefreshBufferSeconds(tokenCache.refreshBufferSeconds) : 0;
@@ -64,6 +65,7 @@ class JwtSigner {
64
65
  return createSignedJwt({
65
66
  privateKey: this.privateKey,
66
67
  issuer: this.issuer,
68
+ subject: this.subject,
67
69
  });
68
70
  }
69
71
  }
@@ -76,14 +78,19 @@ function createJwtFetcher(signer) {
76
78
  return (0, Fetcher_1.fetcherImpl)(Object.assign(Object.assign({}, args), { headers }));
77
79
  });
78
80
  }
79
- function createSignedJwt({ privateKey, issuer }) {
81
+ function createSignedJwt({ privateKey, issuer, subject }) {
80
82
  const issuedAt = Math.floor(Date.now() / 1000);
81
83
  const expiresAtEpochSeconds = issuedAt + JWT_TTL_SECONDS;
82
- const token = jsonwebtoken_1.default.sign({}, privateKey, {
84
+ const signOptions = {
83
85
  algorithm: "ES256",
84
86
  issuer,
85
87
  expiresIn: JWT_TTL_SECONDS,
86
- });
88
+ };
89
+ // Only add the claim if a value is provided(not null or undefined)
90
+ if (subject != null) {
91
+ signOptions.subject = subject;
92
+ }
93
+ const token = jsonwebtoken_1.default.sign({}, privateKey, signOptions);
87
94
  return {
88
95
  token,
89
96
  expiresAtEpochSeconds,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengovsg/refx-ts-sdk",
3
- "version": "0.0.0-develop-alpha-1763450943",
3
+ "version": "0.0.0-develop-alpha-1763535866",
4
4
  "private": false,
5
5
  "repository": "https://github.com/opengovsg/refer-ts-sdk",
6
6
  "main": "./index.js",
@@ -3,6 +3,7 @@ export declare namespace ReferralExchangeJwtClient {
3
3
  interface Options extends Omit<ReferralExchangeClient.Options, "fetcher" | "apiKey"> {
4
4
  privateKey: string;
5
5
  apiKeyName: string;
6
+ subject?: string;
6
7
  tokenCache?: TokenCacheOptions;
7
8
  }
8
9
  interface TokenCacheOptions {
@@ -30,17 +30,18 @@ const Fetcher_1 = require("../core/fetcher/Fetcher");
30
30
  const JWT_TTL_SECONDS = 15;
31
31
  class ReferralExchangeJwtClient extends Client_1.ReferralExchangeClient {
32
32
  constructor(options) {
33
- const { privateKey, apiKeyName, tokenCache } = options, baseOptions = __rest(options, ["privateKey", "apiKeyName", "tokenCache"]);
34
- const signer = new JwtSigner({ privateKey, issuer: apiKeyName, tokenCache });
33
+ const { privateKey, apiKeyName, subject, tokenCache } = options, baseOptions = __rest(options, ["privateKey", "apiKeyName", "subject", "tokenCache"]);
34
+ const signer = new JwtSigner({ privateKey, issuer: apiKeyName, subject, tokenCache });
35
35
  const fetcher = createJwtFetcher(signer);
36
36
  super(Object.assign(Object.assign({}, baseOptions), { fetcher }));
37
37
  }
38
38
  }
39
39
  exports.ReferralExchangeJwtClient = ReferralExchangeJwtClient;
40
40
  class JwtSigner {
41
- constructor({ privateKey, issuer, tokenCache }) {
41
+ constructor({ privateKey, issuer, subject, tokenCache }) {
42
42
  this.privateKey = privateKey;
43
43
  this.issuer = issuer;
44
+ this.subject = subject;
44
45
  const cacheEnabled = tokenCache != null;
45
46
  this.cacheEnabled = cacheEnabled;
46
47
  this.refreshBufferSeconds = cacheEnabled ? clampRefreshBufferSeconds(tokenCache.refreshBufferSeconds) : 0;
@@ -64,6 +65,7 @@ class JwtSigner {
64
65
  return createSignedJwt({
65
66
  privateKey: this.privateKey,
66
67
  issuer: this.issuer,
68
+ subject: this.subject,
67
69
  });
68
70
  }
69
71
  }
@@ -76,14 +78,19 @@ function createJwtFetcher(signer) {
76
78
  return (0, Fetcher_1.fetcherImpl)(Object.assign(Object.assign({}, args), { headers }));
77
79
  });
78
80
  }
79
- function createSignedJwt({ privateKey, issuer }) {
81
+ function createSignedJwt({ privateKey, issuer, subject }) {
80
82
  const issuedAt = Math.floor(Date.now() / 1000);
81
83
  const expiresAtEpochSeconds = issuedAt + JWT_TTL_SECONDS;
82
- const token = jsonwebtoken_1.default.sign({}, privateKey, {
84
+ const signOptions = {
83
85
  algorithm: "ES256",
84
86
  issuer,
85
87
  expiresIn: JWT_TTL_SECONDS,
86
- });
88
+ };
89
+ // Only add the claim if a value is provided(not null or undefined)
90
+ if (subject != null) {
91
+ signOptions.subject = subject;
92
+ }
93
+ const token = jsonwebtoken_1.default.sign({}, privateKey, signOptions);
87
94
  return {
88
95
  token,
89
96
  expiresAtEpochSeconds,