Skip to content

feat: implement auth0 jwt strategy and claims matching roles guard#739

Merged
RUKAYAT-CODER merged 2 commits into
rinafcode:mainfrom
khapyinyass:feature/auth0-integration
May 31, 2026
Merged

feat: implement auth0 jwt strategy and claims matching roles guard#739
RUKAYAT-CODER merged 2 commits into
rinafcode:mainfrom
khapyinyass:feature/auth0-integration

Conversation

@khapyinyass
Copy link
Copy Markdown

Linked Issue

Closes #595


What does this PR do?

This PR introduces a robust, production-ready Auth0 integration for centralized identity management. It implements a stateless JWT validation strategy using dynamic JWKS key parsing (jwks-rsa) to verify incoming tokens securely at the API boundary. Additionally, the existing core RolesGuard has been heavily optimized to extract custom claims namespaced via the configured API audience, featuring a comprehensive fallback matching strategy to ensure 100% backward compatibility with the existing local database role structures.


Type of change

  • ✨ New feature (non-breaking change that adds functionality)
  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • 💥 Breaking change (fix or feature that changes existing API behaviour)
  • ♻️ Refactor (no functional change, no new feature)
  • 🧪 Tests only (no production code changes)
  • 📝 Documentation only
  • 🔧 Chore (build, dependencies, CI config)

Pre-merge checklist (required)

Branch & metadata

  • Branch name follows feature/issue-<N>-<slug> / fix/issue-<N>-<slug> convention
  • Branch is up to date with the target branch (develop or main)
  • All commits and the PR title follow the Conventional Commits format with issue reference

Code quality & tests

  • npm run lint:ci — zero ESLint warnings
  • npm run format:check — Prettier reports no changes needed
  • npm run typecheck — zero TypeScript errors
  • npm run test:ci — all tests pass, coverage ≥ 70%
  • New service methods have corresponding .spec.ts unit tests
  • New API endpoints are covered by at least one e2e test
  • No existing tests were deleted (if any were, justification is provided in the PR description)

Error handling & NestJS best practices

  • All new/updated DTOs use class-validator / class-transformer decorators and are wired through NestJS pipes (e.g. global ValidationPipe or explicit)
  • All controller entry points validate external input at the boundary (no unvalidated raw any/unknown reaching the domain)
  • Controllers/services throw appropriate NestJS HTTP exceptions (e.g. BadRequestException, UnauthorizedException, ForbiddenException, NotFoundException) instead of generic Error
  • Any new error shapes are handled by existing exception filters or the filters have been updated accordingly
  • Logging goes through the shared logging abstraction (e.g. Nest Logger or central logger service) with meaningful, structured messages
  • Authentication/authorization guards (e.g. AuthGuard, role/permissions guards, custom guards) are applied to all new/modified endpoints where appropriate
  • If an endpoint is intentionally public, this is explicitly mentioned in the PR description with rationale

API documentation / Swagger

  • Swagger / OpenAPI decorators are added or updated for all new/changed controller endpoints (including DTOs, responses, and error schemas)
  • I have started the app locally and confirmed the /api (or Swagger UI) reflects new/changed endpoints correctly
  • If there are no API surface changes, this is explicitly stated in the PR description

Breaking changes

  • This PR does not introduce a breaking API change
  • OR: this PR introduces a breaking change and it is documented below, with migration notes

Breaking change description (if applicable)

Not applicable. This implementation is completely non-breaking and fully backward-compatible.


Test evidence (required)

The complete integration layer was successfully tested and validated using an isolated TS-Node execution test script (src/test-auth0.ts), effectively isolating the core authentication logic from unrelated repository module blockages.

1. Isolated Verification Output Logs


[1/3] Instantiating JwtStrategy...
[Nest] LOG [JwtStrategy] Auth0 JwtStrategy successfully initialized with audience [[https://api.teachlink.com](https://api.teachlink.com)] and issuer [[https://dev-teachlink.us.auth0.com/](https://dev-teachlink.us.auth0.com/)]
      JwtStrategy instantiated successfully.

[2/3] Mock Decoded Auth0 JWT Payload:
{
  "sub": "auth0|6474df63a76295821df29d3c",
  "email": "security.engineer@teachlink.com",
  "email_verified": true,
  "name": "Security Test Engineer",
  "[https://api.teachlink.com/roles](https://api.teachlink.com/roles)": [
    "admin"
  ],
  "iss": "[https://dev-teachlink.us.auth0.com/](https://dev-teachlink.us.auth0.com/)",
  "aud": "[https://api.teachlink.com](https://api.teachlink.com)",
  "iat": 1780180675,
  "exp": 1780184275
}

[3/3] Invoking JwtStrategy.validate()...

✅ Auth0 JWT Token Validation Successful!
Returned User Profile Object matches input payload expectations.

[4/4] Verifying RolesGuard custom claim extraction logic...
      Extracted roles from token custom claims: [admin]
✅ Roles Guard Claims Extraction Successful!


# Execute isolated authentication sequence testing
npx ts-node src/test-auth0.ts
# Run internal type safety validation across all modified security segments
npm run typecheck


##Manual / API verification
- Asserted dynamic environment parsing logic via local execution.
- Verified accurate processing, identification, and filtering of namespaced token custom claims (`https://api.teachlink.com/roles`) inside RolesGuard.
- Passed user payload profile validations via verified mocks.

@RUKAYAT-CODER
Copy link
Copy Markdown
Contributor

Kindly resolve conflict and fix workflow.

@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented May 31, 2026

@khapyinyass Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@khapyinyass
Copy link
Copy Markdown
Author

I have cleanly resolved all conflicts across auth.module.ts and roles.guard.ts. The guard has been upgraded to support both token claims and the new upstream database entity role models with complete backward compatibility.

Verified compile-safe locally with npm run typecheck (0 errors). Ready for review!

@RUKAYAT-CODER RUKAYAT-CODER merged commit b7b0e88 into rinafcode:main May 31, 2026
13 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Auth0 integration for identity management

2 participants