File size: 2,331 Bytes
63522a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import axios from "axios";
import { AGENT_CANVAS_CLIENT_VERSION } from "./client-source";
import { getLockedCloudAuthMode } from "./agent-server-config";

export const MAIN_APP_AUTHENTICATE_PATH = "/api/authenticate";
export const MAIN_APP_ANALYTICS_EVENTS_PATH = "/api/analytics/events";
export const MAIN_APP_LOGIN_PATH = "/login";
export const MAIN_APP_LOGIN_REDIRECT_PARAM = "returnTo";

const LOCAL_BROWSER_HOSTNAMES = new Set(["localhost", "127.0.0.1", "::1"]);

function getCurrentPathForRedirect(): string {
  if (typeof window === "undefined") return "/";
  return `${window.location.pathname}${window.location.search}${window.location.hash}`;
}

export function isLocalBrowserHost(): boolean {
  if (typeof window === "undefined") return false;
  return LOCAL_BROWSER_HOSTNAMES.has(window.location.hostname);
}

export function shouldUseMainAppCookieAuth(): boolean {
  return getLockedCloudAuthMode() === "cookie" && !isLocalBrowserHost();
}

let canvasAuthenticatedReported = false;

export function _resetCanvasAuthReported(): void {
  canvasAuthenticatedReported = false;
}

export async function reportCanvasAuthenticated(): Promise<void> {
  if (canvasAuthenticatedReported) return;
  canvasAuthenticatedReported = true;
  try {
    await axios.post(
      MAIN_APP_ANALYTICS_EVENTS_PATH,
      {
        event_type: "canvas_authenticated",
        client_version: AGENT_CANVAS_CLIENT_VERSION,
      },
      { withCredentials: true },
    );
  } catch {
    // Analytics must never affect authentication or application startup.
  }
}

export async function authenticateWithMainAppCookie(): Promise<boolean> {
  try {
    await axios.post(MAIN_APP_AUTHENTICATE_PATH, null, {
      withCredentials: true,
    });
    void reportCanvasAuthenticated();
    return true;
  } catch (error) {
    if (axios.isAxiosError(error) && error.response?.status === 401) {
      return false;
    }
    throw error;
  }
}

export function buildMainAppLoginRedirect(
  redirectPath = getCurrentPathForRedirect(),
): string {
  const params = new URLSearchParams({
    [MAIN_APP_LOGIN_REDIRECT_PARAM]: redirectPath,
  });
  return `${MAIN_APP_LOGIN_PATH}?${params.toString()}`;
}

export function redirectToMainAppLogin(): void {
  if (typeof window === "undefined") return;
  window.location.assign(buildMainAppLoginRedirect());
}