mirror of
https://github.com/amir20/dozzle.git
synced 2025-12-21 13:23:07 +01:00
43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"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 {
|
|
http.SetCookie(w, &http.Cookie{
|
|
Name: "jwt",
|
|
Value: token,
|
|
HttpOnly: true,
|
|
Path: "/",
|
|
SameSite: http.SameSiteLaxMode,
|
|
})
|
|
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,
|
|
Expires: time.Unix(0, 0),
|
|
})
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte(http.StatusText(http.StatusOK)))
|
|
}
|