feat: Allow passing additional JWT claims to TokenIssuer

Signed-off-by: David Zemon <david@zemon.name>
This commit is contained in:
David Zemon
2022-09-08 11:36:49 -05:00
parent f4a0706787
commit 19ac96e4c2
3 changed files with 17 additions and 6 deletions
@@ -48,7 +48,12 @@ describe('TokenFactory', () => {
await expect(factory.listPublicKeys()).resolves.toEqual({ keys: [] });
const token = await factory.issueToken({
claims: { sub: entityRef, ent: [entityRef] },
claims: {
sub: entityRef,
ent: [entityRef],
'x-fancy-claim': 'my special claim',
aud: 'this value will be overridden',
},
});
const { keys } = await factory.listPublicKeys();
@@ -60,6 +65,7 @@ describe('TokenFactory', () => {
aud: 'backstage',
sub: entityRef,
ent: [entityRef],
'x-fancy-claim': 'my special claim',
iat: expect.any(Number),
exp: expect.any(Number),
});
@@ -77,8 +77,7 @@ export class TokenFactory implements TokenIssuer {
const key = await this.getKey();
const iss = this.issuer;
const sub = params.claims.sub;
const ent = params.claims.ent;
const { sub, ent, ...additionalClaims } = params.claims;
const aud = 'backstage';
const iat = Math.floor(Date.now() / MS_IN_S);
const exp = iat + this.keyDurationSeconds;
@@ -98,7 +97,7 @@ export class TokenFactory implements TokenIssuer {
throw new AuthenticationError('No algorithm was provided in the key');
}
return new SignJWT({ iss, sub, ent, aud, iat, exp })
return new SignJWT({ ...additionalClaims, iss, sub, ent, aud, iat, exp })
.setProtectedHeader({ alg: key.alg, kid: key.kid })
.setIssuer(iss)
.setAudience(aud)
+8 -2
View File
@@ -28,13 +28,19 @@ export interface AnyJWK extends Record<string, string> {
* @public
*/
export type TokenParams = {
/** The claims that will be embedded within the token */
/**
* The claims that will be embedded within the token. At a minimum, this should include
* the subject claim, `sub`. It is common to also list entity ownership relations in the
* `ent` list. Additional claims may also be added at the developer's discretion except
* for the following list, which will be overwritten by the TokenIssuer: `iss`, `aud`,
* `iat`, and `exp`.
*/
claims: {
/** The token subject, i.e. User ID */
sub: string;
/** A list of entity references that the user claims ownership through */
ent?: string[];
};
} & Record<string, string | string[]>;
};
/**