Skip to content

Expose device info in Admin API UserSessionRepresentation - #51517

Open
markstos wants to merge 1 commit into
keycloak:mainfrom
markstos:issue-47980-device-info-in-admin-api
Open

Expose device info in Admin API UserSessionRepresentation#51517
markstos wants to merge 1 commit into
keycloak:mainfrom
markstos:issue-47980-device-info-in-admin-api

Conversation

@markstos

@markstos markstos commented Aug 6, 2026

Copy link
Copy Markdown

Expose device info (OS, browser, device) in Admin API UserSessionRepresentation,
addressing #47980.

This is also a feature we'd like downstream in the Mozilla Thundermail project.

In addition the automated test coverage added, additional manual testing was performed, as below in the QA Log.

This PR was generated with GenAI and includes test coverage and docs.

Thanks!

QA Log

Manual testeing result:

❯ xh GET http://localhost:8080/admin/realms/issue-47980/users/af71c77e-8a81-480f-8a27-5631972ed4e8/sessions 'Authorization:Bearer REDACTED'
HTTP/1.1 200 OK
Cache-Control: no-cache
Content-Length: 380
Content-Type: application/json;charset=UTF-8
Referrer-Policy: no-referrer
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Robots-Tag: none

[
    {
        "id": "nyPSR-_aTSXdwpDgDk45PVqY",
        "username": "device-user",
        "userId": "af71c77e-8a81-480f-8a27-5631972ed4e8",
        "ipAddress": "127.0.0.1",
        "start": 1786025333000,
        "lastAccess": 1786025333000,
        "rememberMe": false,
        "clients": {
            "36a93a08-d5a1-4e11-b0e5-28337904246d": "device-test"
        },
        "transientUser": false,
        "os": "iOS",
        "osVersion": "17.5",
        "browser": "Mobile Safari/17.5",
        "device": "iPhone",
        "mobile": true
    }
]

Copilot AI balanced review requested due to automatic review settings August 6, 2026 14:30
@markstos
markstos requested review from a team as code owners August 6, 2026 14:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Exposes session device metadata through Admin API representations.

Changes:

  • Adds OS, browser, device, and mobile fields.
  • Maps stored device notes into session responses.
  • Updates TypeScript definitions, documentation, and tests.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
core/.../UserSessionRepresentation.java Adds device fields and accessors.
server-spi-private/.../ModelToRepresentation.java Maps attached device metadata.
js/.../userSessionRepresentation.ts Updates TypeScript API types.
docs/.../admin-rest-api.adoc Documents response fields.
tests/.../SessionTest.java Adds device-field assertions.

Comment thread docs/documentation/server_development/topics/admin-rest-api.adoc Outdated
Closes keycloak#47980

Signed-off-by: Mark Stosberg <mstosberg@thunderbird.net>
Copilot AI review requested due to automatic review settings August 6, 2026 16:14
@markstos
markstos force-pushed the issue-47980-device-info-in-admin-api branch from c237e15 to e4c3c5b Compare August 6, 2026 16:14
@markstos

markstos commented Aug 6, 2026

Copy link
Copy Markdown
Author

Copilot feedback has been addressed with amended commit.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

Suppressed comments (2)

tests/base/src/test/java/org/keycloak/tests/admin/client/SessionTest.java:135

  • These non-null checks would still pass if the new fields were mapped incorrectly (for example, if OS and device were swapped) or reduced to placeholder values. Assert the known parser output for this fixed user agent so the test verifies each API field's value.
        assertNotNull(rep.getOs());
        assertNotNull(rep.getOsVersion());
        assertNotNull(rep.getBrowser());
        assertNotNull(rep.getDevice());

tests/base/src/test/java/org/keycloak/tests/admin/client/SessionTest.java:117

  • The new assertions exercise the client-scoped /clients/{id}/user-sessions API, not the /users/{user-id}/sessions endpoint this change documents and targets. Keep the existing client-session coverage, but retrieve user.admin().getUserSessions() and apply the device assertions to that response as well.
        List<UserSessionRepresentation> sessions = account.getUserSessions(0, 5);

@ahus1

ahus1 commented Aug 10, 2026

Copy link
Copy Markdown
Member

See my comment #47980 (comment) - this might not be the API you are looking for / should be looking for if you want to use it for the same reasons as the original author of the issue. Can you please confirm your use case?

Given that it was rejected by @pedroigor in #6127 in 2017, let me double-check: A lot of time has passed, and it hasn't been added. Do we want to be pragmatic to add it now? Either as properties (as suggested here), or as a nested class with the device info (preferred).

In addition to this, there would more items to fix: Recent browsers don't send all the high-entropy data by default any more. The new "Sec-UA" headers sent by Chrome are still experimental and Keycloak does not support them (until someone adds support for it).

When adding device info to the responses, it, it might be good to add it to SessionRepresentation which is used in the new admin console - but let's wait for @pedroigor's response.

@markstos

Copy link
Copy Markdown
Author

@ahus1 Thanks for the reply.

Can you please confirm your use case?

The end goal is to build a "Connected Apps" interface which lists details like app name and IP of client which use offline_access.

You are right call out that a user self-service API is a more secure design than using the admin API. We may have landed on looking a the Admin API because it's the only one documented under "API documentation" on the Keycloak website:

https://www.keycloak.org/documentation

By exploring the codebase some more, I've found GET /realms/{realm}/account/applications does expose offlineAccess, and that may be sufficient for us.

@ahus1 ahus1 self-assigned this Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants