ssr-keycloak
Version:
SSR compatible Keycloak authentication library for React applications
158 lines (157 loc) • 4.73 kB
TypeScript
export interface KeycloakConfig {
url: string;
realm: string;
clientId: string;
clientSecret?: string;
redirectUri?: string;
postLogoutRedirectUri?: string;
}
export type KeycloakFlow = 'authorization_code' | 'pkce';
export interface KeycloakUser {
id: string;
username: string;
email?: string;
firstName?: string;
lastName?: string;
fullName?: string;
roles: string[];
groups: string[];
emailVerified: boolean;
enabled: boolean;
realmAccess: {
roles: string[];
};
resourceAccess: Record<string, {
roles: string[];
}>;
}
export interface KeycloakTokens {
accessToken: string;
refreshToken: string;
idToken: string;
expiresAt: number;
refreshExpiresAt: number;
}
export interface KeycloakSession {
sessionId: string;
userId: string;
tokens: KeycloakTokens;
user: KeycloakUser;
realm: string;
clientId: string;
lastActivity: number;
expiresAt: number;
}
export interface KeycloakCookie {
name: string;
value: string;
expires?: Date;
maxAge?: number;
domain?: string;
path?: string;
secure?: boolean;
httpOnly?: boolean;
sameSite?: 'strict' | 'lax' | 'none';
}
export interface KeycloakServerContext {
isAuthenticated: boolean;
user: KeycloakUser | null;
tokens: KeycloakTokens | null;
session: KeycloakSession | null;
hasRole: (role: string, resource?: string) => boolean;
hasAnyRole: (roles: string[], resource?: string) => boolean;
hasAllRoles: (roles: string[], resource?: string) => boolean;
getUserRoles: (resource?: string) => string[];
}
export interface KeycloakClientContext extends KeycloakServerContext {
isLoading: boolean;
error: string | null;
login: (options?: LoginOptions) => Promise<void>;
logout: (redirectUri?: string) => Promise<void>;
refreshToken: () => Promise<boolean>;
updateToken: (minValidity: number) => Promise<boolean>;
clearSession: () => void;
isSessionValid: () => boolean;
}
export interface LoginOptions {
flow?: KeycloakFlow;
redirectUri?: string;
scope?: string;
state?: string;
nonce?: string;
}
export interface KeycloakProviderProps {
config: KeycloakConfig;
children: React.ReactNode;
onAuthSuccess?: (user: KeycloakUser) => void;
onAuthError?: (error: any) => void;
onAuthLogout?: () => void;
autoRefreshToken?: boolean;
refreshTokenInterval?: number;
enableLogging?: boolean;
}
export interface KeycloakOpenIDConfig {
issuer: string;
authorization_endpoint: string;
token_endpoint: string;
userinfo_endpoint: string;
end_session_endpoint: string;
jwks_uri: string;
check_session_iframe: string;
grant_types_supported: string[];
response_types_supported: string[];
subject_types_supported: string[];
id_token_signing_alg_values_supported: string[];
scopes_supported: string[];
token_endpoint_auth_methods_supported: string[];
claims_supported: string[];
code_challenge_methods_supported: string[];
}
export interface KeycloakRealmInfo {
realm: string;
public_key: string;
token_service: string;
account_service: string;
tokens_not_before: number;
access_token_lifespan: number;
access_token_lifespan_for_implicit_flow: number;
sso_session_idle_timeout: number;
sso_session_max_lifespan: number;
offline_session_idle_timeout: number;
offline_session_max_lifespan: number;
access_code_lifespan: number;
access_code_lifespan_user_action: number;
access_code_lifespan_login: number;
action_token_generated_by_user_lifespan: number;
action_token_generated_by_admin_lifespan: number;
oauth2_device_code_lifespan: number;
oauth2_device_polling_interval: number;
revoke_refresh_token: boolean;
refresh_token_max_reuse: number;
access_token_max_lifespan: number;
access_token_max_lifespan_for_implicit_flow: number;
login_timeout: number;
login_action_timeout: number;
oauth2_device_code_lifespan_user_action: number;
oauth2_device_code_lifespan_login: number;
}
export interface KeycloakMiddlewareConfig {
protectedRoutes?: string[];
publicRoutes?: string[];
loginRoute?: string;
redirectToLogin?: boolean;
cookieName?: string;
cookieSecret?: string;
}
export interface KeycloakRequestContext {
cookies: Record<string, string>;
headers: Record<string, string>;
url: string;
method: string;
}
export interface KeycloakResponseContext {
setCookie: (cookie: KeycloakCookie) => void;
deleteCookie: (name: string) => void;
redirect: (url: string) => void;
json: (data: any) => void;
}