| import { decrypt, encrypt } from "@midday/encryption"; |
|
|
| export function getInboxIdFromEmail(email: string) { |
| return email.split("@").at(0); |
| } |
|
|
| |
| export interface OAuthStatePayload { |
| teamId: string; |
| provider: "gmail" | "outlook"; |
| source: "inbox" | "apps"; |
| } |
|
|
| |
| |
| |
| |
| export function encryptOAuthState(payload: OAuthStatePayload): string { |
| return encrypt(JSON.stringify(payload)); |
| } |
|
|
| |
| |
| |
| |
| export function decryptOAuthState( |
| encryptedState: string, |
| ): OAuthStatePayload | null { |
| try { |
| const decrypted = decrypt(encryptedState); |
| const parsed = JSON.parse(decrypted); |
|
|
| |
| if ( |
| typeof parsed.teamId !== "string" || |
| !["gmail", "outlook"].includes(parsed.provider) || |
| !["inbox", "apps"].includes(parsed.source) |
| ) { |
| return null; |
| } |
|
|
| return parsed as OAuthStatePayload; |
| } catch { |
| return null; |
| } |
| } |
|
|
| export function getInboxEmail(inboxId: string) { |
| if (process.env.NODE_ENV !== "production") { |
| return `${inboxId}@inbox.staging.midday.ai`; |
| } |
|
|
| return `${inboxId}@inbox.midday.ai`; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function isAuthenticationError(errorMessage: string): boolean { |
| if (!errorMessage) return false; |
|
|
| const message = errorMessage.toLowerCase(); |
|
|
| |
| const oauthErrors = [ |
| "invalid_request", |
| "invalid_client", |
| "invalid_grant", |
| "unauthorized_client", |
| "unsupported_grant_type", |
| "invalid_scope", |
| "access_denied", |
| "invalid_token", |
| "token_expired", |
| ]; |
|
|
| |
| const httpAuthErrors = [ |
| "401", |
| "403", |
| "unauthorized", |
| "forbidden", |
| "unauthenticated", |
| ]; |
|
|
| |
| const googleSpecificErrors = [ |
| "authentication required", |
| "re-authentication required", |
| "reauthentication required", |
| "authentication failed", |
| "refresh token is invalid", |
| "access token is invalid", |
| "credentials have been revoked", |
| "token has been expired or revoked", |
| "invalid credentials", |
| "permission denied", |
| "insufficient permissions", |
| "api key not valid", |
| "api key expired", |
| ]; |
|
|
| |
| const microsoftSpecificErrors = [ |
| "invalidauthenticationtoken", |
| "lifetime validation failed", |
| "token is expired", |
| "aadsts700082", |
| "aadsts50076", |
| "aadsts700084", |
| "aadsts65001", |
| ]; |
|
|
| |
| const allAuthPatterns = [ |
| ...oauthErrors, |
| ...httpAuthErrors, |
| ...googleSpecificErrors, |
| ...microsoftSpecificErrors, |
| ]; |
|
|
| return allAuthPatterns.some((pattern) => message.includes(pattern)); |
| } |
|
|