I've been assigned to overhaul an app that relies on Keycloak. Currently, when a user logs into the app, their session only lasts for 5 minutes before their access token expires. In the JavaScript code, each function checks if there's an updated access token in the response and if so then stores it. That means if the user is just browsing around the app without making a request, then they'll be logged out. I'm wanting to fix it so that if there's user activity of any sort (even just moving the mouse or browsing through pages), then the access token can be updated.
All of the other requests work fine, and the login request returns a refresh token in its response. However, when I make a POST request to get a new access token using the refresh token, then I get a 401 error. I've tried making this request inside the app and also using Postman and Insomnia, and I always get a 401 error.
Here's the request I'm making:
- POST method
- URL: http://localhost:8080/admin/realms/{realm-name}/protocol/openid-connect/token
- Form:
- client_id: admin-cli
- grant_type: refresh_token
- refresh_token: [refresh token here]
- Headers:
- Content-Type: when using Insomnia, it's "application/x-www-form-urlencoded"; in the app, it's "application/json"
I've read that a client_secret is required when the client is confidential. However, 'admin-cli' is not confidential and doesn't have a client secret set up. Also, I've checked in the Keycloak Admin UI that refresh tokens are enabled.
Thanks for any help, it's much appreciated!
UPDATE: JWTs decoded
Here's the payloads of the access token and refresh token from BenchVue's answer:
ACCESS TOKEN
{
"exp": 1707230290,
"iat": 1707229990,
"jti": "41833ad2-d6a3-45d6-9ac3-fd4793c749a5",
"iss": "http://localhost:8080/realms/my-realm",
"sub": "f273bb9e-7a2a-49c3-b7f5-241efd4a4afd",
"typ": "Bearer",
"azp": "admin-cli",
"session_state": "114d3130-4502-4e63a448-9fc96e464879",
"acr": "1",
"scope": "profile email",
"sid": "114d3130-4502-4e63-a448-9fc96e464879",
"email_verified": true,
"preferred_username": "user1"
}
REFRESH TOKEN
{
"exp": 1707231790,
"iat": 1707229990,
"jti": "ecfc896f-93a8-4aa3-a2f9-c323d91c66ef",
"iss": "http://localhost:8080/realms/my-realm",
"aud": "http://localhost:8080/realms/my-realm",
"sub": "f273bb9e-7a2a-49c3-b7f5-241efd4a4afd",
"typ": "Refresh",
"azp": "admin-cli",
"session_state": "114d3130-4502-4e63-a448-9fc96e464879",
"scope": "profile email",
"sid": "114d3130-4502-4e63-a448-9fc96e464879"
}
And here's the access and refresh token from company app:
ACCESS TOKEN
{
"exp": 1707230135,
"iat": 1707229835,
"jti": "54942e59-4d25-4233-aefd-6c6c9a972c0a",
"iss": "http://localhost:8080/realms/[redacted]",
"sub": "8dd3a5a5-b467-4d65-9b2b-95da87d8bb36",
"typ": "Bearer",
"azp": "admin-cli",
"session_state": "558057da-dd11-43e6-ab10-aa35aa4a7235",
"acr": "1",
"scope": "email profile",
"sid": "558057da-dd11-43e6-ab10-aa35aa4a7235",
"email_verified": true,
"name": "[redacted]",
"preferred_username": "[redacted]",
"given_name": "[redacted]",
"family_name": "[redacted]",
"email": "[redacted]@test.com"
}
REFRESH TOKEN
{
"exp": 1707231635,
"iat": 1707229835,
"jti": "8dc3d5c1-636a-4645-b653-070a267da710",
"iss": "http://localhost:8080/realms/[redacted]",
"aud": "http://localhost:8080/realms/[redacted]",
"sub": "8dd3a5a5-b467-4d65-9b2b-95da87d8bb36",
"typ": "Refresh",
"azp": "admin-cli",
"session_state": "558057da-dd11-43e6-ab10-aa35aa4a7235",
"scope": "email profile",
"sid": "558057da-dd11-43e6-ab10-aa35aa4a7235"
}




Two cases
Case 1 - Client authentication OFF
Will this setting can get new tokens
Step 1 get tokens
In
teststabInput Body with
x-www-form-urlencodedformatStep 2 get new tokens by refresh token
Input Body with
x-www-form-urlencodedformatCase 2 - Client authentication ON
This URL and body data will get new tokens
URL
Input Body with
x-www-form-urlencodedformatI will demo the whole process from user logging to get the
refresh tokenby API in your local PC.Requirement for Demo
Save as
docker-compose.ymlRun it
It will launch Keycloak version 23.0.3
Setting Keycloak
Step 1Create 'my_realm'Step 2Create 'my_client'Step 3Add redirect URI 'http://localhost:3000/auth/callback'Step 4settingmy_clientconfigurationStep 5copy Client Secret for demo(server.js)Step 6create user1 and set password by '1234'Demo code
Save as 'server.js'
Install server dependencies
run server.js
login user1
Open Browser
After login, the will be displayed token in the Browser
Get new Tokens
Get new Tokens by Postman
Copy the refresh_token from Browser to Postman Then click the
Sendbutton, the other setting is to follow the top explain.