Debian Codegraph @

api/v0/auth/login.go

raw symbols SCIP
1 package auth
2
3 import (
4 "errors"
5 "fmt"
6 "time"
7
8 "github.com/mesos/mesos-go/api/v0/auth/callback"
9 "github.com/mesos/mesos-go/api/v0/upid"
10 "golang.org/x/net/context"
11 )
12
13 var (
14 // No login provider name has been specified in a context.Context
15 NoLoginProviderName = errors.New("missing login provider name in context")
16 )
17
18 // Main client entrypoint into the authentication APIs: clients are expected to
19 // invoke this func with a context containing a login provider name value.
20 // This may be written as:
21 // providerName := ... // the user has probably configured this via some flag
22 // handler := ... // handlers provide data like usernames and passwords
23 // ctx := ... // obtain some initial or timed context
24 // err := auth.Login(auth.WithLoginProvider(ctx, providerName), handler)
25 func Login(ctx context.Context, handler callback.Handler) error {
26 name, ok := LoginProviderFrom(ctx)
27 if !ok {
28 return NoLoginProviderName
29 }
30 provider, ok := getAuthenticateeProvider(name)
31 if !ok {
32 return fmt.Errorf("unrecognized login provider name in context: %s", name)
33 }
34 return provider.Authenticate(ctx, handler)
35 }
36
37 // Unexported key type, avoids conflicts with other context-using packages. All
38 // context items registered from this package should use keys of this type.
39 type loginKeyType int
40
41 const (
42 // name of login provider to use
43 loginProviderNameKey loginKeyType = iota
44
45 // upid.UPID of some parent process
46 parentUpidKey
47
48 // time.Duration that limits the overall duration of an auth attempt
49 timeoutKey
50 )
51
52 // Return a context that inherits all values from the parent ctx and specifies
53 // the login provider name given here. Intended to be invoked before calls to
54 // Login().
55 func WithLoginProvider(ctx context.Context, providerName string) context.Context {
56 return context.WithValue(ctx, loginProviderNameKey, providerName)
57 }
58
59 // Return the name of the login provider specified in this context.
60 func LoginProviderFrom(ctx context.Context) (name string, ok bool) {
61 name, ok = ctx.Value(loginProviderNameKey).(string)
62 return
63 }
64
65 // Return the name of the login provider specified in this context, or empty
66 // string if none.
67 func LoginProvider(ctx context.Context) string {
68 name, _ := LoginProviderFrom(ctx)
69 return name
70 }
71
72 func WithParentUPID(ctx context.Context, pid upid.UPID) context.Context {
73 return context.WithValue(ctx, parentUpidKey, pid)
74 }
75
76 func ParentUPIDFrom(ctx context.Context) (pid upid.UPID, ok bool) {
77 pid, ok = ctx.Value(parentUpidKey).(upid.UPID)
78 return
79 }
80
81 func ParentUPID(ctx context.Context) (upid *upid.UPID) {
82 if upid, ok := ParentUPIDFrom(ctx); ok {
83 return &upid
84 }
85 return nil
86 }
87
88 func TimeoutFrom(ctx context.Context) (d time.Duration, ok bool) {
89 d, ok = ctx.Value(timeoutKey).(time.Duration)
90 return
91 }
92
93 func Timeout(ctx context.Context) (d time.Duration) {
94 d, _ = TimeoutFrom(ctx)
95 return
96 }
97
98 func WithTimeout(ctx context.Context, d time.Duration) context.Context {
99 return context.WithValue(ctx, timeoutKey, d)
100 }