- 7 minutes to read

Add Users and Claims in Keycloak

Give colleagues their own sign-in to the Nodinite Docker sandbox, and decide what each of them sees. The sandbox signs users in through Keycloak, an OAuth 2.0 and OpenID Connect identity provider, so access works with Claims, Policies, and Roles instead of Windows accounts. This is the same model as any Nodinite installation in OAuth 2.0 mode (Install Nodinite v7 - OpenID), which makes the sandbox a good place to learn it.

  • ✅ Add users in the Keycloak admin console, with their own password
  • ✅ Give a user a value in the token, as a user attribute or a realm role
  • ✅ Match that value with a Nodinite Claim, group Claims in a Policy, and grant Roles

In the Keycloak admin console, open the realm dev, add the user contoso.finance, set a password that isn't temporary, and assign the realm role nodinite-finance, animated walkthrough (7.12)
Animated walkthrough: a new Keycloak user with a password and a realm role, which reaches the token as roles. Steps 1 and 2 below.

Understanding How a Keycloak User Gets Access

Nodinite never stores sandbox users. At sign-in, Keycloak issues an access token with claims about the user, and the Web API matches those claims with the Claims of every Policy. A user who matches all Claims of a Policy gets that Policy's Roles, and the Roles decide what the user sees.

flowchart LR subgraph KC["fa:fa-id-card Keycloak, realm dev"] direction TB User["fa:fa-user User"] Attr["fa:fa-tag Attribute Group"] Role["fa:fa-user-tag Realm role"] User --> Attr User --> Role end subgraph Token["fa:fa-key Access token"] direction TB GroupClaim["Group"] RolesClaim["roles"] end subgraph Nod["fa:fa-shield-halved Nodinite"] direction TB Claim["fa:fa-key Claim"] --> Policy["fa:fa-shield-halved Policy"] Policy --> NRole["fa:fa-users-class Role"] end Attr --> GroupClaim Role --> RolesClaim Token -->|"matched"| Claim style KC fill:#FFD525 style Token fill:#2678BD style Nod fill:#93d16d

Diagram: a Keycloak user attribute or realm role becomes a claim in the access token; Nodinite matches it with a Claim, and the Policy that holds the Claim grants its Roles.

What Reaches the Token

The sandbox's Keycloak realm is called dev, and the Web Client signs in through its webclient client. That client puts these values in the token:

In Keycloak Claim in the token Key of the Nodinite Claim Example value
User attribute Group Group Group Admin
Realm roles roles http://schemas.microsoft.com/ws/2008/06/identity/claims/role nodinite-finance

The Key of a roles claim is the long name, not roles: .NET renames the standard role claim before Nodinite compares, so a Claim with the Key roles never matches. See How the Key is matched.

The Sample Administrator

The sandbox ships with the user Administrator, whose attribute Group is Admin. The token therefore carries Group = Admin, the Claim that the sandbox's Policy for the Administrators Role requires. Give another user the same attribute, and that user is an administrator too.

Membership of the Keycloak group Admin is not the same thing. Keycloak writes group membership into the token as the group's full path, Group = /Admin, which doesn't match the Claim Group = Admin, so such a user gets no Role. Use the attribute.

Attribute or Realm Role?

User attribute Realm role
Set on Each user, as a key and a value The realm, then assigned to users
Best for Making a user an administrator, as Administrator is Groups of users with the same access, such as a team
Claim Key in Nodinite Group http://schemas.microsoft.com/ws/2008/06/identity/claims/role
Rename later Change it on every user Change it once on the role

Before You Start

