Implement some authentication
This commit is contained in:
7
lua/auth.d.tl
Normal file
7
lua/auth.d.tl
Normal file
@@ -0,0 +1,7 @@
|
||||
local record Auth
|
||||
id: string
|
||||
token: string
|
||||
server: string
|
||||
end
|
||||
|
||||
return Auth
|
||||
@@ -2,12 +2,13 @@ local json = require("json")
|
||||
local Framebuffer = require("framebuffer")
|
||||
local Ringbuffer = require("ringbuffer")
|
||||
local Socket = require("socket")
|
||||
local UUID <const> = "8b9faf9f-9470-4a50-b405-0af5f0152550"
|
||||
local ENDPOINT <const> = "ws://localhost:8000/ipmi/computer/" .. UUID .. "/ws"
|
||||
local auth = require("auth")
|
||||
local ENDPOINT <const> = auth.server:gsub("http", "ws", 1) .. "/ipmi/computer/" .. auth.id .. "/ws"
|
||||
local HEADERS <const> = { ["Authorization"] = "Bearer " .. auth.token }
|
||||
|
||||
print("[MAIN] Init")
|
||||
|
||||
local socket = Socket.new(ENDPOINT)
|
||||
local socket = Socket.new(ENDPOINT, HEADERS)
|
||||
|
||||
-- Set up framebuffer capture and statusline
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ local record Socket
|
||||
close: function(self: Socket)
|
||||
signal_viewer_connect: function(self: Socket, connected: boolean)
|
||||
_endpoint: string
|
||||
_headers: {string: string}
|
||||
_callback: StateCallback
|
||||
_ws: http.Websocket
|
||||
end
|
||||
@@ -60,7 +61,7 @@ impl.send = function(self: Socket, message: string)
|
||||
end
|
||||
|
||||
impl.reconnect = function(self: Socket)
|
||||
local r = http.websocket(self._endpoint)
|
||||
local r = http.websocket(self._endpoint, self._headers)
|
||||
if r ~= false then
|
||||
self._ws = r as http.Websocket
|
||||
self:_set_state("ok")
|
||||
@@ -80,10 +81,11 @@ impl.signal_viewer_connect = function(self: Socket, connected: boolean)
|
||||
self:_set_state(new_state)
|
||||
end
|
||||
|
||||
local function new(endpoint: string): Socket
|
||||
local function new(endpoint: string, headers: {string: string}): Socket
|
||||
return setmetatable({
|
||||
state = "reset",
|
||||
_endpoint = endpoint,
|
||||
_headers = headers,
|
||||
_callback = function(_: State) end,
|
||||
_ws = nil,
|
||||
}, { __index = impl })
|
||||
|
||||
Reference in New Issue
Block a user