feat: add purpose to jwt with consts

This commit is contained in:
Derrick Hammer 2024-02-13 20:58:17 -05:00
parent 764a7cbdaf
commit 9f6f2c9c87
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 12 additions and 4 deletions

View File

@ -253,7 +253,7 @@ func (s AccountServiceDefault) PinByID(uploadId uint, accountID uint) error {
}
func (s AccountServiceDefault) doLogin(user *models.User, ip string) (string, error) {
token, err := GenerateToken(s.config.GetString("core.domain"), s.identity, user.ID)
token, err := GenerateToken(s.config.GetString("core.domain"), s.identity, user.ID, JWTPurposeLogin)
if err != nil {
return "", err
}

View File

@ -6,16 +6,24 @@ import (
"time"
)
func GenerateToken(domain string, privateKey ed25519.PrivateKey, userID uint) (string, error) {
return GenerateTokenWithDuration(domain, privateKey, userID, time.Hour*24)
type JWTPurpose string
const (
JWTPurposeLogin JWTPurpose = "login"
)
func GenerateToken(domain string, privateKey ed25519.PrivateKey, userID uint, purpose JWTPurpose) (string, error) {
return GenerateTokenWithDuration(domain, privateKey, userID, time.Hour*24, purpose)
}
func GenerateTokenWithDuration(domain string, privateKey ed25519.PrivateKey, userID uint, duration time.Duration) (string, error) {
func GenerateTokenWithDuration(domain string, privateKey ed25519.PrivateKey, userID uint, duration time.Duration, purpose JWTPurpose) (string, error) {
// Define the claims
claims := jwt.MapClaims{
"iss": domain,
"sub": userID,
"exp": time.Now().Add(duration).Unix(),
"iat": time.Now().Unix(),
"aud": string(purpose),
}
// Create the token