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) {
|
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 {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,16 +6,24 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateToken(domain string, privateKey ed25519.PrivateKey, userID uint) (string, error) {
|
type JWTPurpose string
|
||||||
return GenerateTokenWithDuration(domain, privateKey, userID, time.Hour*24)
|
|
||||||
|
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
|
// Define the claims
|
||||||
claims := jwt.MapClaims{
|
claims := jwt.MapClaims{
|
||||||
"iss": domain,
|
"iss": domain,
|
||||||
"sub": userID,
|
"sub": userID,
|
||||||
"exp": time.Now().Add(duration).Unix(),
|
"exp": time.Now().Add(duration).Unix(),
|
||||||
"iat": time.Now().Unix(),
|
"iat": time.Now().Unix(),
|
||||||
|
"aud": string(purpose),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the token
|
// Create the token
|
||||||
|
|
Loading…
Reference in New Issue