-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
95 lines (85 loc) · 3.2 KB
/
main.ts
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import axios, { AxiosError } from 'axios';
declare global {
interface Window {
AUTH_SERVICE: string;
}
}
const AUTH_SERVICE = window.AUTH_SERVICE;
console.log("AUTH_SERVICE:", AUTH_SERVICE);
/**
* Function to process input from the client.
* @params:
* - code_challenge: (PKCE challenge pair's challenge string)
* - client_id: (Public client identifier)
* - scope: (Array: can be CREATE, READ, UPDATE, DELETE)
* - referer: (URL of the client that called it)
**/
type handleFlowInputFromClientParams = {
code_challenge: string;
client_id: string;
scope: string[];
referer: string; // URL of the client that called it
};
export const processInputOutput = async () => {
let code_challenge: string | null;
let client_id: string | null;
let scope: string[] = [];
let referer: string | null;
try {
// Extract URL parameters
const url = new URL(window.location.href);
code_challenge = url.searchParams.get('code_challenge');
client_id = url.searchParams.get('client_id');
scope = url.searchParams.getAll('scope');
referer = url.searchParams.get('origin');
// Validate required parameters
if (!code_challenge || !client_id || !scope.length || !referer) {
console.error("Missing required parameters in the URL.");
return;
}
console.error("success")
} catch (error) {
console.error("Error while parsing URL parameters:", error);
return;
}
referer = decodeURIComponent(referer);
// Reading AUTH_SERVICE from the global window object
let responseUrl: string;
try {
responseUrl = (window as any).AUTH_SERVICE || '';
if (!responseUrl) {
throw new Error("AUTH_SERVICE URL is missing.");
}
} catch (error) {
console.error("Error while retrieving AUTH_SERVICE URL:", error);
return;
}
type handleFlowInputFromClientBody = Omit<handleFlowInputFromClientParams, "referer">;
const body: handleFlowInputFromClientBody = { code_challenge, client_id, scope };
try {
// Send the request to the backend
const response = await axios.post(responseUrl, body);
const authCode = response.data.authorization_code;
if (response.data && response.data.authorization_code) {
try {
// Send message to the opener window
window.opener?.postMessage({ "authorization_code": response.data.authorization_code }, referer);
} catch (error) {
console.error("Error while posting message to the opener:", error);
}
} else {
throw new Error("Invalid response type from service: missing 'authCode'.");
}
console.log("Success! Response:", response.data);
} catch (error: any) {
if (error instanceof AxiosError) {
console.error("Axios error occurred:", error.message);
if (error.response) {
console.error("Response data:", error.response.data);
console.error("Response status:", error.response.status);
}
} else {
console.error("Unexpected error occurred:", error);
}
}
};