mirror of
https://github.com/amir20/dozzle.git
synced 2026-06-23 04:10:12 +00:00
dd2eba59b2
Deploy VitePress site to Pages / build (push) Has been cancelled
Deploy VitePress site to Pages / Deploy (push) Has been cancelled
Push container / Push branches and PRs (push) Has been cancelled
Test / Typecheck (push) Has been cancelled
Test / JavaScript Tests (push) Has been cancelled
Test / Go Tests (push) Has been cancelled
Test / Go Staticcheck (push) Has been cancelled
Test / Integration Tests (push) Has been cancelled
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func (h *handler) createToken(w http.ResponseWriter, r *http.Request) {
|
|
user := r.PostFormValue("username")
|
|
pass := r.PostFormValue("password")
|
|
|
|
if token, err := h.config.Authorization.Authorizer.CreateToken(user, pass); err == nil {
|
|
expires := time.Time{}
|
|
if h.config.Authorization.TTL > 0 {
|
|
expires = time.Now().Add(h.config.Authorization.TTL)
|
|
}
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
Name: "jwt",
|
|
Value: token,
|
|
HttpOnly: true,
|
|
Path: "/",
|
|
SameSite: http.SameSiteLaxMode,
|
|
Secure: isHTTPS(r),
|
|
Expires: expires,
|
|
})
|
|
log.Info().Str("user", user).Msg("Token created")
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte(http.StatusText(http.StatusOK)))
|
|
} else {
|
|
log.Error().Err(err).Msg("Failed to create token")
|
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
|
}
|
|
}
|
|
|
|
func (h *handler) deleteToken(w http.ResponseWriter, r *http.Request) {
|
|
http.SetCookie(w, &http.Cookie{
|
|
Name: "jwt",
|
|
Value: "",
|
|
HttpOnly: true,
|
|
Path: "/",
|
|
SameSite: http.SameSiteLaxMode,
|
|
Secure: isHTTPS(r),
|
|
Expires: time.Unix(0, 0),
|
|
})
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte(http.StatusText(http.StatusOK)))
|
|
}
|
|
|
|
// isHTTPS reports whether the original client request used HTTPS, accounting
|
|
// for TLS terminated at an upstream reverse proxy via X-Forwarded-Proto.
|
|
func isHTTPS(r *http.Request) bool {
|
|
if r.TLS != nil {
|
|
return true
|
|
}
|
|
return strings.EqualFold(r.Header.Get("X-Forwarded-Proto"), "https")
|
|
}
|