8000 Add per-application session termination to Account Console by muhammedogz · Pull Request #47922 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/documentation/server_admin/topics/account.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ The *Applications* menu item shows users which applications you can access. In t

image:images/account-console-applications.png[Applications]

==== Ending an application session

You can end your active sessions with a specific application directly from the *Applications* page. Applications that have an active session are marked as *In use* and display an *End application session* button. The Account Console itself does not display this button.

.Procedure

. Click *Applications* in the menu.
. Find the application whose session you want to end.
. Click *End application session* next to the application.
. Click *Confirm* in the confirmation dialog.

Your sessions with the application are ended, including any offline sessions. If the application supports it, {project_name} also notifies the application. Because this does not sign you out from {project_name}, the application can sign you in again without asking for your credentials when you access it again. To fully sign out from all applications, use the *Sign out* action on the *Device activity* page instead.

Ending an application session does not revoke the access you granted to the application, so it can obtain new sessions the next time you use it. To revoke the granted access, use the *Remove access* action for the application.

=== Viewing group memberships

You can view the groups you are associated with by clicking the *Groups* menu.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ close=Close
email=Email
signOutWarning=Sign out the session?
removeConsentError=Could not remove consent due to\: {{error}}
endApplicationSession=End application session
endApplicationSessionSuccess=Ended the application session for {{name}}
endApplicationSessionError=Could not end the application session due to\: {{error}}
endApplicationSessionMessage=This ends your sessions with {{name}}, including any offline sessions. Because your browser is still signed in, the application can sign you in again without asking for your credentials when you access it again.
Comment thread
ahus1 marked this conversation as resolved.
Resolved
signOutAllDevicesWarning=This action will sign out all the devices that have signed in to your account, including the current device you are using.
unShareSuccess=Resource successfully un-shared.
signingIn=Signing in
Expand Down
33 changes: 29 additions & 4 deletions js/apps/account-ui/src/api/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,43 @@ export async function deleteConsent(
context: KeycloakContext<BaseEnvironment>,
id: string,
) {
return request(`/applications/${encodeURIComponent(id)}/consent`, context, {
method: "DELETE",
});
const response = await request(
`/applications/${encodeURIComponent(id)}/consent`,
context,
{ method: "DELETE" },
);
if (!response.ok) {
throw await parseResponse(response);
}
return response;
}

