A RESTful API built with Express.js and MongoDB for managing company IT assets with role-based access control, JWT authentication, and comprehensive security hardening.
This project demonstrates secure backend API development practices, implementing authentication, authorization, input validation, and defense-in-depth security measures. Built as a learning project to master modern backend development and security best practices.
- User Authentication - Signup and login with bcrypt password hashing
- JWT Authorization - Stateless token-based authentication
- Role-Based Access Control - Admin and employee roles with different permissions
- Asset Management - Full CRUD operations for IT assets (laptops, monitors, routers, etc.)
- User Promotion - Admins can promote employees to admin role
- Password Hashing - bcrypt with 12 salt rounds (one-way hashing)
- JWT Tokens - 1-hour expiry, payload contains only userId and role
- Security Headers - Helmet middleware sets secure HTTP headers
- Rate Limiting - Login endpoint limited to 3 attempts per 15 minutes
- Input Sanitization - Prevents NoSQL injection attacks
- Input Validation - Validates username, password, and asset fields
- Defense in Depth - Multiple security layers at every level
| Technology | Purpose |
|---|---|
| Node.js | JavaScript runtime environment |
| Express.js 5.2 | Web application framework |
| MongoDB | NoSQL document database |
| Mongoose | MongoDB ODM with schema validation |
| bcrypt | Password hashing library |
| jsonwebtoken | JWT creation and verification |
| helmet | Security HTTP headers |
| express-rate-limit | Rate limiting middleware |
| express-mongo-sanitize | NoSQL injection prevention |
| dotenv | Environment variable management |
- Node.js 18+ installed
- MongoDB 7.0+ running locally or Atlas connection string
- npm or yarn package manager
-
Clone the repository
git clone https://github.com/aheedhul/SecureAssetManagementAPI.git cd SecureAssetManagementAPI -
Install dependencies
npm install
-
Configure environment variables
Create a
.envfile in the root directory:JWT_SECRET=your-super-secret-jwt-key-here MONGO_URI=mongodb://localhost:27017/secure PORT=3000
-
Start the server
npm run dev
Server will run on
http://localhost:3000
| Method | Endpoint | Auth | Role | Description |
|---|---|---|---|---|
| POST | /auth/signup |
β | - | Register new user (default: employee) |
| POST | /auth/login |
β | - | Authenticate and receive JWT |
| GET | /auth/profile |
β | Any | Get current user info |
| GET | /auth/admin |
β | Admin | Access admin dashboard |
| PUT | /auth/promote/:id |
β | Admin | Promote user to admin role |
| Method | Endpoint | Auth | Role | Description |
|---|---|---|---|---|
| POST | /asset/create |
β | Admin | Create new asset |
| GET | /asset/fetch |
β | Any | Get assets assigned to current user |
| PUT | /asset/update/:id |
β | Admin | Update asset details |
| DELETE | /asset/delete/:id |
β | Admin | Delete asset |
1. POST /auth/signup
β Password hashed with bcrypt (12 rounds)
β User saved to database
β Returns: 201 Created
2. POST /auth/login
β Verify password against bcrypt hash
β Generate JWT with { userId, role }
β Returns: { token: "eyJhbG..." }
3. Protected Routes
β Send: Authorization: Bearer <token>
β Middleware verifies JWT
β Attaches user to req.user
β Controller executes
SecureAssetManagementAPI/
βββ app.js # Entry point, middleware setup
βββ config/
β βββ database.js # MongoDB connection
βββ controllers/
β βββ authController.js # Auth business logic
β βββ assetController.js # Asset CRUD logic
βββ middlewares/
β βββ authMiddleware.js # JWT verification
β βββ adminMiddleware.js # Role-based authorization
β βββ validateMiddleware.js # Input validation
βββ models/
β βββ user.js # User schema
β βββ asset.js # Asset schema
βββ routes/
β βββ authRoutes.js # Auth endpoints
β βββ assetRoutes.js # Asset endpoints
βββ .env # Environment variables (gitignored)
βββ .gitignore
βββ package.json
βββ README.md
POST http://localhost:3000/auth/signup
{
"username": "john",
"password": "john1234"
}POST http://localhost:3000/auth/login
{
"username": "john",
"password": "john1234"
}
// Copy the token from responseGET http://localhost:3000/auth/profile
Headers:
Authorization: Bearer <your-token-here>POST http://localhost:3000/asset/create
Headers:
Authorization: Bearer <admin-token>
{
"assetName": "MacBook Pro",
"type": "laptop",
"serialNum": "MBP001",
"assignedTo": "<user-objectid>"
}| Layer | Protection | Purpose |
|---|---|---|
| 1 | Helmet | Security HTTP headers |
| 2 | Input Validation | Validate data format and constraints |
| 3 | Mongo Sanitize | Prevent NoSQL injection |
| 4 | Rate Limiting | Prevent brute force attacks |
| 5 | JWT Authentication | Verify user identity |
| 6 | Role Authorization | Enforce access control |
| 7 | bcrypt Hashing | Protect stored passwords |
- β Broken Access Control - Role-based middleware
- β Cryptographic Failures - bcrypt hashing, JWT secret in .env
- β Injection - Input sanitization and validation
- β Security Misconfiguration - Helmet security headers
- β Authentication Failures - Password validation, JWT expiry
- β Logging Failures - Error logging (console.error)
This project demonstrates understanding of:
- RESTful API design principles
- JWT-based stateless authentication
- Password security with bcrypt
- Role-based access control (RBAC)
- Middleware chains in Express
- MongoDB with Mongoose ODM
- Input validation and sanitization
- Security best practices (OWASP)
- Environment variable management
- Error handling patterns
- Audit logging for security compliance
- Asset assignment history tracking
- Pagination and filtering for large datasets
- Refresh token rotation
- Email verification for signup
- Password reset functionality
- Comprehensive logging with Winston
- API documentation with Swagger
- Unit and integration tests
- Docker containerization
Built with security-first mindset | Demonstrating backend fundamentals for cybersecurity