mirror of
https://github.com/go-kit/kit.git
synced 2025-07-15 01:04:44 +02:00
improve error handling and style
This commit is contained in:
@ -1,4 +1,8 @@
|
|||||||
`package auth/basic` provides a Basic Authentication middleware [Mozilla article](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication).
|
This package provides a Basic Authentication middleware.
|
||||||
|
|
||||||
|
It'll try to compare credentials from Authentication request header to a username/password pair in middleware constructor.
|
||||||
|
|
||||||
|
More details about this type of authentication can be found in [Mozilla article](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication).
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
@ -6,7 +10,7 @@
|
|||||||
import httptransport "github.com/go-kit/kit/transport/http"
|
import httptransport "github.com/go-kit/kit/transport/http"
|
||||||
|
|
||||||
httptransport.NewServer(
|
httptransport.NewServer(
|
||||||
endpoint.Chain(AuthMiddleware(cfg.auth.user, cfg.auth.password, "Example Realm"))(makeUppercaseEndpoint()),
|
AuthMiddleware(cfg.auth.user, cfg.auth.password, "Example Realm")(makeUppercaseEndpoint()),
|
||||||
decodeMappingsRequest,
|
decodeMappingsRequest,
|
||||||
httptransport.EncodeJSONResponse,
|
httptransport.EncodeJSONResponse,
|
||||||
httptransport.ServerBefore(httptransport.PopulateRequestContext),
|
httptransport.ServerBefore(httptransport.PopulateRequestContext),
|
||||||
|
@ -34,7 +34,8 @@ func (e AuthError) Headers() http.Header {
|
|||||||
return http.Header{
|
return http.Header{
|
||||||
"Content-Type": []string{"text/plain; charset=utf-8"},
|
"Content-Type": []string{"text/plain; charset=utf-8"},
|
||||||
"X-Content-Type-Options": []string{"nosniff"},
|
"X-Content-Type-Options": []string{"nosniff"},
|
||||||
"WWW-Authenticate": []string{fmt.Sprintf(`Basic realm=%q`, e.Realm)}}
|
"WWW-Authenticate": []string{fmt.Sprintf(`Basic realm=%q`, e.Realm)},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseBasicAuth parses an HTTP Basic Authentication string.
|
// parseBasicAuth parses an HTTP Basic Authentication string.
|
||||||
@ -69,7 +70,11 @@ func AuthMiddleware(requiredUser, requiredPassword, realm string) endpoint.Middl
|
|||||||
|
|
||||||
return func(next endpoint.Endpoint) endpoint.Endpoint {
|
return func(next endpoint.Endpoint) endpoint.Endpoint {
|
||||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||||
auth := ctx.Value(httptransport.ContextKeyRequestAuthorization).(string)
|
auth, ok := ctx.Value(httptransport.ContextKeyRequestAuthorization).(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, AuthError{realm}
|
||||||
|
}
|
||||||
|
|
||||||
givenUser, givenPassword, ok := parseBasicAuth(auth)
|
givenUser, givenPassword, ok := parseBasicAuth(auth)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, AuthError{realm}
|
return nil, AuthError{realm}
|
||||||
|
@ -20,9 +20,11 @@ func TestWithBasicAuth(t *testing.T) {
|
|||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
authHeader string
|
authHeader interface{}
|
||||||
want want
|
want want
|
||||||
}{
|
}{
|
||||||
|
{"Isn't valid with nil header", nil, want{nil, AuthError{realm}}},
|
||||||
|
{"Isn't valid with non-string header", 42, want{nil, AuthError{realm}}},
|
||||||
{"Isn't valid without authHeader", "", want{nil, AuthError{realm}}},
|
{"Isn't valid without authHeader", "", want{nil, AuthError{realm}}},
|
||||||
{"Isn't valid for wrong user", makeAuthString("wrong-user", requiredPassword), want{nil, AuthError{realm}}},
|
{"Isn't valid for wrong user", makeAuthString("wrong-user", requiredPassword), want{nil, AuthError{realm}}},
|
||||||
{"Isn't valid for wrong password", makeAuthString(requiredUser, "wrong-password"), want{nil, AuthError{realm}}},
|
{"Isn't valid for wrong password", makeAuthString(requiredUser, "wrong-password"), want{nil, AuthError{realm}}},
|
||||||
|
Reference in New Issue
Block a user