-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
161 lines (129 loc) · 3.39 KB
/
jwt.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
/*
Copyright 2019 Dmitry Kolesnikov, All Rights Reserved
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gouldian
import (
"strings"
"github.com/fogfish/gouldian/v2/internal/optics"
)
/*
JWTClaim is function type to extract claims from token
*/
type JWTClaim func(Token) string
/*
JWT combinator defines primitives to match JWT token in the HTTP requests.
endpoint := µ.GET(
µ.JWT(µ.Token.Username, "joedoe"),
)
endpoint(
mock.Input(
mock.JWT(µ.Token{"username": "joedoe"})
)
) == nil
*/
func JWT[T Pattern](claim JWTClaim, val T) Endpoint {
switch v := any(val).(type) {
case string:
return jwtClaim(claim).Is(v)
case Lens:
return jwtClaim(claim).To(v)
default:
panic("type system failure")
}
}
type jwtClaim JWTClaim
/*
Is matches a key of JWT to defined literal value
*/
func (claim jwtClaim) Is(val string) Endpoint {
return func(ctx *Context) error {
if ctx.JWT == nil {
return ErrNoMatch
}
if claim(ctx.JWT) != val {
return ErrNoMatch
}
return nil
}
}
/*
To matches key of JWT value to the request context. It uses lens abstraction to
decode value into Golang type. The Endpoint causes no-match if param
value cannot be decoded to the target type. See optics.Lens type for details.
*/
func (claim jwtClaim) To(lens optics.Lens) Endpoint {
return func(ctx *Context) error {
if ctx.JWT == nil {
return ErrNoMatch
}
if val := claim(ctx.JWT); val != "" {
return ctx.Put(lens, val)
}
return ErrNoMatch
}
}
/*
JWTMaybe matches key of JWT to the request context. It uses lens abstraction to
decode value into Golang type. The Endpoint does not cause no-match
if header value cannot be decoded to the target type. See optics.Lens type for details.
type MyT struct{ Username string }
username := µ.Optics1[MyT, string]()
e := µ.GET( µ.JWTMaybe(µ.JWT.Sub).Maybe(username) )
e(mock.Input(mock.JWT(µ.JWT{"username": "joedoe"}))) == nil
*/
func JWTMaybe(claim JWTClaim, lens optics.Lens) Endpoint {
return func(ctx *Context) error {
if ctx.JWT == nil {
return ErrNoMatch
}
if val := claim(ctx.JWT); val != "" {
ctx.Put(lens, val)
}
return nil
}
}
/*
JWTOneOf matches a key of JWT if it contains one of the tokens
µ.GET( µ.JWTOneOf(µ.JWT.Scope, "ro", "rw") )
*/
func JWTOneOf(claim JWTClaim, vals ...string) Endpoint {
return func(ctx *Context) error {
if ctx.JWT == nil {
return ErrNoMatch
}
val := claim(ctx.JWT)
for _, x := range vals {
if strings.Contains(val, x) {
return nil
}
}
return ErrNoMatch
}
}
/*
JWTAllOf matches a key of JWT if it contains one of the tokens
µ.GET( µ.JWTAllOf(µ.JWT.Scope, "ro", "rw") )
*/
func JWTAllOf(claim JWTClaim, vals ...string) Endpoint {
return func(ctx *Context) error {
if ctx.JWT == nil {
return ErrNoMatch
}
val := claim(ctx.JWT)
for _, x := range vals {
if !strings.Contains(val, x) {
return ErrNoMatch
}
}
return nil
}
}