mirror of
https://github.com/hanzoai/simple-coredns-manager.git
synced 2026-10-30 19:55:29 +00:00
Full-featured web UI for managing CoreDNS Corefile and host files with diff preview, Docker-based reload, and master password authentication. Built for MWIT-LINK infrastructure DNS management. Co-authored-by: Hanzo Dev <[email protected]>
51 lines
1.0 KiB
Go
51 lines
1.0 KiB
Go
package auth
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
const (
|
|
CookieName = "jwt"
|
|
TokenExpiry = 24 * time.Hour
|
|
)
|
|
|
|
func VerifyPassword(password string, hash []byte) bool {
|
|
return bcrypt.CompareHashAndPassword(hash, []byte(password)) == nil
|
|
}
|
|
|
|
func GenerateToken(secret []byte) (string, error) {
|
|
claims := jwt.MapClaims{
|
|
"authenticated": true,
|
|
"exp": time.Now().Add(TokenExpiry).Unix(),
|
|
"iat": time.Now().Unix(),
|
|
}
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
return token.SignedString(secret)
|
|
}
|
|
|
|
func SetCookie(w http.ResponseWriter, tokenString string) {
|
|
http.SetCookie(w, &http.Cookie{
|
|
Name: CookieName,
|
|
Value: tokenString,
|
|
Path: "/",
|
|
HttpOnly: true,
|
|
SameSite: http.SameSiteStrictMode,
|
|
MaxAge: int(TokenExpiry.Seconds()),
|
|
})
|
|
}
|
|
|
|
func ClearCookie(w http.ResponseWriter) {
|
|
http.SetCookie(w, &http.Cookie{
|
|
Name: CookieName,
|
|
Value: "",
|
|
Path: "/",
|
|
HttpOnly: true,
|
|
SameSite: http.SameSiteStrictMode,
|
|
MaxAge: -1,
|
|
})
|
|
}
|