2024-03-26 17:54:22 -07:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2024-03-29 14:49:57 -07:00
|
|
|
"errors"
|
2024-03-31 17:49:09 -07:00
|
|
|
"go-cook/api/domain"
|
2024-03-26 17:54:22 -07:00
|
|
|
"go-cook/api/repositories"
|
|
|
|
"net/http"
|
2024-03-31 17:49:09 -07:00
|
|
|
"strings"
|
2024-03-26 17:54:22 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2024-03-29 14:49:57 -07:00
|
|
|
const (
|
|
|
|
ErrJwtMissing = "auth token is missing"
|
|
|
|
ErrJwtClaimsMissing = "claims missing on token"
|
|
|
|
ErrJwtExpired = "auth token has expired"
|
2024-03-31 17:49:09 -07:00
|
|
|
ErrJwtScopeMissing = "required scope is missing"
|
2024-03-29 14:49:57 -07:00
|
|
|
)
|
|
|
|
|
2024-03-26 17:54:22 -07:00
|
|
|
type JwtToken struct {
|
|
|
|
Exp time.Time `json:"exp"`
|
2024-03-31 17:49:09 -07:00
|
|
|
Iss string `json:"iss"`
|
2024-03-26 17:54:22 -07:00
|
|
|
Authorized bool `json:"authorized"`
|
|
|
|
UserName string `json:"username"`
|
2024-03-31 17:49:09 -07:00
|
|
|
Scopes []string `json:"scopes"`
|
2024-03-26 17:54:22 -07:00
|
|
|
jwt.RegisteredClaims
|
|
|
|
}
|
|
|
|
|
2024-03-31 17:49:09 -07:00
|
|
|
func (j JwtToken) IsValid(scope string) error {
|
|
|
|
err := j.hasExpired()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = j.hasScope(scope)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j JwtToken) hasExpired() error {
|
|
|
|
// Check to see if the token has expired
|
|
|
|
hasExpired := j.Exp.Compare(time.Now())
|
|
|
|
if hasExpired == -1 {
|
|
|
|
return errors.New(ErrJwtExpired)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j JwtToken) hasScope(scope string) error {
|
|
|
|
// they have the scope to access everything, so let them pass.
|
|
|
|
if strings.Contains(domain.ScopeAll, scope) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range j.Scopes {
|
|
|
|
if strings.Contains(s, scope) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New(ErrJwtScopeMissing)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) generateJwt(username string) (string, error) {
|
|
|
|
secret := []byte(h.Config.JwtSecret)
|
2024-03-26 17:54:22 -07:00
|
|
|
|
2024-03-27 17:24:23 -07:00
|
|
|
token := jwt.New(jwt.SigningMethodHS256)
|
2024-03-26 17:54:22 -07:00
|
|
|
claims := token.Claims.(jwt.MapClaims)
|
|
|
|
claims["exp"] = time.Now().Add(10 * time.Minute)
|
|
|
|
claims["authorized"] = true
|
2024-03-27 21:55:25 -07:00
|
|
|
claims["username"] = username
|
2024-03-26 17:54:22 -07:00
|
|
|
|
2024-03-31 17:49:09 -07:00
|
|
|
var scopes []string
|
|
|
|
scopes = append(scopes, domain.ScopeRecipeRead)
|
|
|
|
claims["scopes"] = scopes
|
|
|
|
|
2024-03-26 17:54:22 -07:00
|
|
|
tokenString, err := token.SignedString(secret)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokenString, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) AuthRegister(c echo.Context) error {
|
|
|
|
username := c.QueryParam("username")
|
|
|
|
_, err := h.userRepo.GetByName(username)
|
|
|
|
if err != nil {
|
|
|
|
// if we have an err, validate that if its not user not found.
|
|
|
|
// if the user is not found, we can use that name
|
|
|
|
if err.Error() != repositories.ErrUserNotFound {
|
2024-03-31 17:49:09 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{
|
2024-03-26 17:54:22 -07:00
|
|
|
HttpCode: http.StatusInternalServerError,
|
2024-03-29 14:49:57 -07:00
|
|
|
Message: err.Error(),
|
|
|
|
})
|
2024-03-26 17:54:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
password := c.QueryParam("password")
|
|
|
|
err = h.UserService.CheckPasswordForRequirements(password)
|
|
|
|
if err != nil {
|
2024-03-31 17:49:09 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{
|
2024-03-26 17:54:22 -07:00
|
|
|
HttpCode: http.StatusInternalServerError,
|
2024-03-29 14:49:57 -07:00
|
|
|
Message: err.Error(),
|
2024-03-26 17:54:22 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = h.userRepo.Create(username, password)
|
|
|
|
if err != nil {
|
2024-03-31 17:49:09 -07:00
|
|
|
return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{
|
2024-03-26 17:54:22 -07:00
|
|
|
HttpCode: http.StatusInternalServerError,
|
2024-03-29 14:49:57 -07:00
|
|
|
Message: err.Error(),
|
2024-03-26 17:54:22 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) AuthLogin(c echo.Context) error {
|
|
|
|
username := c.QueryParam("username")
|
|
|
|
password := c.QueryParam("password")
|
2024-03-29 14:49:57 -07:00
|
|
|
|
2024-03-26 17:54:22 -07:00
|
|
|
// check if the user exists
|
|
|
|
err := h.UserService.DoesUserExist(username)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure the hash matches
|
|
|
|
err = h.UserService.DoesPasswordMatchHash(username, password)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
|
2024-03-31 17:49:09 -07:00
|
|
|
token, err := h.generateJwt(username)
|
2024-03-26 17:54:22 -07:00
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, token)
|
|
|
|
}
|
|
|
|
|
2024-03-31 17:49:09 -07:00
|
|
|
func (h *Handler) AddScope(c echo.Context) error {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-03-26 17:54:22 -07:00
|
|
|
func (h *Handler) RefreshJwtToken(c echo.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-29 14:49:57 -07:00
|
|
|
|
|
|
|
func (h *Handler) getJwtToken(c echo.Context) (JwtToken, error) {
|
|
|
|
// Make sure that the request came with a jwtToken
|
|
|
|
token, ok := c.Get("user").(*jwt.Token)
|
|
|
|
if !ok {
|
|
|
|
return JwtToken{}, errors.New(ErrJwtMissing)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the claims from the token
|
|
|
|
claims, ok := token.Claims.(*JwtToken)
|
|
|
|
if !ok {
|
|
|
|
return JwtToken{}, errors.New(ErrJwtClaimsMissing)
|
|
|
|
}
|
|
|
|
|
|
|
|
return *claims, nil
|
|
|
|
}
|