16
16
17
17
import { UserRecord , CreateRequest , UpdateRequest } from './user-record' ;
18
18
import { FirebaseApp } from '../firebase-app' ;
19
- import { FirebaseTokenGenerator } from './token-generator' ;
19
+ import { FirebaseTokenGenerator , cryptoSignerFromApp } from './token-generator' ;
20
20
import { FirebaseAuthRequestHandler } from './auth-api-request' ;
21
21
import { AuthClientErrorCode , FirebaseAuthError , ErrorInfo } from '../utils/error' ;
22
22
import { FirebaseServiceInterface , FirebaseServiceInternalsInterface } from '../firebase-service' ;
@@ -26,6 +26,7 @@ import {
26
26
27
27
import * as utils from '../utils/index' ;
28
28
import * as validator from '../utils/validator' ;
29
+ import { FirebaseTokenVerifier , createSessionCookieVerifier , createIdTokenVerifier } from './token-verifier' ;
29
30
30
31
31
32
/**
@@ -82,9 +83,11 @@ export interface SessionCookieOptions {
82
83
export class Auth implements FirebaseServiceInterface {
83
84
public INTERNAL : AuthInternals = new AuthInternals ( ) ;
84
85
85
- private app_ : FirebaseApp ;
86
- private tokenGenerator_ : FirebaseTokenGenerator ;
87
- private authRequestHandler : FirebaseAuthRequestHandler ;
86
+ private readonly app_ : FirebaseApp ;
87
+ private readonly tokenGenerator : FirebaseTokenGenerator ;
88
+ private readonly idTokenVerifier : FirebaseTokenVerifier ;
89
+ private readonly sessionCookieVerifier : FirebaseTokenVerifier ;
90
+ private readonly authRequestHandler : FirebaseAuthRequestHandler ;
88
91
89
92
/**
90
93
* @param {object } app The app for this Auth service.
@@ -99,27 +102,10 @@ export class Auth implements FirebaseServiceInterface {
99
102
}
100
103
101
104
this . app_ = app ;
105
+ this . tokenGenerator = new FirebaseTokenGenerator ( cryptoSignerFromApp ( app ) ) ;
102
106
const projectId = utils . getProjectId ( app ) ;
103
-
104
- // TODO (inlined): plumb this into a factory method for tokenGenerator_ once we
105
- // can generate custom tokens from access tokens.
106
- let serviceAccount ;
107
- if ( typeof app . options . credential . getCertificate === 'function' ) {
108
- serviceAccount = app . options . credential . getCertificate ( ) ;
109
- }
110
- if ( serviceAccount ) {
111
- // Cert credentials and Application Default Credentials created from a service account file
112
- // provide a certificate we can use to mint custom tokens and verify ID tokens.
113
- this . tokenGenerator_ = new FirebaseTokenGenerator ( serviceAccount ) ;
114
- } else if ( validator . isNonEmptyString ( projectId ) ) {
115
- // Google infrastructure like GAE, GCE, and GCF store the GCP / Firebase project ID in an
116
- // environment variable that we can use to get verifyIdToken() to work. createCustomToken()
117
- // still won't work since it requires a private key and client email which we do not have.
118
- const cert : any = {
119
- projectId,
120
- } ;
121
- this . tokenGenerator_ = new FirebaseTokenGenerator ( cert ) ;
122
- }
107
+ this . sessionCookieVerifier = createSessionCookieVerifier ( projectId ) ;
108
+ this . idTokenVerifier = createIdTokenVerifier ( projectId ) ;
123
109
// Initialize auth request handler with the app.
124
110
this . authRequestHandler = new FirebaseAuthRequestHandler ( app ) ;
125
111
}
@@ -143,13 +129,7 @@ export class Auth implements FirebaseServiceInterface {
143
129
* @return {Promise<string> } A JWT for the provided payload.
144
130
*/
145
131
public createCustomToken ( uid : string , developerClaims ?: object ) : Promise < string > {
146
- if ( typeof this . tokenGenerator_ === 'undefined' ) {
147
- throw new FirebaseAuthError (
148
- AuthClientErrorCode . INVALID_CREDENTIAL ,
149
- 'Must initialize app with a cert credential to call auth().createCustomToken().' ,
150
- ) ;
151
- }
152
- return this . tokenGenerator_ . createCustomToken ( uid , developerClaims ) ;
132
+ return this . tokenGenerator . createCustomToken ( uid , developerClaims ) ;
153
133
}
154
134
155
135
/**
@@ -165,14 +145,7 @@ export class Auth implements FirebaseServiceInterface {
165
145
* verification.
166
146
*/
167
147
public verifyIdToken ( idToken : string , checkRevoked : boolean = false ) : Promise < object > {
168
- if ( typeof this . tokenGenerator_ === 'undefined' ) {
169
- throw new FirebaseAuthError (
170
- AuthClientErrorCode . INVALID_CREDENTIAL ,
171
- 'Must initialize app with a cert credential or set your Firebase project ID as the ' +
172
- 'GOOGLE_CLOUD_PROJECT environment variable to call auth().verifyIdToken().' ,
173
- ) ;
174
- }
175
- return this . tokenGenerator_ . verifyIdToken ( idToken )
148
+ return this . idTokenVerifier . verifyJWT ( idToken )
176
149
. then ( ( decodedIdToken : DecodedIdToken ) => {
177
150
// Whether to check if the token was revoked.
178
151
if ( ! checkRevoked ) {
@@ -401,14 +374,7 @@ export class Auth implements FirebaseServiceInterface {
401
374
*/
402
375
public verifySessionCookie (
403
376
sessionCookie : string , checkRevoked : boolean = false ) : Promise < DecodedIdToken > {
404
- if ( typeof this . tokenGenerator_ === 'undefined' ) {
405
- throw new FirebaseAuthError (
406
- AuthClientErrorCode . INVALID_CREDENTIAL ,
407
- 'Must initialize app with a cert credential or set your Firebase project ID as the ' +
408
- 'GOOGLE_CLOUD_PROJECT environment variable to call auth().verifySessionCookie().' ,
409
- ) ;
410
- }
411
- return this . tokenGenerator_ . verifySessionCookie ( sessionCookie )
377
+ return this . sessionCookieVerifier . verifyJWT ( sessionCookie )
412
378
. then ( ( decodedIdToken : DecodedIdToken ) => {
413
379
// Whether to check if the token was revoked.
414
380
if ( ! checkRevoked ) {
0 commit comments