Skip to content

Commit 76ca5ef

Browse files
committed
subDomain
1 parent 79ce44c commit 76ca5ef

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

jwtantpath.go

+41-20
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ type JwksConfig struct {
6262

6363
// Config holds the plugin configuration.
6464
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"`
7071
}
7172

7273
// CreateConfig creates and initializes the plugin configuration.
@@ -75,11 +76,12 @@ func CreateConfig() *Config {
7576
}
7677

7778
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
8385
}
8486

8587
// New creates and returns a plugin instance.
@@ -126,11 +128,12 @@ func New(_ context.Context, next http.Handler, config *Config, name string) (htt
126128
schedule(config)
127129

128130
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,
134137
}, nil
135138
}
136139

@@ -185,11 +188,29 @@ func (ja *JwtAntPath) filter2StarSuffix(currentPath string, parse PathParse) boo
185188
return false
186189
}
187190

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+
188209
func (ja *JwtAntPath) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
189210
currentPath := req.URL.EscapedPath()
190211

191212
if currentPath == "/" {
192-
ja.next.ServeHTTP(rw, req)
213+
nextServer(rw, req, ja)
193214
return
194215
}
195216

@@ -205,24 +226,24 @@ func (ja *JwtAntPath) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
205226
}
206227

207228
if currentPath == parse.path {
208-
ja.next.ServeHTTP(rw, req)
229+
nextServer(rw, req, ja)
209230
return
210231
}
211232

212233
if ja.filter2StarSuffix(currentPath, parse) {
213-
ja.next.ServeHTTP(rw, req)
234+
nextServer(rw, req, ja)
214235
return
215236
}
216237

217238
if ja.filter1Star(currentPath, parse) {
218-
ja.next.ServeHTTP(rw, req)
239+
nextServer(rw, req, ja)
219240
return
220241
}
221242

222243
}
223244

224245
if ja.verifyJwt(rw, req) {
225-
ja.next.ServeHTTP(rw, req)
246+
nextServer(rw, req, ja)
226247
}
227248
}
228249

0 commit comments

Comments
 (0)