Skip to content

Interpret "Authorization: Basic <user>:<pass>" header as basic auth #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lua/rest-nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ rest.run = function(verbose)
headers = result.headers,
raw = config.get("skip_ssl_verification") and { "-k" } or nil,
body = result.body,
auth = result.auth,
dry_run = verbose or false,
bufnr = result.bufnr,
start_line = result.start_line,
Expand Down
34 changes: 31 additions & 3 deletions lua/rest-nvim/request/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,34 @@ local function is_request_line(line)
return false
end

-- If the header_line is "Authorization: Basic <user>:<password>", returns "<user>:<password>" else false
local function get_basic_auth_credentials(header_line)
local header = utils.split(header_line, ":", 1)
local header_name = header[1]
local header_value = header[2]:gsub("^%s*", "")

if header_name ~= "Authorization" then
return false
end

local resolved_value = utils.replace_vars(header_value)
local i, j = string.find(resolved_value, "Basic ")
local colon_location = string.find(resolved_value, ":")

if i == 1 and j == 6 and colon_location > 6 then
return string.sub(resolved_value, 7)
else
return false
end
end

-- get_headers retrieves all the found headers and returns a lua table with them
-- @param bufnr Buffer number, a.k.a id
-- @param start_line Line where the request starts
-- @param end_line Line where the request ends
local function get_headers(bufnr, start_line, end_line)
local headers = {}
local auth = nil
local body_start = end_line

-- Iterate over all buffer lines starting after the request line
Expand All @@ -114,16 +136,21 @@ local function get_headers(bufnr, start_line, end_line)
end

local header = utils.split(line_content, ":")
local basic_auth = get_basic_auth_credentials(line_content)
local header_name = header[1]:lower()
table.remove(header, 1)
local header_value = table.concat(header, ":")
if not utils.contains_comments(header_name) then
headers[header_name] = utils.replace_vars(header_value)
if basic_auth then
auth = basic_auth
else
headers[header_name] = utils.replace_vars(header_value)
end
end
::continue::
end

return headers, body_start
return headers, auth, body_start
end

-- start_request will find the request line (e.g. POST http://localhost:8081/foo)
Expand Down Expand Up @@ -191,7 +218,7 @@ M.get_current_request = function()

local parsed_url = parse_url(vim.fn.getline(start_line))

local headers, body_start = get_headers(bufnr, start_line, end_line)
local headers, auth, body_start = get_headers(bufnr, start_line, end_line)

local body = get_body(bufnr, body_start, end_line)

Expand All @@ -206,6 +233,7 @@ M.get_current_request = function()
url = parsed_url.url,
headers = headers,
body = body,
auth = auth,
bufnr = bufnr,
start_line = start_line,
end_line = end_line,
Expand Down