forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
union.go
42 lines (34 loc) · 1.29 KB
/
union.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package unionrequest
import (
"net/http"
kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
authapi "github.com/openshift/origin/pkg/auth/api"
"github.com/openshift/origin/pkg/auth/authenticator"
)
// TODO remove this in favor of kubernetes types
type Authenticator struct {
Handlers []authenticator.Request
FailOnError bool
}
// NewUnionAuthentication returns a request authenticator that validates credentials using a chain of authenticator.Request objects
func NewUnionAuthentication(authRequestHandlers ...authenticator.Request) authenticator.Request {
return &Authenticator{Handlers: authRequestHandlers}
}
// AuthenticateRequest authenticates the request using a chain of authenticator.Request objects. The first
// success returns that identity. Errors are only returned if no matches are found.
func (authHandler *Authenticator) AuthenticateRequest(req *http.Request) (authapi.UserInfo, bool, error) {
errors := []error{}
for _, currAuthRequestHandler := range authHandler.Handlers {
info, ok, err := currAuthRequestHandler.AuthenticateRequest(req)
if err == nil && ok {
return info, ok, err
}
if err != nil {
if authHandler.FailOnError {
return nil, false, err
}
errors = append(errors, err)
}
}
return nil, false, kerrors.NewAggregate(errors)
}