Skip to content

Commit

Permalink
Delete unused files and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcrammer committed Apr 2, 2024
1 parent 06aad23 commit e8fb2fd
Show file tree
Hide file tree
Showing 70 changed files with 1,283 additions and 3,795 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,5 @@ dist
.pnp.*

# Keys
*.pem
*.pem
.vercel
126 changes: 52 additions & 74 deletions api/classes/authFactorClass.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _AuthFactor = _interopRequireDefault(require("../models/AuthFactor"));
var _User = _interopRequireDefault(require("../models/User"));
var _CodedError = _interopRequireDefault(require("../config/CodedError"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
import AuthFactorModel from "../models/AuthFactor"
import User from "../models/User"
import CodedError from "../config/CodedError"

/**
* @typedef {Object} AuthFactorType
* Options
Expand All @@ -23,6 +17,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
* @property {AuthFactorType} factor - The type of auth factor (TOTP, SMS, etc.)
* @property {string} secret - The secret that is used to generate the TOTP
*/

/**
* Handles the methods for interacitng with the AuthFactor data
*/
Expand All @@ -39,15 +34,16 @@ class AuthFactor {
*/
async createRecord(userId, factor, secret) {
try {
const authFactor = await _AuthFactor.default.create({
const authFactor = await AuthFactorModel.create({
userId,
factor,
secret,
verified: false
});
return authFactor.dataValues;
verified: false,
})

return authFactor.dataValues
} catch (error) {
throw new _CodedError.default(error.message, error.status ?? 500, error.location ?? "AUTHFACTOR|01");
throw new CodedError(error.message, error.status ?? 500, error.location ?? "AUTHFACTOR|01")
}
}

Expand All @@ -62,30 +58,23 @@ class AuthFactor {
*/
async activateRecord(id) {
try {
const authFactor = await _AuthFactor.default.findOne({
where: {
id
}
});
if (!authFactor) throw new _CodedError.default("Auth factor not found", 404, "AUTHFACTOR|02");
const authFactor = await AuthFactorModel.findOne({ where: { id } })
if (!authFactor) throw new CodedError("Auth factor not found", 404, "AUTHFACTOR|02")

await authFactor.update({
verified: true,
verifiedAt: new Date()
});
verifiedAt: new Date(),
})

// update user mfa flag
const user = await _User.default.findOne({
where: {
id: authFactor.userId
}
});
if (!user) throw new _CodedError.default("User not found", 404, "AUTHFACTOR|03");
await user.update({
mfa: true
});
return true;
const user = await User.findOne({ where: { id: authFactor.userId } })
if (!user) throw new CodedError("User not found", 404, "AUTHFACTOR|03")

await user.update({ mfa: true })

return true
} catch (error) {
throw new _CodedError.default(error.message, error.status ?? 500, error.location ?? "AUTHFACTOR|03");
throw new CodedError(error.message, error.status ?? 500, error.location ?? "AUTHFACTOR|03")
}
}

Expand All @@ -98,28 +87,22 @@ class AuthFactor {
*/
async deleteRecord(id) {
try {
const authFactor = await _AuthFactor.default.findOne({
where: {
id
}
});
if (!authFactor) throw new _CodedError.default("Auth factor not found", 404, "AUTHFACTOR|04");
await authFactor.destroy();
const user = await _User.default.findOne({
where: {
id: authFactor.userId
}
});
if (!user) throw new _CodedError.default("User not found", 404, "AUTHFACTOR|05");
const userAuthFactors = await user.getAuthFactors();
const authFactor = await AuthFactorModel.findOne({ where: { id } })
if (!authFactor) throw new CodedError("Auth factor not found", 404, "AUTHFACTOR|04")

await authFactor.destroy()

const user = await User.findOne({ where: { id: authFactor.userId } })
if (!user) throw new CodedError("User not found", 404, "AUTHFACTOR|05")

const userAuthFactors = await user.getAuthFactors()
if (userAuthFactors.length === 0) {
await user.update({
mfa: false
});
await user.update({ mfa: false })
}
return true;

return true
} catch (error) {
throw new _CodedError.default(error.message, error.status ?? 500, error.location ?? "AUTHFACTOR|05");
throw new CodedError(error.message, error.status ?? 500, error.location ?? "AUTHFACTOR|05")
}
}

Expand All @@ -132,28 +115,23 @@ class AuthFactor {
*/
async disableMFA(userId) {
try {
const user = await _User.default.findOne({
where: {
id: userId
}
});
if (!user) throw new _CodedError.default("User not found", 404, "AUTHFACTOR|06");
await user.update({
mfa: false
});
const authFactors = await _AuthFactor.default.getAll({
where: {
userId
}
});
if (!authFactors) throw new _CodedError.default("Auth factors not found", 404, "AUTHFACTOR|07");
authFactors.forEach(async authFactor => {
await authFactor.destroy();
});
return true;
const user = await User.findOne({ where: { id: userId } })
if (!user) throw new CodedError("User not found", 404, "AUTHFACTOR|06")

await user.update({ mfa: false })

const authFactors = await AuthFactorModel.getAll({ where: { userId } })
if (!authFactors) throw new CodedError("Auth factors not found", 404, "AUTHFACTOR|07")

authFactors.forEach(async (authFactor) => {
await authFactor.destroy()
})

return true
} catch (error) {
throw new _CodedError.default(error.message, error.status ?? 500, error.location ?? "AUTHFACTOR|08");
throw new CodedError(error.message, error.status ?? 500, error.location ?? "AUTHFACTOR|08")
}
}
}
var _default = exports.default = AuthFactor;

export default AuthFactor
86 changes: 32 additions & 54 deletions api/classes/capabilitiesClass.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
"use strict";
import CapabilityModel from "../models/Capability"
import RoleModel from "../models/Role"
import CodedError from "../config/CodedError"

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _Capability = _interopRequireDefault(require("../models/Capability"));
var _Role = _interopRequireDefault(require("../models/Role"));
var _CodedError = _interopRequireDefault(require("../config/CodedError"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class Capability {
/**
* Get all capabilities that match the conditions
Expand All @@ -17,12 +11,10 @@ class Capability {
*/
async getCapabilities(conditions = {}) {
try {
const capabilities = await _Capability.default.findAll({
where: conditions
});
return capabilities;
const capabilities = await CapabilityModel.findAll({ where: conditions })
return capabilities
} catch (error) {
throw new _CodedError.default(error.message, 400, "CAPABILITY|00");
throw new CodedError(error.message, 400, "CAPABILITY|00")
}
}

Expand All @@ -31,12 +23,10 @@ class Capability {
*/
async getCapability(conditions = {}) {
try {
const capability = await _Capability.default.findOne({
where: conditions
});
return capability;
const capability = await CapabilityModel.findOne({ where: conditions })
return capability
} catch (error) {
throw new _CodedError.default(error.message, 400, "CAPABILITY|01");
throw new CodedError(error.message, 400, "CAPABILITY|01")
}
}

Expand All @@ -50,24 +40,19 @@ class Capability {
* @returns {Promise<Capability>}
*/
async createCapability(data = {}, options = {}) {
const {
name,
description
} = data;
const {
roles
} = options;
const { name, description } = data
const { roles } = options

try {
const capability = await _Capability.default.create({
name,
description
});
const capability = await CapabilityModel.create({ name, description })

if (roles) {
await capability.setRoles(roles);
await capability.setRoles(roles)
}
return capability;

return capability
} catch (error) {
throw new _CodedError.default(error.message, 400, "CAPABILITY|02");
throw new CodedError(error.message, 400, "CAPABILITY|02")
}
}

Expand All @@ -81,30 +66,23 @@ class Capability {
*/
async deleteCapabilities(capabilities) {
try {
if (!capabilities) throw new _CodedError.default("No capabilities provided", 400, "CAPABILITY|02");
if (!Array.isArray(capabilities)) throw new _CodedError.default("Capabilities must be an array", 400, "CAPABILITY|03");
await _Capability.default.destroy({
where: {
name: capabilities
}
});
if (!capabilities) throw new CodedError("No capabilities provided", 400, "CAPABILITY|02")
if (!Array.isArray(capabilities)) throw new CodedError("Capabilities must be an array", 400, "CAPABILITY|03")
await CapabilityModel.destroy({ where: { name: capabilities } })

// Remove the capabilities from all roles
const roles = await _Role.default.findAll({
include: [{
model: _Capability.default,
as: "capabilities"
}]
});
roles.forEach(async role => {
const roleCapabilities = role.capabilities.map(capability => capability.name);
const updatedCapabilities = roleCapabilities.filter(capability => !capabilities.includes(capability));
await role.setCapabilities(updatedCapabilities);
});
return true;
const roles = await RoleModel.findAll({ include: [{ model: CapabilityModel, as: "capabilities" }] })
roles.forEach(async (role) => {
const roleCapabilities = role.capabilities.map((capability) => capability.name)
const updatedCapabilities = roleCapabilities.filter((capability) => !capabilities.includes(capability))
await role.setCapabilities(updatedCapabilities)
})

return true
} catch (error) {
throw new _CodedError.default(error.message, 400, "CAPABILITY|03");
throw new CodedError(error.message, 400, "CAPABILITY|03")
}
}
}
var _default = exports.default = Capability;

export default Capability
Loading

0 comments on commit e8fb2fd

Please sign in to comment.