refactor(api): requireAuth returns resolved Principal (closes #194)#864
refactor(api): requireAuth returns resolved Principal (closes #194)#864cristim wants to merge 1 commit into
Conversation
authenticatePrincipal resolves the caller's identity once across all three credential kinds (admin API key, user API key, bearer session) and returns a *Principal. requireAuth now delegates to it instead of calling authenticate() which threw the resolved identity away. Router updated to _, err := r.h.requireAuth(...). Nil-auth guard moved before the API-key path so a missing auth service fails closed (401) rather than falling through. Dead userIface/userFields locals removed. TestRequireAuth_* tests extended to assert the returned Principal (Kind, Role, UserID, Session pointer) for each credential type.
|
Warning Review limit reached
More reviews will be available in 25 minutes and 21 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
Summary
requireAuthnow returns(*Principal, error)instead of bareerrorby delegating to the newauthenticatePrincipalhelper (already added in this branch)authenticatePrincipalcleaned up: nil-auth guard moved before the API-key path (fail-closed perfeedback_fail_closed_middleware.md), deaduserIface/userFieldslocals removed_, err := r.h.requireAuth(...)to handle the new return shapeTestRequireAuth_*tests upgraded to assert the returnedPrincipal(kind, role, UserID, Session pointer) for each credential typeTest plan
go build ./...cleango test github.com/LeanerCloud/CUDly/internal/api/... github.com/LeanerCloud/CUDly/internal/auth/...— 1852 passedTestRequireAuth_AdminAPIKeynow assertsPrincipalAdminAPIKey/role=adminTestRequireAuth_UserSessionnow assertsPrincipalSession,UserID,Role,SessionpointerTestRequireAuth_NoCredential_Rejectsstill asserts 401 ClientErrorCloses #194