-
Notifications
You must be signed in to change notification settings - Fork 53
/
handlers.go
98 lines (81 loc) · 2.24 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package challenges
import (
"context"
"github.com/rancher/opni/pkg/util/streams"
)
type ChallengeHandler interface {
DoChallenge(stream streams.Stream) (context.Context, error)
InterceptContext(ctx context.Context) context.Context
}
type conditional struct {
condition func(streamContext context.Context) (bool, error)
ifTrue ChallengeHandler
ifFalse ChallengeHandler
}
type conditionalThen interface {
Then(challenge ChallengeHandler) conditionalElse
}
type conditionalElse interface {
ChallengeHandler
Else(challenge ChallengeHandler) ChallengeHandler
}
func (c *conditional) DoChallenge(cs streams.Stream) (context.Context, error) {
if v, err := c.condition(cs.Context()); err != nil {
return nil, err
} else if v && c.ifTrue != nil {
return c.ifTrue.DoChallenge(cs)
} else if c.ifFalse != nil {
return c.ifFalse.DoChallenge(cs)
}
return cs.Context(), nil
}
func (c *conditional) InterceptContext(ctx context.Context) context.Context {
if v, err := c.condition(ctx); err != nil {
return ctx
} else if v && c.ifTrue != nil {
return c.ifTrue.InterceptContext(ctx)
} else if c.ifFalse != nil {
return c.ifFalse.InterceptContext(ctx)
}
return ctx
}
type ConditionFunc = func(streamContext context.Context) (bool, error)
func If(condition ConditionFunc) conditionalThen {
return &conditional{
condition: condition,
}
}
func (c *conditional) Then(challenge ChallengeHandler) conditionalElse {
c.ifTrue = challenge
return c
}
func (c *conditional) Else(challenge ChallengeHandler) ChallengeHandler {
c.ifFalse = challenge
return c
}
type chained struct {
challenges []ChallengeHandler
}
func Chained(challenges ...ChallengeHandler) ChallengeHandler {
return &chained{
challenges: challenges,
}
}
func (c *chained) DoChallenge(cs streams.Stream) (context.Context, error) {
s := streams.NewStreamWithContext(cs.Context(), cs)
for _, challenge := range c.challenges {
out, err := challenge.DoChallenge(s)
if err != nil {
return nil, err
}
s.SetContext(out)
}
return s.Context(), nil
}
func (c *chained) InterceptContext(ctx context.Context) context.Context {
for _, challenge := range c.challenges {
ctx = challenge.InterceptContext(ctx)
}
return ctx
}
type HandlerFunc func(streams.Stream) (context.Context, error)