Skip to content

feat(thirdparty): Spring Security oauth2Client for the Data Custodian authorization (#146) - #275

Merged
dfcoffin merged 3 commits into
mainfrom
feature/146-thirdparty-oauth2-client
Nov 17, 2026
Merged

dfcoffin merged 3 commits into
mainfrom
feature/146-thirdparty-oauth2-client

Conversation

@dfcoffin

@dfcoffin dfcoffin commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Closes #146.

Summary

The Third Party's customer flow moves from oauth2Login to Spring Security 7 oauth2Client. The TP customer signs in to the TP (form login, #249) and stays signed in while authorizing the TP at the Data Custodian (TP → AS → DC consent → AS → TP callback). The tokens are kept for that customer.

Token storage

  • EspiAuthorizationOAuth2AuthorizedClientService stores authorized clients in the ESPI authorizations table.
    • Columns written: access token, refresh token, expiry (epoch ms), scope, grant type, status.
    • Authorization_code tokens are linked to the TP customer; the client access token belongs to no customer.
  • REQ.21 token-response URIs: Spring keeps only the tokens, so resourceURI, authorizationURI and customerResourceURI are captured from the token response by EspiTokenResponseParameters and stored with the token. A refresh response that does not repeat them keeps the stored URIs.
  • Flyway V6__Add_authorization_client_registration_id.sql (shared, all vendors) adds authorizations.client_registration_id.

Who may start what

TpCustomerAuthorizationRequestResolver handles /oauth2/authorization/{registrationId}:

  • Only a signed-in TP customer (ROLE_USER) may start an authorization_code registration.
  • An anonymous visitor is sent to /login and back afterwards; the TP administrator gets 403.
  • The client_credentials registration is not a redirect: it gets 404 instead of Spring's 500.

The callback (/login/oauth2/code/{registrationId}) returns the customer to /?authorized, or /?authorization_error on a refusal or a forged callback.

Client access token (FB=34_35)

UI

  • TP home: Authorize Monthly / Daily cards for customers, and a Your authorizations table (scope, expiry, URIs).

Removed

PKCE is off in the TP for now (#274)

Still open (AS side)

Tests

DataCustodianAuthorizationTest (new, 9 tests) runs against a stub token endpoint:

  • The authorize redirect carries no PKCE and no nonce.
  • An anonymous visitor is sent to /login.
  • The administrator gets 403, and the client_credentials registration gets 404.
  • The callback stores the token and URIs, and the customer stays signed in.
  • A refused consent stores nothing.
  • A forged callback is rejected.
  • A refresh keeps the URIs.
  • The admin obtains the client access token with its Bulk URIs, and a customer gets 403.

Suites (WSL, with Docker):

Module Run Failed Skipped
handoff 22 0 0
common (V6 on MySQL and PostgreSQL) 908 0 0
Data Custodian 217 0 0
Third Party (schema tests on MySQL and PostgreSQL) 110 0 2

The Data Custodian and Third Party suites ran without AboutControllerTest. That test fails on a JDK whose java.version is exactly 25: the About page contains the Bootstrap class w-25. This branch doesn't cause it (#277).

Manual end-to-end test (local profile):

docs/LOCAL_TESTING.md has a new "Customer flow (TP → AS → DC → TP)" section and updated logins and known limitations.

Review fixes (17cbb15)

  • Client access token: a new one is requested first and replaces the stored one only once issued and saved. A refusal, an unreachable AS or a persistence error keeps the current token in use.
  • Authorization row ids: a deterministic UUID5 of the registration and customer (EspiIdGeneratorService.generateEntityId). Racing saves cannot insert a duplicate row, and ids follow the project's UUID5 rule.
  • Callback page: reports success only for authorization_code registrations, so the TP's client access token never counts as a customer's authorization.
  • URI hand-over: the token-response URIs pass to the store through an in-memory map keyed by access token (5-minute expiry) instead of request attributes, so a token refreshed outside a web request keeps them.
  • Home page: one derived query (findAllByRetailCustomer_UsernameAndClientRegistrationIdIsNotNullOrderByUpdatedDesc).
  • home.html: the unreachable "session ended" message is removed. A callback after the TP session ended now goes to /login first, and a test checks that.
  • Tests:

Second review fixes (3067f5c)

  • Signed-out start: a visitor who starts an authorization signed out no longer consents twice. Starting an authorization clears the request saved before sign-in, which OAuth2AuthorizationCodeGrantFilter would otherwise redirect to after the code exchange.
  • Client access token errors: a missing datacustodian-admin registration, and any failure while obtaining or saving the token, show client_token_error. The token already held stays in use.
  • EspiTokenResponseParameters: now a Spring bean shared by the token-response clients and the token store, instead of static state. Entries never taken expire on both put and take.
  • Small cleanups: EspiIdGeneratorService is injected, and the expiry is read with AuthorizationEntity.getExpirationInstant().
  • Not changed:
    • A save failure inside Spring's code-grant filter would still give a 500. It needs two simultaneous first callbacks, or a ROLE_USER with no customer row.
    • Revisiting the callback page while holding a token still shows "authorized", which is true.
  • Tests:

🤖 Generated with Claude Code

… authorization (#146)

The Third Party's customer flow now uses Spring Security 7 oauth2Client
instead of oauth2Login: the TP customer stays signed in to the TP (form
login) while authorizing it at the Data Custodian, and the tokens are kept
for that customer.

- Tokens are stored in the ESPI authorizations table through a new
  OAuth2AuthorizedClientService (EspiAuthorizationOAuth2AuthorizedClientService),
  with the REQ.21 token-response URIs (resourceURI, authorizationURI,
  customerResourceURI) captured from the token response. A refresh that
  does not repeat them keeps the stored URIs.
- Flyway V6 adds authorizations.client_registration_id (MySQL, PostgreSQL, H2).
- Only a signed-in TP customer (ROLE_USER) may start an authorization_code
  registration; anonymous visitors go to /login, the TP administrator gets 403.
- datacustodian-admin is now the client_credentials registration for the
  TP's client access token (FB=34_35). The TP administrator obtains it on the
  admin page, which shows its scope, expiry, a token prefix and the
  authorizationURI, resourceURI (energy usage Bulk) and customerResourceURI
  (Customer PII Bulk) once the AS returns them (#202).
- The TP home page lists the customer's authorizations.
- PKCE is off in the TP until the AS carries code_challenge through the
  Data Custodian delegation (#274).
- Removed the legacy /oauth/callback controller, the oauth2Login
  user-info settings and the openid/profile scopes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dfcoffin and others added 2 commits November 17, 2026 15:32
…ore (#146)

- Obtaining a new client access token no longer revokes the current one
  first: the new token is requested directly and replaces the stored one only
  once issued and saved. A refusal, an unreachable AS or a persistence error
  (DataAccessException) shows client_token_error and keeps the token in use.
- Authorization rows get a deterministic UUID5 id from the registration and
  customer (EspiIdGeneratorService.generateEntityId) instead of a random v4
  id, so racing saves cannot insert a duplicate row.
- The callback page reports success only for authorization_code
  registrations; the TP's client access token never counts as a customer's
  authorization.
- The ESPI token-response URIs are handed to the store through an in-memory
  map keyed by access token (entries expire after 5 minutes) instead of
  request attributes, so a token refreshed outside a web request keeps them.
- The home page loads the customer's authorizations with one derived query.
- Removed the unreachable "session ended" message from home.html; the test
  now checks that a callback after the session ended requires sign-in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…low (#146)

- A visitor who starts a Data Custodian authorization signed out no longer
  consents twice: starting an authorization clears the request saved before
  sign-in, which OAuth2AuthorizationCodeGrantFilter would otherwise redirect
  to after the code exchange instead of the callback.
- Obtaining a client access token shows client_token_error for a missing
  datacustodian-admin registration and for any failure while obtaining or
  saving the token; the token already held stays in use.
- EspiTokenResponseParameters is a Spring bean shared by the token-response
  clients and the token store instead of static state; entries never taken
  expire on both put and take.
- The token store gets EspiIdGeneratorService injected, and the expiry is
  read with AuthorizationEntity.getExpirationInstant() in both places.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dfcoffin
dfcoffin merged commit f85b0ae into main Nov 17, 2026
12 checks passed
@dfcoffin
dfcoffin deleted the feature/146-thirdparty-oauth2-client branch November 17, 2026 21:35
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.

TP bring-up: missing JAXB Atom marshaller, narrow component scan, and broader OAuth2-client wiring gaps

1 participant