Authx - Retain authentication outcome/metadata #20026
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This improves the internal APIs for Authx. Authx supports additional ways to authenticate requests (e.g. passwords, API keys, JWTs). With this patchset, we have an internal way to see how the user was authenticated, e.g.
Example Use-cases
scope
that allows access a handful of APIs. The JWT is parsed during authentication, but you should preserve thescope
so that it can be consulted during authorization. ("Did thescope
authorize access to this specific entity?")civicrm/ajax/rest
may be used in different ways (e.g. for AJAX or for REST). When evaluating an AJAX request (with implicit/automatic identification based on session-cookie), you should enforce the full CSRF guards. When evaluating a REST request (submitted by a non-browser agent with an API key), the form of the credential may indicate that it's not a CSRF attack. (If an attacker can construct a cross-site request that includes a valid API key, then they've already compromised the account and don't need CSRF shananigans.) Enforcing CSRF may require discriminating based on authentication info.(This patch does not address those example use-cases -- it merely retains data so that it's possible.)
Before
While processing an authentication request,
authx
creates a private/short-lived object containing details of the authentication process (credential-type, user ID, etc).The object is lost/destroyed after we make a decision about authenticating.
After
As before,
authx
creates a private/short-lived object containing details of the authentication process (credential-type, user ID, etc).However, with successful authentication, key metadata is now copied to the session. This includes
credType
,jwt
claims, and more. However, it omits any literal credentials (e.g. passwords and API keys).Technical Details
I originally tried to retain the data as an object (e.g.
\Civi\Authx\AuthenticatorTarget
or\Civi\Authx\Authentication
). This proved problematic during deserialization. (We don't decide when to deserialize - and our classloader may not be available early enough). So instead theauthx
variable is a basicarray
.