Set Up Auth Module #28
Labels
No labels
backend
beginner friendly
bug
chore
documentation
enhancement
frontend
priority/high
priority/low
priority/medium
size/L
size/M
size/S
size/XL
size/XS
stage/backlog
stage/done
stage/in progress
stage/ready
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
ScottyLabs/housing#28
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Overall Objective
To give a short rundown of how this works compared to the traditional better-auth system, rather than managing sessions ourselves with better-auth, the backend acts as a pure OAuth2 resource server trusting JWTs issued by ScottyLabs' Keycloak instance at
idp.scottylabs.organd validating them locally against Keycloak's JWKS endpoint. The frontend will then handle the login flow and will attach the token to every API request as aBearerheader. The backend will then just validate the attached tokens.After this issue:
ctx.authwhich contains the validated JWT payload (Andrew ID, subject, name, etc.)401usertable is lazily populated on first request via aGET /api/merouteSuggested Approach
secretspec.tomlat the project rootWe use the same secrets tooling as TartanVote. Copy and adapt:
Notice there's no
OIDC_CLIENT_IDorOIDC_CLIENT_SECREThere — a resource server only needs the issuer URL to fetch the JWKS. It never exchanges credentials with Keycloak directly.For local dev, run
secretspec populate devto write a.env. Add.envto.gitignore. Ask an org OpenBao admin to ensureDATABASE_URLandOIDC_ISSUERare available under thedorm-hubdev secrets.run
deno add elysia-oauth2-resource-server @elysiajs/swaggerin the backend folder.3. Create
src/auth/middleware.tssrc/auth/user.tsSince there's no login hook, we create the
userrow on the first authenticated request instead:src/index.tsAll other route files just use
ctx.authdirectly — no additional middleware needed sincejwtAuthis mounted globally.secretspec populate devto get your.envdeno task devcurl -H "Authorization: Bearer <token>" http://localhost:3000/api/meuserrow is created in the DB on first hit401http://localhost:3000/api/docsto confirm Swagger loadspreferred_usernamemaps to Andrew IDInspect the JWT payload (paste the token into jwt.io) and verify
preferred_usernameis the Andrew ID. If it's named differently in the ScottyLabs Keycloak realm, updategetOrCreateUseraccordingly.Help & Resources
https://idp.scottylabs.org/realms/scottylabs/protocol/openid-connect/certs. You can open this in a browser to confirm it resolves before running anything