I’m developing the service with consist with two Web API apps (main and second), and two front apps (Web UI and Mobile app). I’m using the Keycloak for auth purposes (OAuth, OIDC, JWT Tokens). I wanted to define the roles in Keycloak that will be used by both front apps (Mobile app by only one role, so it has limited capabilities, but not sure if this is important). On the API side I wanted to use the API scopes and have defined mapping between roles and scopes somewhere. Also, I wanted to have 4 clients defined in Keycloak (main API-, second API-, Web UI-, Mobile-client).
Questions are:
And bonus question: can you recommend me a good tutorial/book for designing the auth solutions like mentioned above?
Earlier I've read a book about the auth protocols, gone through the Keycloak documentation and bunch of different articles about auth with Keycloak but in most cases they are showing only a case with the only one client for all applications. And I'm still not sure what is the right direction.
Well here is the usual type of approach, to demonstrate the main relationships.
SCOPES
When getting started designing scopes, consider using them to represent business areas, eg sales
. Then, use more fine grained scopes if you find a need for them, eg sales:history
.
CLIENTS
By default you should only need to register 2 clients: for the web and mobile apps.
To begin with, you might register each of them with a sales
scope to enable them to call sales-related APIs. If your web app supports extra areas to the mobile app, you might add extra scopes to it, eg sales:history
.
ROLES
You should not try to map scopes to roles. Instead, a role is a claim
within the access token that cannot be evaluated until the user authenticates. You configure each scope to contain one or more claims. Eg a sales
scope could contain a role
claim and result in the role value being issued to the access token.
API AUTHORIZATION
Every API endpoint should reject access tokens without its required scope. For example, one endpoint might require a sales:history
scope and therefore deny access if called by the mobile client.
Ultimately, every API endpoint must enforce its authorization rules correctly. Usually this is primarily done based on the access token claims. Scopes and claims are building blocks for authorization and you can design solutions in multiple ways.