r/intersystems • u/intersystemsdev • 1d ago
How do you use OAuth2 as a native authentication method for IRIS web applications? A beginner walkthrough with Keycloak and a bank API demo
OAuth 2.0 is a standard way to let one application call another application's API without sharing a username and password. The client sends an access token, typically in an `Authorization: Bearer ...` header. Starting in IRIS 2025.2, OAuth2 can be selected as a native authentication method for web applications, so enabling an OAuth2-protected web app is no longer a DIY exercise. IRIS validates the incoming access token for a CSP/Web Application request and establishes a user context (username and roles) from it, like other authentication types do.
OAuth2 covers authorization (what the client is allowed to do). User login and identity claims are usually handled by pairing it with OpenID Connect, which this article leaves out.
## What problem does it solve?
Imagine a REST API for bank account `ACCT-1` under `/bank`:
- `GET /bank/checkbalance` returns `{"dollars": 5}`
- `POST /bank/transfer` takes `{"toAccount": "ACCT-2", "dollars": 2}`A third-party app should be able to monitor the balance but not transfer money. Instead of handing it your IRIS password, you give it a token that is scoped (read balance, not transfer), time-limited, and revocable.
Who plays which role?
| Role | In the demo |
|---|---|
| Resource Owner | The owner of the bank account |
| Client | The third-party app (Postman) |
| Authorization Server | Keycloak: authenticates the user, decides which scopes the client gets, and issues the token |
| Resource Server | IRIS: hosts the API, validates the token, and enforces what it allows |
The third-party app never sees your IRIS password. It presents a token, and IRIS makes the allow or deny decision.
Step 0: Hostnames and the issuer
Token validation checks the issuer claim (`iss`). If Keycloak issues a token with the issuer `http://localhost:8080/...` but IRIS validates it using `http://keycloak:8080/...`, IRIS rejects the token because the issuers don't match. `localhost` on your host is not the same as `localhost` inside a container. The demo therefore uses the hostname `keycloak` everywhere.
Add this line to your hosts file (Windows: `C:\Windows\System32\drivers\etc\hosts`, Linux or Mac: `/etc/hosts`, usually with sudo):
127.0.0.1 keycloakFrom then on use `http://keycloak:8080`, not `http://localhost:8080`. The demo uses HTTP to keep setup simple. In production, use HTTPS with real certificates, otherwise tokens and sessions can be intercepted.
## Step 1: The authorization server
Keycloak is already prepared for the demo (realm, clients, users, scopes). The admin console is at `http://keycloak:8080/keycloak/admin/master/console/` (admin/admin).
Step 2: Tell IRIS who the authorization server is
In the Management Portal go to System Administration > Security > OAuth 2.0 > Client, click Create Server Description, set the Issuer URL (in the demo `http://keycloak:8080/keycloak/realms/bank`), then click Discover and Save. IRIS pulls the endpoints and metadata it needs: authorization endpoint, token endpoint, JWKS URI, and so on.
Step 3: Configure IRIS as the resource server
Create a Resource Server entry:
- Name: IRIS Bank Resource Server
- Server Definition: `http://keycloak:8080/keycloak/realms/bank`
- Audiences: `bank-demo`, `bank-monitor`
The token's audience (`aud`) is its intended recipient. By configuring audiences, you tell IRIS to accept only tokens issued for this API.
Step 4: Write the authenticator class
The key step: token scopes become IRIS roles, and your web application or REST endpoints can require those roles. That is what makes `/checkbalance` succeed for a monitor token while `/transfer` returns 403 Forbidden unless the token has the transfer scope. A custom class is optional, since you could use `%OAuth2.ResourceServer.SimpleAuthenticator` and specify which token property maps to the user and role, but the article creates one for completeness:
Class Bank.Authenticator Extends %OAuth2.ResourceServer.Authenticator
{
Method Authenticate(claims As %DynamicObject, oidc As %Boolean, Output properties As %String) As %Status
{
// Map token -> IRIS username
Set properties("Username") = claims."preferred_username"
// Map scopes -> IRIS roles
Set scopeList = $ListFromString(claims.scope," ")
Set roles = ""
If $ListFind(scopeList,"bank.balance.read") > 0 {
Set roles = roles_",BankBalanceRead,%DB_USER"
}
If $ListFind(scopeList,"bank.transfer.write") > 0 {
Set roles = roles_",BankTransferWrite,%DB_USER"
}
If $Extract(roles,1)="," Set roles=$Extract(roles,2,*)
Set properties("Roles") = roles
Quit $$$OK
}
}Compile it, then set it as the authenticator class on your resource server and save.
Step 5: Enable OAuth2 on the web application
First enable it at the system level under System Administration > Security > System Security > Authentication/Web Session Options. Then, on the Web Application definition, select OAuth2 as an allowed authentication method. The dispatch class checks that the client has the necessary roles.
Step 6: Test it
Load the Postman collection and environment. The demo users are `user1/123` (account `ACCT-1`) and `user2/123` (account `ACCT-2`).
- In Postman, under Authorization, click Get New Access Token and log in as `user1`, then click Use Token
- Send GET to `/checkbalance`: it returns 5 dollars
- Clear cookies and log in as `user2`: the balance is 0 dollars
- Get a token for `user1` and try to transfer money to `user2`: it fails with 403 Forbidden, because this monitoring app lacks the required scopes
- Try requests 3 and 4, which simulate a client with full access: you can both check the balance and transfer funds
What it does not do
From the comments: native OAuth2 authentication lets IRIS validate a bearer token that a client has already obtained and included in the request. It does not initiate the OAuth authorization flow or redirect an unauthenticated browser to the authorization server. For an existing browser-based UI that should redirect users to an OAuth or OIDC identity provider for login, you still need delegated authentication. Scope values are space-delimited, per the OAuth 2.0 standard (RFC 6749, section 3.3).
Links
- Article: https://community.intersystems.com/post/getting-started-oauth-your-web-apps
- Video demo: https://www.youtube.com/watch?v=4mfWQwcKcMI
- Open Exchange demo: https://openexchange.intersystems.com/package/OAuth2-Native-Authentication-for-Web-Applications
- What's new in IRIS 2025.2: https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls?KEY=GCRN_new20252#GCRN_new20252_speed
- OAuth 2.0: An Overview (course): https://learning.intersystems.com/course/view.php?name=OAuth%202.0%3A%20An%20Overview
Do you already validate bearer tokens in IRIS REST apps, and do you map scopes to roles like this, or handle permissions another way?