export async function deleteSession(
context: KeycloakContext<BaseEnvironment>,
id?: string,
) {
return request(`/sessions${id ? `/${id}` : ""}`, context, {
const response = await request(`/sessions${id ? `/${id}` : ""}`, context, {
method: "DELETE",
});
if (!response.ok) {
throw await parseResponse(response);
}
return response;
}

export async function deleteApplicationSessions(
context: KeycloakContext<BaseEnvironment>,
clientId: string,
) {
const response = await request(
`/applications/${encodeURIComponent(clientId)}/sessions`,
context,
{ method: "DELETE" },
);
if (!response.ok) {
throw await parseResponse(response);
}
return response;
}

export async function getCredentials({ signal, context }: CallOptions) {
Expand Down
53 changes: 51 additions & 2 deletions js/apps/account-ui/src/applications/Applications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ import { useState } from "react";
import { useTranslation } from "react-i18next";

import { AccountEnvironment } from "..";
import { deleteConsent, getApplications } from "../api/methods";
import {
deleteApplicationSessions,
deleteConsent,
getApplications,
} from "../api/methods";
import { ClientRepresentation } from "../api/representations";
import { Page } from "../components/page/Page";
import type { TFuncKey } from "../i18n-type";
Expand Down Expand Up @@ -84,6 +88,16 @@ export const Applications = () => {
}
};

const endApplicationSession = async (id: string, name: string) => {
try {
await deleteApplicationSessions(context, id);
refresh();
addAlert(t("endApplicationSessionSuccess", { name }));
} catch (error) {
addError("endApplicationSessionError", error);
}
};

if (!applications) {
return <Spinner />;
}
Expand Down Expand Up @@ -125,6 +139,11 @@ export const Applications = () => {
>
<strong>{t("status")}</strong>
</DataListCell>,
<DataListCell
key="applications-list-action-header"
width={2}
className="pf-v5-u-pt-md"
/>,
]}
/>
</DataListItemRow>
Expand Down Expand Up @@ -180,7 +199,37 @@ export const Applications = () => {
{application.offlineAccess ? ", " + t("offlineAccess") : ""}
</DataListCell>,
<DataListCell width={2} key={`status${application.clientId}`}>
{application.inUse ? t("inUse") : t("notInUse")}
{application.inUse || application.offlineAccess
? t("inUse")
: t("notInUse")}
Comment on lines +202 to +204
</DataListCell>,
<DataListCell width={2} key={`action${application.clientId}`}>
{(application.inUse || application.offlineAccess) &&
application.clientId !== context.environment.clientId && (
<ContinueCancelModal
buttonTitle={t("endApplicationSession")}
modalTitle={t("endApplicationSession")}
continueLabel={t("confirm")}
cancelLabel={t("cancel")}
buttonVariant="secondary"
=>
endApplicationSession(
application.clientId,
label(
t,
application.clientName || application.clientId,
),
)
}
>
{t("endApplicationSessionMessage", {
name: label(
t,
application.clientName || application.clientId,
),
})}
Comment thread
ahus1 marked this conversation as resolved.
Resolved
</ContinueCancelModal>
)}
</DataListCell>,
]}
/>
Expand Down
144 changes: 144 additions & 0 deletions js/apps/account-ui/test/applications.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,148 @@ test.describe("Applications", () => {
await expect(applications.nth(1)).toContainText("Beta Application");
await expect(applications.nth(2)).toContainText("Zebra Application");
});

test("ends the session of an application", async ({ page }) => {
await using testBed = await createTestBed();

// Mock an application with an active session, the backend behavior is covered by AccountRestServiceTest.
await page.route("**/applications", async (route) => {
await route.fulfill({
status: 200,
contentType: "application/json",
body: JSON.stringify([
{
clientId: "my-application",
clientName: "My Application",
inUse: true,
},
]),
});
});

let sessionsDeleted = false;
await page.route(
"**/applications/my-application/sessions",
async (route) => {
expect(route.request().method()).toBe("DELETE");
sessionsDeleted = true;
await route.fulfill({ status: 204 });
},
);

// Log in and navigate to the applications page.
await login(page, testBed.realm);
await page.getByTestId("applications").click();

// The application has an active session, so the action is shown.
const endSessionButton = page.getByRole("button", {
name: "End application session",
exact: true,
});
await expect(endSessionButton).toBeVisible();

// Click the action and confirm the modal.
await endSessionButton.click();
await page.getByRole("button", { name: "Confirm", exact: true }).click();

// Expect a success alert after the sessions have been deleted.
await expect(page.getByTestId("last-alert")).toContainText(
"Ended the application session for My Application",
);
expect(sessionsDeleted).toBe(true);
});

test("does not show the end session action for applications without an active session or for the Account Console itself", async ({
page,
}) => {
await using testBed = await createTestBed();

await page.route("**/applications", async (route) => {
await route.fulfill({
status: 200,
contentType: "application/json",
body: JSON.stringify([
{
clientId: "account-console",
clientName: "Account Console",
inUse: true,
},
{
clientId: "other-application",
clientName: "Other Application",
inUse: false,
},
]),
});
});

// Log in and navigate to the applications page.
await login(page, testBed.realm);
await page.getByTestId("applications").click();

// Neither the Account Console itself nor an application without an active session shows the action.
await expect(page.getByTestId("applications-list-item")).toHaveCount(2);
await expect(
page.getByRole("button", {
name: "End application session",
exact: true,
}),
).toHaveCount(0);
});

