🔧 Programmierung 🕛 vor 2 Monaten 7 Min Lesezeit
0

Getting started with OAuth in your Web Apps

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

This article is intended as a beginner level article for people that want to learn how to use OAuth2 in their web applications natively.


There is an accompanying video/demo that may be helpful here: 







and you can reproduce this locally with the Open Exchange application attached.


OAuth2 as a native authentication type for web applications


OAuth (Open Authorization) 2.0 is a standard way to let one application call another application’s API without sharing a username and password. Instead of sending credentials on every request, the client sends an access token (typically in an Authorization: Bearer ... header).


OAuth2 focuses on authorization (what the client is allowed to do). If you also need user login and identity claims, OAuth2 is commonly paired with OpenID Connect (OIDC) — but in this article we’ll stay focused on OAuth2 access tokens and scopes.


If you want a quick refresher, this short video is a good overview: , 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.


Concretely, IRIS can validate an incoming access token for a CSP/Web Application request and then establish a user context (username + roles) based on that token, just like other authentication types do.


(For reference on the older, more manual approach, see .)


The Characters


OAuth has a few “characters”:




  • Resource Owner (the user/owner of the bank account)


  • Client (the third-party app; in this demo we use Postman as the client)


  • Authorization Server (Keycloak; authenticates the user & authorizes the request, deciding what scopes the client can receive, and issues the token)


  • Resource Server (IRIS; hosts /myBankInfo, validates the token, and enforces what the token is allowed to do). The third-party app never sees your IRIS password — it presents a token, and IRIS makes the allow/deny decision.


Step 0: Prerequisites (avoid issuer / hostname issues)


Note: This demo uses HTTP to keep setup simple. In production you should use HTTPS (and real certificates), otherwise tokens and sessions can be intercepted.


This Open Exchange demo runs multiple Docker containers. One important rule to remember is:



  • localhost on your host is not the same as localhost inside a container.

OAuth token validation checks the token’s issuer claim (iss). If Keycloak issues a token with an issuer like http://localhost:8080/... but IRIS discovers/validates it using http://keycloak:8080/..., IRIS will reject the token because those issuers do not match.


To keep the issuer stable, this demo uses the hostname keycloak consistently from both the host and the containers.


On Windows, edit: C:\Windows\System32\drivers\etc\hosts and add:


127.0.0.1 keycloak


On Linux/Mac, edit /etc/hosts and add the same line (you’ll typically need sudo).


From this point on, use http://keycloak:8080 (not http://localhost:8080) when configuring Postman and IRIS.


Step 1: Configure the Authorization Server (Keycloak)


For the demo, the Authorization Server is Keycloak and it is already prepared for this use case (realm, clients, users, scopes). No work is needed here.


You can access the Keycloak admin console at .


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 will pull the endpoints and metadata it needs from the server (authorization endpoint, token endpoint, JWKS URI, etc.).






Step 3: Configure IRIS as the Resource Server


Next, create a Resource Server entry so IRIS can validate tokens and enforce permissions:






Fill in the details of your resource server, for example:


Name: IRIS Bank Resource Server


Server Definition: http://keycloak:8080/keycloak/realms/bank


Audiences: bank-demo, bank-monitor


What is “Audience”? The token’s audience (aud) is the “intended recipient” of the token. By configuring audiences here, you are telling IRIS to accept only tokens that were issued for this API (i.e., tokens whose aud matches one of these values).


Click save.



in your own implementations and just fill in what token property should be attributed to the role and user. However, for the sake of completeness we will create a simple custom authenticator class.


Step 4: Create your Authenticator Class


What should be authenticated? We will create a simple class Bank.Authenticator that maps token claims/scopes into an IRIS username and IRIS roles.


This is the key step that lets IRIS enforce “read-only” vs “transfer” behavior:



  • The token’s scopes become IRIS roles.

  • Your web application (and/or your REST endpoints) can require those roles.


In other words, this is what makes /checkbalance  succeed for a “monitor” token while /transfer returns 403 Forbidden unless the token includes the transfer scope.


CODE
Class Bank.Authenticator Extends %OAuth2.ResourceServer.Authenticator
{

ClassMethod HasScope(scopeStr As %String, scope As %String) As %Boolean
{
Quit ((" "_scopeStr_" ") [ (" "_scope_" "))
}

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 scopeStr = claims.scope
Set roles = ""
If ..HasScope(scopeStr,"bank.balance.read") {
Set roles = roles_",BankBalanceRead,%DB_USER"
}
If ..HasScope(scopeStr,"bank.transfer.write") {
Set roles = roles_",BankTransferWrite,%DB_USER"
}

If $Extract(roles,1)="," Set roles=$Extract(roles,2,*)

Set properties("Roles") = roles
Quit $$$OK
}

}

Once you compile the class you will be able to set your authenticator class in your resource server:





Finally, on your Web Application definition, select OAuth2 as an allowed authentication method. The dispatch class will check that the client has the necessary roles.





In Postman, on Authorization click Get New Access Token:





Log in with user1/123. Click proceed and then click Use Token.





Clear cookies and try logging in with user 2 and you should see them have 0 dollars in their balance.


Now get a token for user 1 and try to transfer user 2 a couple dollars. It should fail with 403 Forbidden as this “app” does not have the required scopes (it is only monitoring the bank account and should not be able to transfer money).





The new OAuth2 native authentication type ensures it is intuitive to keep your web applications safe, and after all, that's what the I in IRIS is all about.

Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
View conference room and meeting location details directly on the Google Meet homepage
1 Quelle
Xbox speaks out on rumors of Xbox Game Pass dropping 'day one' games in a purported 2027 restructure — here's what we know
1 Quelle
Notfall-Patch von Microsoft: Dringende Windows-Updates beheben schwere Server-Fehler
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Getting started with OAuth in your Web Apps

Thematisch verwandte Begriffe: Getting, started, with, OAuth · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...