fix(keys): return proper error when failing to create duplicate recovery key
This commit is contained in:
Родитель
e1dbf18762
Коммит
4954b69d2d
|
@ -293,6 +293,8 @@ for `code` and `errno` are:
|
|||
Recovery key is not valid.
|
||||
* `code: 400, errno: 160`:
|
||||
This request requires two step authentication enabled on your account.
|
||||
* `code: 400, errno: 161`:
|
||||
Recovery key already exists.
|
||||
* `code: 503, errno: 201`:
|
||||
Service unavailable
|
||||
* `code: 503, errno: 202`:
|
||||
|
|
|
@ -1299,6 +1299,13 @@ module.exports = (
|
|||
log.trace({op: 'DB.createRecoveryKey', uid})
|
||||
|
||||
return this.pool.post(SAFE_URLS.createRecoveryKey, { uid }, { recoveryKeyId, recoveryData })
|
||||
.catch((err) => {
|
||||
if (isRecordAlreadyExistsError(err)) {
|
||||
throw error.recoveryKeyExists()
|
||||
}
|
||||
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
SAFE_URLS.getRecoveryKey = new SafeUrl(
|
||||
|
|
10
lib/error.js
10
lib/error.js
|
@ -73,6 +73,7 @@ var ERRNO = {
|
|||
RECOVERY_KEY_NOT_FOUND: 158,
|
||||
RECOVERY_KEY_INVALID: 159,
|
||||
TOTP_REQUIRED: 160,
|
||||
RECOVERY_KEY_EXISTS: 161,
|
||||
|
||||
SERVER_BUSY: 201,
|
||||
FEATURE_NOT_ENABLED: 202,
|
||||
|
@ -829,6 +830,15 @@ AppError.totpRequired = () => {
|
|||
})
|
||||
}
|
||||
|
||||
AppError.recoveryKeyExists = () => {
|
||||
return new AppError({
|
||||
code: 400,
|
||||
error: 'Bad Request',
|
||||
errno: ERRNO.RECOVERY_KEY_EXISTS,
|
||||
message: 'Recovery key already exists.'
|
||||
})
|
||||
}
|
||||
|
||||
AppError.backendServiceFailure = (service, operation) => {
|
||||
return new AppError({
|
||||
code: 500,
|
||||
|
|
|
@ -153,6 +153,18 @@ describe('remote recovery keys', function () {
|
|||
})
|
||||
})
|
||||
|
||||
it('should fail to create recovery key when one already exists', () => {
|
||||
return createMockRecoveryKey(client.uid, keys.kB)
|
||||
.then((result) => {
|
||||
recoveryKeyId = result.recoveryKeyId
|
||||
recoveryData = result.recoveryData
|
||||
return client.createRecoveryKey(result.recoveryKeyId, result.recoveryData)
|
||||
.then(assert.fail, (err) => {
|
||||
assert.equal(err.errno, 161, 'correct errno')
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
describe('check recovery key status', () => {
|
||||
describe('with sessionToken', () => {
|
||||
it('should return true if recovery key exists', () => {
|
||||
|
|
Загрузка…
Ссылка в новой задаче