Skip to content

feat: interpret auth tokens #529

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
@@ -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
12 changes: 8 additions & 4 deletions doc/rest-nvim.txt
Original file line number Diff line number Diff line change
@@ -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*
24 changes: 24 additions & 0 deletions lua/rest-nvim/autocmds.lua
Original file line number Diff line number Diff line change
@@ -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

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()
@@ -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", {
3 changes: 3 additions & 0 deletions lua/rest-nvim/config/default.lua
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions lua/rest-nvim/config/init.lua
Original file line number Diff line number Diff line change
@@ -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
Loading