Skip to content
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

Add basic auth #17

Merged
merged 1 commit into from
Nov 30, 2022
Merged
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
53 changes: 53 additions & 0 deletions src/components/WebServer/Middleware/BasicAuthMiddleware.bs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace Http

class BasicAuthMiddleware extends HttpRouter
public Username = "playlet"
public Password = "1234"

function new()
super()

m.All("*", function(context as object) as boolean
router = context.router
request = context.request
response = context.response

if not router.IsAuthorized(request, router)
response.headers["WWW-Authenticate"] = `Basic realm="Playlet web app"`
response.Default(401, "Unauthorized")
return true
end if

return false
end function)
end function

function IsAuthorized(request as HttpRequest, router as HttpRouter) as boolean
authHeader = request.headers["Authorization"]
if authHeader = invalid
return false
end if

if not authHeader.StartsWith("Basic ")
return false
end if

encoded = authHeader.Mid(6)

buffer = createObject("roByteArray")
buffer.FromBase64String(encoded)

decoded = buffer.ToAsciiString()

components = decoded.Tokenize(":")

if components.Count() = 2
return components[0] = router.Username and components[1] = router.Password
end if

return false
end function

end class

end namespace
8 changes: 7 additions & 1 deletion src/components/WebServer/Task/WebServerTask.bs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import "pkg:/components/WebServer/Middleware/RegistryApiRouter.bs"
import "pkg:/components/WebServer/Middleware/StateApiRouter.bs"
import "pkg:/components/WebServer/Middleware/CommandsApiRouter.bs"
import "pkg:/components/WebServer/Middleware/CorsMiddleware.bs"
import "pkg:/components/WebServer/Middleware/BasicAuthMiddleware.bs"

#const WEB_SERVER_BASIC_AUTH = false

function Init()
m.top.functionName = "WebServerLoop"
Expand All @@ -32,9 +35,12 @@ function WebServerLoop()
m.server.UseRouter(homeRouter)
m.server.UseRouter(new Http.CommandsApiRouter())
m.server.UseRouter(new Http.StateApiRouter())
m.server.UseRouter(new Http.RegistryApiRouter())
m.server.UseRouter(new Http.InvidiousRouter())
m.server.UseRouter(new Http.HttpStaticFilesRouter(m.settings.WwwRoot))
#if WEB_SERVER_BASIC_AUTH
m.server.UseRouter(new Http.BasicAuthMiddleware())
#end if
m.server.UseRouter(new Http.RegistryApiRouter())
m.server.UseRouter(new Http.HttpDefaultRouter())

timeout = m.settings.Timeout
Expand Down
2 changes: 1 addition & 1 deletion src/source/utils/WebUtils.bs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ namespace WebUtils
if hcm = invalid
hcm = { n200: "OK", n206: "Partial Content" }
hcm.append({ n301: "Moved Permanently", n304: "Not Modified" })
hcm.append({ n400: "Bad Request", n403: "Forbidden", n404: "Not Found", n413: "Request Entity Too Large" })
hcm.append({ n400: "Bad Request", n401: "Unauthorized", n403: "Forbidden", n404: "Not Found", n413: "Request Entity Too Large" })
hcm.append({ n500: "Internal Server Error", n501: "Not Implemented" })
m.HttpTitles = hcm
end if
Expand Down