Skip to content

Commit 40ceee1

Browse files
wip: add md5 dependency
1 parent b388cee commit 40ceee1

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

lua/rest-nvim/autocmds.lua

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@
4343

4444
local autocmds = {}
4545

46+
---@param req rest.Request
47+
local function interpret_basic_auth(req)
48+
local auth_header = req.headers["authorization"]
49+
if not auth_header then
50+
return
51+
end
52+
local auth_header_value = auth_header[#auth_header]
53+
local auth_type, id, pw = auth_header_value:match("(%w+)%s+([^:%s]+)%s*[:(%s+)]%s*(.*)")
54+
if auth_type == "Basic" then
55+
auth_header[#auth_header] = "Basic " .. require("base64").encode(id .. ":" .. pw)
56+
elseif auth_type == "Digest" then
57+
-- TODO: implement digest tokens... but how?
58+
-- we can update headers but digest tokens require multiple requests
59+
-- So it can only be supported after we have chained requests support.
60+
-- see https://github.com/catwell/lua-http-digest/blob/master/http-digest.lua
61+
-- as example implementation of digest tokens
62+
else
63+
require("rest-nvim.logger").info("Unsupported auth-type:", auth_type)
64+
end
65+
end
66+
4667
---Set up Rest autocommands group
4768
---@package
4869
function autocmds.setup()
@@ -82,12 +103,7 @@ function autocmds.setup()
82103
end
83104
end
84105
if hooks.interpret_basic_auth then
85-
local auth_header = req.headers["authorization"]
86-
if auth_header then
87-
local auth_header_value = auth_header[#auth_header]
88-
local id, pw = auth_header_value:match("Basic%s+([^:%s]+)%s*[:(%s+)]%s*(.*)")
89-
auth_header[#auth_header] = "Basic " .. require("base64").encode(id .. ":" .. pw)
90-
end
106+
interpret_basic_auth(req)
91107
end
92108
end,
93109
})

nix/plugin-overlay.nix

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@
2020
};
2121
disabled = luaOlder "5.1";
2222
}) {};
23+
md5 = luaself.callPackage ({
24+
buildLuarocksPackage,
25+
fetchurl,
26+
fetchzip,
27+
luaOlder,
28+
}:
29+
buildLuarocksPackage {
30+
pname = "md5";
31+
version = "1.3-1";
32+
knownRockspec = (fetchurl {
33+
url = "mirror://luarocks/md5-1.3-1.rockspec";
34+
sha256 = "sha256-2S6Dvk0yazW8DCJf2DYxrLW11+B7r6DCJZ7CMCMAfSI=";
35+
}).outPath;
36+
src = fetchzip {
37+
url = "https://github.com/lunarmodules/md5/archive/refs/tags/1.3.zip";
38+
sha256 = "sha256-mCN7mmCGf2vdXriT1q8rrhUmKh49AY8A+cZVCu6XJzY=";
39+
};
40+
disabled = luaOlder "5.0";
41+
}) {};
2342
rest-nvim = luaself.callPackage ({
2443
buildLuarocksPackage,
2544
fetchurl,
@@ -39,6 +58,7 @@
3958
xml2lua
4059
fidget-nvim
4160
base64
61+
md5
4262
tree-sitter-http
4363
];
4464
}) {};

nix/test-overlay.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
xml2lua
1616
fidget-nvim
1717
final.lua51Packages.base64
18+
final.lua51Packages.md5
1819
final.lua51Packages.tree-sitter-http
1920
];
2021
extraPackages = [

spec/examples/examples_spec.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,9 @@ describe("builtin request hooks", function()
158158
end)
159159
end)
160160
end)
161+
it("make sure md5 work", function()
162+
local md5 = require("md5")
163+
local md5sum = md5.sumhexa
164+
assert.same("9236657b478ea807fdfa275d24990843", md5sum("qwer:asdf"))
165+
-- TODO: implement digest auth with https://github.com/catwell/lua-http-digest/blob/master/http-digest.lua
166+
end)

0 commit comments

Comments
 (0)