From 661398ca53c85c89a96be4ea5874872d60484af8 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Fri, 4 Jul 2014 04:28:25 +0200 Subject: [PATCH] Nicer BasicAuth API --- auth.go | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/auth.go b/auth.go index eab1d946..a8720c41 100644 --- a/auth.go +++ b/auth.go @@ -16,12 +16,7 @@ type ( Code string User string } - Account struct { - User string - Password string - } - - Accounts []Account + Accounts map[string]string Pairs []BasicAuthPair ) @@ -34,13 +29,13 @@ func processCredentials(accounts Accounts) (Pairs, error) { return nil, errors.New("Empty list of authorized credentials.") } pairs := make(Pairs, 0, len(accounts)) - for _, account := range accounts { - if len(account.User) == 0 || len(account.Password) == 0 { + for user, password := range accounts { + if len(user) == 0 || len(password) == 0 { return nil, errors.New("User or password is empty") } - base := account.User + ":" + account.Password + base := user + ":" + password code := "Basic " + base64.StdEncoding.EncodeToString([]byte(base)) - pairs = append(pairs, BasicAuthPair{code, account.User}) + pairs = append(pairs, BasicAuthPair{code, user}) } // We have to sort the credentials in order to use bsearch later. sort.Sort(pairs)