feat(go): add jwt token lifetime from config

This commit is contained in:
sshakndr
2025-01-30 12:08:10 +07:00
committed by Sergey Linnik
parent 326fd4a586
commit 46cf8baa28
12 changed files with 73 additions and 19 deletions

View File

@ -1,5 +1,7 @@
# Change Log
- golang: jwt token lifetime from config
## 1.13.0
- nodejs: rename in wopi
- nodejs: using faviconUrl from WOPI discovery

View File

@ -4,6 +4,7 @@ linters:
- cyclop
- depguard
- dogsled
- durationcheck
- err113
- errchkjson
- execinquery

View File

@ -11,6 +11,7 @@
"DOC_SERVER_COMMAND_URL" : "command",
"JWT_IS_ENABLED" : false,
"JWT_EXPIRES_IN" : 5,
"JWT_SECRET" : "secret",
"JWT_HEADER" : "Authorization",

View File

@ -20,6 +20,7 @@ package config
import (
"path/filepath"
"runtime"
"time"
"github.com/ONLYOFFICE/document-server-integration/utils"
"github.com/spf13/viper"
@ -36,6 +37,7 @@ type ApplicationConfig struct {
DocumentServerPreloader string `mapstructure:"DOC_SERVER_PRELOADER_URL"`
DocumentServerCommandUrl string `mapstructure:"DOC_SERVER_COMMAND_URL"`
JwtEnabled bool `mapstructure:"JWT_IS_ENABLED"`
JwtExpiresIn time.Duration `mapstructure:"JWT_EXPIRES_IN"`
JwtHeader string `mapstructure:"JWT_HEADER"`
JwtSecret string `mapstructure:"JWT_SECRET"`
StoragePath string `mapstructure:"STORAGE_PATH"`

View File

@ -22,10 +22,12 @@ import (
"fmt"
"net/http"
"strings"
"time"
"github.com/ONLYOFFICE/document-server-integration/server/managers"
"github.com/ONLYOFFICE/document-server-integration/server/models"
"github.com/ONLYOFFICE/document-server-integration/server/shared"
"github.com/golang-jwt/jwt"
)
func (srv *DefaultServerEndpointsHandler) Config(w http.ResponseWriter, r *http.Request) {
@ -71,6 +73,10 @@ func (srv *DefaultServerEndpointsHandler) Config(w http.ResponseWriter, r *http.
),
Mode: "edit",
},
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Minute * srv.config.JwtExpiresIn).Unix(),
IssuedAt: time.Now().Unix(),
},
}
secret := strings.TrimSpace(srv.config.JwtSecret)

View File

@ -23,11 +23,13 @@ import (
"net/http"
"net/url"
"strings"
"time"
"github.com/ONLYOFFICE/document-server-integration/server/managers"
"github.com/ONLYOFFICE/document-server-integration/server/models"
"github.com/ONLYOFFICE/document-server-integration/server/shared"
"github.com/ONLYOFFICE/document-server-integration/utils"
"github.com/golang-jwt/jwt"
)
func (srv *DefaultServerEndpointsHandler) Reference(w http.ResponseWriter, r *http.Request) {
@ -99,6 +101,10 @@ func (srv *DefaultServerEndpointsHandler) Reference(w http.ResponseWriter, r *ht
},
Link: remoteAddr + "/editor?filename=" + url.QueryEscape(fileName),
Path: fileName,
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Minute * srv.config.JwtExpiresIn).Unix(),
IssuedAt: time.Now().Unix(),
},
}
secret := strings.TrimSpace(srv.config.JwtSecret)

View File

