Skip to content

Latest commit

 

History

History
79 lines (46 loc) · 5.35 KB

authentication.md

File metadata and controls

79 lines (46 loc) · 5.35 KB

Sprint 1 authentication backend

The routes below implement requirements 1, 2 and 3. All examples use JSON request bodies and existing database accounts. No signup route is provided.

Configuration

Copy .env.example to .env in the repository root. Set MONGODB_URI for your local MongoDB server and set a random JWT_SECRET. Generate the secret once with:

node -e "console.log(require('node:crypto').randomBytes(32).toString('hex'))"

Keep the secret in .env. The file is ignored by Git. Production requires JWT_SECRET; development uses an ephemeral random secret if it is absent, which invalidates existing tokens and OTPs when the API restarts.

Password reset requires an SMTP provider. Set SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASS and SMTP_FROM using that provider's settings. The sender must be allowed by the provider, and messages are addressed to the existing account's GUC email.

For port 587, use SMTP_SECURE=false and SMTP_REQUIRE_TLS=true. For port 465, use SMTP_SECURE=true. An unauthenticated SMTP relay can omit SMTP_USER and SMTP_PASS. Until email is configured, the forgot-password endpoint returns 503 with an explanatory message.

Start the backend with:

npm run dev --workspace @bobos/api

The npm startup command loads the root .env using Node's built-in environment-file support. The API exits with a configuration message if MongoDB cannot be reached. Existing team catalogue, academic-term, profile, notification and student-record routes remain mounted alongside authentication.

Requirement 1: login

POST /api/identity/login

{ "email": "name@student.guc.edu.eg", "password": "<current university password>" }

The backend normalizes email case and whitespace, checks the GUC domain, looks up an existing active account, and verifies its bcrypt password hash. It returns 200 with a signed token and safe user fields, 400 for invalid input, or 401 for invalid credentials/inactive accounts. All five roles are supported.

Protected requests use Authorization: Bearer <token>. Tokens expire after seven days. Protected routes also check the database's current active state and authentication version, so password reset invalidates existing tokens immediately.

Requirement 2: password reset

  1. POST /api/identity/forgot-password
{ "email": "name@student.guc.edu.eg" }

The user receives a six-digit code by email. The response never contains the code and gives the same success message for unknown/inactive accounts. Only a keyed hash is stored, in the user's hidden passwordReset field. Codes expire after 10 minutes. Requests for the same account have a 60-second cooldown.

  1. POST /api/identity/reset-password
{ "email": "name@student.guc.edu.eg", "otp": "123456", "newPassword": "<new university password>" }

Keep the OTP as a string to preserve leading zeros. Five incorrect attempts invalidate the code. A successful reset atomically replaces the password hash, removes the code and invalidates existing sessions. A used, expired or replaced code cannot change the password again. New passwords require at least eight characters and at most 72 UTF-8 bytes, matching bcrypt's input limit. The user then logs in with the new password.

The embedded state makes the update atomic on standalone local MongoDB; a replica set is not required. The pre-existing PasswordResetToken model remains available but is not used by these routes.

Requirement 3: logout

POST /api/identity/logout with Authorization: Bearer <token> and an empty JSON body {}.

Logout increments the account's persisted authentication version. All existing sessions for that account become invalid, including the supplied token, and protected endpoints return 401 if those tokens are reused. This implementation logs out all devices. The client should also remove its stored token after success. A new login still works and issues a token with the updated version.

The endpoint identifies the user only through the authenticated token; request-body user IDs cannot log out another account. Missing, invalid, expired and already invalidated tokens return 401.

Request limits

Authentication verification is limited to 30 unsuccessful requests per IP per minute. Successful sign-ins and resets are excluded. Forgot-password email requests have a separate limit of 10 per IP per 15 minutes. Excess requests return 429. These IP counters use the process-local store; a deployment with multiple API processes needs a shared store. Account OTP expiry, cooldown and attempt limits are enforced by MongoDB and persist across restarts. Configure trusted proxies narrowly if hosting behind a reverse proxy.

Verification

npm test

The integration tests start a temporary real MongoDB process and a local SMTP server, and exercise the combined Express app's endpoints over HTTP. They do not send mail to real inboxes or modify your bobos database. The MongoDB test binary may download on the first run; there is no system MongoDB installation requirement for these tests. Real inbox delivery still needs your team's SMTP configuration.

Implementation references: Nodemailer SMTP, express-rate-limit usage, MongoDB test server.