From 8616177e7eb3b9e18313cfb9bd17220364005f70 Mon Sep 17 00:00:00 2001 From: "khuong@breezing" Date: Sun, 23 Mar 2025 18:41:22 +0700 Subject: [PATCH 1/4] fix auth issue --- lua/copilot/auth.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lua/copilot/auth.lua b/lua/copilot/auth.lua index 4b5106b4..b13355d9 100644 --- a/lua/copilot/auth.lua +++ b/lua/copilot/auth.lua @@ -1,3 +1,5 @@ +-- lua/copilot/auth.lua (fixed version) + local api = require("copilot.api") local c = require("copilot.client") local logger = require("copilot.logger") @@ -82,7 +84,7 @@ function M.setup(client) local close_signin_popup = open_signin_popup(signin.userCode, signin.verificationUri) - local sicerr, confirm = api.sign_in_confirm(client, { userCode = signin.userCode }) + local sicerr, confirm = api.sign_in_confirm(client, { userId = signin.userCode }) close_signin_popup() @@ -157,7 +159,7 @@ end M.get_cred = function() local userdata = - vim.json.decode(vim.api.nvim_eval("readfile('" .. find_config_path() .. "/github-copilot/hosts.json')")[1]) + 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 } From cf18a18650d7880e78937fd0c80de7adcfcf9241 Mon Sep 17 00:00:00 2001 From: "khuong@breezing" Date: Sun, 23 Mar 2025 18:53:49 +0700 Subject: [PATCH 2/4] fix --- lua/copilot/auth.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lua/copilot/auth.lua b/lua/copilot/auth.lua index b13355d9..9865deb3 100644 --- a/lua/copilot/auth.lua +++ b/lua/copilot/auth.lua @@ -1,5 +1,3 @@ --- lua/copilot/auth.lua (fixed version) - local api = require("copilot.api") local c = require("copilot.client") local logger = require("copilot.logger") @@ -84,6 +82,7 @@ function M.setup(client) local close_signin_popup = open_signin_popup(signin.userCode, signin.verificationUri) + -- Thay đổi từ userCode thành userId theo đúng định nghĩa API local sicerr, confirm = api.sign_in_confirm(client, { userId = signin.userCode }) close_signin_popup() From d608b419c2d2378e03f9e13373b3e43aa2127cbd Mon Sep 17 00:00:00 2001 From: "khuong@breezing" Date: Sun, 23 Mar 2025 19:11:24 +0700 Subject: [PATCH 3/4] fix typo --- lua/copilot/auth.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/copilot/auth.lua b/lua/copilot/auth.lua index 9865deb3..f5015da8 100644 --- a/lua/copilot/auth.lua +++ b/lua/copilot/auth.lua @@ -83,7 +83,7 @@ function M.setup(client) local close_signin_popup = open_signin_popup(signin.userCode, signin.verificationUri) -- Thay đổi từ userCode thành userId theo đúng định nghĩa API - local sicerr, confirm = api.sign_in_confirm(client, { userId = signin.userCode }) + local sicerr, confirm = api.sign_in_confirm(client, { userCode = signin.userCode }) close_signin_popup() From 8c7119e71dafb3b382aa82d26ea74b311eb21ee2 Mon Sep 17 00:00:00 2001 From: "khuong@breezing" Date: Sun, 23 Mar 2025 19:19:05 +0700 Subject: [PATCH 4/4] quick fix --- lua/copilot/auth.lua | 127 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 119 insertions(+), 8 deletions(-) diff --git a/lua/copilot/auth.lua b/lua/copilot/auth.lua index f5015da8..65afbf59 100644 --- a/lua/copilot/auth.lua +++ b/lua/copilot/auth.lua @@ -8,6 +8,87 @@ 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( @@ -82,7 +163,7 @@ function M.setup(client) local close_signin_popup = open_signin_popup(signin.userCode, signin.verificationUri) - -- Thay đổi từ userCode thành userId theo đúng định nghĩa API + -- Fixed parameter name from userId to userCode local sicerr, confirm = api.sign_in_confirm(client, { userCode = signin.userCode }) close_signin_popup() @@ -104,9 +185,20 @@ function M.setup(client) end function M.signin() - c.use_client(function(client) - M.setup(client) - end) + -- 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() @@ -133,7 +225,8 @@ function M.signout() end) end -local function find_config_path() +-- 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 @@ -150,6 +243,7 @@ local function find_config_path() logger.error("could not find config path") end end + return nil end local function oauth_user(token) @@ -157,10 +251,27 @@ local function oauth_user(token) end M.get_cred = function() - local userdata = - vim.json.decode(vim.api.nvim_eval("readfile('" .. find_config_path() .. "/github-copilot/hosts.json')")[1]) + 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 = oauth_user(token) + local user = userdata["github.com"].user or M.get_github_username(token) + return { user = user, token = token } end