JWT key issue fix
All checks were successful
Automated Container Build / build-and-push (push) Successful in 1m24s
All checks were successful
Automated Container Build / build-and-push (push) Successful in 1m24s
This commit is contained in:
parent
e1fdbd9388
commit
07aad3b8d4
5 changed files with 103 additions and 40 deletions
|
|
@ -18,6 +18,7 @@ type Config struct {
|
|||
BackupDir string
|
||||
MaxLoginAttempts int
|
||||
LockoutSeconds int
|
||||
OnlyOfficeJWT string
|
||||
}
|
||||
|
||||
func Load() *Config {
|
||||
|
|
@ -29,6 +30,7 @@ func Load() *Config {
|
|||
JWTExpirySecs: getEnvInt("JWT_EXPIRY_SECS", 900), // 15 minutes
|
||||
MaxLoginAttempts: getEnvInt("MAX_LOGIN_ATTEMPTS", 5),
|
||||
LockoutSeconds: getEnvInt("LOCKOUT_SECONDS", 300), // 5 minutes
|
||||
OnlyOfficeJWT: getEnv("ONLYOFFICE_JWT_SECRET", ""),
|
||||
}
|
||||
|
||||
cfg.DBPath = cfg.DataDir + "/drive.db"
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import (
|
|||
"git.elijahkuntz.com/Elijah/drive/config"
|
||||
"git.elijahkuntz.com/Elijah/drive/middleware"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
type OnlyOfficeHandler struct {
|
||||
|
|
@ -127,3 +128,28 @@ func (h *OnlyOfficeHandler) CreateBlank(c *fiber.Ctx) error {
|
|||
"name": filepath.Base(finalPath),
|
||||
})
|
||||
}
|
||||
|
||||
// SignConfig takes the OnlyOffice configuration payload and signs it with the JWT secret
|
||||
func (h *OnlyOfficeHandler) SignConfig(c *fiber.Ctx) error {
|
||||
if h.Config.OnlyOfficeJWT == "" {
|
||||
return c.Status(500).JSON(fiber.Map{"error": "JWT secret is not configured on the server"})
|
||||
}
|
||||
|
||||
var payload map[string]interface{}
|
||||
if err := c.BodyParser(&payload); err != nil {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "invalid json payload"})
|
||||
}
|
||||
|
||||
// Create a new JWT token using the HS256 algorithm
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims(payload))
|
||||
|
||||
// Sign the token with our secret
|
||||
signedToken, err := token.SignedString([]byte(h.Config.OnlyOfficeJWT))
|
||||
if err != nil {
|
||||
return c.Status(500).JSON(fiber.Map{"error": "failed to sign token"})
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"token": signedToken,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -188,8 +188,9 @@ func main() {
|
|||
protected.Post("/files/upload", fsHandler.Upload)
|
||||
protected.Post("/files/check-conflicts", fsHandler.CheckConflicts)
|
||||
|
||||
// OnlyOffice blanks
|
||||
// OnlyOffice
|
||||
protected.Post("/onlyoffice/create", onlyOfficeHandler.CreateBlank)
|
||||
protected.Post("/onlyoffice/sign", onlyOfficeHandler.SignConfig)
|
||||
|
||||
protected.Get("/files/folders-tree", fsHandler.GetFolderTree)
|
||||
|
||||
|
|
|
|||
Reference in a new issue