54 lines
1.5 KiB
Plaintext
54 lines
1.5 KiB
Plaintext
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
|