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"
|
2024-04-01 17:48:38 -07:00
|
|
|
"log"
|
2024-03-26 17:54:22 -07:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2024-03-29 14:49:57 -07:00
|
|
|
const (
|
2024-04-01 17:48:38 -07:00
|
|
|
ErrJwtMissing = "auth token is missing"
|
|
|
|
ErrJwtClaimsMissing = "claims missing on token"
|
|
|
|
ErrJwtExpired = "auth token has expired"
|
|
|
|
ErrJwtScopeMissing = "required scope is missing"
|
|
|
|
ErrUserNotFound = "requested user does not exist"
|
|
|
|
ErrUsernameAlreadyExists = "the requested username already exists"
|
2024-03-29 14:49:57 -07:00
|
|
|
)
|
|
|
|
|
2024-03-26 17:54:22 -07:00
|
|
|
func (h *Handler) AuthRegister(c echo.Context) error {
|
2024-04-01 17:48:38 -07:00
|
|
|
username := c.FormValue("username")
|
|
|
|
password := c.FormValue("password")
|
|
|
|
|
|
|
|
//username := c.QueryParam("username")
|
|
|
|
exists, err := h.userRepo.GetByName(username)
|
2024-03-26 17:54:22 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2024-04-01 17:48:38 -07:00
|
|
|
if exists.Name == username {
|
|
|
|
return h.InternalServerErrorResponse(c, ErrUsernameAlreadyExists)
|
|
|
|
}
|
2024-03-26 17:54:22 -07:00
|
|
|
|
2024-04-01 17:48:38 -07:00
|
|
|
//password := c.QueryParam("password")
|
2024-03-26 17:54:22 -07:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-01 17:48:38 -07:00
|
|
|
_, err = h.userRepo.Create(username, password, domain.ScopeRecipeRead)
|
2024-03-26 17:54:22 -07:00
|
|
|
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 {
|
2024-04-01 17:48:38 -07:00
|
|
|
formValues, err := c.FormParams()
|
|
|
|
if err != nil {
|
|
|
|
h.InternalServerErrorResponse(c, err.Error())
|
|
|
|
}
|
|
|
|
log.Println(formValues)
|
|
|
|
username := formValues.Get("name")
|
|
|
|
password := formValues.Get("password")
|
2024-03-29 14:49:57 -07:00
|
|
|
|
2024-03-31 18:05:33 -07:00
|
|
|
// Check to see if they are trying to login with the admin token
|
|
|
|
if username == "" {
|
2024-03-31 18:10:53 -07:00
|
|
|
return h.validateAdminToken(c, password)
|
2024-03-31 18:05:33 -07:00
|
|
|
}
|
|
|
|
|
2024-03-26 17:54:22 -07:00
|
|
|
// check if the user exists
|
2024-04-01 17:48:38 -07:00
|
|
|
err = h.UserService.DoesUserExist(username)
|
2024-03-26 17:54:22 -07:00
|
|
|
if err != nil {
|
2024-03-31 18:05:33 -07:00
|
|
|
return h.InternalServerErrorResponse(c, err.Error())
|
2024-03-26 17:54:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure the hash matches
|
|
|
|
err = h.UserService.DoesPasswordMatchHash(username, password)
|
|
|
|
if err != nil {
|
2024-03-31 18:05:33 -07:00
|
|
|
return h.InternalServerErrorResponse(c, err.Error())
|
2024-03-26 17:54:22 -07:00
|
|
|
}
|
|
|
|
|
2024-03-31 17:49:09 -07:00
|
|
|
token, err := h.generateJwt(username)
|
2024-03-26 17:54:22 -07:00
|
|
|
if err != nil {
|
2024-03-31 18:05:33 -07:00
|
|
|
return h.InternalServerErrorResponse(c, err.Error())
|
2024-03-26 17:54:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, token)
|
|
|
|
}
|
|
|
|
|
2024-03-31 18:10:53 -07:00
|
|
|
func (h *Handler) validateAdminToken(c echo.Context, password string) error {
|
|
|
|
if h.Config.AdminToken != password {
|
|
|
|
return h.ReturnUnauthorizedResponse(c, ErrUserNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := h.generateAdminJwt("admin")
|
|
|
|
if err != nil {
|
|
|
|
return h.InternalServerErrorResponse(c, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, token)
|
|
|
|
}
|
|
|
|
|
2024-03-31 18:05:33 -07:00
|
|
|
//func (h *Handler) AddScope(c echo.Context) error {
|
|
|
|
//
|
|
|
|
//}
|
2024-03-31 17:49:09 -07:00
|
|
|
|
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
|
|
|
|
}
|