Skip to content

feat: CurrentUser + OptionalCurrentUser extractors with JWT validation - #13

Merged
ajianaz merged 1 commit into
developfrom
feat/current-user-middleware
Jun 15, 2026
Merged

ajianaz merged 1 commit into
developfrom
feat/current-user-middleware

Conversation

@ajianaz

@ajianaz ajianaz commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Summary

Replaces the placeholder middleware with full FromRequestParts extractors that read the session cookie and validate the JWT.

Closes #2

Extractors

CurrentUser

  • Reads session cookie → validates JWT via validate_jwt(token, app_secret)
  • Returns 401 Unauthorized if no cookie or invalid token
  • Generic over state S where AuthConfig: FromRef<S>

OptionalCurrentUser

  • Same logic but never rejects — returns Option<CurrentUser>
  • For endpoints that behave differently for anonymous vs authenticated users

Changes

  • Modified: crates/rungu-auth/src/middleware.rs — full implementation (was 39 → 217 lines)
  • Modified: crates/rungu-auth/src/lib.rs — export OptionalCurrentUser
  • Modified: crates/rungu-api/src/lib.rs — impl FromRef<AppState> for AuthConfig
  • Modified: crates/rungu-auth/Cargo.toml — added axum-extra dependency
  • Modified: Cargo.toml — added axum-extra with cookie feature to workspace

Design decision

The extractors are generic over S where AuthConfig: FromRef<S> to avoid a circular dependency between rungu-auth and rungu-api. The FromRef<AppState> impl lives in rungu-api.

Test plan

  • cargo check --workspace ✅
  • cargo test --package rungu_auth — 6/6 pass ✅
  • cargo clippy -- -D warnings ✅
  • cargo fmt --check ✅

#2)

- Replace placeholder middleware with full FromRequestParts<AppState> impl
- CurrentUser: reads session cookie, validates JWT via validate_jwt, returns 401 on failure
- OptionalCurrentUser: never rejects, returns Option<CurrentUser>
- Generic over state S where AuthConfig: FromRef<S> (avoids circular dep)
- Add FromRef<AppState> for AuthConfig in rungu-api
- Add axum-extra cookie feature to workspace deps
- Export OptionalCurrentUser from rungu-auth lib
- 6 unit tests (valid token, no cookie, invalid token — for both extractors)
@github-actions

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-cli · BYOK · MIT

@ajianaz
ajianaz merged commit c0bffcb into develop Jun 15, 2026
10 checks passed
@ajianaz
ajianaz deleted the feat/current-user-middleware branch June 15, 2026 15:25
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.

Axum middleware — CurrentUser extractor with JWT validation

1 participant