Add basic auth (#17)
This commit is contained in:
1 parent
1734425b9c
commit
301cfe1b82
3 files changed
+61
-2
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user