mirror of
https://github.com/stashapp/stash.git
synced 2025-12-06 16:34:02 +01:00
* Update a number of dependencies (incl. CVE fixes) Includes some dependencies that were upgraded in #4106 as well as a few more dependencies. Some deps that have been upgraded had CVEs. Notably, upgrades deprecated dependencies such as: - `github.com/go-chi/chi` (replaced with `/v5`) - `github.com/gofrs/uuid` (replaced with `/v5`) - `github.com/hashicorp/golang-lru` (replaced with `/v2` which uses generics) * Upgraded a few more deps * lint * reverted yaml library to v2 * remove unnecessary mod replace * Update chromedp Fixes #3733
55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package manager
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
"github.com/stashapp/stash/internal/manager/config"
|
|
)
|
|
|
|
var ErrInvalidToken = errors.New("invalid apikey")
|
|
|
|
const APIKeySubject = "APIKey"
|
|
|
|
type APIKeyClaims struct {
|
|
UserID string `json:"uid"`
|
|
jwt.RegisteredClaims
|
|
}
|
|
|
|
func GenerateAPIKey(userID string) (string, error) {
|
|
claims := &APIKeyClaims{
|
|
UserID: userID,
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
Subject: APIKeySubject,
|
|
IssuedAt: jwt.NewNumericDate(time.Now()),
|
|
},
|
|
}
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
|
ss, err := token.SignedString(config.GetInstance().GetJWTSignKey())
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return ss, nil
|
|
}
|
|
|
|
// GetUserIDFromAPIKey validates the provided api key and returns the user ID
|
|
func GetUserIDFromAPIKey(apiKey string) (string, error) {
|
|
claims := &APIKeyClaims{}
|
|
token, err := jwt.ParseWithClaims(apiKey, claims, func(t *jwt.Token) (interface{}, error) {
|
|
return config.GetInstance().GetJWTSignKey(), nil
|
|
})
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if !token.Valid {
|
|
return "", ErrInvalidToken
|
|
}
|
|
|
|
return claims.UserID, nil
|
|
}
|