From 4103061a4a8d977fe0700e89b13c3ce51ec1d92f Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Mon, 23 Mar 2015 04:38:32 +0100 Subject: [PATCH] Refactores BasicAuth --- auth.go | 13 +++++-------- auth_test.go | 4 ++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/auth.go b/auth.go index 9caf072e..0cf64e59 100644 --- a/auth.go +++ b/auth.go @@ -33,10 +33,7 @@ func (a authPairs) Less(i, j int) bool { return a[i].Value < a[j].Value } // the key is the user name and the value is the password, as well as the name of the Realm // (see http://tools.ietf.org/html/rfc2617#section-1.2) func BasicAuthForRealm(accounts Accounts, realm string) HandlerFunc { - pairs, err := processAccounts(accounts) - if err != nil { - panic(err) - } + pairs := processAccounts(accounts) return func(c *Context) { // Search user in the slice of allowed credentials user, ok := searchCredential(pairs, c.Request.Header.Get("Authorization")) @@ -61,14 +58,14 @@ func BasicAuth(accounts Accounts) HandlerFunc { return BasicAuthForRealm(accounts, "") } -func processAccounts(accounts Accounts) (authPairs, error) { +func processAccounts(accounts Accounts) authPairs { if len(accounts) == 0 { - return nil, errors.New("Empty list of authorized credentials") + panic("Empty list of authorized credentials") } pairs := make(authPairs, 0, len(accounts)) for user, password := range accounts { if len(user) == 0 { - return nil, errors.New("User can not be empty") + panic("User can not be empty") } base := user + ":" + password value := "Basic " + base64.StdEncoding.EncodeToString([]byte(base)) @@ -79,7 +76,7 @@ func processAccounts(accounts Accounts) (authPairs, error) { } // We have to sort the credentials in order to use bsearch later. sort.Sort(pairs) - return pairs, nil + return pairs } func searchCredential(pairs authPairs, auth string) (string, bool) { diff --git a/auth_test.go b/auth_test.go index 067dfb19..1ea1d50b 100644 --- a/auth_test.go +++ b/auth_test.go @@ -27,7 +27,7 @@ func TestBasicAuthSucceed(t *testing.T) { r.ServeHTTP(w, req) if w.Code != 200 { - t.Errorf("Response code should be Ok, was: %s", w.Code) + t.Errorf("Response code should be Ok, was: %d", w.Code) } bodyAsString := w.Body.String() @@ -52,7 +52,7 @@ func TestBasicAuth401(t *testing.T) { r.ServeHTTP(w, req) if w.Code != 401 { - t.Errorf("Response code should be Not autorized, was: %s", w.Code) + t.Errorf("Response code should be Not autorized, was: %d", w.Code) } if w.HeaderMap.Get("WWW-Authenticate") != "Basic realm=\"Authorization Required\"" {