Debian Codegraph @

api/v0/auth/interface.go

raw symbols SCIP
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 // SPI interface: login provider implementations support this interface, clients
14 // do not authenticate against this directly, instead they should use Login()
15 type Authenticatee interface {
16 // Returns no errors if successfully authenticated, otherwise a single
17 // error.
18 Authenticate(ctx context.Context, handler callback.Handler) error
19 }
20
21 // Func adapter for interface: allow func's to implement the Authenticatee interface
22 // as long as the func signature matches
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 // Authentication was attempted and failed (likely due to incorrect credentials, too
31 // many retries within a time window, etc). Distinctly different from authentication
32 // errors (e.g. network errors, configuration errors, etc).
33 AuthenticationFailed = errors.New("authentication failed")
34
35 authenticateeProviders = make(map[string]Authenticatee) // authentication providers dict
36 providerLock sync.Mutex
37 )
38
39 // Register an authentication provider (aka "login provider"). packages that
40 // provide Authenticatee implementations should invoke this func in their
41 // init() to register.
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 // Look up an authentication provider by name, returns non-nil and true if such
56 // a provider is found.
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 }