Financial-Grade OAuth Agent

Financial-Grade OAuth Agent

Advanced Security
Download on GitHub
On this page


The role of the OAuth Agent in the below diagram is to handle the OpenID Connect flow for Single Page Applications (SPA). This includes issuing of secure cookies for the browser and managing follow-on operations for token refresh, retrieving user info and signing out:

Token Handler

This implementation is coded in Kotlin and implements OpenID Connect using the following three financial-grade features. The SPA then uses only the strongest HTTP Only, SameSite=strict cookies in the browser, while JWT access tokens are used in a standard way by APIs.

OAuth Agent Operations

The following operations represent the OAuth Agent's API interface, and all of these are designed to serve SPAs via Ajax requests from the browser. This enables the SPA to fully control behavior such as when redirects occur:

POST /login/startStart a login by providing the request URL to the SPA and setting temporary cookies.
POST /login/endValidate the request, complete the login and return HTTP only encrypted cookies to the browser.
GET /userInfoReturn Personally Identifiable Information (PII), such as the user name, to the SPA.
GET /claimsReturn claims from the ID token to the SPA, containing authentication related information.
POST /refreshRefresh the current access token and rewrite secure cookies.
POST /logoutClear secure cookies and return an end session request URL.

An OAuth Agent is a tricky API to develop, since its main focus is on browser and OAuth infrastructure:

Authorization CodesThe API needs to receive real authorization codes in order to get tokens.
HTTP RedirectsThe SPA client therefore needs to perform real HTTP redirects and user logins.
Cross Origin RequestsThe API and its SPA client must both deal with Cross Origin Request Sharing (CORS).
CookiesThe API operations need to frequently read, write and update encrypted cookies containing tokens.

Get the Code

First get the code, which is a standard Kotlin API that uses Spring Boot's HTTP server, and the repository contains both code and test resources. The OAuth logic for the OAuth Agent can be studied and adapted if needed.

Token Handler Resources


First ensure that the following components are installed. You can run the API with only Java and OpenSSL, but to run a full system you will also need a license file for the Curity Identity Server with support for financial-grade features. This will be the case if you have a trial license.

Configure Development URLs

When running on a development computer, the OAuth Agent is configured to use these base URLs:

ComponentBase URL
Token Handlerhttps://api.example.local:8080/oauth-agent
Curity Identity Server Endpointshttps://login.example.local:8443
Curity Identity Server Admin UIhttps://localhost:6749/admin

For this to work you must first add the following entries to the local computer's hosts file:  api.example.local login.example.local
:1 localhost

Next run the following command, which executes a script that uses OpenSSL to generate local development certificates:

cd certs

The following client and server certificates are then created, along with a root certificate authority, and they will be used by the token handler components and the Curity Identity Server:

Token Handler Certificates

Finally configure the Java runtime to trust the root certificate authority, via a command that points to your Java SDK:

sudo "$JAVA_HOME/bin/keytool" -import -alias -cacerts -file ./certs/ -storepass changeit -noprompt

Run the OAuth Agent

Run the API from the command line via gradle, or use your preferred Java IDE:

./gradlew bootRun

You can then simulate a request from the SPA via the following test command, to ensure that connectivity is working:

curl -X POST https://api.example.local:8080/oauth-agent/login/start \
-H "origin: https://www.example.local" | jq

Run Integration Tests

When working on the OAuth Agent as an API it is useful to initially avoid a browser and use a test driven approach. The OAuth Agent comes with a suite of Spock integration tests which act as an SPA that calls the OAuth Agent. These tests use Wiremock to mock the endpoints of the Curity Identity Server. Integration tests are then run with gradle or from an IDE, and result in the following output:

./gradlew test --rerun-tasks

Integration Tests

Run End-to-End Tests

This is done by first copying a license file into the test/idsvr folder and then running the deploy script, which will spin up the Curity Identity Server in a Docker container:

cd test/idsvr

You can then log in to the Admin UI with credentials admin / Password1 to view client configuration details. OAuth requests on behalf of the SPA are initiated from the OAuth Agent, which uses Mutual TLS client authentication when it calls the Authorization Server:

Mutual-TLS Client

Later, when you have finished with the OAuth Agent, free all Docker resources by running this script:

cd test/idsvr

Next run the following script based client, which sends the same HTTP requests as an SPA, but avoids the need to continually switch to a browser and extract values such as authorization codes. This scripted client uses the popular curl and jq tools:

cd test

The script runs a complete SPA workflow, using a preconfigured user account shipped with the Docker deployment. This begins with a login, then continues with refresh and user info requests, finishing with a logout. This fast feedback will enable any coding bugs in the OAuth Agent to be quickly found and ironed out:

Financial-grade OAuth Agent Workflow

OpenID Connect Messages

The OAuth Agent provides front channel requests to the SPA by creating and returning a URL. The OAuth Agent gets the authorization request URL by posting a pushed authorization request to the Authorization Server, containing the standard OpenID Connect parameters:

POST https://login.example.local:8443/oauth/v2/oauth-authorize/par


The result is a URL of the following form, where PAR protects against potential request tampering in the browser. This URL is returned to the SPA client, which has no knowledge of OAuth security details, and simply redirects on the request URL it is given:


When a login completes, the SPA receives a JWT query parameter containing the code and state. Use of JARM protects against potential response tampering in the browser. The SPA then posts its response URL containing the JWT to the OAuth Agent:

  "exp": 1637593781,
  "iss": "https://login.example.local:8443/oauth/v2/oauth-anonymous",
  "aud": "spa-client",
  "iat": 1637593761,
  "purpose": "authz_response",
  "code": "dLjNJphJveReaQflxvl2oUTxCxxeDguk",
  "state": "kl9kbxyMFnzXpgzgDsVYqiYZ7YC1sHwpkSzrEbE7ygfTqmXAUwLeb9mq7BO5QqsI",
  "session_state": "/pE6l/goEZJOawote6lqdYUjNZTKI4lNMFnPJNOM4XU=.LmJ6oN371McY"

The OAuth Agent deals with validating the response JWT and extracting the code and state parameters. After validation checks the flow continues in the standard way, by redeeming the code for tokens. This is done via a back channel request to the Authorization Server, which again uses Mutual TLS to authenticate the client:

POST https://login.example.local:8443/oauth/v2/oauth-token


Tokens returned in the response are then stored in HTTP Only encrypted secure cookies that are returned to the browser but not accessible from Javascript code. AES256 symmetric encryption is used to encrypt the cookies, with a key only known to the OAuth Agent.

Browser Integration

Once the OAuth Agent is working as expected and any code changes have been completed, you can deploy it and test with real SPAs running in the browser. This is simply a case of pointing the SPA to the base URL of the OAuth Agent in the deployed environment:

    "businessApiBaseUrl": "",
    "oauth": {
        "oauthAgentBaseUrl": ""


This code example showed how the token handler pattern can be used to extend an SPA with financial-grade website security features. If required companies can further adapt Curity's token handler implementation for their own purposes, by following the API development steps in this tutorial.