@@ -62,11 +62,12 @@ type JwksConfig struct {
62
62
63
63
// Config holds the plugin configuration.
64
64
type Config struct {
65
- Paths []string `json:"paths,omitempty" toml:"paths,omitempty" yaml:"paths,omitempty"`
66
- HeaderKey string `json:"headerKey,omitempty" toml:"headerKey,omitempty" yaml:"headerKey,omitempty"`
67
- SecureKey string `json:"secureKey,omitempty" toml:"secureKey,omitempty" yaml:"secureKey,omitempty"`
68
- Jwks JwksConfig `json:"jwks,omitempty" toml:"jwks,omitempty" yaml:"jwks,omitempty"`
69
- key * KeyHS256
65
+ Paths []string `json:"paths,omitempty" toml:"paths,omitempty" yaml:"paths,omitempty"`
66
+ HeaderKey string `json:"headerKey,omitempty" toml:"headerKey,omitempty" yaml:"headerKey,omitempty"`
67
+ SecureKey string `json:"secureKey,omitempty" toml:"secureKey,omitempty" yaml:"secureKey,omitempty"`
68
+ Jwks JwksConfig `json:"jwks,omitempty" toml:"jwks,omitempty" yaml:"jwks,omitempty"`
69
+ key * KeyHS256
70
+ AllowedSubDomainOfOrigins []string `json:"allowedSubDomainOfOrigins,omitempty" toml:"allowedSubDomainOfOrigins,omitempty" yaml:"allowedSubDomainOfOrigins,omitempty"`
70
71
}
71
72
72
73
// CreateConfig creates and initializes the plugin configuration.
@@ -75,11 +76,12 @@ func CreateConfig() *Config {
75
76
}
76
77
77
78
type JwtAntPath struct {
78
- name string
79
- next http.Handler
80
- pathParses []PathParse
81
- headerKey string
82
- key * KeyHS256
79
+ name string
80
+ next http.Handler
81
+ pathParses []PathParse
82
+ headerKey string
83
+ key * KeyHS256
84
+ allowedSubDomainOfOrigins []string
83
85
}
84
86
85
87
// New creates and returns a plugin instance.
@@ -126,11 +128,12 @@ func New(_ context.Context, next http.Handler, config *Config, name string) (htt
126
128
schedule (config )
127
129
128
130
return & JwtAntPath {
129
- name : name ,
130
- next : next ,
131
- pathParses : pathParses ,
132
- headerKey : config .HeaderKey ,
133
- key : & key ,
131
+ name : name ,
132
+ next : next ,
133
+ pathParses : pathParses ,
134
+ headerKey : config .HeaderKey ,
135
+ key : & key ,
136
+ allowedSubDomainOfOrigins : config .AllowedSubDomainOfOrigins ,
134
137
}, nil
135
138
}
136
139
@@ -185,11 +188,29 @@ func (ja *JwtAntPath) filter2StarSuffix(currentPath string, parse PathParse) boo
185
188
return false
186
189
}
187
190
191
+ func allowOrigin (rw http.ResponseWriter , req * http.Request , allowedSubDomainOfOrigins []string ) {
192
+ if allowedSubDomainOfOrigins == nil || len (allowedSubDomainOfOrigins ) == 0 {
193
+ return
194
+ }
195
+ for _ , v := range allowedSubDomainOfOrigins {
196
+ if strings .HasSuffix (req .Host , v ) {
197
+ origin := req .Header .Get ("Origin" )
198
+ rw .Header ().Add ("Access-Control-Allow-Origin" , origin )
199
+ break
200
+ }
201
+ }
202
+ }
203
+
204
+ func nextServer (rw http.ResponseWriter , req * http.Request , ja * JwtAntPath ) {
205
+ allowOrigin (rw , req , ja .allowedSubDomainOfOrigins )
206
+ ja .next .ServeHTTP (rw , req )
207
+ }
208
+
188
209
func (ja * JwtAntPath ) ServeHTTP (rw http.ResponseWriter , req * http.Request ) {
189
210
currentPath := req .URL .EscapedPath ()
190
211
191
212
if currentPath == "/" {
192
- ja . next . ServeHTTP (rw , req )
213
+ nextServer (rw , req , ja )
193
214
return
194
215
}
195
216
@@ -205,24 +226,24 @@ func (ja *JwtAntPath) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
205
226
}
206
227
207
228
if currentPath == parse .path {
208
- ja . next . ServeHTTP (rw , req )
229
+ nextServer (rw , req , ja )
209
230
return
210
231
}
211
232
212
233
if ja .filter2StarSuffix (currentPath , parse ) {
213
- ja . next . ServeHTTP (rw , req )
234
+ nextServer (rw , req , ja )
214
235
return
215
236
}
216
237
217
238
if ja .filter1Star (currentPath , parse ) {
218
- ja . next . ServeHTTP (rw , req )
239
+ nextServer (rw , req , ja )
219
240
return
220
241
}
221
242
222
243
}
223
244
224
245
if ja .verifyJwt (rw , req ) {
225
- ja . next . ServeHTTP (rw , req )
246
+ nextServer (rw , req , ja )
226
247
}
227
248
}
228
249
0 commit comments