Merge pull request #231 from frankbille/patch-1
Add customizable Realm for Basic authentication
This commit is contained in:
commit
1a237a848e
19
auth.go
19
auth.go
@ -8,6 +8,7 @@ import (
|
|||||||
"crypto/subtle"
|
"crypto/subtle"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -28,9 +29,10 @@ func (a authPairs) Len() int { return len(a) }
|
|||||||
func (a authPairs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
func (a authPairs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
func (a authPairs) Less(i, j int) bool { return a[i].Value < a[j].Value }
|
func (a authPairs) Less(i, j int) bool { return a[i].Value < a[j].Value }
|
||||||
|
|
||||||
// Implements a basic Basic HTTP Authorization. It takes as argument a map[string]string where
|
// Implements a basic Basic HTTP Authorization. It takes as arguments a map[string]string where
|
||||||
// the key is the user name and the value is the password.
|
// the key is the user name and the value is the password, as well as the name of the Realm
|
||||||
func BasicAuth(accounts Accounts) HandlerFunc {
|
// (see http://tools.ietf.org/html/rfc2617#section-1.2)
|
||||||
|
func BasicAuthForRealm(accounts Accounts, realm string) HandlerFunc {
|
||||||
pairs, err := processAccounts(accounts)
|
pairs, err := processAccounts(accounts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -40,7 +42,10 @@ func BasicAuth(accounts Accounts) HandlerFunc {
|
|||||||
user, ok := searchCredential(pairs, c.Request.Header.Get("Authorization"))
|
user, ok := searchCredential(pairs, c.Request.Header.Get("Authorization"))
|
||||||
if !ok {
|
if !ok {
|
||||||
// Credentials doesn't match, we return 401 Unauthorized and abort request.
|
// Credentials doesn't match, we return 401 Unauthorized and abort request.
|
||||||
c.Writer.Header().Set("WWW-Authenticate", "Basic realm=\"Authorization Required\"")
|
if realm == "" {
|
||||||
|
realm = "Authorization Required"
|
||||||
|
}
|
||||||
|
c.Writer.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=\"%s\"", realm))
|
||||||
c.Fail(401, errors.New("Unauthorized"))
|
c.Fail(401, errors.New("Unauthorized"))
|
||||||
} else {
|
} else {
|
||||||
// user is allowed, set UserId to key "user" in this context, the userId can be read later using
|
// user is allowed, set UserId to key "user" in this context, the userId can be read later using
|
||||||
@ -50,6 +55,12 @@ func BasicAuth(accounts Accounts) HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Implements a basic Basic HTTP Authorization. It takes as argument a map[string]string where
|
||||||
|
// the key is the user name and the value is the password.
|
||||||
|
func BasicAuth(accounts Accounts) HandlerFunc {
|
||||||
|
return BasicAuthForRealm(accounts, "")
|
||||||
|
}
|
||||||
|
|
||||||
func processAccounts(accounts Accounts) (authPairs, error) {
|
func processAccounts(accounts Accounts) (authPairs, error) {
|
||||||
if len(accounts) == 0 {
|
if len(accounts) == 0 {
|
||||||
return nil, errors.New("Empty list of authorized credentials")
|
return nil, errors.New("Empty list of authorized credentials")
|
||||||
|
24
auth_test.go
24
auth_test.go
@ -59,3 +59,27 @@ func TestBasicAuth401(t *testing.T) {
|
|||||||
t.Errorf("WWW-Authenticate header is incorrect: %s", w.HeaderMap.Get("Content-Type"))
|
t.Errorf("WWW-Authenticate header is incorrect: %s", w.HeaderMap.Get("Content-Type"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBasicAuth401WithCustomRealm(t *testing.T) {
|
||||||
|
req, _ := http.NewRequest("GET", "/login", nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
r := New()
|
||||||
|
accounts := Accounts{"foo": "bar"}
|
||||||
|
r.Use(BasicAuthForRealm(accounts, "My Custom Realm"))
|
||||||
|
|
||||||
|
r.GET("/login", func(c *Context) {
|
||||||
|
c.String(200, "autorized")
|
||||||
|
})
|
||||||
|
|
||||||
|
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:password")))
|
||||||
|
r.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
if w.Code != 401 {
|
||||||
|
t.Errorf("Response code should be Not autorized, was: %s", w.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
if w.HeaderMap.Get("WWW-Authenticate") != "Basic realm=\"My Custom Realm\"" {
|
||||||
|
t.Errorf("WWW-Authenticate header is incorrect: %s", w.HeaderMap.Get("Content-Type"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user