It's possible to check if an user has an active/valid session through REST API in Keycloak?

java, keycloak

Solution

you can check your session with this address:

http://keycloakAddressAndPort/auth/realms/develop/protocol/openid-connect/userinfo

if your session is not ok you see this response (http status code 401):

{
    "error": "invalid_request",
    "error_description": "User session not found or doesn't have client attached on it"
}

and if that is ok you see something like this (http status code 200):

{
    "sub": "c0c25095-63e7-471d-a39e-f3b157c91fd7",
    "email_verified": true,
    "name": "Amir Azizkhani",
    "preferred_username": "a.azizkhani@...",
    "given_name": "Amir",
    "family_name": "Azizkhani",
    "email": "a.azizkhani@...."
}

complete postman json:

{
    "info": {
        "_postman_id": "77ce65c3-948a-4b3d-a97b-b11cd00c593b",
        "name": "Spring Keycloak",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [
    
        {
            "name": "openid-connect/userinfo",
            "request": {
                "auth": {
                    "type": "bearer",
                    "bearer": [
                        {
                            "key": "token",
                            "value": "eyJhbGciOiJSUzI1NiIsInR5cC...",
                            "type": "string"
                        }
                    ]
                },
                "method": "GET",
                "header": [
                    {
                        "key": "accept",
                        "value": "application/json"
                    }
                ],
                "url": {
                    "raw": "http://192.168.131.33:8080/auth/realms/develop/protocol/openid-connect/userinfo",
                    "protocol": "http",
                    "host": [
                        "192",
                        "168",
                        "131",
                        "33"
                    ],
                    "port": "8080",
                    "path": [
                        "auth",
                        "realms",
                        "develop",
                        "protocol",
                        "openid-connect",
                        "userinfo"
                    ]
                },
                "description": "Dont forget to set Bearer value obtains from token request"
            },
            "response": []
        }
    ]
}

Problem

It's possible to check if an user have an active/valid session through REST API? I'm using the Java REST Admin client. I saw the UserSessionRepresentation returned by ``` List<UserSessionRepresentation> usr = Keycloak.realm("realmId").users().get("userId").getUserSessions(); ``` But UserSessionRepresentation do not have the information I want. It's possible?

Original source