You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/11/28 02:12:23 UTC

[GitHub] [dubbo-go-pixiu] ztelur commented on a change in pull request #303: feat:auth filter

ztelur commented on a change in pull request #303:
URL: https://github.com/apache/dubbo-go-pixiu/pull/303#discussion_r757832311



##########
File path: pkg/filter/auth/jwt/jwt.go
##########
@@ -0,0 +1,222 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 jwt
+
+import (
+	"encoding/json"
+	"fmt"
+	stdHttp "net/http"
+	"strings"
+	"time"
+)
+
+import (
+	"github.com/MicahParks/keyfunc"
+	jwt4 "github.com/golang-jwt/jwt/v4"
+)
+
+import (
+	"github.com/apache/dubbo-go-pixiu/pkg/common/constant"
+	"github.com/apache/dubbo-go-pixiu/pkg/common/extension/filter"
+	"github.com/apache/dubbo-go-pixiu/pkg/context/http"
+	"github.com/apache/dubbo-go-pixiu/pkg/logger"
+)
+
+const (
+	Kind = constant.HTTPAuthJwtFilter
+)
+
+func init() {
+	filter.RegisterHttpFilter(&Plugin{})
+}
+
+type (
+	// Plugin is http filter plugin.
+	Plugin struct {
+	}
+
+	// Filter is http filter instance
+	Filter struct {
+		cfg          *Config
+		errMsg       []byte
+		providerJwks map[string]Provider
+	}
+
+	// Config describe the config of Filter
+	Config struct {
+		ErrMsg    string      `yaml:"err_msg" json:"err_msg" mapstructure:"err_msg"`
+		Rules     []Rules     `yaml:"rules" json:"rules" mapstructure:"rules"`
+		Providers []Providers `yaml:"providers" json:"providers" mapstructure:"providers"`
+	}
+)
+
+func (p Plugin) Kind() string {
+	return Kind
+}
+
+func (p *Plugin) CreateFilter() (filter.HttpFilter, error) {
+	return &Filter{cfg: &Config{}, providerJwks: map[string]Provider{}}, nil
+}
+
+func (f *Filter) PrepareFilterChain(ctx *http.HttpContext) error {
+	ctx.AppendFilterFunc(f.Handle)
+	return nil
+}
+
+func (f *Filter) Handle(ctx *http.HttpContext) {
+
+	path := ctx.Request.RequestURI
+
+	router := false
+
+	for _, rule := range f.cfg.Rules {
+		if strings.HasPrefix(path, rule.Match.Prefix) {
+			router = true
+			if f.validAny(rule, ctx) || f.validAll(rule, ctx) {
+				ctx.Next()
+				break
+			}
+		}
+	}
+
+	if router {

Review comment:
       为什么router为true就返回401,不是还需要判断validAny和validAll嘛

##########
File path: pkg/filter/auth/jwt/jwt.go
##########
@@ -0,0 +1,222 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 jwt
+
+import (
+	"encoding/json"
+	"fmt"
+	stdHttp "net/http"
+	"strings"
+	"time"
+)
+
+import (
+	"github.com/MicahParks/keyfunc"
+	jwt4 "github.com/golang-jwt/jwt/v4"
+)
+
+import (
+	"github.com/apache/dubbo-go-pixiu/pkg/common/constant"
+	"github.com/apache/dubbo-go-pixiu/pkg/common/extension/filter"
+	"github.com/apache/dubbo-go-pixiu/pkg/context/http"
+	"github.com/apache/dubbo-go-pixiu/pkg/logger"
+)
+
+const (
+	Kind = constant.HTTPAuthJwtFilter
+)
+
+func init() {
+	filter.RegisterHttpFilter(&Plugin{})
+}
+
+type (
+	// Plugin is http filter plugin.
+	Plugin struct {
+	}
+
+	// Filter is http filter instance
+	Filter struct {
+		cfg          *Config
+		errMsg       []byte
+		providerJwks map[string]Provider
+	}
+
+	// Config describe the config of Filter
+	Config struct {
+		ErrMsg    string      `yaml:"err_msg" json:"err_msg" mapstructure:"err_msg"`
+		Rules     []Rules     `yaml:"rules" json:"rules" mapstructure:"rules"`
+		Providers []Providers `yaml:"providers" json:"providers" mapstructure:"providers"`
+	}
+)
+
+func (p Plugin) Kind() string {
+	return Kind
+}
+
+func (p *Plugin) CreateFilter() (filter.HttpFilter, error) {
+	return &Filter{cfg: &Config{}, providerJwks: map[string]Provider{}}, nil
+}
+
+func (f *Filter) PrepareFilterChain(ctx *http.HttpContext) error {
+	ctx.AppendFilterFunc(f.Handle)
+	return nil
+}
+
+func (f *Filter) Handle(ctx *http.HttpContext) {
+
+	path := ctx.Request.RequestURI
+
+	router := false
+
+	for _, rule := range f.cfg.Rules {
+		if strings.HasPrefix(path, rule.Match.Prefix) {
+			router = true
+			if f.validAny(rule, ctx) || f.validAll(rule, ctx) {
+				ctx.Next()
+				break
+			}
+		}
+	}
+
+	if router {
+		ctx.WriteJSONWithStatus(stdHttp.StatusUnauthorized, f.errMsg)
+		ctx.Abort()
+		return
+	}
+
+	ctx.Next()
+
+}
+
+func valuePrefix(value, prefix string) string {
+	if prefix == "" {
+		return value
+	}
+	return strings.TrimPrefix(value, prefix)
+}
+
+func (f *Filter) validAny(rule Rules, ctx *http.HttpContext) bool {
+
+	providerName := rule.Requires.RequiresAny.ProviderName
+
+	if provider, ok := f.providerJwks[providerName]; ok {
+		ctx.Request.Header.Set(provider.forwardPayloadHeader, provider.issuer)
+		if key := ctx.Request.Header.Get(provider.headers.Name); key != "" {
+			token, err := jwt4.Parse(valuePrefix(key, provider.headers.ValuePrefix), provider.jwk.Keyfunc)
+			if err != nil {
+				logger.Warnf("failed to parse JWKs from JSON. provider:%s Error: %s", providerName, err.Error())
+				return false
+			}
+			return token.Valid
+		}
+	}
+
+	return false

Review comment:
       validAny和 validAll 函数的语义辛苦加一下注释




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org