equipes keycloak flow
This commit is contained in:
parent
b6978fb6b0
commit
2663d6f23a
@ -2,7 +2,6 @@ import { NextResponse } from "next/server";
|
|||||||
import { getServerSession } from "next-auth";
|
import { getServerSession } from "next-auth";
|
||||||
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
|
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
|
||||||
import { getKeycloakAdminClient } from "@/lib/keycloak";
|
import { getKeycloakAdminClient } from "@/lib/keycloak";
|
||||||
import { RoleRepresentation } from "@keycloak/keycloak-admin-client/lib/defs/roleRepresentation";
|
|
||||||
|
|
||||||
export async function GET(
|
export async function GET(
|
||||||
request: Request,
|
request: Request,
|
||||||
@ -14,7 +13,12 @@ export async function GET(
|
|||||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const { userId } = params;
|
// Safely access the userId parameter
|
||||||
|
const userId = String(params?.userId || '');
|
||||||
|
if (!userId) {
|
||||||
|
return NextResponse.json({ error: "User ID is required" }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
const kcAdminClient = await getKeycloakAdminClient();
|
const kcAdminClient = await getKeycloakAdminClient();
|
||||||
|
|
||||||
// Get all available roles
|
// Get all available roles
|
||||||
@ -48,7 +52,12 @@ export async function PUT(
|
|||||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const { userId } = params;
|
// Safely access the userId parameter
|
||||||
|
const userId = String(params?.userId || '');
|
||||||
|
if (!userId) {
|
||||||
|
return NextResponse.json({ error: "User ID is required" }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
const { roles } = await request.json();
|
const { roles } = await request.json();
|
||||||
const kcAdminClient = await getKeycloakAdminClient();
|
const kcAdminClient = await getKeycloakAdminClient();
|
||||||
|
|
||||||
@ -62,19 +71,19 @@ export async function PUT(
|
|||||||
|
|
||||||
// Find roles to add and remove
|
// Find roles to add and remove
|
||||||
const rolesToAdd = roles.filter(
|
const rolesToAdd = roles.filter(
|
||||||
(role: string) => !currentRoles.realmMappings?.some((r: RoleRepresentation) => r.name === role)
|
(role: string) => !currentRoles.realmMappings?.some((r: any) => r.name === role)
|
||||||
);
|
);
|
||||||
const rolesToRemove = currentRoles.realmMappings?.filter(
|
const rolesToRemove = currentRoles.realmMappings?.filter(
|
||||||
(role: RoleRepresentation) => !roles.includes(role.name)
|
(role: any) => !roles.includes(role.name)
|
||||||
);
|
);
|
||||||
|
|
||||||
// Add new roles
|
// Add new roles
|
||||||
for (const roleName of rolesToAdd) {
|
for (const roleName of rolesToAdd) {
|
||||||
const role = availableRoles.find((r: RoleRepresentation) => r.name === roleName);
|
const role = availableRoles.find((r: any) => r.name === roleName);
|
||||||
if (role) {
|
if (role) {
|
||||||
await kcAdminClient.users.addRealmRoleMappings({
|
await kcAdminClient.users.addRealmRoleMappings({
|
||||||
id: userId,
|
id: userId,
|
||||||
roles: [role],
|
roles: [role as any],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,7 +92,7 @@ export async function PUT(
|
|||||||
if (rolesToRemove && rolesToRemove.length > 0) {
|
if (rolesToRemove && rolesToRemove.length > 0) {
|
||||||
await kcAdminClient.users.delRealmRoleMappings({
|
await kcAdminClient.users.delRealmRoleMappings({
|
||||||
id: userId,
|
id: userId,
|
||||||
roles: rolesToRemove,
|
roles: rolesToRemove as any,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -212,7 +212,7 @@ export function Sidebar({ isOpen, onClose }: SidebarProps) {
|
|||||||
icon: Building2,
|
icon: Building2,
|
||||||
href: "/mediation",
|
href: "/mediation",
|
||||||
iframe: process.env.NEXT_PUBLIC_IFRAME_MEDIATIONS_URL,
|
iframe: process.env.NEXT_PUBLIC_IFRAME_MEDIATIONS_URL,
|
||||||
requiredRole: "mediation",
|
requiredRole: ["mediation", "expression"],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@ -21,27 +21,51 @@ export async function getKeycloakAdminClient(): Promise<KcAdminClient> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const kcAdminClient = new KcAdminClient({
|
const keycloakUrl = process.env.KEYCLOAK_BASE_URL || process.env.NEXT_PUBLIC_KEYCLOAK_ISSUER || 'http://localhost:8080';
|
||||||
baseUrl: process.env.NEXT_PUBLIC_KEYCLOAK_ISSUER || 'http://localhost:8080',
|
const adminClientId = process.env.KEYCLOAK_ADMIN_CLIENT_ID || 'admin-cli';
|
||||||
realmName: 'master', // Use master realm to manage other realms
|
const adminUsername = process.env.KEYCLOAK_ADMIN_USERNAME || 'admin';
|
||||||
});
|
const adminPassword = process.env.KEYCLOAK_ADMIN_PASSWORD || 'admin';
|
||||||
|
const realmName = process.env.KEYCLOAK_REALM || 'cercle';
|
||||||
|
|
||||||
// Authenticate admin client
|
console.log(`Connecting to Keycloak at ${keycloakUrl}, realm: ${realmName}`);
|
||||||
await kcAdminClient.auth({
|
|
||||||
clientId: process.env.KEYCLOAK_ADMIN_CLIENT_ID || 'admin-cli',
|
|
||||||
username: process.env.KEYCLOAK_ADMIN_USERNAME || 'admin',
|
|
||||||
password: process.env.KEYCLOAK_ADMIN_PASSWORD || 'admin',
|
|
||||||
grantType: 'password',
|
|
||||||
} as Credentials);
|
|
||||||
|
|
||||||
// Set the target realm to work with
|
try {
|
||||||
kcAdminClient.setConfig({
|
const kcAdminClient = new KcAdminClient({
|
||||||
realmName: process.env.KEYCLOAK_REALM || 'cercle',
|
baseUrl: keycloakUrl,
|
||||||
});
|
realmName: 'master', // Use master realm to manage other realms
|
||||||
|
});
|
||||||
|
|
||||||
// Cache the admin client
|
// Authenticate admin client
|
||||||
adminClient = kcAdminClient;
|
await kcAdminClient.auth({
|
||||||
return kcAdminClient;
|
clientId: adminClientId,
|
||||||
|
username: adminUsername,
|
||||||
|
password: adminPassword,
|
||||||
|
grantType: 'password',
|
||||||
|
} as Credentials);
|
||||||
|
|
||||||
|
console.log('Successfully authenticated with Keycloak admin client');
|
||||||
|
|
||||||
|
// Set the target realm to work with
|
||||||
|
kcAdminClient.setConfig({
|
||||||
|
realmName: realmName,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Cache the admin client
|
||||||
|
adminClient = kcAdminClient;
|
||||||
|
return kcAdminClient;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error connecting to Keycloak:', error);
|
||||||
|
// Add more detailed error information
|
||||||
|
if (error instanceof Error) {
|
||||||
|
console.error(`Error message: ${error.message}`);
|
||||||
|
console.error(`Error cause: ${error.cause}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// For debugging - show what values we're trying to use (without exposing the password)
|
||||||
|
console.error(`Debug info - URL: ${keycloakUrl}, Client ID: ${adminClientId}, Username: ${adminUsername}, Realm: ${realmName}`);
|
||||||
|
|
||||||
|
throw new Error(`Failed to connect to Keycloak: ${error instanceof Error ? error.message : String(error)}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user