121 lines
2.8 KiB
Go
121 lines
2.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"templ-test/client"
|
|
"templ-test/domain"
|
|
"templ-test/models"
|
|
"templ-test/services"
|
|
"time"
|
|
|
|
"github.com/a-h/templ"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type Handlers struct {
|
|
api client.ApiClient
|
|
cfg services.EnvConfig
|
|
}
|
|
|
|
func NewHandlerClient(api client.ApiClient, cfg services.EnvConfig) *Handlers {
|
|
h := Handlers{
|
|
api: api,
|
|
cfg: cfg,
|
|
}
|
|
|
|
return &h
|
|
}
|
|
|
|
func (h *Handlers) Register(group echo.Group) {
|
|
group.GET("/", h.HomeHandler)
|
|
group.GET("/settings", h.Settings)
|
|
group.POST("/settings", h.SettingsPost)
|
|
//group.GET("/list", h.ListHandler)
|
|
|
|
auth := group.Group("/auth")
|
|
auth.GET("/login", h.AuthLogin)
|
|
auth.POST("/login", h.AuthLoginPost)
|
|
auth.GET("/cookie", h.AuthShowCookies)
|
|
}
|
|
|
|
func Render(ctx echo.Context, statusCode int, t templ.Component) error {
|
|
ctx.Response().Writer.WriteHeader(statusCode)
|
|
ctx.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)
|
|
|
|
// take the request context and make it a var
|
|
request := ctx.Request().Context()
|
|
//Check to see if we the echo context has the cookie we are looking for, if so, create a new context based on what we had and add the value
|
|
darkMode, err := ctx.Cookie(domain.CookieSettingsDarkMode)
|
|
if err == nil {
|
|
request = context.WithValue(request, domain.CookieSettingsDarkMode, darkMode.Value)
|
|
}
|
|
|
|
return t.Render(request, ctx.Response().Writer)
|
|
}
|
|
|
|
type jwtToken struct {
|
|
Exp time.Time `json:"exp"`
|
|
Iss string `json:"iss"`
|
|
Authorized bool `json:"authorized"`
|
|
UserName string `json:"username"`
|
|
Scopes []string `json:"scopes"`
|
|
jwt.RegisteredClaims
|
|
}
|
|
|
|
func ValidateJwt(ctx echo.Context, sharedSecret, issuer string) (jwtToken, error) {
|
|
cookies := GetCookieValues(ctx)
|
|
if cookies.Token == "" {
|
|
return jwtToken{}, errors.New("JWT Bearer Token is missing")
|
|
}
|
|
|
|
token, err := jwt.ParseWithClaims(cookies.Token, &jwtToken{}, func(token *jwt.Token) (interface{}, error) {
|
|
return []byte(sharedSecret), nil
|
|
})
|
|
if err != nil {
|
|
return jwtToken{}, err
|
|
}
|
|
|
|
if !token.Valid {
|
|
return jwtToken{}, errors.New("invalid jwt token")
|
|
}
|
|
|
|
claims := token.Claims.(*jwtToken)
|
|
if !claims.Exp.After(time.Now()) {
|
|
return jwtToken{}, errors.New("the jwt token has expired")
|
|
}
|
|
if claims.Iss != issuer {
|
|
return jwtToken{}, errors.New("the issuer was invalid")
|
|
}
|
|
|
|
return *claims, nil
|
|
}
|
|
|
|
func GetCookieValues(ctx echo.Context) models.AllCookies {
|
|
m := models.AllCookies{}
|
|
|
|
token, err := ctx.Cookie(domain.CookieToken)
|
|
if err == nil {
|
|
m.Token = token.Value
|
|
}
|
|
|
|
user, err := ctx.Cookie(domain.CookieUser)
|
|
if err == nil {
|
|
m.Username = user.Value
|
|
}
|
|
|
|
refresh, err := ctx.Cookie(domain.CookieRefreshToken)
|
|
if err == nil {
|
|
m.RefreshToken = refresh.Value
|
|
}
|
|
|
|
darkMode, err := ctx.Cookie(domain.CookieSettingsDarkMode)
|
|
if err == nil {
|
|
m.DarkMode = darkMode.Value
|
|
}
|
|
|
|
return m
|
|
}
|