@@ -35,15 +35,36 @@ const BLACKLISTED_CLAIMS = [
35
35
// Audience to use for Firebase Auth Custom tokens
36
36
const FIREBASE_AUDIENCE = 'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit' ;
37
37
38
+ /**
39
+ * CryptoSigner interface represents an object that can be used to sign JWTs.
40
+ */
38
41
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
+ */
39
48
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
+ */
40
55
getAccount ( ) : Promise < string > ;
41
56
}
42
57
58
+ /**
59
+ * Represents the header of a JWT.
60
+ */
43
61
interface JWTHeader {
44
62
alg : string ;
45
63
}
46
64
65
+ /**
66
+ * Represents the body of a JWT.
67
+ */
47
68
interface JWTBody {
48
69
claims ?: object ;
49
70
uid : string ;
@@ -54,9 +75,18 @@ interface JWTBody {
54
75
sub : string ;
55
76
}
56
77
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
+ */
57
82
export class ServiceAccountSigner implements CryptoSigner {
58
83
private readonly certificate_ : Certificate ;
59
84
85
+ /**
86
+ * Create a new CryptoSigner instance from the given service account certificate.
87
+ *
88
+ * @param {Certificate } certificate A service account certificate.
89
+ */
60
90
constructor ( certificate : Certificate ) {
61
91
if ( ! certificate ) {
62
92
throw new FirebaseAuthError (
@@ -79,6 +109,14 @@ export class ServiceAccountSigner implements CryptoSigner {
79
109
}
80
110
}
81
111
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
+ */
82
120
export class IAMSigner implements CryptoSigner {
83
121
private readonly httpClient : AuthorizedHttpClient ;
84
122
private serviceAccountId : string ;
@@ -146,6 +184,13 @@ export class IAMSigner implements CryptoSigner {
146
184
}
147
185
}
148
186
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
+ */
149
194
export function signerFromApp ( app : FirebaseApp ) : CryptoSigner {
150
195
const cert = app . options . credential . getCertificate ( ) ;
151
196
if ( cert != null && validator . isNonEmptyString ( cert . privateKey ) && validator . isNonEmptyString ( cert . clientEmail ) ) {
@@ -155,7 +200,7 @@ export function signerFromApp(app: FirebaseApp): CryptoSigner {
155
200
}
156
201
157
202
/**
158
- * Class for generating and verifying different types of Firebase Auth tokens (JWTs).
203
+ * Class for generating different types of Firebase Auth tokens (JWTs).
159
204
*/
160
205
export class FirebaseTokenGenerator {
161
206
0 commit comments