import { ClusterToken, Token, UserData } from "@kumori/aurora-interfaces"; interface HandleTokenEventParams { eventData: any; tokenMap: Map; userData: UserData; } interface HandleTokenEventResult { isUserToken: boolean; isClusterToken: boolean; userToken: Token | null; clusterToken: ClusterToken | null; updatedUserTokens: Token[]; tokenId: string; } /** * Handles the "token" event from WebSocket messages * Processes both user tokens and cluster tokens */ export const handleTokenEvent = ({ eventData, tokenMap, userData, }: HandleTokenEventParams): HandleTokenEventResult => { const tokenParentKind = eventData.id.parent?.kind; const tokenId = eventData.id.name; if (tokenParentKind === "user") { const tokenInMap = tokenMap.get(tokenId); const newUserToken: Token = { name: tokenId, tenant: eventData.meta?.labels?.tenant || eventData.spec?.tenant || "", description: eventData.meta?.description || "", lastUsed: tokenInMap?.lastUsed || "", expiration: eventData.meta?.expiration ? new Date(eventData.meta.expiration * 1000).toISOString() : "", token: tokenInMap?.token || "", }; if (!tokenInMap) { tokenMap.set(tokenId, newUserToken); } const updatedUserTokens = [...userData.tokens]; const existingTokenIndex = updatedUserTokens.findIndex( (t) => t.name === tokenId ); if (existingTokenIndex !== -1) { updatedUserTokens[existingTokenIndex] = newUserToken; } else { updatedUserTokens.push(newUserToken); } return { isUserToken: true, isClusterToken: false, userToken: newUserToken, clusterToken: null, updatedUserTokens, tokenId, }; } else { const clusterTokenValue = eventData.spec.jwt; const clusterToken: ClusterToken = { token: clusterTokenValue, }; return { isUserToken: false, isClusterToken: true, userToken: null, clusterToken, updatedUserTokens: userData.tokens, tokenId: clusterTokenValue, }; } }; interface HandleTokenOperationSuccessParams { responsePayload: any; } interface HandleTokenOperationSuccessResult { token: Token; } /** * Handles successful token creation operation * Returns the token with full data from the response */ export const handleTokenOperationSuccess = ({ responsePayload, }: HandleTokenOperationSuccessParams): HandleTokenOperationSuccessResult => { const tokenWithData: Token = { name: responsePayload.data.jti, tenant: "", description: responsePayload.data.description || "", expiration: "", token: responsePayload.data.token, lastUsed: new Date().toISOString(), }; return { token: tokenWithData, }; };