refactor: create and export GenerateTokenWithDuration and GenerateToken

This commit is contained in:
Derrick Hammer 2024-01-20 07:30:46 -05:00
parent 8c86ecc5b7
commit 4378da70da
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 7 additions and 4 deletions

View File

@ -88,7 +88,7 @@ func (s AccountServiceImpl) LoginPassword(email string, password string) (string
return "", err return "", err
} }
token, err := generateToken(s.portal.Identity(), user.ID) token, err := GenerateToken(s.portal.Identity(), user.ID)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -105,7 +105,7 @@ func (s AccountServiceImpl) LoginPubkey(pubkey string) (string, error) {
return "", result.Error return "", result.Error
} }
token, err := generateToken(s.portal.Identity(), model.UserID) token, err := GenerateToken(s.portal.Identity(), model.UserID)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -6,12 +6,15 @@ import (
"time" "time"
) )
func generateToken(privateKey ed25519.PrivateKey, userID uint) (string, error) { func GenerateToken(privateKey ed25519.PrivateKey, userID uint) (string, error) {
return GenerateTokenWithDuration(privateKey, userID, time.Hour*24)
}
func GenerateTokenWithDuration(privateKey ed25519.PrivateKey, userID uint, duration time.Duration) (string, error) {
// Define the claims // Define the claims
claims := jwt.MapClaims{ claims := jwt.MapClaims{
"iss": "portal", "iss": "portal",
"sub": userID, "sub": userID,
"exp": time.Now().Add(time.Hour * 24).Unix(), "exp": time.Now().Add(duration).Unix(),
} }
// Create the token // Create the token