-
Notifications
You must be signed in to change notification settings - Fork 392
fix: Throw error on user disabled and check revoked set true #1401
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c5dbbb5
fix: Throw error on user disabled and check revoked set true
xil222 ae944f4
Merge branch 'master' into verifyIdToken
xil222 d7703f0
resolve 2 calls on getUser(), improve tests and lints
xil222 45a7316
Merge branch 'verifyIdToken' of https://github.com/firebase/firebase-…
xil222 d361bf1
Merge branch 'master' into verifyIdToken
xil222 f03333e
remove currentUser.reload
xil222 1aab944
Merge branch 'verifyIdToken' of https://github.com/firebase/firebase-…
xil222 b03bcff
add return
xil222 40a6ab6
Tweak tests
xil222 72f20d5
small fix
xil222 b85651f
Use async and await instead of chain in integration test and change C…
xil222 a8ee801
retry
xil222 c508523
Add special case of authEmulator
xil222 6175c59
fix emulator on issue
xil222 1a687fb
remove firebase-tool version changes
xil222 43e3664
useMockIdToken for unit test
xil222 50c33d8
fix typos
xil222 cf49852
Merge branch 'master' into verifyIdToken
xil222 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -760,6 +760,20 @@ describe('admin.auth', () => { | |
safeDelete(userRecord.uid); | ||
} | ||
}); | ||
|
||
it('A user with user record disabled is unable to sign in', async () => { | ||
const password = 'password'; | ||
const email = '[email protected]'; | ||
return admin.auth().updateUser(updateUser.uid, { disabled : true , password, email }) | ||
.then(() => { | ||
return clientAuth().signInWithEmailAndPassword(email, password); | ||
}) | ||
.then(() => { | ||
throw new Error('Unexpected success'); | ||
}, (error) => { | ||
expect(error).to.have.property('code', 'auth/user-disabled'); | ||
}); | ||
}); | ||
}); | ||
|
||
it('getUser() fails when called with a non-existing UID', () => { | ||
|
@@ -833,53 +847,113 @@ describe('admin.auth', () => { | |
}); | ||
}); | ||
|
||
it('verifyIdToken() fails when called with an invalid token', () => { | ||
return admin.auth().verifyIdToken('invalid-token') | ||
.should.eventually.be.rejected.and.have.property('code', 'auth/argument-error'); | ||
}); | ||
describe('verifyIdToken()', () => { | ||
const uid = generateRandomString(20).toLowerCase(); | ||
const email = uid + '@example.com'; | ||
const password = 'password'; | ||
const userData = { | ||
uid, | ||
email, | ||
emailVerified: false, | ||
password, | ||
}; | ||
|
||
// Create the test user before running this suite of tests. | ||
before(() => { | ||
return admin.auth().createUser(userData); | ||
}); | ||
|
||
if (authEmulatorHost) { | ||
describe('Auth emulator support', () => { | ||
const uid = 'authEmulatorUser'; | ||
before(() => { | ||
return admin.auth().createUser({ | ||
uid, | ||
email: '[email protected]', | ||
password: 'p4ssword', | ||
// Sign out after each test. | ||
afterEach(() => { | ||
return clientAuth().signOut(); | ||
}); | ||
|
||
after(() => { | ||
return safeDelete(uid); | ||
}); | ||
|
||
it('verifyIdToken() fails when called with an invalid token', () => { | ||
return admin.auth().verifyIdToken('invalid-token') | ||
.should.eventually.be.rejected.and.have.property('code', 'auth/argument-error'); | ||
}); | ||
|
||
it('verifyIdToken() fails with checkRevoked set to true and corresponding user disabled', async () => { | ||
const { user } = await clientAuth().signInWithEmailAndPassword(email, password); | ||
expect(user).to.exist; | ||
expect(user!.email).to.equal(email); | ||
|
||
const idToken = await user!.getIdToken(); | ||
let decodedIdToken = await admin.auth().verifyIdToken(idToken, true); | ||
expect(decodedIdToken.uid).to.equal(uid); | ||
expect(decodedIdToken.email).to.equal(email); | ||
|
||
const userRecord = await admin.auth().updateUser(uid, { disabled: true }); | ||
expect(userRecord.uid).to.equal(uid); | ||
expect(userRecord.email).to.equal(email); | ||
expect(userRecord.disabled).to.equal(true); | ||
|
||
try { | ||
// If it is in emulator mode, a user-disabled error will be thrown. | ||
decodedIdToken = await admin.auth().verifyIdToken(idToken, false); | ||
expect(decodedIdToken.uid).to.equal(uid); | ||
} catch (error) { | ||
if (authEmulatorHost) { | ||
expect(error).to.have.property('code', 'auth/user-disabled'); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
|
||
try { | ||
await admin.auth().verifyIdToken(idToken, true); | ||
} catch (error) { | ||
expect(error).to.have.property('code', 'auth/user-disabled'); | ||
} | ||
}); | ||
|
||
if (authEmulatorHost) { | ||
describe('Auth emulator support', () => { | ||
const uid = 'authEmulatorUser'; | ||
before(() => { | ||
return admin.auth().createUser({ | ||
uid, | ||
email: '[email protected]', | ||
password: 'p4ssword', | ||
}); | ||
}); | ||
after(() => { | ||
return admin.auth().deleteUser(uid); | ||
}); | ||
}); | ||
after(() => { | ||
return admin.auth().deleteUser(uid); | ||
}); | ||
|
||
it('verifyIdToken() succeeds when called with an unsigned token', () => { | ||
const unsignedToken = mocks.generateIdToken({ | ||
algorithm: 'none', | ||
audience: projectId, | ||
issuer: 'https://securetoken.google.com/' + projectId, | ||
subject: uid, | ||
it('verifyIdToken() succeeds when called with an unsigned token', () => { | ||
const unsignedToken = mocks.generateIdToken({ | ||
algorithm: 'none', | ||
audience: projectId, | ||
issuer: 'https://securetoken.google.com/' + projectId, | ||
subject: uid, | ||
}); | ||
return admin.auth().verifyIdToken(unsignedToken); | ||
}); | ||
return admin.auth().verifyIdToken(unsignedToken); | ||
}); | ||
|
||
it('verifyIdToken() fails when called with a token with wrong project', () => { | ||
const unsignedToken = mocks.generateIdToken({ algorithm: 'none', audience: 'nosuch' }); | ||
return admin.auth().verifyIdToken(unsignedToken) | ||
.should.eventually.be.rejected.and.have.property('code', 'auth/argument-error'); | ||
}); | ||
it('verifyIdToken() fails when called with a token with wrong project', () => { | ||
const unsignedToken = mocks.generateIdToken({ algorithm: 'none', audience: 'nosuch' }); | ||
return admin.auth().verifyIdToken(unsignedToken) | ||
.should.eventually.be.rejected.and.have.property('code', 'auth/argument-error'); | ||
}); | ||
|
||
it('verifyIdToken() fails when called with a token that does not belong to a user', () => { | ||
const unsignedToken = mocks.generateIdToken({ | ||
algorithm: 'none', | ||
audience: projectId, | ||
issuer: 'https://securetoken.google.com/' + projectId, | ||
subject: 'nosuch', | ||
it('verifyIdToken() fails when called with a token that does not belong to a user', () => { | ||
const unsignedToken = mocks.generateIdToken({ | ||
algorithm: 'none', | ||
audience: projectId, | ||
issuer: 'https://securetoken.google.com/' + projectId, | ||
subject: 'nosuch', | ||
}); | ||
return admin.auth().verifyIdToken(unsignedToken) | ||
.should.eventually.be.rejected.and.have.property('code', 'auth/user-not-found'); | ||
}); | ||
return admin.auth().verifyIdToken(unsignedToken) | ||
.should.eventually.be.rejected.and.have.property('code', 'auth/user-not-found'); | ||
}); | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
describe('Link operations', () => { | ||
const uid = generateRandomString(20).toLowerCase(); | ||
|
@@ -1982,6 +2056,44 @@ describe('admin.auth', () => { | |
.should.eventually.be.rejected.and.have.property('code', 'auth/argument-error'); | ||
}); | ||
}); | ||
|
||
it('fails with checkRevoked set to true and corresponding user disabled', async () => { | ||
const expiresIn = 24 * 60 * 60 * 1000; | ||
const customToken = await admin.auth().createCustomToken(uid, { admin: true, groupId: '1234' }); | ||
const { user } = await clientAuth().signInWithCustomToken(customToken); | ||
expect(user).to.exist; | ||
|
||
const idToken = await user!.getIdToken(); | ||
const decodedIdTokenClaims = await admin.auth().verifyIdToken(idToken); | ||
expect(decodedIdTokenClaims.uid).to.be.equal(uid); | ||
|
||
const sessionCookie = await admin.auth().createSessionCookie(idToken, { expiresIn }); | ||
let decodedIdToken = await admin.auth().verifySessionCookie(sessionCookie, true); | ||
expect(decodedIdToken.uid).to.equal(uid); | ||
|
||
const userRecord = await admin.auth().updateUser(uid, { disabled : true }); | ||
// Ensure disabled field has been updated. | ||
expect(userRecord.uid).to.equal(uid); | ||
expect(userRecord.disabled).to.equal(true); | ||
|
||
try { | ||
// If it is in emulator mode, a user-disabled error will be thrown. | ||
decodedIdToken = await admin.auth().verifySessionCookie(sessionCookie, false); | ||
expect(decodedIdToken.uid).to.equal(uid); | ||
} catch (error) { | ||
if (authEmulatorHost) { | ||
expect(error).to.have.property('code', 'auth/user-disabled'); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
|
||
try { | ||
await admin.auth().verifySessionCookie(sessionCookie, true); | ||
} catch (error) { | ||
expect(error).to.have.property('code', 'auth/user-disabled'); | ||
} | ||
}); | ||
}); | ||
|
||
describe('importUsers()', () => { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.