| 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 |
|
| 15 |
NoLoginProviderName = errors.New("missing login provider name in context") |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|
| 38 |
|
| 39 |
type loginKeyType int |
| 40 |
|
| 41 |
const ( |
| 42 |
|
| 43 |
loginProviderNameKey loginKeyType = iota |
| 44 |
|
| 45 |
|
| 46 |
parentUpidKey |
| 47 |
|
| 48 |
|
| 49 |
timeoutKey |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
func WithLoginProvider(ctx context.Context, providerName string) context.Context { |
| 56 |
return context.WithValue(ctx, loginProviderNameKey, providerName) |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
func LoginProviderFrom(ctx context.Context) (name string, ok bool) { |
| 61 |
name, ok = ctx.Value(loginProviderNameKey).(string) |
| 62 |
return |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 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 |
} |