Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions middleware/enhancedAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
const crypto = require('crypto')

class EnhancedAuthMiddleware {
constructor(config = {}) {
constructor (config = {}) {
this.jwtSecret = config.jwtSecret || process.env.JWT_SECRET
this.jwtRefreshSecret = config.jwtRefreshSecret || process.env.JWT_REFRESH_SECRET
this.jwtRefreshSecret =
config.jwtRefreshSecret || process.env.JWT_REFRESH_SECRET

Check notice on line 14 in middleware/enhancedAuth.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

middleware/enhancedAuth.js#L14

Missing semicolon.
this.tokenBlacklist = new Set()
this.roles = new Map()
this.initializeRoles()
}

initializeRoles() {
initializeRoles () {
this.roles.set('admin', {
permissions: [
'system:read',
Expand All @@ -42,18 +43,15 @@
})

this.roles.set('user', {
permissions: [
'projects:read',
'projects:write:own'
],
permissions: ['projects:read', 'projects:write:own'],
level: 10
})
}

/**
* Middleware to verify JWT token
*/
verifyToken() {
verifyToken () {
return (req, res, next) => {
try {
const authHeader = req.headers.authorization
Expand Down Expand Up @@ -94,7 +92,8 @@
next()
} catch (error) {
const statusCode = error.name === 'TokenExpiredError' ? 401 : 401
const code = error.name === 'TokenExpiredError' ? 'AUTH_EXPIRED' : 'AUTH_INVALID'
const code =
error.name === 'TokenExpiredError' ? 'AUTH_EXPIRED' : 'AUTH_INVALID'

Check notice on line 96 in middleware/enhancedAuth.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

middleware/enhancedAuth.js#L96

Missing semicolon.

return res.status(statusCode).json({
error: error.message || 'Authentication failed',
Expand All @@ -107,7 +106,7 @@
/**
* Middleware to enforce role-based access
*/
requireRole(...allowedRoles) {
requireRole (...allowedRoles) {
return (req, res, next) => {
if (!req.user) {
return res.status(401).json({
Expand Down Expand Up @@ -140,7 +139,7 @@
/**
* Middleware to enforce permissions
*/
requirePermission(...requiredPermissions) {
requirePermission (...requiredPermissions) {
return (req, res, next) => {
if (!req.user) {
return res.status(401).json({
Expand All @@ -153,7 +152,7 @@
const roleInfo = this.roles.get(userRole)
const userPermissions = roleInfo?.permissions || []

const hasPermission = requiredPermissions.some(perm =>
const hasPermission = requiredPermissions.some((perm) =>
userPermissions.includes(perm)
)

Expand All @@ -178,7 +177,7 @@
/**
* Generate access and refresh tokens
*/
generateTokens(user) {
generateTokens (user) {
const accessToken = jwt.sign(
{
id: user.id,
Expand Down Expand Up @@ -213,14 +212,14 @@
/**
* Revoke a token (add to blacklist)
*/
revokeToken(token) {
revokeToken (token) {
this.tokenBlacklist.add(token)
}

/**
* Verify refresh token and generate new access token
*/
refreshAccessToken(refreshToken) {
refreshAccessToken (refreshToken) {
try {
const decoded = jwt.verify(refreshToken, this.jwtRefreshSecret, {
algorithms: ['HS256']
Expand Down Expand Up @@ -249,14 +248,14 @@
/**
* Get RBAC role permissions
*/
getRolePermissions(role) {
getRolePermissions (role) {
return this.roles.get(role)?.permissions || []
}

/**
* Check if user has specific permission
*/
hasPermission(userRole, permission) {
hasPermission (userRole, permission) {
const permissions = this.getRolePermissions(userRole)
return permissions.includes(permission)
}
Expand Down
Loading