Skip to content

Commit 17514e3

Browse files
committed
Add GitHub Copilot auth token fetcher
1 parent e4b2336 commit 17514e3

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

Tool/Sources/GitHubCopilotService/GitHubCopilotExtension.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,35 @@ public final class GitHubCopilotExtension: BuiltinExtension {
2020
extensionUsage.isSuggestionServiceInUse || extensionUsage.isChatServiceInUse
2121
}
2222

23+
public struct AuthToken: Codable {
24+
public let user: String
25+
public let oauth_token: String
26+
public let githubAppId: String
27+
}
28+
29+
public static var authToken: AuthToken? {
30+
guard let urls = try? GitHubCopilotBaseService.createFoldersIfNeeded()
31+
else { return nil }
32+
let path = urls.supportURL
33+
.appendingPathComponent("undefined")
34+
.appendingPathComponent(".config")
35+
.appendingPathComponent("github-copilot")
36+
.appendingPathComponent("apps.json").path
37+
guard FileManager.default.fileExists(atPath: path) else { return nil }
38+
39+
do {
40+
let data = try Data(contentsOf: URL(fileURLWithPath: path))
41+
let json = try JSONSerialization
42+
.jsonObject(with: data, options: []) as? [String: [String: String]]
43+
guard let firstEntry = json?.values.first else { return nil }
44+
let jsonData = try JSONSerialization.data(withJSONObject: firstEntry, options: [])
45+
return try JSONDecoder().decode(AuthToken.self, from: jsonData)
46+
} catch {
47+
Logger.gitHubCopilot.error(error.localizedDescription)
48+
return nil
49+
}
50+
}
51+
2352
let workspacePool: WorkspacePool
2453

2554
let serviceLocator: ServiceLocatorType
@@ -139,7 +168,6 @@ protocol ServiceLocatorType {
139168
class ServiceLocator: ServiceLocatorType {
140169
let workspacePool: WorkspacePool
141170

142-
143171
init(workspacePool: WorkspacePool) {
144172
self.workspacePool = workspacePool
145173
}

0 commit comments

Comments
 (0)