mirror of
https://github.com/restic/rest-server.git
synced 2025-10-19 15:43:21 +00:00
31 lines
605 B
Go
31 lines
605 B
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"path/filepath"
|
|
)
|
|
|
|
func Authorize(r *http.Request, c *Context) error {
|
|
file := filepath.Join(c.path, ".htpasswd")
|
|
htpasswd, err := NewHtpasswdFromFile(file)
|
|
if err != nil {
|
|
return errors.New("internal server error")
|
|
}
|
|
|
|
username, password, ok := r.BasicAuth()
|
|
if !ok {
|
|
return errors.New("malformed basic auth credentials")
|
|
}
|
|
|
|
if !htpasswd.Validate(username, password) {
|
|
return errors.New("unknown user")
|
|
}
|
|
|
|
repo, err := RepositoryName(r.RequestURI)
|
|
if err != nil || repo != username {
|
|
return errors.New("wrong repository")
|
|
}
|
|
|
|
return nil
|
|
}
|