-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathauth.ts
More file actions
115 lines (99 loc) · 2.94 KB
/
Copy pathauth.ts
File metadata and controls
115 lines (99 loc) · 2.94 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
export namespace AuthCopilot {
const CLIENT_ID = "Iv1.b507a08c87ecfe98";
const DEVICE_CODE_URL = "https://github.com/login/device/code";
const ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token";
const COPILOT_API_KEY_URL =
"https://api.github.com/copilot_internal/v2/token";
interface DeviceCodeResponse {
device_code: string;
user_code: string;
verification_uri: string;
expires_in: number;
interval: number;
}
interface AccessTokenResponse {
access_token?: string;
error?: string;
error_description?: string;
}
interface CopilotTokenResponse {
token: string;
expires_at: number;
refresh_in: number;
endpoints: {
api: string;
};
}
export async function authorize() {
const deviceResponse = await fetch(DEVICE_CODE_URL, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"User-Agent": "GitHubCopilotChat/0.26.7",
},
body: JSON.stringify({
client_id: CLIENT_ID,
scope: "read:user",
}),
});
const deviceData: DeviceCodeResponse = await deviceResponse.json();
return {
device: deviceData.device_code,
user: deviceData.user_code,
verification: deviceData.verification_uri,
interval: deviceData.interval || 5,
expiry: deviceData.expires_in,
};
}
export async function poll(device_code: string) {
const response = await fetch(ACCESS_TOKEN_URL, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"User-Agent": "GitHubCopilotChat/0.26.7",
},
body: JSON.stringify({
client_id: CLIENT_ID,
device_code,
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
}),
});
if (!response.ok) return { status: "failed" };
const data: AccessTokenResponse = await response.json();
if (data.access_token) {
return {
status: "success",
refresh: data.access_token,
access: "",
expires: 0,
};
}
if (data.error === "authorization_pending") return { status: "pending" };
if (data.error) return { status: "failed" } as const;
return { status: "pending" };
}
export async function access(refresh: string) {
const response = await fetch(COPILOT_API_KEY_URL, {
headers: {
Accept: "application/json",
Authorization: `Bearer ${refresh}`,
...HEADERS,
},
});
if (!response.ok) return;
const tokenData: CopilotTokenResponse = await response.json();
return {
refresh,
access: tokenData.token,
expires: tokenData.expires_at * 1000,
};
}
export const HEADERS: Record<string, string> = {
"User-Agent": "GitHubCopilotChat/0.26.7",
"Editor-Version": "vscode/1.99.3",
"Editor-Plugin-Version": "copilot-chat/0.26.7",
"Copilot-Integration-Id": "vscode-chat",
};
}