Code cleanup
This commit is contained in:
1 parent
20cc4db097
commit
f5cdbe1bd3
6 files changed
+27
-19
No files matched your search
+1
-1
@@ -143,7 +143,7 @@ func HandleDelete(resourceManager resource.Manager) gin.HandlerFunc {
|
||||
path := c.Query("path")
|
||||
log.Printf("Deleting '%s'...", path)
|
||||
if err := resourceManager.Delete(path); err != nil {
|
||||
c.AbortWithError(400, err)
|
||||
_ = c.AbortWithError(400, err)
|
||||
} else {
|
||||
log.Printf("Deleted '%s'", path)
|
||||
c.Status(204)
|
||||
|
||||
+4
-4
@@ -32,14 +32,14 @@ func Auth() gin.HandlerFunc {
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
if strings.HasPrefix(authHeader, "Basic ") && hasBasicAuth {
|
||||
if strings.TrimPrefix(authHeader, "Basic ") == basicAuth {
|
||||
c.Set("secure", "basic")
|
||||
c.Set("secure", security.TypeBasic)
|
||||
return
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(authHeader, "Api ") && hasBasicAuth {
|
||||
key := strings.TrimPrefix(authHeader, "Api ")
|
||||
if hasValidApiKey(apiAuths, key) {
|
||||
c.Set("secure", "api")
|
||||
c.Set("secure", security.TypeApi)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -47,13 +47,13 @@ func Auth() gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func Secure(types ...string) gin.HandlerFunc {
|
||||
func Secure(types ...security.Type) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
value, exists := c.Get("secure")
|
||||
if !exists {
|
||||
abort(c, nil, http.StatusUnauthorized, "missing auth")
|
||||
} else {
|
||||
securityType := value.(string)
|
||||
securityType := security.Type(value.(string))
|
||||
for _, t := range types {
|
||||
if t == securityType {
|
||||
return
|
||||
|
||||
+3
-2
@@ -6,6 +6,7 @@ import (
|
||||
"os"
|
||||
"resource-manager/domain/cache"
|
||||
"resource-manager/domain/resource"
|
||||
"resource-manager/security"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -31,13 +32,13 @@ func RegisterRoutes(server *gin.Engine) {
|
||||
|
||||
resourceManager := resource.NewManager(cacheManager, expiration)
|
||||
|
||||
resourcesGroup := server.Group("/api/v1/resources", Secure("basic", "api"))
|
||||
resourcesGroup := server.Group("/api/v1/resources", Secure(security.TypeBasic, security.TypeApi))
|
||||
resourcesGroup.POST("", HandleUpload(resourceManager))
|
||||
resourcesGroup.GET("presign", HandlePresign(resourceManager))
|
||||
resourcesGroup.PUT("copy", HandleCopy(resourceManager))
|
||||
resourcesGroup.DELETE("", HandleDelete(resourceManager))
|
||||
|
||||
server.GET("/api/v1/resources", Secure("basic", "api", "token"), HandleDownload(resourceManager))
|
||||
server.GET("/api/v1/resources", Secure(security.TypeBasic, security.TypeApi, security.TypeToken), HandleDownload(resourceManager))
|
||||
}
|
||||
|
||||
func loadExpiration() time.Duration {
|
||||
|
||||
Reference in new issue
Block a user