Implement shared-secret JWT authentication for backend services#4
Open
google-labs-jules[bot] wants to merge 1 commit into
Open
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem & Context
Currently, backend microservices (
admin-serviceandstorage-service) expose highly sensitive administrative routes (e.g., container management, service state controls) and file system operations (e.g., file deletion) directly to the network. Because these endpoints lack authorization verification, they pose a severe security risk of unauthorized destructive actions or arbitrary system manipulation.Solution Overview
To secure these services without introducing complex IAM infrastructure or modifying the existing Nginx reverse proxy architecture, we have implemented a Shared Secret JWT Authentication model.
By utilizing the existing
NEXTAUTH_SECRETon the Next.js server to sign short-lived (5-minute expiration) HMAC-SHA256 tokens, we establish a secure trust relationship between the frontend gateway and backend microservices. The backend services verify these tokens locally using Node's nativecryptomodule, maintaining high routing performance with zero external dependency overhead.Key Decisions & Rationale
NEXTAUTH_SECRETto the client browser, we introduced a Next.js API route (/api/auth/token). This route validates the client's current NextAuth session before issuing a signed, short-lived JWT token.cryptofunctions. This keeps the backend services lightweight and avoids importing heavy external JWT libraries.Authorization: Bearer <token>header. For scenarios where headers are unsupported (e.g., direct download links or static media sources), the token is securely appended as a short-lived query parameter.Summary of Changes
1. Frontend & Gateway (Next.js)
/api/auth/token: A server-side API endpoint that verifies the NextAuth user session and signs a secure JWT token containing the issuer, subject, and expiration timestamp.lib/authFetch.ts: A fetch utility wrapper that transparently fetches a valid token, caches it for its lifetime, and appends it to outgoing requests to secured backend endpoints.authFetchinto container administration actions (app/admin/page.tsx) and file management operations (app/cloud/page.tsx).2. Backend Microservices (
admin-service&storage-service)HTTP 401 Unauthorizedstatus before running any service business logic.Verification and Acceptance Criteria
HTTP 401 Unauthorizedstatus.