Debian Codegraph @

api/v0/auth/sasl/context.go

raw symbols SCIP
1 package sasl
2
3 import (
4 "net"
5 "strconv"
6
7 "golang.org/x/net/context"
8 )
9
10 // unexported to prevent collisions with context keys defined in
11 // other packages.
12 type _key int
13
14 // If this package defined other context keys, they would have
15 // different integer values.
16 const (
17 statusKey _key = iota
18 bindingAddressKey // bind address for login-related network ops
19 bindingPortKey // port to bind auth listener if a specific port is needed
20 )
21
22 func withStatus(ctx context.Context, s statusType) context.Context {
23 return context.WithValue(ctx, statusKey, s)
24 }
25
26 func statusFrom(ctx context.Context) statusType {
27 s, ok := ctx.Value(statusKey).(statusType)
28 if !ok {
29 panic("missing status in context")
30 }
31 return s
32 }
33
34 func WithBindingAddress(ctx context.Context, address net.IP) context.Context {
35 return context.WithValue(ctx, bindingAddressKey, address)
36 }
37
38 func BindingAddressFrom(ctx context.Context) net.IP {
39 obj := ctx.Value(bindingAddressKey)
40 if addr, ok := obj.(net.IP); ok {
41 return addr
42 } else {
43 return nil
44 }
45 }
46
47 func WithBindingPort(ctx context.Context, port uint16) context.Context {
48 return context.WithValue(ctx, bindingPortKey, port)
49 }
50
51 func BindingPortFrom(ctx context.Context) string {
52 if port, ok := ctx.Value(bindingPortKey).(uint16); ok {
53 return strconv.Itoa(int(port))
54 }
55 return "0"
56 }