BasicAuth increase speed up

This commit is contained in:
john 2020-06-14 00:23:57 +08:00
parent 5e40c1d49c
commit 5b17ae8def

34
auth.go
View File

@ -18,25 +18,22 @@ const AuthUserKey = "user"
// Accounts defines a key/value for user/pass list of authorized logins. // Accounts defines a key/value for user/pass list of authorized logins.
type Accounts map[string]string type Accounts map[string]string
type authPair struct { // authKVMap defines a key/value for Authorization token / user
value string type authKVMap map[string]string
user string
}
type authPairs []authPair
func (a authPairs) searchCredential(authValue string) (string, bool) { func (a authKVMap) searchCredential(authValue string) (string, bool) {
if authValue == "" { if authValue == "" {
return "", false return "", false
} }
for _, pair := range a { user,ok:=a[authValue]
if pair.value == authValue { if !ok{
return pair.user, true return "", false
}
} }
return "", false return user,true
} }
// BasicAuthForRealm returns a Basic HTTP Authorization middleware. It takes as arguments a map[string]string where // BasicAuthForRealm returns a Basic HTTP Authorization middleware. It takes as arguments a map[string]string where
// the key is the user name and the value is the password, as well as the name of the Realm. // the key is the user name and the value is the password, as well as the name of the Realm.
// If the realm is empty, "Authorization Required" will be used by default. // If the realm is empty, "Authorization Required" will be used by default.
@ -46,10 +43,10 @@ func BasicAuthForRealm(accounts Accounts, realm string) HandlerFunc {
realm = "Authorization Required" realm = "Authorization Required"
} }
realm = "Basic realm=" + strconv.Quote(realm) realm = "Basic realm=" + strconv.Quote(realm)
pairs := processAccounts(accounts) pairMap := processAccounts(accounts)
return func(c *Context) { return func(c *Context) {
// Search user in the slice of allowed credentials // Search user in the slice of allowed credentials
user, found := pairs.searchCredential(c.requestHeader("Authorization")) user, found := pairMap.searchCredential(c.requestHeader("Authorization"))
if !found { if !found {
// Credentials doesn't match, we return 401 and abort handlers chain. // Credentials doesn't match, we return 401 and abort handlers chain.
c.Header("WWW-Authenticate", realm) c.Header("WWW-Authenticate", realm)
@ -69,19 +66,16 @@ func BasicAuth(accounts Accounts) HandlerFunc {
return BasicAuthForRealm(accounts, "") return BasicAuthForRealm(accounts, "")
} }
func processAccounts(accounts Accounts) authPairs { func processAccounts(accounts Accounts) authKVMap {
length := len(accounts) length := len(accounts)
assert1(length > 0, "Empty list of authorized credentials") assert1(length > 0, "Empty list of authorized credentials")
pairs := make(authPairs, 0, length) pairMap := make(authKVMap)
for user, password := range accounts { for user, password := range accounts {
assert1(user != "", "User can not be empty") assert1(user != "", "User can not be empty")
value := authorizationHeader(user, password) value := authorizationHeader(user, password)
pairs = append(pairs, authPair{ pairMap[value]=user
value: value,
user: user,
})
} }
return pairs return pairMap
} }
func authorizationHeader(user, password string) string { func authorizationHeader(user, password string) string {