func (basicAuth *BasicAuth) Authenticate(ctx *Context) (ok bool, err error) { username, password := RequestBasicAuth(ctx.Request) if username != "" { p, ok := basicAuth.UserPassword[username] if ok && p == utils.SHA1Base64String(password) { ctx.AuthUser = username return true, nil } } SendBasicAuthRequired(ctx.Response, basicAuth.Realm) return false, nil }
// NewBasicAuth creates a BasicAuth instance with a series of // not encrypted usernames and passwords that are provided in alternating order. func NewBasicAuth(realm string, usernamesAndPasswords ...string) *BasicAuth { userPass := make(map[string]string, len(usernamesAndPasswords)/2) for i := 0; i < len(usernamesAndPasswords)/2; i++ { username := usernamesAndPasswords[i*2] password := usernamesAndPasswords[i*2+1] userPass[username] = utils.SHA1Base64String(password) } return &BasicAuth{ Realm: realm, UserPassword: userPass, } }