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
12 changes: 6 additions & 6 deletions js/apps/admin-ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import { AuthWall } from "./root/AuthWall";
export const mainPageContentId = "kc-main-content-page-container";

const AppContexts = ({ children }: PropsWithChildren) => (
<WhoAmIContextProvider>
<RealmsProvider>
<RealmContextProvider>
<RealmsProvider>
<RealmContextProvider>
<WhoAmIContextProvider>
<RecentRealmsProvider>
<AccessContextProvider>
<Help>
Expand All @@ -34,9 +34,9 @@ const AppContexts = ({ children }: PropsWithChildren) => (
</Help>
</AccessContextProvider>
</RecentRealmsProvider>
</RealmContextProvider>
</RealmsProvider>
</WhoAmIContextProvider>
</WhoAmIContextProvider>
</RealmContextProvider>
</RealmsProvider>
);

export const App = () => {
Expand Down
10 changes: 8 additions & 2 deletions js/apps/admin-ui/src/context/whoami/WhoAmI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { adminClient } from "../../admin-client";
import environment from "../../environment";
import { DEFAULT_LOCALE, i18n } from "../../i18n/i18n";
import { useFetch } from "../../utils/useFetch";
import { useRealm } from "../realm-context/RealmContext";

export class WhoAmI {
constructor(private me?: WhoAmIRepresentation) {
Expand Down Expand Up @@ -66,15 +67,20 @@ export const useWhoAmI = () => useRequiredContext(WhoAmIContext);

export const WhoAmIContextProvider = ({ children }: PropsWithChildren) => {
const [whoAmI, setWhoAmI] = useState<WhoAmI>(new WhoAmI());
const { realm } = useRealm();
const [key, setKey] = useState(0);

useFetch(
() => adminClient.whoAmI.find({ realm: environment.loginRealm }),
() =>
adminClient.whoAmI.find({
realm: environment.loginRealm,
currentRealm: realm!,
}),
(me) => {
const whoAmI = new WhoAmI(me);
setWhoAmI(whoAmI);
},
[key],
[key, realm],
);

return (
Expand Down
6 changes: 5 additions & 1 deletion js/libs/keycloak-admin-client/src/resources/whoAmI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ export class WhoAmI extends Resource<{ realm?: string }> {
});
}

public find = this.makeRequest<{}, WhoAmIRepresentation>({
public find = this.makeRequest<
{ currentRealm: string },
WhoAmIRepresentation
>({
method: "GET",
path: "/whoami",
queryParamKeys: ["currentRealm"],
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public Response whoAmIPreFlight() {
@GET
@Produces(MediaType.APPLICATION_JSON)
@NoCache
public Response whoAmI() {
public Response whoAmI(@QueryParam("currentRealm") String currentRealm) {
RealmManager realmManager = new RealmManager(session);
AuthenticationManager.AuthResult authResult = new AppAuthManager.BearerTokenAuthenticator(session)
.setRealm(realm)
Expand Down Expand Up @@ -231,7 +231,7 @@ public Response whoAmI() {
if (createRealmRole != null) {
createRealm = user.hasRole(createRealmRole);
}
addMasterRealmAccess(user, realmAccess);
addMasterRealmAccess(user, currentRealm, realmAccess);
} else {
logger.debug("setting up realm access for a realm user");
addRealmAccess(realm, user, realmAccess);
Expand All @@ -251,11 +251,9 @@ private void addRealmAccess(RealmModel realm, UserModel user, Map<String, Set<St
getRealmAdminAccess(realm, realmAdminApp, user, realmAdminAccess);
}

private void addMasterRealmAccess(UserModel user, Map<String, Set<String>> realmAdminAccess) {
session.realms().getRealmsStream().forEach(realm -> {
ClientModel realmAdminApp = realm.getMasterAdminClient();
getRealmAdminAccess(realm, realmAdminApp, user, realmAdminAccess);
});
private void addMasterRealmAccess(UserModel user, String currentRealm, Map<String, Set<String>> realmAdminAccess) {
final RealmModel realm = session.realms().getRealmByName(currentRealm);
getRealmAdminAccess(realm, realm.getMasterAdminClient(), user, realmAdminAccess);
}

private static <T> HashSet<T> union(Set<T> set1, Set<T> set2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
*/
package org.keycloak.testsuite.admin;

import org.keycloak.Config;
import com.fasterxml.jackson.databind.JsonNode;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.keycloak.Config;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.broker.provider.util.SimpleHttp;
import org.keycloak.representations.AccessTokenResponse;
Expand Down Expand Up @@ -69,12 +69,11 @@ public void changeRealmTokenAlgorithm() throws Exception {
AccessTokenResponse accessToken = adminClient.tokenManager().getAccessToken();
assertNotNull(adminClient.realms().findAll());

String whoAmiUrl = suiteContext.getAuthServerInfo().getContextRoot().toString() + "/auth/admin/master/console/whoami";
String whoAmiUrl = suiteContext.getAuthServerInfo().getContextRoot().toString() + "/auth/admin/master/console/whoami?currentRealm=master";

JsonNode jsonNode = SimpleHttp.doGet(whoAmiUrl, client).auth(accessToken.getToken()).asJson();

assertTrue("Permissions for " + Config.getAdminRealm() + " realm.", jsonNode.at("/realm_access/" + Config.getAdminRealm()).isArray());
assertTrue("Permissions for " + REALM_NAME + " realm.", jsonNode.at("/realm_access/" + REALM_NAME).isArray());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ public void changeRealmTokenAlgorithm() throws Exception {
assertEquals(Algorithm.ES256, verifier.getHeader().getAlgorithm().name());

assertNotNull(adminClient.realms().findAll());

String whoAmiUrl = suiteContext.getAuthServerInfo().getContextRoot().toString() + "/auth/admin/master/console/whoami";

JsonNode jsonNode = SimpleHttp.doGet(whoAmiUrl, client).auth(accessToken.getToken()).asJson();
assertNotNull(jsonNode.get("realm"));
assertNotNull(jsonNode.get("userId"));
} finally {
TokenSignatureUtil.changeRealmTokenSignatureProvider("master", adminClient, defaultSignatureAlgorithm);
}
Expand Down