2024-05-04 11:47:30 -07:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
2024-05-05 10:02:17 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
2024-05-04 11:47:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ErrJwtMissing = "auth token is missing"
|
|
|
|
ErrJwtClaimsMissing = "claims missing on token"
|
|
|
|
ErrJwtExpired = "auth token has expired"
|
|
|
|
ErrJwtScopeMissing = "required scope is missing"
|
2024-05-07 18:19:41 -07:00
|
|
|
ErrJwtInvalidIssuer = "incorrect server issued the token"
|
2024-05-04 11:47:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type JwtToken struct {
|
|
|
|
Exp time.Time `json:"exp"`
|
|
|
|
Iss string `json:"iss"`
|
|
|
|
Authorized bool `json:"authorized"`
|
|
|
|
UserName string `json:"username"`
|
2024-05-07 19:14:37 -07:00
|
|
|
UserId int64 `json:"userId"`
|
2024-05-04 11:47:30 -07:00
|
|
|
Scopes []string `json:"scopes"`
|
|
|
|
jwt.RegisteredClaims
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j JwtToken) IsValid(scope string) error {
|
|
|
|
err := j.hasExpired()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-07 18:19:41 -07:00
|
|
|
// Check to see if they have the scope to do anything
|
|
|
|
// if they do, let them pass
|
|
|
|
err = j.hasScope(domain.ScopeAll)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-04 11:47:30 -07:00
|
|
|
err = j.hasScope(scope)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j JwtToken) GetUsername() string {
|
|
|
|
return j.UserName
|
|
|
|
}
|
|
|
|
|
2024-05-07 19:14:37 -07:00
|
|
|
func (j JwtToken) GetUserId() int64 {
|
|
|
|
return j.UserId
|
|
|
|
}
|
|
|
|
|
2024-05-04 11:47:30 -07:00
|
|
|
func (j JwtToken) hasExpired() error {
|
|
|
|
// Check to see if the token has expired
|
2024-05-07 22:01:32 -07:00
|
|
|
//hasExpired := j.Exp.Compare(time.Now())
|
|
|
|
hasExpired := time.Now().Compare(j.Exp)
|
|
|
|
if hasExpired == 1 {
|
2024-05-04 11:47:30 -07:00
|
|
|
return errors.New(ErrJwtExpired)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-07 18:19:41 -07:00
|
|
|
// This will check the users token to make sure they have the correct scope to access the handler.
|
|
|
|
// It will evaluate if you have the admin scope or the required scope for the handler.
|
2024-05-04 11:47:30 -07:00
|
|
|
func (j JwtToken) hasScope(scope string) error {
|
|
|
|
// they have the scope to access everything, so let them pass.
|
2024-05-07 18:19:41 -07:00
|
|
|
userScopes := strings.Join(j.Scopes, "")
|
|
|
|
if strings.Contains(domain.ScopeAll, userScopes) {
|
2024-05-04 11:47:30 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-07 18:19:41 -07:00
|
|
|
if strings.Contains(userScopes, scope) {
|
|
|
|
return nil
|
2024-05-04 11:47:30 -07:00
|
|
|
}
|
2024-05-07 18:19:41 -07:00
|
|
|
|
2024-05-04 11:47:30 -07:00
|
|
|
return errors.New(ErrJwtScopeMissing)
|
|
|
|
}
|
|
|
|
|
2024-05-07 22:01:32 -07:00
|
|
|
func (h *Handler) generateJwt(username, issuer string, userScopes []string, userId int64) (string, error) {
|
|
|
|
return h.generateJwtWithExp(username, issuer, userScopes, userId, time.Now().Add(10*time.Minute))
|
2024-05-04 11:47:30 -07:00
|
|
|
}
|
|
|
|
|
2024-05-07 22:01:32 -07:00
|
|
|
func (h *Handler) generateJwtWithExp(username, issuer string, userScopes []string, userId int64, expiresAt time.Time) (string, error) {
|
2024-05-04 11:47:30 -07:00
|
|
|
secret := []byte(h.config.JwtSecret)
|
|
|
|
|
|
|
|
// Anyone who wants to decrypt the key needs to use the same method
|
|
|
|
token := jwt.New(jwt.SigningMethodHS256)
|
|
|
|
claims := token.Claims.(jwt.MapClaims)
|
|
|
|
claims["exp"] = expiresAt
|
|
|
|
claims["authorized"] = true
|
|
|
|
claims["username"] = username
|
|
|
|
claims["iss"] = issuer
|
2024-05-07 19:14:37 -07:00
|
|
|
claims["userId"] = userId
|
2024-05-04 11:47:30 -07:00
|
|
|
|
|
|
|
var scopes []string
|
2024-05-07 22:01:32 -07:00
|
|
|
scopes = append(scopes, userScopes...)
|
2024-05-07 18:19:41 -07:00
|
|
|
claims["scopes"] = scopes
|
2024-05-04 11:47:30 -07:00
|
|
|
|
|
|
|
tokenString, err := token.SignedString(secret)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokenString, nil
|
|
|
|
}
|
2024-05-05 10:02:17 -07:00
|
|
|
|
2024-05-07 18:19:41 -07:00
|
|
|
func (h *Handler) getJwtTokenFromContext(c echo.Context) (JwtToken, error) {
|
2024-05-05 10:02:17 -07:00
|
|
|
// 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
|
|
|
|
}
|