added new basic auth endpoint

This commit is contained in:
ston1th 2022-08-21 14:59:31 +02:00
commit 8c9ab3b8d2
10 changed files with 249 additions and 51 deletions

View file

@ -154,28 +154,46 @@ func (c *Context) Error(i interface{}) {
func (c *Context) NotFound() {
c.Template("notFoundHandler")
c.Data = webData{Title: "404"}
c.Status = http.StatusNotFound
c.Response.WriteHeader(http.StatusNotFound)
c.setStatus(http.StatusNotFound)
c.Exec()
}
func (c *Context) Forbidden() {
c.Template("forbiddenHandler")
c.Data = webData{Title: "403"}
c.Status = http.StatusForbidden
c.Response.WriteHeader(http.StatusForbidden)
c.setStatus(http.StatusForbidden)
c.Exec()
}
func (c *Context) setStatus(status int) {
c.Status = status
c.Response.WriteHeader(status)
}
func (c *Context) plainResponse(msg string, status int) {
defer c.accessLog()
c.Status = status
http.Error(c.Response, msg, status)
}
func (c *Context) PlainUnauthorized() {
c.SetHeader("WWW-Authenticate", `Basic realm="auth/basic", charset="UTF-8"`)
c.plainResponse("unauthorized", http.StatusUnauthorized)
}
func (c *Context) PlainForbidden() {
c.SetHeader("WWW-Authenticate", `Basic realm="auth/basic", charset="UTF-8"`)
c.plainResponse("forbidden", http.StatusForbidden)
}
func (c *Context) PlainOK() {
c.plainResponse("ok", http.StatusOK)
}
func (c *Context) Template(name string) {
c.T = c.Srv.ts.Get(name)
}
func (c *Context) Write(buf []byte) (err error) {
_, err = c.Response.Write(buf)
return
}
func (c *Context) SetHeader(name, value string) {
c.Response.Header().Set(name, value)
}