-
Notifications
You must be signed in to change notification settings - Fork 4
/
validator.go
233 lines (201 loc) · 7.54 KB
/
validator.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package bascule
import (
"context"
"reflect"
)
// Validator represents a general strategy for validating tokens. Token validation
// typically happens during authentication.
type Validator[S any] interface {
// Validate validates a token. If this validator needs to interact
// with external systems, the supplied context can be passed to honor
// cancelation semantics. Additionally, the source object from which the
// token was taken is made available.
//
// This method may be passed a token that it doesn't support, e.g. a Basic
// validator can be passed a JWT token. In that case, this method should
// simply return a nil error.
//
// If this method returns a nil token, then the supplied token should be used
// as is. If this method returns a non-nil token, that new new token should be
// used instead. This allows a validator to augment a token with additional
// data, possibly from an external system or database.
Validate(ctx context.Context, source S, t Token) (Token, error)
}
// Validate applies several validators to the given token. Although each individual
// validator may return a nil Token to indicate that there is no change in the token,
// this function will always return a non-nil Token.
//
// This function returns the validated Token and a nil error to indicate success.
// If any validator fails, this function halts further validation and returns
// the error.
func Validate[S any](ctx context.Context, source S, original Token, v ...Validator[S]) (validated Token, err error) {
next := original
for i, prev := 0, next; err == nil && i < len(v); i, prev = i+1, next {
next, err = v[i].Validate(ctx, source, prev)
if next == nil {
// no change in the token
next = prev
}
}
if err == nil {
validated = next
}
return
}
// Validators is an aggregate Validator that returns validity if and only if
// all of its contained validators return validity.
type Validators[S any] []Validator[S]
// Append tacks on more validators to this aggregate, returning the possibly new
// instance. The semantics of this method are the same as the built-in append.
func (vs Validators[S]) Append(more ...Validator[S]) Validators[S] {
return append(vs, more...)
}
// Validate executes each contained validator in order, returning validity only
// if all validators pass. Any validation failure prevents subsequent validators
// from running.
func (vs Validators[S]) Validate(ctx context.Context, source S, t Token) (Token, error) {
return Validate(ctx, source, t, vs...)
}
// ValidatorFunc defines the closure signatures that are allowed as Validator instances.
type ValidatorFunc[S any] interface {
~func(Token) error |
~func(S, Token) error |
~func(Token) (Token, error) |
~func(S, Token) (Token, error) |
~func(context.Context, Token) error |
~func(context.Context, S, Token) error |
~func(context.Context, Token) (Token, error) |
~func(context.Context, S, Token) (Token, error)
}
// validatorFunc is an internal type that implements Validator. Used to normalize
// and uncurry a closure.
type validatorFunc[S any] func(context.Context, S, Token) (Token, error)
func (vf validatorFunc[S]) Validate(ctx context.Context, source S, t Token) (next Token, err error) {
next, err = vf(ctx, source, t)
if next == nil {
next = t
}
return
}
var (
validatorTokenReturnError = reflect.TypeOf((func(Token) error)(nil))
validatorTokenReturnTokenAndError = reflect.TypeOf((func(Token) (Token, error))(nil))
validatorContextTokenReturnError = reflect.TypeOf((func(context.Context, Token) error)(nil))
validatorContextTokenReturnTokenError = reflect.TypeOf((func(context.Context, Token) (Token, error))(nil))
)
// asValidatorSimple tries simple conversions on f. This function will not catch
// user-defined types.
func asValidatorSimple[S any, F ValidatorFunc[S]](f F) (v Validator[S]) {
switch vf := any(f).(type) {
case func(Token) error:
v = validatorFunc[S](
func(ctx context.Context, source S, t Token) (Token, error) {
return t, vf(t)
},
)
case func(S, Token) error:
v = validatorFunc[S](
func(ctx context.Context, source S, t Token) (Token, error) {
return t, vf(source, t)
},
)
case func(Token) (Token, error):
v = validatorFunc[S](
func(ctx context.Context, source S, t Token) (next Token, err error) {
next, err = vf(t)
if next == nil {
next = t
}
return
},
)
case func(S, Token) (Token, error):
v = validatorFunc[S](
func(ctx context.Context, source S, t Token) (next Token, err error) {
next, err = vf(source, t)
if next == nil {
next = t
}
return
},
)
case func(context.Context, Token) error:
v = validatorFunc[S](
func(ctx context.Context, source S, t Token) (Token, error) {
return t, vf(ctx, t)
},
)
case func(context.Context, S, Token) error:
v = validatorFunc[S](
func(ctx context.Context, source S, t Token) (Token, error) {
return t, vf(ctx, source, t)
},
)
case func(context.Context, Token) (Token, error):
v = validatorFunc[S](
func(ctx context.Context, source S, t Token) (next Token, err error) {
next, err = vf(ctx, t)
if next == nil {
next = t
}
return
},
)
case func(context.Context, S, Token) (Token, error):
v = validatorFunc[S](vf)
}
return
}
// AsValidator takes a ValidatorFunc closure and returns a Validator instance that
// executes that closure. This function can also convert custom types which can
// be converted to any of the closure signatures.
func AsValidator[S any, F ValidatorFunc[S]](f F) Validator[S] {
// first, try the simple way:
if v := asValidatorSimple[S](f); v != nil {
return v
}
// next, support user-defined types that are closures that do not
// require the source type.
fVal := reflect.ValueOf(f)
switch {
case fVal.CanConvert(validatorTokenReturnError):
return asValidatorSimple[S](
fVal.Convert(validatorTokenReturnError).Interface().(func(Token) error),
)
case fVal.CanConvert(validatorTokenReturnTokenAndError):
return asValidatorSimple[S](
fVal.Convert(validatorTokenReturnTokenAndError).Interface().(func(Token) (Token, error)),
)
case fVal.CanConvert(validatorContextTokenReturnError):
return asValidatorSimple[S](
fVal.Convert(validatorContextTokenReturnError).Interface().(func(context.Context, Token) error),
)
case fVal.CanConvert(validatorContextTokenReturnTokenError):
return asValidatorSimple[S](
fVal.Convert(validatorContextTokenReturnTokenError).Interface().(func(context.Context, Token) (Token, error)),
)
}
// finally: user-defined types that are closures involving the source type S.
// we have to look these up here, due to the way generics in golang work.
if ft := reflect.TypeOf((func(S, Token) error)(nil)); fVal.CanConvert(ft) {
return asValidatorSimple[S](
fVal.Convert(ft).Interface().(func(S, Token) error),
)
} else if ft := reflect.TypeOf((func(S, Token) (Token, error))(nil)); fVal.CanConvert(ft) {
return asValidatorSimple[S](
fVal.Convert(ft).Interface().(func(S, Token) (Token, error)),
)
} else if ft := reflect.TypeOf((func(context.Context, S, Token) error)(nil)); fVal.CanConvert(ft) {
return asValidatorSimple[S](
fVal.Convert(ft).Interface().(func(context.Context, S, Token) error),
)
} else {
// we know this can be converted to this final type
ft := reflect.TypeOf((func(context.Context, S, Token) (Token, error))(nil))
return asValidatorSimple[S](
fVal.Convert(ft).Interface().(func(context.Context, S, Token) (Token, error)),
)
}
}