Add: Support for Authenticated Uploads

This commit is contained in:
Prajjwal Singh 2017-11-27 22:25:06 +05:30
parent d0c7241b31
commit 5f114dcac8
4 changed files with 44 additions and 12 deletions

View file

@ -51,6 +51,7 @@ aws-access-key | aws access key | | AWS_ACCESS_KEY
aws-secret-key | aws access key | | AWS_SECRET_KEY
bucket | aws bucket | | BUCKET
basedir | path storage for local provider| |
auth-key | key to use for authentication (must be supplied in 'Authorization' header with each request)| |
lets-encrypt-hosts | hosts to use for lets encrypt certificates (comma seperated) | |
log | path to log file| |

View file

@ -139,6 +139,12 @@ var globalFlags = []cli.Flag{
Name: "profiler",
Usage: "enable profiling",
},
cli.StringFlag{
Name: "auth-key",
Usage: "auth-key",
Value: "",
EnvVar: "AUTH_KEY",
},
}
type Cmd struct {
@ -198,6 +204,10 @@ func New() *Cmd {
options = append(options, server.VirustotalKey(v))
}
if v := c.String("auth-key"); v != "" {
options = append(options, server.AuthenticateUploads(v))
}
if v := c.String("clamav-host"); v != "" {
options = append(options, server.ClamavHost(v))
}

View file

@ -760,3 +760,17 @@ func LoveHandler(h http.Handler) http.HandlerFunc {
h.ServeHTTP(w, r)
}
}
func (s *Server) AuthenticatedHandler (h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
authKey := r.Header.Get ("Authorization")
if s.AuthKey != "" && authKey != s.AuthKey {
log.Printf("Recieved: Bad Auth Token: %s", authKey)
http.Error(w, errors.New("Bad Auth Token").Error(), 403)
return
}
h.ServeHTTP(w, r)
}
}

View file

@ -75,6 +75,12 @@ func VirustotalKey(s string) OptionFn {
}
}
func AuthenticateUploads(key string) OptionFn {
return func(srvr *Server) {
srvr.AuthKey = key
}
}
func Listener(s string) OptionFn {
return func(srvr *Server) {
srvr.ListenerString = s
@ -189,6 +195,7 @@ type Server struct {
locks map[string]*sync.Mutex
rateLimitRequests int
AuthKey string
storage Storage
@ -317,10 +324,10 @@ func (s *Server) Run() {
r.HandleFunc("/{filename}/virustotal", s.virusTotalHandler).Methods("PUT")
r.HandleFunc("/{filename}/scan", s.scanHandler).Methods("PUT")
r.HandleFunc("/put/{filename}", s.putHandler).Methods("PUT")
r.HandleFunc("/upload/{filename}", s.putHandler).Methods("PUT")
r.HandleFunc("/{filename}", s.putHandler).Methods("PUT")
r.HandleFunc("/", s.postHandler).Methods("POST")
r.HandleFunc("/put/{filename}", s.AuthenticatedHandler (http.HandlerFunc (s.putHandler))).Methods("PUT")
r.HandleFunc("/upload/{filename}", s.AuthenticatedHandler (http.HandlerFunc (s.putHandler))).Methods("PUT")
r.HandleFunc("/{filename}", s.AuthenticatedHandler (http.HandlerFunc (s.putHandler))).Methods("PUT")
r.HandleFunc("/", s.AuthenticatedHandler (http.HandlerFunc (s.postHandler))).Methods("POST")
// r.HandleFunc("/{page}", viewHandler).Methods("GET")
r.NotFoundHandler = http.HandlerFunc(s.notFoundHandler)