feat: add purpose to jwt with consts
This commit is contained in:
parent
764a7cbdaf
commit
9f6f2c9c87
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue