Skip to content

Commit

Permalink
Nicer BasicAuth API
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Jul 4, 2014
1 parent c8cb943 commit 661398c
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ type (
Code string
User string
}
Account struct {
User string
Password string
}

Accounts []Account
Accounts map[string]string
Pairs []BasicAuthPair
)

Expand All @@ -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)
Expand Down

0 comments on commit 661398c

Please sign in to comment.