Skip to content

feat(#117): #[RequiresAuth] checks SecurityContext::isAuthenticated() directly#133

Merged
usernane merged 1 commit into
devfrom
feat/117-requires-auth-security-context
Jun 1, 2026
Merged

feat(#117): #[RequiresAuth] checks SecurityContext::isAuthenticated() directly#133
usernane merged 1 commit into
devfrom
feat/117-requires-auth-security-context

Conversation

@usernane

@usernane usernane commented Jun 1, 2026

Copy link
Copy Markdown
Member

Summary

Make #[RequiresAuth] check SecurityContext::isAuthenticated() instead of calling isAuthorized() (which defaults to false). Implements ADR-0003.

Motivation

#[RequiresAuth] was effectively broken — it called isAuthorized() which defaults to false, requiring every service to override it with boilerplate. The attribute name implies "check if user is authenticated," which is what SecurityContext::isAuthenticated() does. Fixes #117.

Changes

  • checkMethodAuthorization(): method-level #[RequiresAuth] now checks SecurityContext::isAuthenticated()
  • checkMethodAuthorization(): when method has no auth annotations, checks class-level #[RequiresAuth] before falling back to isAuthorized()
  • WebServicesManager::isAuth(): class-level #[RequiresAuth] with no method annotations checks SecurityContext::isAuthenticated()
  • Added WebService::hasClassLevelRequiresAuth() helper

UI Changes

N/A

How to Test / Verify

php vendor/bin/phpunit tests/WebFiori/Tests/Http/RequiresAuthSecurityContextTest.php

8 new tests, 19 assertions. Full suite: 550 tests pass.

Breaking Changes and Migration Steps

Behavior change for services using #[RequiresAuth]:

  • Before: always denied (because isAuthorized() defaults to false)
  • After: checks SecurityContext::isAuthenticated() (works as intended)

Services without auth attributes are unchanged.

Checklist

  • I reviewed my own diff before requesting review
  • My commits follow Conventional Commits
  • The title of the pull request follows Conventional Commits
  • I added/updated tests (or explained why not)
  • I updated docs (if needed)
  • I ran lint/cs-fixer (if applicable)
  • I considered backward compatibility
  • I considered security

Related issues

Closes #117
ADR: https://github.com/WebFiori/docs/blob/main/adr/0003-requires-auth-security-context.md

… directly

- Method-level #[RequiresAuth] now checks SecurityContext instead of isAuthorized()
- Class-level #[RequiresAuth] with no method annotations checks SecurityContext
- checkMethodAuthorization() falls back to class-level check before isAuthorized()
- Add hasClassLevelRequiresAuth() helper method
- Services without auth attributes still use isAuthorized() (unchanged)
- 8 new tests covering method-level, class-level, AllowAnonymous override, fallback
@sonarqubecloud

sonarqubecloud Bot commented Jun 1, 2026

Copy link
Copy Markdown

@codecov

codecov Bot commented Jun 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.68%. Comparing base (8107785) to head (2598fb9).

Additional details and impacted files
@@             Coverage Diff              @@
##                dev     #133      +/-   ##
============================================
+ Coverage     93.64%   93.68%   +0.04%     
- Complexity     1303     1306       +3     
============================================
  Files            39       39              
  Lines          3177     3184       +7     
============================================
+ Hits           2975     2983       +8     
+ Misses          202      201       -1     
Flag Coverage Δ
php-8.3 93.68% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@usernane usernane merged commit b60aa66 into dev Jun 1, 2026
6 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.

1 participant