| 1 |
package auth |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"sync" |
| 7 |
|
| 8 |
log "github.com/golang/glog" |
| 9 |
"github.com/mesos/mesos-go/api/v0/auth/callback" |
| 10 |
"golang.org/x/net/context" |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
type Authenticatee interface { |
| 16 |
|
| 17 |
|
| 18 |
Authenticate(ctx context.Context, handler callback.Handler) error |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
type AuthenticateeFunc func(ctx context.Context, handler callback.Handler) error |
| 24 |
|
| 25 |
func (f AuthenticateeFunc) Authenticate(ctx context.Context, handler callback.Handler) error { |
| 26 |
return f(ctx, handler) |
| 27 |
} |
| 28 |
|
| 29 |
var ( |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
AuthenticationFailed = errors.New("authentication failed") |
| 34 |
|
| 35 |
authenticateeProviders = make(map[string]Authenticatee) |
| 36 |
providerLock sync.Mutex |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
func RegisterAuthenticateeProvider(name string, auth Authenticatee) (err error) { |
| 43 |
providerLock.Lock() |
| 44 |
defer providerLock.Unlock() |
| 45 |
|
| 46 |
if _, found := authenticateeProviders[name]; found { |
| 47 |
err = fmt.Errorf("authentication provider already registered: %v", name) |
| 48 |
} else { |
| 49 |
authenticateeProviders[name] = auth |
| 50 |
log.V(1).Infof("registered authentication provider: %v", name) |
| 51 |
} |
| 52 |
return |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
func getAuthenticateeProvider(name string) (provider Authenticatee, ok bool) { |
| 58 |
providerLock.Lock() |
| 59 |
defer providerLock.Unlock() |
| 60 |
|
| 61 |
provider, ok = authenticateeProviders[name] |
| 62 |
return |
| 63 |
} |