feat(keycloak): verify keycloak and allow access to api #519
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.
Description
The commit adds Keycloak JWT support. We now recognise JWTs issued by the configured Keycloak realm, fetch the signing key via JWKS (jwks-rsa), and verify RS256 tokens. For those tokens we temporarily use sub as id so existing APIs keep working; local RSA tokens continue to verify with the existing public key. The commit also adds a watch-targeted test script, pulls in the new dependencies (jwks-rsa, jose, TypeScript types), and extends JWTService.spec.js to exercise the Keycloak flow by obtaining a real token via the Keycloak token endpoint. Finally, verifyJWTHandler awaits the asynchronous verification.
Issue(s) addressed
What kind of change(s) does this PR introduce?
Please check if the PR fulfils these requirements
Issue
What is the current behavior?
API only trusted JWTs it issued itself (signed with the local private key); external Keycloak access tokens were rejected.
JWTService.verify was synchronous and verification middleware assumed immediate return.
No focused test script for iterating on JWT logic.
What is the new behavior?
Incoming tokens are checked for the configured Keycloak issuer; if present we fetch the JWKS, verify RS256 signatures, and treat the Keycloak subject (sub) as the caller id for compatibility with existing routes. Local RSA tokens still work.
verifyJWTHandler now awaits the async verification.
A test-watch-jwt script plus an integration spec validate the Keycloak flow.
Other useful information
Newly discovered Issues
Issue 1: Keycloak login creates orphaned wallet trust rows
Summary
Keycloak tokens are accepted by
JWTService.verify, but we setresult.id = result.sub. When a Keycloak caller hitsPOST /wallets,walletService.createWalletuses thatidasactor_wallet_id/originator_wallet_id. If no wallet exists with thatUUID, we insert trust records pointing to a non-existent parent wallet (no FK constraint catches it). The API still returns 201, leaving orphaned trust rows.Impact
wallet_trustmay break or allow unexpected access.Steps
POST /wallets(include API key + new wallet data).wallet_trust.actor_wallet_id = Keycloak sub, but wallet table has no such id.Proposed FixIssue 2: e2e tests expect 200 but API now returns 201 on create flows
Summary
Several end-to-end tests fail on master because they expect HTTP 200 for operations that now intentionally respond with 201 Created.
Failing specs
Each asserts
response.status === 200but the API returns201for successful trust relationship creation and managed wallet creation. The assertions in__tests__/e2e/libs/assertionLibrary.equals throw (expected 201 to equal 200).Why it happens
Handlers set 201 after creating new resources:
Proposed fix
Notes
These failures occur on a clean checkout without my changes.
After adjusting the assertions, the tests should pass against current API behavior.