Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

@accounts/two-factor

Package Overview
Dependencies
Maintainers
6
Versions
122
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@accounts/two-factor - npm Package Compare versions

Comparing version
0.30.0
to
0.31.0
+3
-3
lib/errors.js

@@ -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

@@ -1,1 +0,1 @@

{"version":3,"file":"two-factor.js","sourceRoot":"","sources":["../src/two-factor.ts"],"names":[],"mapp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AC,CAAC;QAChD,IAAI,CAAC,IAAI,EAAE;YACT,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;SACnD;QACD,MAAM,gBAAgB,GAAG,+BAAuB,CAAC,IAAI,CAAC,CAAC;QACvD,6CAA6C;QAC7C,IAAI,CAAC,gBAAgB,EAAE;YACrB,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC;SAC1D;QACD,IACE,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC;YACpB,MAAM,EAAE,gBAAgB,CAAC,MAAM,CAAC,MAAM;YACtC,QAAQ,EAAE,QAAQ;YAClB,KAAK,EAAE,IAAI;YACX,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM;SAC5B,CAAC,EACF;YACA,IAAI,CAAC,EAAE,CAAC,YAAY,CAAC,MAAM,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;SAChD;aAAM;YACL,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;SACtD;IACH,CAAC;CACF;AAtHD,8BAsHC"}

@@ -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;

@@ -1,1 +0,1 @@

{"version":3,"file":"user.js","sourceRoot":"","sources":["../../src/utils/user.ts"],"names":[],"mappings":";;;AAGA;;GAEG;AACU,QAAA,uBAAuB,GAAG,UAAC,IAAU;;IAChD,mBAAO,IAAI,CAAC,QAAQ,0CAAG,YAAY,oCAAK,IAAI,CAAC;AAC/C,CAAC,CAAC"}
{"version":3,"file":"user.js","sourceRoot":"","sources":["../../src/utils/user.ts"],"names":[],"mappings":";;;AAGA;;GAEG;AACU,QAAA,uBAAuB,GAAG,CAAC,IAAU,EAA2B,EAAE;;IAC7E,mBAAO,IAAI,CAAC,QAAQ,0CAAG,YAAY,oCAAK,IAAI,CAAC;AAC/C,CAAC,CAAC"}
{
"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"
}