mirror of https://github.com/gin-gonic/gin.git
Nicer BasicAuth API
This commit is contained in:
parent
c8cb943ef7
commit
661398ca53
15
auth.go
15
auth.go
|
@ -16,12 +16,7 @@ type (
|
||||||
Code string
|
Code string
|
||||||
User string
|
User string
|
||||||
}
|
}
|
||||||
Account struct {
|
Accounts map[string]string
|
||||||
User string
|
|
||||||
Password string
|
|
||||||
}
|
|
||||||
|
|
||||||
Accounts []Account
|
|
||||||
Pairs []BasicAuthPair
|
Pairs []BasicAuthPair
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -34,13 +29,13 @@ func processCredentials(accounts Accounts) (Pairs, error) {
|
||||||
return nil, errors.New("Empty list of authorized credentials.")
|
return nil, errors.New("Empty list of authorized credentials.")
|
||||||
}
|
}
|
||||||
pairs := make(Pairs, 0, len(accounts))
|
pairs := make(Pairs, 0, len(accounts))
|
||||||
for _, account := range accounts {
|
for user, password := range accounts {
|
||||||
if len(account.User) == 0 || len(account.Password) == 0 {
|
if len(user) == 0 || len(password) == 0 {
|
||||||
return nil, errors.New("User or password is empty")
|
return nil, errors.New("User or password is empty")
|
||||||
}
|
}
|
||||||
base := account.User + ":" + account.Password
|
base := user + ":" + password
|
||||||
code := "Basic " + base64.StdEncoding.EncodeToString([]byte(base))
|
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.
|
// We have to sort the credentials in order to use bsearch later.
|
||||||
sort.Sort(pairs)
|
sort.Sort(pairs)
|
||||||
|
|
Loading…
Reference in New Issue