@accounts/two-factor
Advanced tools
Comparing version 0.30.0 to 0.31.0
@@ -6,7 +6,7 @@ "use strict"; | ||
userNotFound: 'User not found', | ||
codeDidNotMatch: "2FA code didn't match", | ||
userTwoFactorNotSet: "2FA not set", | ||
userTwoFactorAlreadySet: "2FA already set", | ||
codeDidNotMatch: `2FA code didn't match`, | ||
userTwoFactorNotSet: `2FA not set`, | ||
userTwoFactorAlreadySet: `2FA already set`, | ||
codeRequired: '2FA code required', | ||
}; | ||
//# sourceMappingURL=errors.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TwoFactor = void 0; | ||
var tslib_1 = require("tslib"); | ||
var speakeasy = tslib_1.__importStar(require("speakeasy")); | ||
var errors_1 = require("./errors"); | ||
var utils_1 = require("./utils"); | ||
var defaultOptions = { | ||
const tslib_1 = require("tslib"); | ||
const speakeasy = tslib_1.__importStar(require("speakeasy")); | ||
const errors_1 = require("./errors"); | ||
const utils_1 = require("./utils"); | ||
const defaultOptions = { | ||
secretLength: 20, | ||
@@ -13,7 +13,6 @@ window: 0, | ||
}; | ||
var TwoFactor = /** @class */ (function () { | ||
function TwoFactor(options) { | ||
if (options === void 0) { options = {}; } | ||
class TwoFactor { | ||
constructor(options = {}) { | ||
this.serviceName = 'two-factor'; | ||
this.options = tslib_1.__assign(tslib_1.__assign({}, defaultOptions), options); | ||
this.options = { ...defaultOptions, ...options }; | ||
} | ||
@@ -23,36 +22,30 @@ /** | ||
*/ | ||
TwoFactor.prototype.setStore = function (store) { | ||
setStore(store) { | ||
this.db = store; | ||
}; | ||
} | ||
/** | ||
* Authenticate a user with a 2fa code | ||
*/ | ||
TwoFactor.prototype.authenticate = function (user, code) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var twoFactorService; | ||
return tslib_1.__generator(this, function (_a) { | ||
if (!code) { | ||
throw new Error(this.options.errors.codeRequired); | ||
} | ||
twoFactorService = utils_1.getUserTwoFactorService(user); | ||
// If user does not have 2fa set return error | ||
if (!twoFactorService) { | ||
throw new Error(this.options.errors.userTwoFactorNotSet); | ||
} | ||
if (!speakeasy.totp.verify({ | ||
secret: twoFactorService.secret.base32, | ||
encoding: 'base32', | ||
token: code, | ||
window: this.options.window, | ||
})) { | ||
throw new Error(this.options.errors.codeDidNotMatch); | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); | ||
}; | ||
async authenticate(user, code) { | ||
if (!code) { | ||
throw new Error(this.options.errors.codeRequired); | ||
} | ||
const twoFactorService = utils_1.getUserTwoFactorService(user); | ||
// If user does not have 2fa set return error | ||
if (!twoFactorService) { | ||
throw new Error(this.options.errors.userTwoFactorNotSet); | ||
} | ||
if (!speakeasy.totp.verify({ | ||
secret: twoFactorService.secret.base32, | ||
encoding: 'base32', | ||
token: code, | ||
window: this.options.window, | ||
})) { | ||
throw new Error(this.options.errors.codeDidNotMatch); | ||
} | ||
} | ||
/** | ||
* Generate a new two factor secret | ||
*/ | ||
TwoFactor.prototype.getNewAuthSecret = function () { | ||
getNewAuthSecret() { | ||
return speakeasy.generateSecret({ | ||
@@ -62,3 +55,3 @@ length: this.options.secretLength, | ||
}); | ||
}; | ||
} | ||
/** | ||
@@ -69,83 +62,60 @@ * Verify the code is correct | ||
*/ | ||
TwoFactor.prototype.set = function (userId, secret, code) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var user, twoFactorService; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!code) { | ||
throw new Error(this.options.errors.codeRequired); | ||
} | ||
return [4 /*yield*/, this.db.findUserById(userId)]; | ||
case 1: | ||
user = _a.sent(); | ||
if (!user) { | ||
throw new Error(this.options.errors.userNotFound); | ||
} | ||
twoFactorService = utils_1.getUserTwoFactorService(user); | ||
// If user already have 2fa return error | ||
if (twoFactorService) { | ||
throw new Error(this.options.errors.userTwoFactorAlreadySet); | ||
} | ||
if (!speakeasy.totp.verify({ | ||
secret: secret.base32, | ||
encoding: 'base32', | ||
token: code, | ||
window: this.options.window, | ||
})) return [3 /*break*/, 3]; | ||
twoFactorService = { | ||
secret: secret, | ||
}; | ||
return [4 /*yield*/, this.db.setService(userId, this.serviceName, twoFactorService)]; | ||
case 2: | ||
_a.sent(); | ||
return [3 /*break*/, 4]; | ||
case 3: throw new Error(this.options.errors.codeDidNotMatch); | ||
case 4: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
async set(userId, secret, code) { | ||
if (!code) { | ||
throw new Error(this.options.errors.codeRequired); | ||
} | ||
const user = await this.db.findUserById(userId); | ||
if (!user) { | ||
throw new Error(this.options.errors.userNotFound); | ||
} | ||
let twoFactorService = utils_1.getUserTwoFactorService(user); | ||
// If user already have 2fa return error | ||
if (twoFactorService) { | ||
throw new Error(this.options.errors.userTwoFactorAlreadySet); | ||
} | ||
if (speakeasy.totp.verify({ | ||
secret: secret.base32, | ||
encoding: 'base32', | ||
token: code, | ||
window: this.options.window, | ||
})) { | ||
twoFactorService = { | ||
secret, | ||
}; | ||
await this.db.setService(userId, this.serviceName, twoFactorService); | ||
} | ||
else { | ||
throw new Error(this.options.errors.codeDidNotMatch); | ||
} | ||
} | ||
/** | ||
* Remove two factor for a user | ||
*/ | ||
TwoFactor.prototype.unset = function (userId, code) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var user, twoFactorService; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!code) { | ||
throw new Error(this.options.errors.codeRequired); | ||
} | ||
return [4 /*yield*/, this.db.findUserById(userId)]; | ||
case 1: | ||
user = _a.sent(); | ||
if (!user) { | ||
throw new Error(this.options.errors.userNotFound); | ||
} | ||
twoFactorService = utils_1.getUserTwoFactorService(user); | ||
// If user does not have 2fa set return error | ||
if (!twoFactorService) { | ||
throw new Error(this.options.errors.userTwoFactorNotSet); | ||
} | ||
if (speakeasy.totp.verify({ | ||
secret: twoFactorService.secret.base32, | ||
encoding: 'base32', | ||
token: code, | ||
window: this.options.window, | ||
})) { | ||
this.db.unsetService(userId, this.serviceName); | ||
} | ||
else { | ||
throw new Error(this.options.errors.codeDidNotMatch); | ||
} | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
return TwoFactor; | ||
}()); | ||
async unset(userId, code) { | ||
if (!code) { | ||
throw new Error(this.options.errors.codeRequired); | ||
} | ||
const user = await this.db.findUserById(userId); | ||
if (!user) { | ||
throw new Error(this.options.errors.userNotFound); | ||
} | ||
const twoFactorService = utils_1.getUserTwoFactorService(user); | ||
// If user does not have 2fa set return error | ||
if (!twoFactorService) { | ||
throw new Error(this.options.errors.userTwoFactorNotSet); | ||
} | ||
if (speakeasy.totp.verify({ | ||
secret: twoFactorService.secret.base32, | ||
encoding: 'base32', | ||
token: code, | ||
window: this.options.window, | ||
})) { | ||
this.db.unsetService(userId, this.serviceName); | ||
} | ||
else { | ||
throw new Error(this.options.errors.codeDidNotMatch); | ||
} | ||
} | ||
} | ||
exports.TwoFactor = TwoFactor; | ||
//# sourceMappingURL=two-factor.js.map |
@@ -7,3 +7,3 @@ "use strict"; | ||
*/ | ||
exports.getUserTwoFactorService = function (user) { | ||
exports.getUserTwoFactorService = (user) => { | ||
var _a, _b; | ||
@@ -10,0 +10,0 @@ return (_b = (_a = user.services) === null || _a === void 0 ? void 0 : _a['two-factor']) !== null && _b !== void 0 ? _b : null; |
{ | ||
"name": "@accounts/two-factor", | ||
"version": "0.30.0", | ||
"version": "0.31.0", | ||
"license": "MIT", | ||
@@ -29,3 +29,3 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@accounts/types": "^0.30.0", | ||
"@accounts/types": "^0.31.0", | ||
"@types/speakeasy": "2.0.2", | ||
@@ -40,3 +40,3 @@ "speakeasy": "^2.0.0", | ||
}, | ||
"gitHead": "a883d3b90c481e9c73a7655ef2aadac092fc6b0e" | ||
"gitHead": "8938531dd59f40e14e029c8785433acb455bd58c" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19770
426
+ Added@accounts/types@0.31.1(transitive)
- Removed@accounts/types@0.30.0(transitive)
Updated@accounts/types@^0.31.0