1
0
mirror of https://github.com/haiwen/seafile-server.git synced 2025-09-07 02:10:05 +00:00

Add json exp (#699)

* Add json exp

* Add json exp and add gen jwt token

* Gen seahub and notif jwt token

* Add exp for gen jwt token

* Delete gen_jwt_token API

* Delete set exp to 72 hour

---------

Co-authored-by: 杨赫然 <heran.yang@seafile.com>
This commit is contained in:
feiniks
2024-09-18 17:59:24 +08:00
committed by GitHub
parent 72753e152b
commit 61126d108c
12 changed files with 93 additions and 123 deletions

View File

@@ -1,7 +1,12 @@
package utils
import (
"fmt"
"time"
jwt "github.com/golang-jwt/jwt/v5"
"github.com/google/uuid"
"github.com/haiwen/seafile-server/fileserver/option"
)
func IsValidUUID(u string) bool {
@@ -22,3 +27,55 @@ func IsObjectIDValid(objID string) bool {
}
return true
}
type SeahubClaims struct {
Exp int64 `json:"exp"`
IsInternal bool `json:"is_internal"`
jwt.RegisteredClaims
}
func (*SeahubClaims) Valid() error {
return nil
}
func GenSeahubJWTToken() (string, error) {
claims := new(SeahubClaims)
claims.Exp = time.Now().Add(time.Second * 300).Unix()
claims.IsInternal = true
token := jwt.NewWithClaims(jwt.GetSigningMethod("HS256"), claims)
tokenString, err := token.SignedString([]byte(option.JWTPrivateKey))
if err != nil {
err := fmt.Errorf("failed to gen seahub jwt token: %w", err)
return "", err
}
return tokenString, nil
}
type MyClaims struct {
Exp int64 `json:"exp"`
RepoID string `json:"repo_id"`
UserName string `json:"username"`
jwt.RegisteredClaims
}
func (*MyClaims) Valid() error {
return nil
}
func GenNotifJWTToken(repoID, user string, exp int64) (string, error) {
claims := new(MyClaims)
claims.Exp = exp
claims.RepoID = repoID
claims.UserName = user
token := jwt.NewWithClaims(jwt.GetSigningMethod("HS256"), claims)
tokenString, err := token.SignedString([]byte(option.JWTPrivateKey))
if err != nil {
err := fmt.Errorf("failed to gen jwt token for repo %s: %w", repoID, err)
return "", err
}
return tokenString, nil
}