novelty.go

Single word (yes/no) site for app engine in go
git clone https://wehaveforgeathome.hates.computer/novelty.go.git
Log | Files | Refs | LICENSE

commit 1f929fc4f22df70b2ddd814cc17474e40cab92f3
parent 76c745b28df7d2f4a554d14964a962a6ab12696a
Author: Ryan Wolf <johnwayne@pseudony.ms>
Date:   Sun, 17 Feb 2019 09:36:42 -0500

dont need hack around broken basicauth anymore

Diffstat:
Dbasicauth/basicauth.go | 29-----------------------------
Dbasicauth/basicauth_test.go | 60------------------------------------------------------------
Mnovelty/novelty.go | 6++----
3 files changed, 2 insertions(+), 93 deletions(-)

diff --git a/basicauth/basicauth.go b/basicauth/basicauth.go @@ -1,29 +0,0 @@ -package basicauth - -import ( - "encoding/base64" - "errors" - "strings" -) - -func Decode(h string) (u string, p string, e error) { - prefix := "Basic " - if !strings.HasPrefix(h, prefix) { - e = errors.New("Bad Request") - return - } - h = h[6:] - auth, err := base64.StdEncoding.DecodeString(h) - if err != nil { - e = errors.New("Bad Request") - return - } - fields := strings.Split(string(auth), ":") - if len(fields) != 2 { - e = errors.New("Bad Request") - return - } - u = fields[0] - p = fields[1] - return -} diff --git a/basicauth/basicauth_test.go b/basicauth/basicauth_test.go @@ -1,60 +0,0 @@ -package basicauth - -import ( - "encoding/base64" - "testing" -) - -func TestDecodeEmpty(t *testing.T) { - if _, _, err := Decode(""); err == nil { - t.Error("Should fail on empty") - } -} - -func TestDecodeMissingPrefix(t *testing.T) { - if _, _, err := Decode("Bees"); err == nil { - t.Error("Should fail on missing prefix") - } -} - -func TestDecodeEmptySuffix(t *testing.T) { - if _, _, err := Decode("Basic "); err == nil { - t.Error("Should fail on empty suffix") - } -} - -func TestDecodeInvalidSuffix(t *testing.T) { - if _, _, err := Decode("Basic !*@&#@"); err == nil { - t.Error("Should fail on invalid suffix") - } -} - -func TestDecodeNoDelimiter(t *testing.T) { - a := base64.StdEncoding.EncodeToString([]byte("bees")) - if _, _, err := Decode(a); err == nil { - t.Error("Should fail on no delmiter") - } -} - -func TestDecodeTooManyDelimiters(t *testing.T) { - a := base64.StdEncoding.EncodeToString([]byte("b:e:s")) - if _, _, err := Decode(a); err == nil { - t.Error("Should fail on too many delimiters") - } -} - -func TestDecodeValidAuth(t *testing.T) { - u := "ryan" - p := "clowns" - a := base64.StdEncoding.EncodeToString([]byte(u + ":" + p)) - username, password, err := Decode("Basic " + a) - if err != nil { - t.Error("Error should be nil") - } - if username != u { - t.Errorf("Wrong username. Expected '%s' got '%s'", u, username) - } - if password != p { - t.Errorf("Wrong password. Expected '%s' got '%s'", p, password) - } -} diff --git a/novelty/novelty.go b/novelty/novelty.go @@ -3,7 +3,6 @@ package novelty import ( "appengine" "appengine/datastore" - "basicauth" "html/template" "net/http" ) @@ -37,9 +36,8 @@ func getAnswer(w http.ResponseWriter, r *http.Request) { } func authorized(r *http.Request) bool { - h := r.Header.Get("Authorization") - _, password, err := basicauth.Decode(h) - if err != nil { + _, password, ok := r.BasicAuth() + if !ok { return false } c := appengine.NewContext(r)