Step 1: Add the User in Keycloak

  1. Open the Keycloak admin console at http://<environment name>.localhost:8080/admin, for example http://demosandbox.localhost:8080/admin, with the Keycloak (sign-in) port you chose. Sign in with the Keycloak administrator: username admin, password P1ngP0ng. This account manages Keycloak only; it is not a Nodinite user. The console's yellow banner, You are logged in as a temporary admin user, is expected in the sandbox.

  2. In the menu on the left, click Manage realms, then the realm dev. The master realm, marked Current realm at first, is Keycloak's own; Nodinite users never go there.

    The Manage realms page of the Keycloak admin console, with master marked Current realm and dev as a link (7.12)
    Open dev: it holds the Nodinite users and the webclient client.

  3. Open Users and click Add user. Enter the Username, and optionally the Email, First name, and Last name, then click Create.

  4. Open the Credentials tab and click Set password. Enter the password twice, and switch Temporary off unless the user should choose a new password at the first sign-in. Click Save, then Save password in the Set password? dialog.

    The Set password dialog for contoso.finance with both password fields filled and Temporary off (7.12)
    With Temporary off, the user signs in with this password, without a password change.

Important

Keycloak keeps what you change in its admin console, such as users and passwords, inside its container. When the container is recreated, for example by an update that brings a new Keycloak, Keycloak imports the shipped realm again and your changes are gone. Note the users you add, so you can add them again.

Step 2: Give the User a Value in the Token

Do one of these, depending on your choice in Attribute or Realm Role?.

Make the User an Administrator

  1. On the user, open the Attributes tab.

  2. Click Add attributes, enter the Key Group and the Value Admin, and click Save.

    The Attributes tab of the user contoso.admin with the Key Group and the Value Admin, before Save (7.12)
    The attribute Group = Admin is what makes Administrator an administrator, too.

The user now matches the same Claim as Administrator. Skip to Step 4.

Give the User a Realm Role

  1. Open Realm roles, click Create role, enter a Role name such as nodinite-finance, and save. Skip this when the role exists.
  2. Open the user, then the Role mapping tab, and click Assign role.
  3. In the menu that opens, choose Realm roles, tick the role, and click Assign.

Step 3: Match the Value in Nodinite

Sign in to the Web Client as Administrator, and in Administration:

  1. Add the Claim: under Claims, add a Claim with the Key http://schemas.microsoft.com/ws/2008/06/identity/claims/role and the Value nodinite-finance. See Add or manage Claim.
  2. Add the Policy: under Policies, add a Policy, such as Contoso: Finance Users, that holds the Claim and grants the Role the user should have. See Add or manage Policy.
  3. Check the Role: its permission sets decide what the user sees: Log Views, Monitor Views, the Repository Model, and C4 Diagram Sets. A Role with Log Views but without the Log permission gives the user 403 Forbidden, Access level Log Views is required on Log. See Add or manage Role.

As Administrator, add the Claim with the role key and the value nodinite-finance, then the Policy Contoso: Finance Users with that Claim and the Role Contoso: Finance; signed in as contoso.finance, only the Role's two Log Views show, animated walkthrough (7.12)
Animated walkthrough: the Claim and Policy in Nodinite, then the new user's view: exactly the Log Views of the Role.

Warning

Policies can lock everyone out. A Policy matches only when the token carries all its Claims, so a Claim that Keycloak never sends matches nobody, administrators included. A Policy with no Claims matches everyone. Never change the Policy that makes Administrator an administrator, and keep Administrator signed in while you test. Details: What is a Policy?

Step 4: Sign In as the New User

  1. Open a private browser window, so you stay signed in as Administrator in the other one.
  2. Open the Web Client address, such as http://demosandbox.localhost:8081, and sign in with the new user's username and password.
  3. Check that the user sees what the Role grants, and nothing more.

Nodinite keeps each user's Policies and Roles for about 15 seconds, so a Claim or Policy change reaches a signed-in user quickly, without a new sign-in. A change in Keycloak, such as a new attribute or realm role, reaches Nodinite with the user's next token: sign out and in again.

The User Signs In but Sees Nothing

  • The token doesn't carry the value: check the attribute key (Group, case as shown) or the realm role assignment in Keycloak. Membership of the Keycloak group Admin gives /Admin, not Admin.
  • The user sees Access level Log Views is required: the Role has Log Views, but not the Log permission.
  • The Claim Key is roles instead of the long role Key; see How the Key is matched.
  • The Policy holds a second Claim that the user doesn't match.

Next Steps