mirror of https://github.com/perkeep/perkeep.git
require basic auth for getting too
This commit is contained in:
parent
d9e9c6c89b
commit
1b976129dd
|
@ -18,7 +18,7 @@ import "regexp"
|
||||||
var listen *string = flag.String("listen", "0.0.0.0:3179", "host:port to listen on")
|
var listen *string = flag.String("listen", "0.0.0.0:3179", "host:port to listen on")
|
||||||
var storageRoot *string = flag.String("root", "/tmp/camliroot", "Root directory to store files")
|
var storageRoot *string = flag.String("root", "/tmp/camliroot", "Root directory to store files")
|
||||||
|
|
||||||
var sharedSecret string
|
var putPassword string
|
||||||
|
|
||||||
var kGetPutPattern *regexp.Regexp = regexp.MustCompile(`^/camli/(sha1)-([a-f0-9]+)$`)
|
var kGetPutPattern *regexp.Regexp = regexp.MustCompile(`^/camli/(sha1)-([a-f0-9]+)$`)
|
||||||
var kBasicAuthPattern *regexp.Regexp = regexp.MustCompile(`^Basic ([a-zA-Z0-9\+/=]+)`)
|
var kBasicAuthPattern *regexp.Regexp = regexp.MustCompile(`^Basic ([a-zA-Z0-9\+/=]+)`)
|
||||||
|
@ -88,9 +88,18 @@ func putAllowed(req *http.Request) bool {
|
||||||
}
|
}
|
||||||
var outBuf []byte = make([]byte, base64.StdEncoding.DecodedLen(len(matches[1])))
|
var outBuf []byte = make([]byte, base64.StdEncoding.DecodedLen(len(matches[1])))
|
||||||
bytes, err := base64.StdEncoding.Decode(outBuf, []uint8(matches[1]))
|
bytes, err := base64.StdEncoding.Decode(outBuf, []uint8(matches[1]))
|
||||||
fmt.Println("Decoded bytes:", bytes, " error: ", err)
|
if err != nil {
|
||||||
fmt.Println("Got userPass:", string(outBuf))
|
|
||||||
return false
|
return false
|
||||||
|
}
|
||||||
|
password := string(outBuf)
|
||||||
|
fmt.Println("Decoded bytes:", bytes, " error: ", err)
|
||||||
|
fmt.Println("Got userPass:", password)
|
||||||
|
return password != "" && password == putPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
func getAllowed(req *http.Request) bool {
|
||||||
|
// For now...
|
||||||
|
return putAllowed(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleCamli(conn *http.Conn, req *http.Request) {
|
func handleCamli(conn *http.Conn, req *http.Request) {
|
||||||
|
@ -108,6 +117,13 @@ func handleCamli(conn *http.Conn, req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleGet(conn *http.Conn, req *http.Request) {
|
func handleGet(conn *http.Conn, req *http.Request) {
|
||||||
|
if !getAllowed(req) {
|
||||||
|
conn.SetHeader("WWW-Authenticate", "Basic realm=\"camlistored\"")
|
||||||
|
conn.WriteHeader(http.StatusUnauthorized)
|
||||||
|
fmt.Fprintf(conn, "Authentication required.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
objRef := ParsePath(req.URL.Path)
|
objRef := ParsePath(req.URL.Path)
|
||||||
if objRef == nil {
|
if objRef == nil {
|
||||||
badRequestError(conn, "Malformed GET URL.")
|
badRequestError(conn, "Malformed GET URL.")
|
||||||
|
@ -239,8 +255,8 @@ This is camlistored, a Camlistore storage daemon.
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
sharedSecret = os.Getenv("CAMLI_PASSWORD")
|
putPassword = os.Getenv("CAMLI_PASSWORD")
|
||||||
if len(sharedSecret) == 0 {
|
if len(putPassword) == 0 {
|
||||||
fmt.Fprintf(os.Stderr,
|
fmt.Fprintf(os.Stderr,
|
||||||
"No CAMLI_PASSWORD environment variable set.\n")
|
"No CAMLI_PASSWORD environment variable set.\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
Loading…
Reference in New Issue