Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a Java OAuth2.0 authorization server with Keycloak

TL;DR

  • Objective: Java authorization server:
    • OAuth2.0 authorization code grant flow with fine-grained permissions (not a mere SSO server)
    • User management and authentication: custom database
    • Client management and authentication: Keycloak
  • Questions: What are the best practices for implementing a Java authorization server with applicative permissions handling backed on Keycloak?
    • What Keycloak adapter/API should I use in my development?
    • How should the users be managed/appear in Keycloak if they are to appear at all?

Forewarning

I am quite the beginner with Keycloak and, though I think I understand the main principles, it seems to be a rich tool and I fear I may still be mistaken about some aspects of the best ways to use it. Please do not hesitate to correct me.

Context

We are looking at implementing an API requiring our users (henceforth "users") to grant permissions to third party applications (henceforth "clients").

Our users are stored in a custom existing database-based user management system. As for our clients, we are thinking of using Keycloak.

The users consent will be given using an OAuth2.0 Authorization code grant flow. They will log in, specify which permissions they grant and which they deny, and the client then retrieves the access token it will use to access the API.

It is my understanding that Keycloak can handle the authorization token but it should not know anything applicative, which our permissions are. As a consequence, I thought of building a custom authorization server which will use Keycloak for all identity/authentication problems but will handle the applicative permissions by itself.

Then, we will use Keycloak for client authentication and authorization code/access token management, and an applicative part will check the permissions.

Problem

Besides my first experimenting, I've been roaming the Internet for a week now and I'm surprised as I thought this would be quite a standard case. Yet I found next-to-nothing, so maybe I'm not searching correctly.

I've found many Spring/Spring Boot tutorials1 on how to make a "simple authorization server". Those are mainly SSO servers though, and few do manage permissions, with the exception of those mentioned in this SO answer2. That I think we can deal with.

The real problem I have, and that none of the tutorials I have found are treating, is the following:

How do I integrate Keycloak in this authorization server?

I've been having a look at the available Java Adapters. They look OK when it comes to authenticate but I did not see hints about how to manage clients from a custom authorization server (ie administer the realm).

I therefore suppose I should use the admin API. Am I correct and is it good practice? I saw no adapter for that, so I suppose I should then use the REST API.

I also wonder how we should integrate our users in design? Should they be duplicated inside Keycloak? In this case, should we use Keycloak's admin API to push the data from the authorization server or is there a better way?

Finally, am I missing some other obvious point?

Sorry for the long message and the many questions, but it all boils down to one question in the end:

What are the best practices when building an authorization server using Keycloak as a backbone?


1. Some examples: Spring Boot OAuth2 tutorial - A blog post - Another blog post

2. I've mainly focused on the sample app provided by Spring Security OAuth

like image 855
Chop Avatar asked Mar 07 '18 10:03

Chop


People also ask

Does Keycloak support OAuth2?

Understanding Keycloak users, clients, services, and realmsThis is the standard three-step OAuth 2 authentication scheme. As the user, you are the resource owner, the client application is the web portal, the authorization service is Keycloak, and the resource server is a set of microservices.

Is Keycloak an authorization server?

In Keycloak, resource servers are provided with a rich platform for enabling fine-grained authorization for their protected resources, where authorization decisions can be made based on different access control mechanisms. Any client application can be configured to support fine-grained permissions.


1 Answers

Building Java OAuth2.0 authorization server with Keycloak

This is possible but is bit tricky and there is lot of thing which needs to be customised.

You can derive some motivation from below repo.

keycloak-delegate-authn-consent

Building custom Java OAuth2.0 authorization server with MITREid

If you are open to use other implementations of Oauth and OIDC,I can suggest you MITREid which is referrence implementation of OIDC and could be customized to a great deal.Below is the link to its repo and its open source.

I myself used this to requirement similar to yours and it is highly customizable and easy to implement.

https://github.com/mitreid-connect/OpenID-Connect-Java-Spring-Server

MITREid Connect uses Spring Security for its authentication, so you can put whatever component you like into that space. There are lots of good resources on the web about how to write and configure Spring Security filters for custom authentication mechanisms.

You'll want to look at the user-context.xml file for where the user authentication is defined. In the core project this is a simple username/password field against a local database. In others like the LDAP overlay project, this connects to an LDAP server. In some systems, like MIT's "oidc.mit.edu" server, there are actually a handful of different authentication mechanisms that can be used in parallel: LDAP, kerberos, and certificates in that case.

Note that in all cases, you'll still need to have access to a UserInfo data store somewhere. This can be sourced from the database, from LDAP, or from something else, but it needs to be available for each logged in user.

The MITREid Connect server can function as an OpenID Connect Identity Provider (IdP) and an OAuth 2.0 Authorization Server (AS) simultaneously. The server is a Spring application and its configuration files are found in openid-connect-server-webapp/src/main/webapp/WEB-INF/ and end in .xml. The configuration has been split into multiple .xml files to facilitate overrides and custom configuration.

like image 67
Mahesh_Loya Avatar answered Oct 13 '22 07:10

Mahesh_Loya