129 lines
3.1 KiB
Go
129 lines
3.1 KiB
Go
package v1
|
|
|
|
import (
|
|
"errors"
|
|
"go-cook/api/models"
|
|
"go-cook/api/repositories"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
const (
|
|
ErrJwtMissing = "auth token is missing"
|
|
ErrJwtClaimsMissing = "claims missing on token"
|
|
ErrJwtExpired = "auth token has expired"
|
|
)
|
|
|
|
type JwtToken struct {
|
|
Exp time.Time `json:"exp"`
|
|
Authorized bool `json:"authorized"`
|
|
UserName string `json:"username"`
|
|
Token string `json:"token"`
|
|
jwt.RegisteredClaims
|
|
}
|
|
|
|
func generateJwt(username string) (string, error) {
|
|
//TODO use env here
|
|
secret := []byte("ThisIsABadSecretDontReallyUseThis")
|
|
|
|
token := jwt.New(jwt.SigningMethodHS256)
|
|
claims := token.Claims.(jwt.MapClaims)
|
|
claims["exp"] = time.Now().Add(10 * time.Minute)
|
|
claims["authorized"] = true
|
|
claims["username"] = username
|
|
|
|
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 {
|
|
return c.JSON(http.StatusInternalServerError, models.ErrorResponse{
|
|
HttpCode: http.StatusInternalServerError,
|
|
Message: err.Error(),
|
|
})
|
|
}
|
|
}
|
|
|
|
password := c.QueryParam("password")
|
|
err = h.UserService.CheckPasswordForRequirements(password)
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, models.ErrorResponse{
|
|
HttpCode: http.StatusInternalServerError,
|
|
Message: err.Error(),
|
|
})
|
|
}
|
|
|
|
_, err = h.userRepo.Create(username, password)
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, models.ErrorResponse{
|
|
HttpCode: http.StatusInternalServerError,
|
|
Message: err.Error(),
|
|
})
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (h *Handler) AuthLogin(c echo.Context) error {
|
|
username := c.QueryParam("username")
|
|
password := c.QueryParam("password")
|
|
|
|
// 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)
|
|
}
|
|
|
|
token, err := generateJwt(username)
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, err)
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, token)
|
|
}
|
|
|
|
func (h *Handler) RefreshJwtToken(c echo.Context) error {
|
|
return nil
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
// Check to see if the token has expired
|
|
hasExpired := claims.Exp.Compare(time.Now())
|
|
if hasExpired == -1 {
|
|
return JwtToken{}, errors.New(ErrJwtExpired)
|
|
}
|
|
|
|
return *claims, nil
|
|
}
|