forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.lua
More file actions
278 lines (234 loc) · 6.92 KB
/
auth.lua
File metadata and controls
278 lines (234 loc) · 6.92 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
local api = require("copilot.api")
local c = require("copilot.client")
local logger = require("copilot.logger")
local M = {}
local function echo(message)
vim.cmd('echom "[Copilot] ' .. tostring(message):gsub('"', '\\"') .. '"')
end
-- Write hosts.json file with auth information
local function write_hosts_json(token, user)
local config_path = M.find_config_path()
if not config_path then
logger.error("Could not find config path")
return false
end
-- Make sure github-copilot directory exists
local github_copilot_dir = config_path .. "/github-copilot"
if vim.fn.isdirectory(github_copilot_dir) == 0 then
vim.fn.mkdir(github_copilot_dir, "p")
end
local hosts_data = {
["github.com"] = {
oauth_token = token,
user = user
}
}
local json_str = vim.json.encode(hosts_data)
local hosts_file = github_copilot_dir .. "/hosts.json"
local file = io.open(hosts_file, "w")
if not file then
logger.error("Failed to open " .. hosts_file .. " for writing")
return false
end
file:write(json_str)
file:close()
return true
end
-- Direct login with credentials
function M.direct_login()
-- Create input for GitHub token
vim.ui.input({
prompt = "Enter your GitHub Personal Access Token: ",
default = ""
}, function(token)
if not token or token == "" then
echo("Login canceled")
return
end
-- Get GitHub username from token
local username = M.get_github_username(token)
if not username then
echo("Invalid token or network error")
return
end
-- Write credentials to hosts.json
if write_hosts_json(token, username) then
echo("Authentication successful! Logged in as: " .. username)
else
echo("Failed to save authentication information")
end
end)
end
-- Function to get GitHub username using token
function M.get_github_username(token)
local response = vim.fn.system('curl -s --header "Authorization: Bearer ' .. token .. '" https://api.github.com/user')
if vim.v.shell_error ~= 0 then
logger.error("GitHub API request failed: " .. response)
return nil
end
local success, user_data = pcall(vim.json.decode, response)
if not success or not user_data or not user_data.login then
logger.error("Failed to parse GitHub user data")
return nil
end
return user_data.login
end
function M.setup(client)
local function copy_to_clipboard(str)
vim.cmd(string.format(
[[
let @+ = "%s"
let @* = "%s"
]],
str,
str
))
end
local function open_signin_popup(code, url)
local lines = {
" [Copilot] ",
"",
" First copy your one-time code: ",
" " .. code .. " ",
" In your browser, visit: ",
" " .. url .. " ",
"",
" ...waiting, it might take a while and ",
" this popup will auto close once done... ",
}
local height, width = #lines, math.max(unpack(vim.tbl_map(function(line)
return #line
end, lines)))
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
local winid = vim.api.nvim_open_win(bufnr, true, {
relative = "editor",
style = "minimal",
border = "single",
row = (vim.o.lines - height) / 2,
col = (vim.o.columns - width) / 2,
height = height,
width = width,
})
vim.api.nvim_set_option_value("winhighlight", "Normal:Normal", { win = winid })
return function()
vim.api.nvim_win_close(winid, true)
vim.api.nvim_buf_delete(bufnr, { force = true })
end
end
local initiate_setup = coroutine.wrap(function()
local cserr, status = api.check_status(client)
if cserr then
echo(cserr)
return
end
if status.user then
echo("Authenticated as GitHub user: " .. status.user)
return
end
local siierr, signin = api.sign_in_initiate(client)
if siierr then
echo(siierr)
return
end
if not signin.verificationUri or not signin.userCode then
echo("Failed to setup")
return
end
copy_to_clipboard(signin.userCode)
local close_signin_popup = open_signin_popup(signin.userCode, signin.verificationUri)
-- Fixed parameter name from userId to userCode
local sicerr, confirm = api.sign_in_confirm(client, { userCode = signin.userCode })
close_signin_popup()
if sicerr then
echo(sicerr)
return
end
if string.lower(confirm.status) ~= "ok" then
echo("Authentication failure: " .. confirm.error.message)
return
end
echo("Authenticated as GitHub user: " .. confirm.user)
end)
initiate_setup()
end
function M.signin()
-- Ask the user if they want to use direct login or browser login
vim.ui.select(
{"Direct login with token", "Browser login (original method)"},
{prompt = "Choose login method:"},
function(choice)
if choice == "Direct login with token" then
M.direct_login()
else
c.use_client(function(client)
M.setup(client)
end)
end
end
)
end
function M.signout()
c.use_client(function(client)
api.check_status(
client,
{ options = { localChecksOnly = true } },
---@param status copilot_check_status_data
function(err, status)
if err then
echo(err)
return
end
if status.user then
echo("Signed out as GitHub user " .. status.user)
else
echo("Not signed in")
end
api.sign_out(client, function() end)
end
)
end)
end
-- Make find_config_path available to other functions
function M.find_config_path()
local config = vim.fn.expand("$XDG_CONFIG_HOME")
if config and vim.fn.isdirectory(config) > 0 then
return config
elseif vim.fn.has("win32") > 0 then
config = vim.fn.expand("~/AppData/Local")
if vim.fn.isdirectory(config) > 0 then
return config
end
else
config = vim.fn.expand("~/.config")
if vim.fn.isdirectory(config) > 0 then
return config
else
logger.error("could not find config path")
end
end
return nil
end
local function oauth_user(token)
return vim.fn.system('curl -s --header "Authorization: Bearer ' .. token .. '" https://api.github.com/user')
end
M.get_cred = function()
local config_path = M.find_config_path()
local hosts_file = config_path .. "/github-copilot/hosts.json"
-- Check if hosts.json exists
if vim.fn.filereadable(hosts_file) == 0 then
logger.error("hosts.json file not found")
return nil
end
local success, userdata = pcall(function()
return vim.json.decode(vim.fn.readfile(hosts_file)[1])
end)
if not success or not userdata or not userdata["github.com"] then
logger.error("Failed to read hosts.json or invalid format")
return nil
end
local token = userdata["github.com"].oauth_token
local user = userdata["github.com"].user or M.get_github_username(token)
return { user = user, token = token }
end
return M