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
147 lines (123 loc) · 3.61 KB
/
auth.lua
File metadata and controls
147 lines (123 loc) · 3.61 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
local api = require("copilot.api")
local M = {}
function M.setup(client)
local function echo(message)
vim.cmd('echom "[Copilot] ' .. tostring(message):gsub('"', '\\"') .. '"')
end
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_win_set_option(winid, "winhighlight", "Normal:Normal")
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)
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
local function 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
print("Error: could not find config path")
end
end
end
local function json_body(response)
if response.headers["content-type"] == "application/json" then
return vim.json.decode(response.body)
end
end
local function oauth_user(token)
return vim.fn.system('curl -s --header "Authorization: Bearer ' .. token .. '" https://api.github.com/user')
end
local function oauth_save(oauth_token)
local user_data = oauth_user(oauth_token)
local github = { oauth_token = oauth_token, user = user_data.login }
return github
end
M.get_cred = function()
local userdata = vim.json.decode(
vim.api.nvim_eval("readfile('" .. find_config_path() .. "/github-copilot/hosts.json')")[1]
)
local token = userdata["github.com"].oauth_token
local user = oauth_user(token)
return { user = user, token = token }
end
return M