@ -23,6 +23,7 @@ import (
"net/http"
"net/url"
"strings"
"time"
"github.com/ONLYOFFICE/document-server-integration/config"
"github.com/ONLYOFFICE/document-server-integration/server/managers"
@ -59,6 +60,10 @@ func (cm DefaultCommandManager) CommandRequest(method string, docKey string, met
payload := CommandPayload{
C: method,
Key: docKey,
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Minute * cm.config.JwtExpiresIn).Unix(),
IssuedAt: time.Now().Unix(),
},
}
if meta != nil {
payload.Meta = meta
@ -68,7 +73,7 @@ func (cm DefaultCommandManager) CommandRequest(method string, docKey string, met
var headerToken string
secret := strings.TrimSpace(cm.config.JwtSecret)
if secret != "" && cm.config.JwtEnabled {
headerPayload := fillJwtByUrl(uri, payload)
headerPayload := fillJwtByUrl(uri, payload, cm.config)
headerToken, err = cm.JwtManager.JwtSign(headerPayload, []byte(secret))
if err != nil {
return nil, err
@ -103,7 +108,7 @@ func (cm DefaultCommandManager) CommandRequest(method string, docKey string, met
return response, nil
}
func fillJwtByUrl(uri string, payload CommandPayload) CommandRequestHeaderPayload {
func fillJwtByUrl(uri string, payload CommandPayload, config config.ApplicationConfig) CommandRequestHeaderPayload {
urlObj, _ := url.Parse(uri)
query, _ := url.ParseQuery(urlObj.RawQuery)
queryMap := make(map[string]string)
@ -114,5 +119,9 @@ func fillJwtByUrl(uri string, payload CommandPayload) CommandRequestHeaderPayloa
return CommandRequestHeaderPayload{
Query: queryMap,
Payload: payload,
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Minute * config.JwtExpiresIn).Unix(),
IssuedAt: time.Now().Unix(),
},
}
}

View File

@ -24,11 +24,13 @@ import (
"io"
"net/http"
"strings"
"time"
"github.com/ONLYOFFICE/document-server-integration/config"
"github.com/ONLYOFFICE/document-server-integration/server/managers"
"github.com/ONLYOFFICE/document-server-integration/server/shared"
"github.com/ONLYOFFICE/document-server-integration/utils"
"github.com/golang-jwt/jwt"
"go.uber.org/zap"
)
@ -109,6 +111,10 @@ func (cm DefaultConversionManager) GetConverterUri(
Title: utils.GetFileName(docUri),
Key: docKey,
Async: isAsync,
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Minute * cm.config.JwtExpiresIn).Unix(),
IssuedAt: time.Now().Unix(),
},
}
var headerToken string
@ -116,7 +122,13 @@ func (cm DefaultConversionManager) GetConverterUri(
secret := strings.TrimSpace(cm.config.JwtSecret)
if secret != "" && cm.config.JwtEnabled {
headerPayload := managers.ConvertRequestHeaderPayload{Payload: payload}
headerPayload := managers.ConvertRequestHeaderPayload{
Payload: payload,
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Minute * cm.config.JwtExpiresIn).Unix(),
IssuedAt: time.Now().Unix(),
},
}
headerToken, err = cm.JwtManager.JwtSign(headerPayload, []byte(secret))
if err != nil {
return "", "", err

View File

@ -28,6 +28,7 @@ import (
"github.com/ONLYOFFICE/document-server-integration/server/managers"
"github.com/ONLYOFFICE/document-server-integration/server/models"
"github.com/ONLYOFFICE/document-server-integration/utils"
"github.com/golang-jwt/jwt"
"go.uber.org/zap"
)
@ -152,7 +153,7 @@ func (dm DefaultDocumentManager) BuildDocumentConfig(
return nil, err
}
config := models.Config{
config := &models.Config{
Type: parameters.Type,
DocumentType: dm.ConversionManager.GetFileType(parameters.Filename),
Document: models.Document{
@ -223,6 +224,10 @@ func (dm DefaultDocumentManager) BuildDocumentConfig(
},
},
},
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Minute * dm.config.JwtExpiresIn).Unix(),
IssuedAt: time.Now().Unix(),
},
}
secret := strings.TrimSpace(dm.config.JwtSecret)
@ -231,7 +236,7 @@ func (dm DefaultDocumentManager) BuildDocumentConfig(
config.Token = token
}
return &config, nil
return config, nil
}
func (dm DefaultDocumentManager) IsDocumentConvertable(filename string) bool {

View File

@ -23,12 +23,14 @@ import (
"fmt"
"net/http"
"path"
"time"
"github.com/ONLYOFFICE/document-server-integration/config"
"github.com/ONLYOFFICE/document-server-integration/server/managers"
"github.com/ONLYOFFICE/document-server-integration/server/models"
"github.com/ONLYOFFICE/document-server-integration/server/shared"
"github.com/ONLYOFFICE/document-server-integration/utils"
"github.com/golang-jwt/jwt"
"go.uber.org/zap"
)
@ -195,6 +197,10 @@ func (hm DefaultHistoryManager) fetchNextHistoryEntry(
Key: key,
Url: url,
Version: version,
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Minute * hm.config.JwtExpiresIn).Unix(),
IssuedAt: time.Now().Unix(),
},
}
}
@ -261,6 +267,10 @@ func (hm DefaultHistoryManager) GetHistory(
Url: hm.StorageManager.GeneratePublicFileUri(filename, remoteAddress, managers.FileMeta{}),
Version: version,
ChangesUrl: changesUrl,
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Minute * hm.config.JwtExpiresIn).Unix(),
IssuedAt: time.Now().Unix(),
},
}
rhist.History = append(rhist.History, models.History{

View File

@ -20,10 +20,10 @@ package models
import "github.com/golang-jwt/jwt"
type Config struct {
Type string `json:"type"`
Document Document `json:"document"`
DocumentType string `json:"documentType"`
EditorConfig EditorConfig `json:"editorConfig"`
Token string `json:"token,omitempty"`
jwt.StandardClaims `json:"-"`
Type string `json:"type"`
Document Document `json:"document"`
DocumentType string `json:"documentType"`
EditorConfig EditorConfig `json:"editorConfig"`
Token string `json:"token,omitempty"`
jwt.StandardClaims
}

View File

@ -25,12 +25,12 @@ type ReferenceData struct {
}
type Reference struct {
ReferenceData ReferenceData `json:"referenceData"`
Link string `json:"link"`
Path string `json:"path"`
FileType string `json:"fileType"`
Key string `json:"key"`
Url string `json:"url"`
Token string `json:"token,omitempty"`
jwt.StandardClaims `json:"-"`
ReferenceData ReferenceData `json:"referenceData"`
Link string `json:"link"`
Path string `json:"path"`
FileType string `json:"fileType"`
Key string `json:"key"`
Url string `json:"url"`
Token string `json:"token,omitempty"`
jwt.StandardClaims
}