Skip to content
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

fix: Throw error on user disabled and check revoked set true #1401

Merged
merged 18 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 22 additions & 19 deletions src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,20 @@ export class BaseAuth<T extends AbstractAuthRequestHandler> implements BaseAuthI
}

/**
* Verifies a JWT auth token. Returns a Promise with the tokens claims. Rejects
* the promise if the token could not be verified.
* If checkRevoked is set to true, first verifies whether the corresponding userinfo.disabled
* is true:
* Verifies a JWT auth token. Returns a Promise with the tokens claims.
xil222 marked this conversation as resolved.
Show resolved Hide resolved
* Rejects the promise if the token could not be verified.
xil222 marked this conversation as resolved.
Show resolved Hide resolved
* If checkRevoked is set to true, first verifies whether the corresponding
xil222 marked this conversation as resolved.
Show resolved Hide resolved
* user is disabled.
* If yes, an auth/user-disabled error is thrown.
* If no, verifies if the session corresponding to the ID token was revoked.
* If the corresponding user's session was invalidated, an auth/id-token-revoked error is thrown.
* If the corresponding user's session was invalidated, an
* auth/id-token-revoked error is thrown.
* If not specified the check is not applied.
*
* @param {string} idToken The JWT to verify.
* @param {boolean=} checkRevoked Whether to check if the ID token is revoked.
* @return {Promise<DecodedIdToken>} A Promise that will be fulfilled after a successful
* verification.
* @return {Promise<DecodedIdToken>} A Promise that will be fulfilled after
xil222 marked this conversation as resolved.
Show resolved Hide resolved
* a successful verification.
*/
public verifyIdToken(idToken: string, checkRevoked = false): Promise<DecodedIdToken> {
const isEmulator = useEmulator();
Expand Down Expand Up @@ -509,28 +510,30 @@ export class BaseAuth<T extends AbstractAuthRequestHandler> implements BaseAuthI
}

