auth: fix advertised password to actually advertise.

Change-Id: I3c5dc19acabb2ddcca4877000bad69f3f9074ef2
This commit is contained in:
Brad Fitzpatrick 2011-11-27 22:23:23 -05:00
parent 7619e6c174
commit db84536371
1 changed files with 12 additions and 14 deletions

View File

@ -28,7 +28,6 @@ import (
var kBasicAuthPattern *regexp.Regexp = regexp.MustCompile(`^Basic ([a-zA-Z0-9\+/=]+)`) var kBasicAuthPattern *regexp.Regexp = regexp.MustCompile(`^Basic ([a-zA-Z0-9\+/=]+)`)
var ( var (
Type string // how to do auth. currently supported: "userpass"
mode AuthMode // the auth logic corresponding to Type mode AuthMode // the auth logic corresponding to Type
) )
@ -45,22 +44,21 @@ func FromConfig(authConfig string) (AuthMode, os.Error) {
if len(pieces) < 1 { if len(pieces) < 1 {
return nil, fmt.Errorf("Invalid auth string: %q", authConfig) return nil, fmt.Errorf("Invalid auth string: %q", authConfig)
} }
Type = pieces[0] authType := pieces[0]
switch Type {
if pw := os.Getenv("CAMLI_ADVERTISED_PASSWORD"); pw != "" {
mode = &DevAuth{pw}
return mode, nil
}
switch authType {
case "userpass": case "userpass":
if len(pieces) != 3 { if len(pieces) != 3 {
return nil, fmt.Errorf("Wrong userpass auth string; needs to be \"userpass:user:password\"") return nil, fmt.Errorf("Wrong userpass auth string; needs to be \"userpass:user:password\"")
} }
mode = &UserPass{pieces[1], pieces[2]} mode = &UserPass{pieces[1], pieces[2]}
case "":
password := os.Getenv("CAMLI_ADVERTISED_PASSWORD")
if password != "" {
mode = &DevAuth{password}
} else {
return nil, fmt.Errorf("No auth string provided and no \"CAMLI_ADVERTISED_PASSWORD\" defined")
}
default: default:
return nil, fmt.Errorf("Unknown auth type: %q", Type) return nil, fmt.Errorf("Unknown auth type: %q", authType)
} }
return mode, nil return mode, nil
} }
@ -130,8 +128,8 @@ func TriedAuthorization(req *http.Request) bool {
func SendUnauthorized(conn http.ResponseWriter) { func SendUnauthorized(conn http.ResponseWriter) {
realm := "camlistored" realm := "camlistored"
if Type == "dev" { if devAuth, ok := mode.(*DevAuth); ok {
realm = "Any username, password is: " + mode.(*DevAuth).Password realm = "Any username, password is: " + devAuth.Password
} }
conn.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=%q", realm)) conn.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=%q", realm))
conn.WriteHeader(http.StatusUnauthorized) conn.WriteHeader(http.StatusUnauthorized)