diff options
Diffstat (limited to 'app')
| -rw-r--r-- | app/app.go | 7 | ||||
| -rw-r--r-- | app/middleware/auth.go | 15 |
2 files changed, 21 insertions, 1 deletions
@@ -97,10 +97,15 @@ func NewApp(cfg *Config) (*App, error) { // Setup Router authPassword := os.Getenv("auth_password") + isSandstorm := os.Getenv("SANDSTORM") r := mux.NewRouter().StrictSlash(true) r.HandleFunc("/", a.indexHandler).Methods("GET", "OPTIONS") - r.HandleFunc("/upload", middleware.OptionallyRequireAdminAuth(a.uploadHandler, authPassword)).Methods("GET", "OPTIONS", "POST") + if isSandstorm == "1" { + r.HandleFunc("/upload", middleware.RequireSandstormPermission(a.uploadHandler, "upload")).Methods("GET", "OPTIONS", "POST") + } else { + r.HandleFunc("/upload", middleware.OptionallyRequireAdminAuth(a.uploadHandler, authPassword)).Methods("GET", "OPTIONS", "POST") + } r.HandleFunc("/import", a.importHandler).Methods("GET", "OPTIONS", "POST") r.HandleFunc("/v/{id}.mp4", a.videoHandler).Methods("GET") r.HandleFunc("/v/{prefix}/{id}.mp4", a.videoHandler).Methods("GET") diff --git a/app/middleware/auth.go b/app/middleware/auth.go index 61a58e3..c023492 100644 --- a/app/middleware/auth.go +++ b/app/middleware/auth.go @@ -3,6 +3,7 @@ package middleware import ( "crypto/subtle" "net/http" + "strings" log "github.com/sirupsen/logrus" ) @@ -48,3 +49,17 @@ func OptionallyRequireAdminAuth(handler http.HandlerFunc, password string) http. handler(w, r) } } + +func RequireSandstormPermission(handler http.HandlerFunc, permissionNeeded string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + // Failed + if !strings.Contains(r.Header.Get("X-Sandstorm-Permissions"), permissionNeeded) { + http.Error(w, "Unauthorized", http.StatusUnauthorized) + log.Debugln("No upload capability granted") + return + } + + handler(w, r) + } +} |
