-
Notifications
You must be signed in to change notification settings - Fork 376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for creating custom tokens without service account credentials #285
Changes from 19 commits
eb5aae1
877fc17
2974090
6ac609b
64bfedd
e4d1b11
9ba16e2
b0a4309
f8bb726
d2c2b89
20a91fa
d31d971
c45ccdf
7e38847
1063d4a
c6dac08
55ce947
9d6e50c
41a313a
c89b96a
b51a8e3
d396206
7602ed8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
|
||
import {UserRecord, CreateRequest, UpdateRequest} from './user-record'; | ||
import {FirebaseApp} from '../firebase-app'; | ||
import {FirebaseTokenGenerator} from './token-generator'; | ||
import {FirebaseTokenGenerator, signerFromApp} from './token-generator'; | ||
import {FirebaseAuthRequestHandler} from './auth-api-request'; | ||
import {AuthClientErrorCode, FirebaseAuthError, ErrorInfo} from '../utils/error'; | ||
import {FirebaseServiceInterface, FirebaseServiceInternalsInterface} from '../firebase-service'; | ||
|
@@ -26,6 +26,7 @@ import { | |
|
||
import * as utils from '../utils/index'; | ||
import * as validator from '../utils/validator'; | ||
import { FirebaseTokenVerifier, newSessionCookieVerifier, newIdTokenVerifier } from './token-verifier'; | ||
|
||
|
||
/** | ||
|
@@ -84,6 +85,8 @@ export class Auth implements FirebaseServiceInterface { | |
|
||
private app_: FirebaseApp; | ||
private tokenGenerator_: FirebaseTokenGenerator; | ||
private idTokenVerifier_: FirebaseTokenVerifier; | ||
private sessionCookieVerifier_: FirebaseTokenVerifier; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
private authRequestHandler: FirebaseAuthRequestHandler; | ||
|
||
/** | ||
|
@@ -99,27 +102,10 @@ export class Auth implements FirebaseServiceInterface { | |
} | ||
|
||
this.app_ = app; | ||
this.tokenGenerator_ = new FirebaseTokenGenerator(signerFromApp(app)); | ||
const projectId = utils.getProjectId(app); | ||
|
||
// TODO (inlined): plumb this into a factory method for tokenGenerator_ once we | ||
// can generate custom tokens from access tokens. | ||
let serviceAccount; | ||
if (typeof app.options.credential.getCertificate === 'function') { | ||
serviceAccount = app.options.credential.getCertificate(); | ||
} | ||
if (serviceAccount) { | ||
// Cert credentials and Application Default Credentials created from a service account file | ||
// provide a certificate we can use to mint custom tokens and verify ID tokens. | ||
this.tokenGenerator_ = new FirebaseTokenGenerator(serviceAccount); | ||
} else if (validator.isNonEmptyString(projectId)) { | ||
// Google infrastructure like GAE, GCE, and GCF store the GCP / Firebase project ID in an | ||
// environment variable that we can use to get verifyIdToken() to work. createCustomToken() | ||
// still won't work since it requires a private key and client email which we do not have. | ||
const cert: any = { | ||
projectId, | ||
}; | ||
this.tokenGenerator_ = new FirebaseTokenGenerator(cert); | ||
} | ||
this.sessionCookieVerifier_ = newSessionCookieVerifier(projectId); | ||
this.idTokenVerifier_ = newIdTokenVerifier(projectId); | ||
// Initialize auth request handler with the app. | ||
this.authRequestHandler = new FirebaseAuthRequestHandler(app); | ||
} | ||
|
@@ -165,14 +151,7 @@ export class Auth implements FirebaseServiceInterface { | |
* verification. | ||
*/ | ||
public verifyIdToken(idToken: string, checkRevoked: boolean = false): Promise<object> { | ||
if (typeof this.tokenGenerator_ === 'undefined') { | ||
throw new FirebaseAuthError( | ||
AuthClientErrorCode.INVALID_CREDENTIAL, | ||
'Must initialize app with a cert credential or set your Firebase project ID as the ' + | ||
'GOOGLE_CLOUD_PROJECT environment variable to call auth().verifyIdToken().', | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you remove this check here and not in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed this one too, and updated the error message in the verifier. |
||
return this.tokenGenerator_.verifyIdToken(idToken) | ||
return this.idTokenVerifier_.verifyJWT(idToken) | ||
.then((decodedIdToken: DecodedIdToken) => { | ||
// Whether to check if the token was revoked. | ||
if (!checkRevoked) { | ||
|
@@ -408,7 +387,7 @@ export class Auth implements FirebaseServiceInterface { | |
'GOOGLE_CLOUD_PROJECT environment variable to call auth().verifySessionCookie().', | ||
); | ||
} | ||
return this.tokenGenerator_.verifySessionCookie(sessionCookie) | ||
return this.sessionCookieVerifier_.verifyJWT(sessionCookie) | ||
.then((decodedIdToken: DecodedIdToken) => { | ||
// Whether to check if the token was revoked. | ||
if (!checkRevoked) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename
newSessionCookieVerifier
andnewIdTokenVerifier
tocreateSessionCookieVerifier
andcreateIdTokenVerifier
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done