(opts: RequiredPermissions)
| 985 | }, |
| 986 | extra: { |
| 987 | ...accountInfo, |
| 988 | isAccountLinking, |
| 989 | linkingSession, |
| 990 | }, |
| 991 | }); |
| 992 | if (accountInfo) |
| 993 | posthogClient.capture({ |
| 994 | distinctId: accountInfo.google_user_email, |
| 995 | event: operation + '_failed', |
| 996 | properties: { |
| 997 | error: error instanceof Error ? error.message : String(error), |
| 998 | email: accountInfo.google_user_email, |
| 999 | name: accountInfo.google_user_name, |
| 1000 | hosted_domain: accountInfo.hosted_domain, |
| 1001 | isAccountLinking, |
| 1002 | }, |
| 1003 | }); |
| 1004 | |
| 1005 | // Clear linking session if it was an account linking attempt |
| 1006 | return redirectUrlForCode('UNKNOWN-ERROR'); |
| 1007 | } |
| 1008 | }, |
| 1009 | async jwt({ token, account, user, trigger, profile }) { |
| 1010 | let accountInfo: CreateOrUpdateUserArgs | undefined = undefined; |
| 1011 | try { |
| 1012 | if (!trigger) return token; |
| 1013 | if (!account) throw new Error(`TRAP: No account found: ${trigger}`); |
| 1014 | |
| 1015 | accountInfo = createAccountInfo(account, user, profile); |
| 1016 | const existingUser = await findAndSyncExistingUser(accountInfo); |
| 1017 | |
| 1018 | assert(existingUser, `TRAP: No existing user found for ${accountInfo.google_user_email}`); |
| 1019 | |
| 1020 | token.kiloUserId = existingUser.id; |
| 1021 | |
| 1022 | token.version = JWT_TOKEN_VERSION; |
| 1023 | token.exp = Math.floor(Date.now() / 1000) + secondsInDay * 30; |
| 1024 | token.iat = Math.floor(Date.now() / 1000); |
| 1025 | token.isNewUser = (profile as ExtendedProfile)?.isNewUser || false; |
| 1026 | token.webSessionPepper = existingUser.web_session_pepper; |
| 1027 | token.isAdmin = existingUser.is_admin; |
| 1028 | token.authProvider = accountInfo.provider; |
| 1029 | token.authenticatedAt = token.iat; |
| 1030 | delete token.ssoSourceOrganizationId; |
| 1031 | |
| 1032 | if (accountInfo.provider === 'workos') { |
| 1033 | const domain = getLowerDomainFromEmail(existingUser.google_user_email); |
| 1034 | assert(domain, 'WorkOS user must have a valid primary email domain'); |
| 1035 | const ssoAuthority = await resolveSsoAuthorityForDomain(domain); |
| 1036 | assert( |
| 1037 | ssoAuthority.status === 'required', |
| 1038 | `WorkOS user does not have one active SSO authority for ${domain}` |
| 1039 | ); |
| 1040 | token.ssoSourceOrganizationId = ssoAuthority.sourceOrganizationId; |
| 1041 | } |
| 1042 | |
| 1043 | if (existingUser.is_admin) { |
| 1044 | // Admin audit trail: identify which Kilocode admin authenticated. |
no test coverage detected