1515 */
1616
1717import { FirebaseApp } from '../firebase-app' ;
18- import { Certificate , tryGetCertificate } from './credential' ;
18+ import { ServiceAccountCredential } from './credential' ;
1919import { AuthClientErrorCode , FirebaseAuthError } from '../utils/error' ;
2020import { AuthorizedHttpClient , HttpError , HttpRequestConfig , HttpClient } from '../utils/api-request' ;
2121
@@ -82,28 +82,19 @@ interface JWTBody {
8282 * sign data. Performs all operations locally, and does not make any RPC calls.
8383 */
8484export class ServiceAccountSigner implements CryptoSigner {
85- private readonly certificate : Certificate ;
8685
8786 /**
88- * Creates a new CryptoSigner instance from the given service account certificate .
87+ * Creates a new CryptoSigner instance from the given service account credential .
8988 *
90- * @param {Certificate } certificate A service account certificate .
89+ * @param {ServiceAccountCredential } credential A service account credential .
9190 */
92- constructor ( certificate : Certificate ) {
93- if ( ! certificate ) {
91+ constructor ( private readonly credential : ServiceAccountCredential ) {
92+ if ( ! credential ) {
9493 throw new FirebaseAuthError (
9594 AuthClientErrorCode . INVALID_CREDENTIAL ,
96- 'INTERNAL ASSERT: Must provide a certificate to initialize ServiceAccountSigner.' ,
95+ 'INTERNAL ASSERT: Must provide a service account credential to initialize ServiceAccountSigner.' ,
9796 ) ;
9897 }
99- if ( ! validator . isNonEmptyString ( certificate . clientEmail ) || ! validator . isNonEmptyString ( certificate . privateKey ) ) {
100- throw new FirebaseAuthError (
101- AuthClientErrorCode . INVALID_CREDENTIAL ,
102- 'INTERNAL ASSERT: Must provide a certificate with validate clientEmail and privateKey to ' +
103- 'initialize ServiceAccountSigner.' ,
104- ) ;
105- }
106- this . certificate = certificate ;
10798 }
10899
109100 /**
@@ -113,14 +104,14 @@ export class ServiceAccountSigner implements CryptoSigner {
113104 const crypto = require ( 'crypto' ) ;
114105 const sign = crypto . createSign ( 'RSA-SHA256' ) ;
115106 sign . update ( buffer ) ;
116- return Promise . resolve ( sign . sign ( this . certificate . privateKey ) ) ;
107+ return Promise . resolve ( sign . sign ( this . credential . privateKey ) ) ;
117108 }
118109
119110 /**
120111 * @inheritDoc
121112 */
122113 public getAccountId ( ) : Promise < string > {
123- return Promise . resolve ( this . certificate . clientEmail ) ;
114+ return Promise . resolve ( this . credential . clientEmail ) ;
124115 }
125116}
126117
@@ -232,12 +223,11 @@ export class IAMSigner implements CryptoSigner {
232223 * @return {CryptoSigner } A CryptoSigner instance.
233224 */
234225export function cryptoSignerFromApp ( app : FirebaseApp ) : CryptoSigner {
235- if ( app . options . credential ) {
236- const cert = tryGetCertificate ( app . options . credential ) ;
237- if ( cert != null && validator . isNonEmptyString ( cert . privateKey ) && validator . isNonEmptyString ( cert . clientEmail ) ) {
238- return new ServiceAccountSigner ( cert ) ;
239- }
226+ const credential = app . options . credential ;
227+ if ( credential instanceof ServiceAccountCredential ) {
228+ return new ServiceAccountSigner ( credential ) ;
240229 }
230+
241231 return new IAMSigner ( new AuthorizedHttpClient ( app ) , app . options . serviceAccountId ) ;
242232}
243233
0 commit comments