Skip to content

Commit

Permalink
improoved upload session handling and added longer auth sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirari04 committed Jan 13, 2024
1 parent c48523a commit 977d7da
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 16 deletions.
4 changes: 3 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type Config struct {
ProjectDocumentation string `validate:"required,min=1,max=512"`
ProjectDownload string `validate:"required,min=1,max=512"`

JwtSecretKey string `validate:"required,min=8,max=512"`
JwtSecretKey string `validate:"required,min=8,max=512"`
JwtUploadSecretKey string `validate:"required,min=8,max=512"`

CookieDomain string `validate:"required,min=8,max=225"`

Expand Down Expand Up @@ -129,6 +130,7 @@ func Setup() {
ENV.ProjectDownload = getEnv("ProjectDownload", "https://github.com/notfound")

ENV.JwtSecretKey = getEnv("JwtSecretKey", "secretkey")
ENV.JwtUploadSecretKey = getEnv("JwtUploadSecretKey", "secretkeyupload")

ENV.CookieDomain = getEnv("CookieDomain", "secretkey")

Expand Down
3 changes: 2 additions & 1 deletion controllers/CreateUploadChunckController.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/google/uuid"
)

// this route is not securet with user jwt token so it doesnt invalidate the chunck because the session invalidated during the upload time
func CreateUploadChunck(c *fiber.Ctx) error {
// parse & validate request
var validation models.UploadChunckValidation
Expand Down Expand Up @@ -43,7 +44,7 @@ func CreateUploadChunck(c *fiber.Ctx) error {
}

// business logic
status, response, err := logic.CreateUploadChunck(*validation.Index, validation.SessionJwtToken, filePath, c.Locals("UserID").(uint))
status, response, err := logic.CreateUploadChunck(*validation.Index, validation.SessionJwtToken, filePath)
if err != nil {
os.Remove(filePath)
return c.Status(status).SendString(err.Error())
Expand Down
8 changes: 2 additions & 6 deletions helpers/DynamicJwt.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package helpers

import (
"ch/kirari04/videocms/config"
"time"

"github.com/golang-jwt/jwt/v5"
)

var jwtKey []byte

func GenerateDynamicJWT[T jwt.Claims](claims *T, expire time.Duration) (string, time.Time, error) {
jwtKey = []byte(config.ENV.JwtSecretKey)
func GenerateDynamicJWT[T jwt.Claims](claims *T, expire time.Duration, jwtKey []byte) (string, time.Time, error) {
expirationTime := time.Now().Add(expire)
token := jwt.NewWithClaims(jwt.SigningMethodHS256, *claims)
tokenString, err := token.SignedString(jwtKey)
Expand All @@ -20,9 +18,7 @@ func GenerateDynamicJWT[T jwt.Claims](claims *T, expire time.Duration) (string,
return tokenString, expirationTime, nil
}

func VerifyDynamicJWT[T jwt.Claims](tknStr string, claims T) (*jwt.Token, T, error) {
jwtKey = []byte(config.ENV.JwtSecretKey)

func VerifyDynamicJWT[T jwt.Claims](tknStr string, claims T, jwtKey []byte) (*jwt.Token, T, error) {
tkn, err := jwt.ParseWithClaims(tknStr, claims, func(token *jwt.Token) (interface{}, error) {
return jwtKey, nil
})
Expand Down
11 changes: 6 additions & 5 deletions logic/CreateUploadChunck.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logic

import (
"ch/kirari04/videocms/config"
"ch/kirari04/videocms/helpers"
"ch/kirari04/videocms/inits"
"ch/kirari04/videocms/models"
Expand All @@ -13,19 +14,19 @@ import (
"github.com/gofiber/fiber/v2"
)

func CreateUploadChunck(index uint, sessionToken string, fromFile string, userId uint) (status int, response string, err error) {
func CreateUploadChunck(index uint, sessionToken string, fromFile string) (status int, response string, err error) {
// validate token
token, claims, err := helpers.VerifyDynamicJWT(sessionToken, &models.UploadSessionClaims{})
token, claims, err := helpers.VerifyDynamicJWT(sessionToken, &models.UploadSessionClaims{}, []byte(config.ENV.JwtUploadSecretKey))
if err != nil || claims == nil {
log.Printf("err: %v", err)
return fiber.StatusBadRequest, "", errors.New("broken upload session token")
}
if !token.Valid {
return fiber.StatusBadRequest, "", errors.New("invalid upload session token")
}
if (*claims).UserID != userId {
return fiber.StatusForbidden, "", fiber.ErrForbidden
}
// if (*claims).UserID != userId {
// return fiber.StatusForbidden, "", fiber.ErrForbidden
// }

//check if session still active
uploadSession := models.UploadSession{}
Expand Down
2 changes: 1 addition & 1 deletion logic/CreateUploadFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
*/
func CreateUploadFile(sessionToken string, userId uint) (status int, response *models.Link, err error) {
// validate token
token, claims, err := helpers.VerifyDynamicJWT(sessionToken, &models.UploadSessionClaims{})
token, claims, err := helpers.VerifyDynamicJWT(sessionToken, &models.UploadSessionClaims{}, []byte(config.ENV.JwtUploadSecretKey))
if err != nil && claims != nil {
log.Printf("err: %v", err)
return fiber.StatusBadRequest, nil, errors.New("broken upload session token")
Expand Down
2 changes: 1 addition & 1 deletion logic/CreateUploadSession.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func CreateUploadSession(toFolder uint, fileName string, uploadSessionUUID strin
}

maxUploadDuration := time.Hour * 2
token, expirationTime, err := helpers.GenerateDynamicJWT[models.UploadSessionClaims](&claims, maxUploadDuration)
token, expirationTime, err := helpers.GenerateDynamicJWT[models.UploadSessionClaims](&claims, maxUploadDuration, []byte(config.ENV.JwtUploadSecretKey))
if err != nil {
log.Printf("Failed to generate jwt token for upload session: %v", err)
return fiber.StatusInternalServerError, nil, fiber.ErrInternalServerError
Expand Down
5 changes: 4 additions & 1 deletion routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func Api() {
inits.Api.Get("/p/pages", controllers.ListPublicWebPage)
inits.Api.Get("/p/page", controllers.GetPublicWebPage)

// requires uploadsession jwt inside body
inits.Api.Post("/pcu/chunck", controllers.CreateUploadChunck)

// Routes that require to be authenticated
protectedApi := inits.Api.Group("", middlewares.Auth)
protectedApi.Post("/folder", controllers.CreateFolder)
Expand Down Expand Up @@ -66,6 +69,6 @@ func Api() {
protectedApi.Get("/pcu/sessions", controllers.GetUploadSessions)
protectedApi.Post("/pcu/session", controllers.CreateUploadSession)
protectedApi.Delete("/pcu/session", controllers.DeleteUploadSession)
protectedApi.Post("/pcu/chunck", controllers.CreateUploadChunck)
// protectedApi.Post("/pcu/chunck", controllers.CreateUploadChunck)
protectedApi.Post("/pcu/file", controllers.CreateUploadFile)
}

0 comments on commit 977d7da

Please sign in to comment.