Skip to content
Draft
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ local default_config = {
user_agent = "rest.nvim v" .. require("rest-nvim.api").VERSION,
---@type boolean Set `Content-Type` header when it is empty and body is provided
set_content_type = true,
---@type boolean Interpret `Authorization` header when it is set in form of
---"Basic username:password" or "Basic username password"
interpret_basic_auth = true,
},
},
---@class rest.Config.Response
Expand Down
12 changes: 8 additions & 4 deletions doc/rest-nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,14 @@ rest.Opts.Request *rest.Opts.Request*
rest.Opts.Request.Hooks *rest.Opts.Request.Hooks*

Fields: ~
{encode_url?} (boolean) Encode URL before making request (Default: `true`)
{user_agent?} (string) Set `User-Agent` header when it is empty. Set as empty string to disable.
(Default: `rest.nvim {version}`)
{set_content_type?} (boolean) Set `Content-Type` header when it is empty but request body is provided
{encode_url?} (boolean) Encode URL before making request (Default: `true`)
{user_agent?} (string) Set `User-Agent` header when it is empty. Set as empty string to disable.
(Default: `rest.nvim {version}`)
{set_content_type?} (boolean) Set `Content-Type` header when it is empty but request body is provided
{interpret_basic_auth?} (boolean) Interpret `Authorization` header when it is set in form of
"Basic username:password" or "Basic username password"
It will convert header to "Basic <base64-encoded-username:password>"
(Default: `true`)


rest.Opts.Response *rest.Opts.Response*
Expand Down
24 changes: 24 additions & 0 deletions lua/rest-nvim/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@

local autocmds = {}

---@param req rest.Request
local function interpret_basic_auth(req)
local auth_header = req.headers["authorization"]
if not auth_header then
return
end
local auth_header_value = auth_header[#auth_header]
local auth_type, id, pw = auth_header_value:match("(%w+)%s+([^:%s]+)%s*[:(%s+)]%s*(.*)")
if auth_type == "Basic" then
auth_header[#auth_header] = "Basic " .. require("base64").encode(id .. ":" .. pw)
elseif auth_type == "Digest" then

Check failure on line 56 in lua/rest-nvim/autocmds.lua

View workflow job for this annotation

GitHub Actions / luacheck

empty if branch
-- TODO: implement digest tokens... but how?
-- we can update headers but digest tokens require multiple requests
-- So it can only be supported after we have chained requests support.
-- see https://github.com/catwell/lua-http-digest/blob/master/http-digest.lua
-- as example implementation of digest tokens
else
require("rest-nvim.logger").info("Unsupported auth-type:", auth_type)
end
end

---Set up Rest autocommands group
---@package
function autocmds.setup()
Expand Down Expand Up @@ -81,6 +102,9 @@
end
end
end
if hooks.interpret_basic_auth then
interpret_basic_auth(req)
end
end,
})
vim.api.nvim_create_autocmd("User", {
Expand Down
3 changes: 3 additions & 0 deletions lua/rest-nvim/config/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ local default_config = {
user_agent = "rest.nvim v" .. require("rest-nvim.api").VERSION,
---@type boolean Set `Content-Type` header when it is empty and body is provided
set_content_type = true,
---@type boolean Interpret `Authorization` header when it is set in form of
---"Basic username:password" or "Basic username password"
interpret_basic_auth = true,
},
},
---@class rest.Config.Response
Expand Down
5 changes: 5 additions & 0 deletions lua/rest-nvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ local config
---@field user_agent? string
--- Set `Content-Type` header when it is empty but request body is provided
---@field set_content_type? boolean
--- Interpret `Authorization` header when it is set in form of
--- "Basic username:password" or "Basic username password"
--- It will convert header to "Basic <base64-encoded-username:password>"
--- (Default: `true`)
---@field interpret_basic_auth? boolean

---@class rest.Opts.Response
--- Default response hooks (aka. request handlers) configuration
Expand Down
Loading
Loading