start of some ghetto HTTP Basic Auth

This commit is contained in:
Brad Fitzpatrick 2010-06-13 21:51:18 -07:00
parent e7b00b6e2c
commit a2bed507d5
2 changed files with 26 additions and 1 deletions

View File

@ -5,6 +5,7 @@
package main package main
import "crypto/sha1" import "crypto/sha1"
import "encoding/base64"
import "flag" import "flag"
import "fmt" import "fmt"
import "hash" import "hash"
@ -20,6 +21,7 @@ var storageRoot *string = flag.String("root", "/tmp/camliroot", "Root directory
var sharedSecret string var sharedSecret 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\+/=]+)`)
type ObjectRef struct { type ObjectRef struct {
hashName string hashName string
@ -75,6 +77,22 @@ func serverError(conn *http.Conn, err os.Error) {
fmt.Fprintf(conn, "Server error: %s\n", err) fmt.Fprintf(conn, "Server error: %s\n", err)
} }
func putAllowed(req *http.Request) bool {
auth, present := req.Header["Authorization"]
if !present {
return false
}
matches := kBasicAuthPattern.MatchStrings(auth)
if len(matches) != 2 {
return false
}
var outBuf []byte = make([]byte, base64.StdEncoding.DecodedLen(len(matches[1])))
bytes, err := base64.StdEncoding.Decode(outBuf, []uint8(matches[1]))
fmt.Println("Decoded bytes:", bytes, " error: ", err)
fmt.Println("Got userPass:", string(outBuf))
return false
}
func handleCamli(conn *http.Conn, req *http.Request) { func handleCamli(conn *http.Conn, req *http.Request) {
if (req.Method == "PUT") { if (req.Method == "PUT") {
handlePut(conn, req) handlePut(conn, req)
@ -143,6 +161,13 @@ func handlePut(conn *http.Conn, req *http.Request) {
return return
} }
if !putAllowed(req) {
conn.SetHeader("WWW-Authenticate", "Basic realm=\"camlistored\"")
conn.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(conn, "Authentication required.")
return
}
// TODO(bradfitz): authn/authz checks here. // TODO(bradfitz): authn/authz checks here.
hashedDirectory := objRef.DirectoryName() hashedDirectory := objRef.DirectoryName()

View File

@ -16,6 +16,6 @@ $url =~ s!/$!!;
$url .= "/camli/sha1-$sha1"; $url .= "/camli/sha1-$sha1";
print "PUT'ing to $url ...\n"; print "PUT'ing to $url ...\n";
system("curl", "-T", $file, $url) and die "Curl failed."; system("curl", "-u", "test:foo", "-T", $file, $url) and die "Curl failed.";