fix: OnlyOffice edit session token expiration and add session expired modal
Some checks failed
Automated Container Build / build-and-push (push) Failing after 15s
Some checks failed
Automated Container Build / build-and-push (push) Failing after 15s
This commit is contained in:
parent
1f83d57942
commit
218fb40743
7 changed files with 130 additions and 16 deletions
|
|
@ -137,6 +137,26 @@ func GenerateDownloadToken(userID int, username, secret, filePath string) (strin
|
|||
return token.SignedString([]byte(secret))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// GenerateEditToken creates a long-lived edit token for OnlyOffice sessions.
|
||||
func GenerateEditToken(userID int, username, secret, filePath string) (string, error) {
|
||||
claims := jwt.MapClaims{
|
||||
"sub": userID,
|
||||
"username": username,
|
||||
"exp": time.Now().Add(24 * time.Hour).Unix(),
|
||||
"iat": time.Now().Unix(),
|
||||
"type": "edit",
|
||||
}
|
||||
|
||||
if filePath != "" {
|
||||
claims["path"] = filePath
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
return token.SignedString([]byte(secret))
|
||||
}
|
||||
|
||||
// ValidateDownloadToken validates a download token
|
||||
func ValidateDownloadToken(tokenString, secret, expectedUsername, expectedPath string) error {
|
||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||
|
|
@ -165,3 +185,40 @@ func ValidateDownloadToken(tokenString, secret, expectedUsername, expectedPath s
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateEditToken validates an edit token
|
||||
func ValidateEditToken(tokenString, secret, expectedUsername, expectedPath string) error {
|
||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte(secret), nil
|
||||
})
|
||||
|
||||
if err != nil || !token.Valid {
|
||||
return err
|
||||
}
|
||||
|
||||
claims, ok := token.Claims.(jwt.MapClaims)
|
||||
if !ok {
|
||||
return jwt.ErrSignatureInvalid
|
||||
}
|
||||
|
||||
tokenType, _ := claims["type"].(string)
|
||||
if tokenType != "edit" {
|
||||
return jwt.ErrSignatureInvalid
|
||||
}
|
||||
|
||||
if expectedUsername != "" {
|
||||
if username, ok := claims["username"].(string); !ok || username != expectedUsername {
|
||||
return jwt.ErrSignatureInvalid
|
||||
}
|
||||
}
|
||||
|
||||
if expectedPath != "" {
|
||||
if path, ok := claims["path"].(string); ok && path != "" {
|
||||
if path != expectedPath {
|
||||
return jwt.ErrSignatureInvalid
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue