Skip to content

Commit 55ce947

Browse files
committed
Added comments and documentation
1 parent c6dac08 commit 55ce947

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/auth/token-generator.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,36 @@ const BLACKLISTED_CLAIMS = [
3535
// Audience to use for Firebase Auth Custom tokens
3636
const FIREBASE_AUDIENCE = 'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit';
3737

38+
/**
39+
* CryptoSigner interface represents an object that can be used to sign JWTs.
40+
*/
3841
export interface CryptoSigner {
42+
/**
43+
* Cryptographically signs a buffer of data.
44+
*
45+
* @param {Buffer} buffer The data to be signed.
46+
* @returns {Promise<object>} A promise that resolves with a base64-encoded signature.
47+
*/
3948
sign(buffer: Buffer): Promise<Buffer>;
49+
50+
/**
51+
* Returns the ID of the service account used to sign tokens.
52+
*
53+
* @returns {Promise<string>} A promise that resolves with a service account ID.
54+
*/
4055
getAccount(): Promise<string>;
4156
}
4257

58+
/**
59+
* Represents the header of a JWT.
60+
*/
4361
interface JWTHeader {
4462
alg: string;
4563
}
4664

65+
/**
66+
* Represents the body of a JWT.
67+
*/
4768
interface JWTBody {
4869
claims?: object;
4970
uid: string;
@@ -54,9 +75,18 @@ interface JWTBody {
5475
sub: string;
5576
}
5677

78+
/**
79+
* A CryptoSigner implementation that uses an explicitly specified service account private key to
80+
* sign data. Performs all operations locally, and does not make any RPC calls.
81+
*/
5782
export class ServiceAccountSigner implements CryptoSigner {
5883
private readonly certificate_: Certificate;
5984

85+
/**
86+
* Create a new CryptoSigner instance from the given service account certificate.
87+
*
88+
* @param {Certificate} certificate A service account certificate.
89+
*/
6090
constructor(certificate: Certificate) {
6191
if (!certificate) {
6292
throw new FirebaseAuthError(
@@ -79,6 +109,14 @@ export class ServiceAccountSigner implements CryptoSigner {
79109
}
80110
}
81111

112+
/**
113+
* A CryptoSigner implementation that uses the remote IAM service to sign data. If initialized without
114+
* a service account ID, attempts to discover a service account ID by consulting the local Metadata
115+
* service. This will succeed in managed environments like Google Cloud Functions and App Engine.
116+
*
117+
* @see https://cloud.google.com/iam/reference/rest/v1/projects.serviceAccounts/signBlob
118+
* @see https://cloud.google.com/compute/docs/storing-retrieving-metadata
119+
*/
82120
export class IAMSigner implements CryptoSigner {
83121
private readonly httpClient: AuthorizedHttpClient;
84122
private serviceAccountId: string;
@@ -146,6 +184,13 @@ export class IAMSigner implements CryptoSigner {
146184
}
147185
}
148186

187+
/**
188+
* Create a new CryptoSigner instance for the given app. If the app has been initialized with a service
189+
* account credential, creates a ServiceAccountSigner. Otherwise creates an IAMSigner.
190+
*
191+
* @param {FirebaseApp} app A FirebaseApp instance.
192+
* @returns {CryptoSigner} A CryptoSigner instance.
193+
*/
149194
export function signerFromApp(app: FirebaseApp): CryptoSigner {
150195
const cert = app.options.credential.getCertificate();
151196
if (cert != null && validator.isNonEmptyString(cert.privateKey) && validator.isNonEmptyString(cert.clientEmail)) {
@@ -155,7 +200,7 @@ export function signerFromApp(app: FirebaseApp): CryptoSigner {
155200
}
156201

157202
/**
158-
* Class for generating and verifying different types of Firebase Auth tokens (JWTs).
203+
* Class for generating different types of Firebase Auth tokens (JWTs).
159204
*/
160205
export class FirebaseTokenGenerator {
161206

0 commit comments

Comments
 (0)