test("shows the end session action for an application with only offline access", async ({
page,
}) => {
await using testBed = await createTestBed();

// The application has no active online session, only offline access.
await page.route("**/applications", async (route) => {
await route.fulfill({
status: 200,
contentType: "application/json",
body: JSON.stringify([
{
clientId: "offline-application",
clientName: "Offline Application",
inUse: false,
offlineAccess: true,
},
]),
});
});

let sessionsDeleted = false;
await page.route(
"**/applications/offline-application/sessions",
async (route) => {
expect(route.request().method()).toBe("DELETE");
sessionsDeleted = true;
await route.fulfill({ status: 204 });
},
);

// Log in and navigate to the applications page.
await login(page, testBed.realm);
await page.getByTestId("applications").click();

// Offline access counts as "in use" and exposes the action.
const listItem = page.getByTestId("applications-list-item");
await expect(listItem).toContainText("In use");

const endSessionButton = page.getByRole("button", {
name: "End application session",
exact: true,
});
await expect(endSessionButton).toBeVisible();

// Click the action and confirm the modal.
await endSessionButton.click();
await page.getByRole("button", { name: "Confirm", exact: true }).click();

// Expect a success alert after the sessions have been deleted.
await expect(page.getByTestId("last-alert")).toContainText(
"Ended the application session for Offline Application",
);
expect(sessionsDeleted).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.keycloak.representations.idm.GroupRepresentation;
import org.keycloak.services.ErrorResponse;
import org.keycloak.services.managers.Auth;
import org.keycloak.services.managers.AuthenticationManager;
import org.keycloak.services.managers.UserConsentManager;
import org.keycloak.services.messages.Messages;
import org.keycloak.services.resources.account.resources.ResourcesService;
Expand Down Expand Up @@ -391,6 +392,56 @@ public Response revokeConsent(final @PathParam("clientId") String clientId) {
return Response.noContent().build();
}

/**
* Ends all active online and offline sessions of the user for the client with the given client id.
* The client sessions are detached from the user sessions, and the client is notified
* via backchannel logout when supported. A user session that no longer has any client
* sessions afterward is removed.
*
* @param clientId client id to end the sessions for
* @return returns 204 if the sessions were ended
*/
@Path("/applications/{clientId}/sessions")
@DELETE
@NoCache
public Response revokeApplicationSessions(final @PathParam("clientId") String clientId) {
checkAccountApiEnabled();
auth.require(AccountRoles.MANAGE_ACCOUNT);

event.event(EventType.LOGOUT);
ClientModel client = realm.getClientByClientId(clientId);
if (client == null) {
// Return 204 instead of 404 to prevent client enumeration via this endpoint.
String msg = String.format("No client with clientId: %s found.", clientId);
event.detail(Details.REASON, msg);
event.error(Errors.CLIENT_NOT_FOUND);
return Response.noContent().build();
}
Comment thread
ahus1 marked this conversation as resolved.
Resolved
Comment on lines +411 to +419

Stream.concat(session.sessions().getUserSessionsStream(realm, user), session.sessions().getOfflineUserSessionsStream(realm, user))
.filter(userSession -> userSession.getAuthenticatedClientSessionByClient(client.getId()) != null)
.toList() // collect to avoid concurrent modification.
.forEach(userSession -> {
Comment on lines +421 to +424
AuthenticationManager.backchannelLogoutUserSessionFromClient(session, realm, userSession, client,
session.getContext().getUri(), headers);
if (userSession.getAuthenticatedClientSessions().isEmpty()) {
Comment thread
ahus1 marked this conversation as resolved.
Resolved
// The last client session was ended, remove the now empty user session as well.
if (userSession.isOffline()) {
session.sessions().removeOfflineUserSession(realm, userSession);
} else {
session.sessions().removeUserSession(realm, userSession);
}
}
event.clone()
.event(EventType.LOGOUT)
.detail(Details.REVOKED_CLIENT, client.getClientId())
.session(userSession)
.success();
});

return Response.noContent().build();
}

/**
* Creates or updates the consent of the given, requested consent for
* the client with the given client id. Returns the appropriate REST response.
Expand Down
Loading
Loading
0