2024-05-12 10:45:16 -07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-05-26 10:00:44 -07:00
|
|
|
"context"
|
2024-05-18 22:38:12 -07:00
|
|
|
"errors"
|
2024-05-12 10:45:16 -07:00
|
|
|
"net/http"
|
2024-07-07 07:58:26 -07:00
|
|
|
"strings"
|
2024-05-18 22:38:12 -07:00
|
|
|
"time"
|
2024-05-12 10:45:16 -07:00
|
|
|
|
2024-07-07 07:58:26 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/config"
|
2024-05-18 22:38:12 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/domain"
|
2024-07-07 07:58:26 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/views/layout"
|
2024-05-19 10:02:08 -07:00
|
|
|
"github.com/a-h/templ"
|
2024-05-26 19:30:04 -07:00
|
|
|
"github.com/golang-jwt/jwt/v5"
|
2024-05-12 10:45:16 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2024-05-18 22:38:12 -07:00
|
|
|
func SetCookie(c echo.Context, key, value, path string) {
|
2024-05-12 10:45:16 -07:00
|
|
|
cookie := new(http.Cookie)
|
|
|
|
cookie.Name = key
|
|
|
|
cookie.Value = value
|
2024-05-18 22:38:12 -07:00
|
|
|
if path != "" {
|
|
|
|
cookie.Path = path
|
|
|
|
}
|
2024-05-12 10:45:16 -07:00
|
|
|
c.SetCookie(cookie)
|
|
|
|
}
|
2024-05-18 22:38:12 -07:00
|
|
|
|
|
|
|
type jwtToken struct {
|
2024-05-26 19:30:04 -07:00
|
|
|
Exp time.Time `json:"exp"`
|
|
|
|
Iss string `json:"iss"`
|
|
|
|
Authorized bool `json:"authorized"`
|
|
|
|
UserName string `json:"username"`
|
|
|
|
UserId int64 `json:"userId"`
|
|
|
|
Scopes []string `json:"scopes"`
|
|
|
|
SessionToken string `json:"sessionToken"`
|
2024-05-18 22:38:12 -07:00
|
|
|
jwt.RegisteredClaims
|
|
|
|
}
|
|
|
|
|
2024-07-07 07:58:26 -07:00
|
|
|
func IsLoggedIn(ctx echo.Context) error {
|
|
|
|
_, err := GetUserInfo(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func HasValidScope(ctx echo.Context, scope string) error {
|
|
|
|
token, err := GetUserInfo(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
userScopes := strings.Join(token.Scopes, ",")
|
|
|
|
if strings.Contains(userScopes, scope) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("required permission is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUserInfo(ctx echo.Context) (jwtToken, error) {
|
|
|
|
cfg := config.New()
|
2024-05-18 22:38:12 -07:00
|
|
|
cookie, err := ctx.Cookie(domain.CookieToken)
|
|
|
|
if err != nil {
|
|
|
|
return jwtToken{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cookie.Value == "" {
|
|
|
|
return jwtToken{}, errors.New("JWT Bearer Token is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := jwt.ParseWithClaims(cookie.Value, &jwtToken{}, func(token *jwt.Token) (interface{}, error) {
|
2024-07-07 07:58:26 -07:00
|
|
|
return []byte(cfg.JwtSecret), nil
|
2024-05-18 22:38:12 -07:00
|
|
|
})
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
return *claims, nil
|
|
|
|
}
|
2024-05-19 10:02:08 -07:00
|
|
|
|
2024-05-26 19:30:04 -07:00
|
|
|
func GetJwtToken(c echo.Context) string {
|
|
|
|
cookie, err := c.Cookie(domain.CookieToken)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return cookie.Value
|
|
|
|
}
|
|
|
|
|
2024-05-19 10:02:08 -07:00
|
|
|
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()
|
2024-07-07 07:58:26 -07:00
|
|
|
|
|
|
|
//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
|
|
|
|
username, err := ctx.Cookie(domain.CookieUser)
|
|
|
|
if err == nil {
|
|
|
|
request = context.WithValue(request, domain.UserNameContext, username.Value)
|
|
|
|
} else {
|
|
|
|
request = context.WithValue(request, domain.UserNameContext, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.Render(request, ctx.Response().Writer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RenderError(ctx echo.Context, err error) error {
|
|
|
|
var t templ.Component = layout.Error(err)
|
|
|
|
ctx.Response().Writer.WriteHeader(200)
|
|
|
|
ctx.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)
|
|
|
|
|
|
|
|
// take the request context and make it a var
|
|
|
|
request := ctx.Request().Context()
|
|
|
|
|
2024-05-19 10:02:08 -07:00
|
|
|
//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
|
2024-05-26 10:00:44 -07:00
|
|
|
username, err := ctx.Cookie(domain.CookieUser)
|
|
|
|
if err == nil {
|
|
|
|
request = context.WithValue(request, domain.UserNameContext, username.Value)
|
|
|
|
} else {
|
|
|
|
request = context.WithValue(request, domain.UserNameContext, "")
|
|
|
|
}
|
2024-05-19 10:02:08 -07:00
|
|
|
|
|
|
|
return t.Render(request, ctx.Response().Writer)
|
|
|
|
}
|