User Management¶
This guide explains how to create and manage users in Keycloak for testing the Authentication Test API.
Prerequisites¶
- Keycloak installed and running
secure-testrealm createdschedule-userrole createdauthentication-test-apiclient configured
Creating Test Users¶
We'll create two test users as specified in the requirements:
- testuser1 - User with schedule-user role
- testuser2 - User with schedule-user role
Step 1: Create First User (testuser1)¶
Create User:
- Select "secure-test" realm from dropdown
- Go to "Users" in left menu
- Click "Create new user"
- Enter User Details:
- Username:
testuser1(required) - Email:
testuser1@example.com - Email verified: ✓ ON
- First name:
Test - Last name:
User One - Enabled: ✓ ON (user can login)
- Required user actions: (leave empty)
- Click "Create"
Set Password:
- Go to "Credentials" tab
- Click "Set password"
- Enter Password Details:
- Password:
password123 - Password confirmation:
password123 - Temporary: ✗ OFF (user won't need to change password)
- Click "Save"
- Confirm by clicking "Save password"
Assign Role:
- Go to "Role mapping" tab
- Click "Assign role"
- Filter by realm roles
- Select "schedule-user"
- Click "Assign"
You should see schedule-user in the "Assigned roles" list.
# Get admin token
TOKEN=$(curl -X POST http://localhost:8080/realms/master/protocol/openid-connect/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=admin" \
-d "password=admin" \
-d "grant_type=password" \
-d "client_id=admin-cli" \
| jq -r '.access_token')
# Create user
curl -X POST http://localhost:8080/admin/realms/secure-test/users \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"username": "testuser1",
"email": "testuser1@example.com",
"emailVerified": true,
"firstName": "Test",
"lastName": "User One",
"enabled": true,
"credentials": [{
"type": "password",
"value": "password123",
"temporary": false
}]
}'
# Get user ID
USER_ID=$(curl -X GET "http://localhost:8080/admin/realms/secure-test/users?username=testuser1" \
-H "Authorization: Bearer $TOKEN" \
| jq -r '.[0].id')
# Get role ID
ROLE_ID=$(curl -X GET http://localhost:8080/admin/realms/secure-test/roles/schedule-user \
-H "Authorization: Bearer $TOKEN" \
| jq -r '.id')
# Assign role to user
curl -X POST "http://localhost:8080/admin/realms/secure-test/users/$USER_ID/role-mappings/realm" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "[{
\"id\": \"$ROLE_ID\",
\"name\": \"schedule-user\"
}]"
Step 2: Create Second User (testuser2)¶
Repeat the same process for the second user:
- Go to "Users" → "Create new user"
- Enter User Details:
- Username:
testuser2 - Email:
testuser2@example.com - Email verified: ✓ ON
- First name:
Test - Last name:
User Two - Enabled: ✓ ON
- Click "Create"
- Set Password:
- Password:
password123 - Temporary: ✗ OFF
- Assign Role:
- Assign
schedule-userrole
# Create second user
curl -X POST http://localhost:8080/admin/realms/secure-test/users \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"username": "testuser2",
"email": "testuser2@example.com",
"emailVerified": true,
"firstName": "Test",
"lastName": "User Two",
"enabled": true,
"credentials": [{
"type": "password",
"value": "password123",
"temporary": false
}]
}'
Get user ID and assign role (same as testuser1)¶
USER_ID=$(curl -X GET "http://localhost:8080/admin/realms/secure-test/users?username=testuser2" \ -H "Authorization: Bearer $TOKEN" \ | jq -r '.[0].id')
curl -X POST "http://localhost:8080/admin/realms/secure-test/users/$USER_ID/role-mappings/realm" \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d "[{ \"id\": \"$ROLE_ID\", \"name\": \"schedule-user\" }]"
## Step 3: Verify Users
### List All Users
=== "Admin Console"
1. Go to "Users"
2. You should see both testuser1 and testuser2
=== "REST API"
```bash
curl -X GET http://localhost:8080/admin/realms/secure-test/users \
-H "Authorization: Bearer $TOKEN" \
| jq '.[] | {username, email, enabled}'
```
### Test User Login
Test that users can authenticate:
```bash
# Test testuser1
curl -X POST http://localhost:8080/realms/secure-test/protocol/openid-connect/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "client_id=authentication-test-api" \
-d "client_secret=YOUR_CLIENT_SECRET" \
-d "grant_type=password" \
-d "username=testuser1" \
-d "password=password123" \
| jq
# Test testuser2
curl -X POST http://localhost:8080/realms/secure-test/protocol/openid-connect/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "client_id=authentication-test-api" \
-d "client_secret=YOUR_CLIENT_SECRET" \
-d "grant_type=password" \
-d "username=testuser2" \
-d "password=password123" \
| jq
Both should return access tokens.
Verify Role Assignment¶
Check that users have the schedule-user role:
# Get user's roles
USER_ID=$(curl -X GET "http://localhost:8080/admin/realms/secure-test/users?username=testuser1" \
-H "Authorization: Bearer $TOKEN" \
| jq -r '.[0].id')
curl -X GET "http://localhost:8080/admin/realms/secure-test/users/$USER_ID/role-mappings/realm" \
-H "Authorization: Bearer $TOKEN" \
| jq '.[] | {name, description}'
Should show schedule-user role.
User Management Operations¶
Update User Information¶
Reset Password¶
- Go to "Users" → Select user
- Go to "Credentials" tab
- Click "Reset password"
- Enter new password
- Set "Temporary" if user should change it
- Click "Save"
Disable User¶
Delete User¶
User Attributes¶
Add Custom Attributes¶
You can add custom attributes to users:
- Go to "Users" → Select user
- Go to "Attributes" tab
- Click "Add attribute"
- Enter key and value
- Click "Save"
Groups (Optional)¶
Create Group¶
- Go to "Groups"
- Click "Create group"
- Enter:
- Name:
api-users - Click "Create"
Assign Users to Group¶
- Go to "Users" → Select user
- Go to "Groups" tab
- Click "Join group"
- Select "api-users"
- Click "Join"
Assign Role to Group¶
- Go to "Groups" → Select "api-users"
- Go to "Role mapping" tab
- Assign "schedule-user" role
Now all users in the group automatically get the role.
Testing with API¶
Get Token for testuser1¶
TOKEN1=$(curl -X POST http://localhost:8080/realms/secure-test/protocol/openid-connect/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "client_id=authentication-test-api" \
-d "client_secret=YOUR_CLIENT_SECRET" \
-d "grant_type=password" \
-d "username=testuser1" \
-d "password=password123" \
| jq -r '.access_token')
Call API¶
Expected response:
{
"user": "testuser1",
"schedule": [
{
"date": "2026-01-28",
"time": "09:00",
"description": "Team standup meeting"
}
]
}
Test with testuser2¶
TOKEN2=$(curl -X POST http://localhost:8080/realms/secure-test/protocol/openid-connect/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "client_id=authentication-test-api" \
-d "client_secret=YOUR_CLIENT_SECRET" \
-d "grant_type=password" \
-d "username=testuser2" \
-d "password=password123" \
| jq -r '.access_token')
curl http://localhost:9080/api/v1/schedule \
-H "Authorization: Bearer $TOKEN2" \
| jq
Each user should see their own schedule.
Bulk User Creation¶
Using Script¶
Create multiple users with a script:
#!/bin/bash
# Get admin token
TOKEN=$(curl -s -X POST http://localhost:8080/realms/master/protocol/openid-connect/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=admin" \
-d "password=admin" \
-d "grant_type=password" \
-d "client_id=admin-cli" \
| jq -r '.access_token')
# Get role ID
ROLE_ID=$(curl -s -X GET http://localhost:8080/admin/realms/secure-test/roles/schedule-user \
-H "Authorization: Bearer $TOKEN" \
| jq -r '.id')
# Create users
for i in {3..10}; do
echo "Creating testuser$i..."
# Create user
curl -s -X POST http://localhost:8080/admin/realms/secure-test/users \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "{
\"username\": \"testuser$i\",
\"email\": \"testuser$i@example.com\",
\"emailVerified\": true,
\"firstName\": \"Test\",
\"lastName\": \"User $i\",
\"enabled\": true,
\"credentials\": [{
\"type\": \"password\",
\"value\": \"password123\",
\"temporary\": false
}]
}"
# Get user ID
USER_ID=$(curl -s -X GET "http://localhost:8080/admin/realms/secure-test/users?username=testuser$i" \
-H "Authorization: Bearer $TOKEN" \
| jq -r '.[0].id')
# Assign role
curl -s -X POST "http://localhost:8080/admin/realms/secure-test/users/$USER_ID/role-mappings/realm" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "[{
\"id\": \"$ROLE_ID\",
\"name\": \"schedule-user\"
}]"
echo "Created testuser$i"
done
echo "Done!"
Troubleshooting¶
User Can't Login¶
- Check user is enabled
- Verify password is correct
- Check user has required role
- Verify client configuration
Token Missing Username¶
Ensure preferred_username mapper is enabled:
- Go to "Client scopes" → "profile"
- Go to "Mappers" tab
- Verify "username" mapper exists
Role Not in Token¶
- Verify user has the role assigned
- Check role mapper configuration in client
- Ensure mapper adds role to access token
Next Steps¶
- API Authentication - Use tokens with the API
- API Endpoints - Test all API endpoints
- Troubleshooting - Common issues