sign url
This commit is contained in:
@@ -4,9 +4,12 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/vaultdrop/backend/internal/service"
|
||||
)
|
||||
|
||||
func ListFiles(c *gin.Context) {
|
||||
@@ -15,7 +18,6 @@ func ListFiles(c *gin.Context) {
|
||||
|
||||
if err != nil {
|
||||
|
||||
fmt.Println("What")
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"data": []interface{}{},
|
||||
"meta": gin.H{
|
||||
@@ -28,6 +30,7 @@ func ListFiles(c *gin.Context) {
|
||||
}
|
||||
|
||||
type Finfo struct {
|
||||
Url string `json:"url"`
|
||||
Name string `json:"name"`
|
||||
Size int64 `json:"size"`
|
||||
Tags []string `json:"tags"`
|
||||
@@ -48,6 +51,7 @@ func ListFiles(c *gin.Context) {
|
||||
}
|
||||
|
||||
ps := Finfo{
|
||||
Url: service.GenerateFileDownloadUrl(i.Name()),
|
||||
Name: i.Name(),
|
||||
Size: i.Size(),
|
||||
Tags: []string{},
|
||||
@@ -67,12 +71,26 @@ func ListFiles(c *gin.Context) {
|
||||
}
|
||||
|
||||
func GetFile(c *gin.Context) {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"error": gin.H{
|
||||
"code": "FILE_NOT_FOUND",
|
||||
"message": "File not found",
|
||||
},
|
||||
})
|
||||
|
||||
exp, _ := strconv.ParseInt(c.Query("expires"), 10, 64)
|
||||
|
||||
r := service.Validate(c.Params.ByName("id"), c.Query("sig"), exp)
|
||||
|
||||
if r != true {
|
||||
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"error": gin.H{
|
||||
"code": "FILE_NOT_FOUND",
|
||||
"message": "File not found",
|
||||
},
|
||||
})
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
c.File(path.Join("./uploads/", c.Params.ByName("id")))
|
||||
|
||||
}
|
||||
|
||||
func UploadFile(c *gin.Context) {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
const secret = "thisismyrandomstring"
|
||||
|
||||
func sign(fileID string, expires int64, secret string) string {
|
||||
data := fmt.Sprintf("%s:%d", fileID, expires)
|
||||
mac := hmac.New(sha256.New, []byte(secret))
|
||||
mac.Write([]byte(data))
|
||||
return hex.EncodeToString(mac.Sum(nil))
|
||||
}
|
||||
|
||||
func GenerateFileDownloadUrl(fileID string) string {
|
||||
expires := time.Now().Add(10 * time.Minute).Unix()
|
||||
sig := sign(fileID, expires, secret)
|
||||
|
||||
url := fmt.Sprintf(
|
||||
"http://192.168.1.142:8080/api/v1/files/%s?expires=%d&sig=%s",
|
||||
fileID,
|
||||
expires,
|
||||
sig,
|
||||
)
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
func Validate(fileID, sig string, expires int64) bool {
|
||||
if time.Now().Unix() > expires {
|
||||
return false
|
||||
}
|
||||
|
||||
expected := sign(fileID, expires, secret)
|
||||
return hmac.Equal([]byte(sig), []byte(expected))
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"mime/multipart"
|
||||
)
|
||||
|
||||
func UploadFile(f *multipart.FileHeader) {
|
||||
fmt.Println(f.Filename)
|
||||
}
|
||||
Reference in New Issue
Block a user