/**
* Verifies a Firebase session cookie. Returns a Promise with the tokens claims. Rejects
* the promise if the cookie could not be verified.
* If checkRevoked is set to true, first verifies whether the corresponding userinfo.disabled
* is true:
* Verifies a Firebase session cookie. Returns a Promise with the tokens claims.
xil222 marked this conversation as resolved.
Show resolved Hide resolved
* Rejects the promise if the cookie could not be verified.
* If checkRevoked is set to true, first verifies whether the corresponding
xil222 marked this conversation as resolved.
Show resolved Hide resolved
* user is true:
xil222 marked this conversation as resolved.
Show resolved Hide resolved
* If yes, an auth/user-disabled error is thrown.
* If no, verifies if the session corresponding to the session cookie was revoked.
* If the corresponding user's session was invalidated, an auth/session-cookie-revoked error
* is thrown.
* If no, verifies if the session corresponding to the session cookie was
* revoked.
* If the corresponding user's session was invalidated, an
* auth/session-cookie-revoked error is thrown.
* If not specified the check is not performed.
*
* @param {string} sessionCookie The session cookie to verify.
* @param {boolean=} checkRevoked Whether to check if the session cookie is revoked.
* @return {Promise<DecodedIdToken>} A Promise that will be fulfilled after a successful
* verification.
* @param {boolean=} checkRevoked Whether to check if the session cookie is
* revoked.
* @return {Promise<DecodedIdToken>} A Promise that will be fulfilled after
* a successful verification.
*/
public verifySessionCookie(
sessionCookie: string, checkRevoked = false): Promise<DecodedIdToken> {
const isEmulator = useEmulator();
return this.sessionCookieVerifier.verifyJWT(sessionCookie, isEmulator)
.then((decodedIdToken: DecodedIdToken) => {
// Whether to check if the cookie was revoked.
if (checkRevoked || isEmulator) {
if (checkRevoked || isEmulator) {
return this.verifyDecodedJWTNotRevokedOrDisabled(
decodedIdToken,
AuthClientErrorCode.SESSION_COOKIE_REVOKED);
Expand Down Expand Up @@ -748,7 +751,7 @@ export class BaseAuth<T extends AbstractAuthRequestHandler> implements BaseAuthI
if (user.disabled) {
throw new FirebaseAuthError(
AuthClientErrorCode.USER_DISABLED,
'The user account has been disabled by an administrator');
'The user record is disabled.');
}
// If no tokens valid after time available, token is not revoked.
if (user.tokensValidAfterTime) {
Expand Down
14 changes: 11 additions & 3 deletions test/integration/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ describe('admin.auth', () => {
return admin.auth().verifyIdToken(currentIdToken, true);
})
.then((decodedIdToken) => {
expect(decodedIdToken.sub).to.equal(uid);
xil222 marked this conversation as resolved.
Show resolved Hide resolved
expect(decodedIdToken.uid).to.equal(uid);
expect(decodedIdToken.email).to.equal(email);
return admin.auth().updateUser(uid, { disabled: true });
Expand All @@ -901,7 +902,10 @@ describe('admin.auth', () => {
expect(userRecord.disabled).to.equal(true);
return admin.auth().verifyIdToken(currentIdToken, false);
})
.then(() => {
.then((decodedIdToken) => {
expect(decodedIdToken.sub).to.equal(uid);
expect(decodedIdToken.uid).to.equal(uid);
expect(decodedIdToken.email).to.equal(email);
return admin.auth().verifyIdToken(currentIdToken, true);
})
.then(() => {
Expand Down Expand Up @@ -2081,15 +2085,19 @@ describe('admin.auth', () => {
currentSessioncookie = sessionCookie;
return admin.auth().verifySessionCookie(sessionCookie, true);
})
.then(() => {
.then((decodedIdToken) => {
xil222 marked this conversation as resolved.
Show resolved Hide resolved
expect(decodedIdToken.sub).to.equal(uid);
expect(decodedIdToken.uid).to.equal(uid);
return admin.auth().updateUser(uid, { disabled : true });
})
.then((userRecord) => {
// Ensure disabled field has been updated.
expect(userRecord.uid).to.equal(uid);
expect(userRecord.disabled).to.equal(true);
return admin.auth().verifySessionCookie(currentSessioncookie, false);
}).then(() => {
}).then((decodedIdToken) => {
expect(decodedIdToken.sub).to.equal(uid);
expect(decodedIdToken.uid).to.equal(uid);
return admin.auth().verifySessionCookie(currentSessioncookie, true);
})
.then(() => {
Expand Down
72 changes: 69 additions & 3 deletions test/unit/auth/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ AUTH_CONFIGS.forEach((testConfig) => {
.resolves(expectedUserRecordDisabled);
expect(expectedUserRecordDisabled.disabled).to.be.equal(true);
stubs.push(getUserStub);
return auth.verifyIdToken(mockSessionCookie, true)
return auth.verifySessionCookie(mockSessionCookie, true)
.then(() => {
throw new Error('Unexpected success');
}, (error) => {
Expand Down Expand Up @@ -3534,9 +3534,9 @@ AUTH_CONFIGS.forEach((testConfig) => {
});

describe('auth emulator support', () => {

let mockAuth = testConfig.init(mocks.app());
const userRecord = getValidUserRecord(getValidGetAccountInfoResponse());
const expectedAccountInfoResponse = getValidGetAccountInfoResponse();
const userRecord = getValidUserRecord(expectedAccountInfoResponse);
const validSince = new Date(userRecord.tokensValidAfterTime!);

const stubs: sinon.SinonStub[] = [];
Expand Down Expand Up @@ -3595,6 +3595,39 @@ AUTH_CONFIGS.forEach((testConfig) => {
});
});

it('verifyIdToken() should reject user disabled before ID tokens revoked', () => {
xil222 marked this conversation as resolved.
Show resolved Hide resolved
// One second before validSince.
const expectedAccountInfoResponseUserDisabled = Object.assign({}, expectedAccountInfoResponse);
expectedAccountInfoResponseUserDisabled.users[0].disabled = true;
const expectedUserRecordDisabled = getValidUserRecord(expectedAccountInfoResponseUserDisabled);
const validSince = new Date(expectedUserRecordDisabled.tokensValidAfterTime!);
const oneSecBeforeValidSince = Math.floor(validSince.getTime() / 1000 - 1);
const getUserStub = sinon.stub(testConfig.Auth.prototype, 'getUser')
.resolves(expectedUserRecordDisabled);
expect(expectedUserRecordDisabled.disabled).to.be.equal(true);
stubs.push(getUserStub);

const unsignedToken = mocks.generateIdToken({
algorithm: 'none',
subject: expectedUserRecordDisabled.uid,
}, {
iat: oneSecBeforeValidSince,
auth_time: oneSecBeforeValidSince, // eslint-disable-line @typescript-eslint/camelcase
});

// verifyIdToken should force checking revocation in emulator mode,
// even if checkRevoked=false.
return mockAuth.verifyIdToken(unsignedToken, false)
.then(() => {
throw new Error('Unexpected success');
}, (error) => {
// Confirm expected error returned.
expect(error).to.have.property('code', 'auth/user-disabled');
// Confirm underlying API called with expected parameters.
expect(getUserStub).to.have.been.calledOnce.and.calledWith(expectedUserRecordDisabled.uid);
});
});

it('verifySessionCookie() should reject revoked session cookies', () => {
const uid = userRecord.uid;
// One second before validSince.
Expand Down Expand Up @@ -3625,6 +3658,39 @@ AUTH_CONFIGS.forEach((testConfig) => {
});
});


it('verifySessionCookie() should reject user disabled before ID tokens revoked', () => {
xil222 marked this conversation as resolved.
Show resolved Hide resolved
// One second before validSince.
const expectedAccountInfoResponseUserDisabled = Object.assign({}, expectedAccountInfoResponse);
expectedAccountInfoResponseUserDisabled.users[0].disabled = true;
const expectedUserRecordDisabled = getValidUserRecord(expectedAccountInfoResponseUserDisabled);
const validSince = new Date(expectedUserRecordDisabled.tokensValidAfterTime!);
const oneSecBeforeValidSince = Math.floor(validSince.getTime() / 1000 - 1);
const getUserStub = sinon.stub(testConfig.Auth.prototype, 'getUser')
.resolves(expectedUserRecordDisabled);
expect(expectedUserRecordDisabled.disabled).to.be.equal(true);
stubs.push(getUserStub);

const unsignedToken = mocks.generateIdToken({
algorithm: 'none',
subject: expectedUserRecordDisabled.uid,
issuer: 'https://session.firebase.google.com/' + mocks.projectId,
}, {
iat: oneSecBeforeValidSince,
auth_time: oneSecBeforeValidSince, // eslint-disable-line @typescript-eslint/camelcase
});

return auth.verifySessionCookie(unsignedToken, true)
.then(() => {
throw new Error('Unexpected success');
}, (error) => {
// Confirm underlying API called with expected parameters.
expect(getUserStub).to.have.been.calledOnce.and.calledWith(expectedUserRecordDisabled.uid);
// Confirm expected error returned.
expect(error).to.have.property('code', 'auth/user-disabled');
});
});

it('verifyIdToken() rejects an unsigned token if auth emulator is unreachable', async () => {
const unsignedToken = mocks.generateIdToken({
algorithm: 'none'
Expand Down