diff --git a/pkg/serverconfigs/http_auth_basic_method.go b/pkg/serverconfigs/http_auth_basic_method.go index be688fe..85ac996 100644 --- a/pkg/serverconfigs/http_auth_basic_method.go +++ b/pkg/serverconfigs/http_auth_basic_method.go @@ -3,9 +3,7 @@ package serverconfigs import ( - "encoding/base64" "encoding/json" - stringutil "github.com/iwind/TeaGo/utils/string" "net/http" ) @@ -13,23 +11,17 @@ import ( type HTTPAuthBasicMethodUser struct { Username string `json:"username"` Password string `json:"password"` - Encoder string `json:"encoding"` } func (this *HTTPAuthBasicMethodUser) Validate(password string) (bool, error) { - switch this.Encoder { - case "md5": - return this.Password == stringutil.Md5(password), nil - case "base64": - return this.Password == base64.StdEncoding.EncodeToString([]byte(password)), nil - default: - return this.Password == password, nil - } + return this.Password == password, nil } // HTTPAuthBasicMethod BasicAuth方法定义 type HTTPAuthBasicMethod struct { - Users []*HTTPAuthBasicMethodUser `json:"users"` + Users []*HTTPAuthBasicMethodUser `json:"users"` + Realm string `json:"realm"` + Charset string `json:"charset"` userMap map[string]*HTTPAuthBasicMethodUser // username => *User } diff --git a/pkg/serverconfigs/http_auth_basic_method_test.go b/pkg/serverconfigs/http_auth_basic_method_test.go index 5a9d8dc..a27c9cb 100644 --- a/pkg/serverconfigs/http_auth_basic_method_test.go +++ b/pkg/serverconfigs/http_auth_basic_method_test.go @@ -6,7 +6,6 @@ import ( "encoding/base64" "github.com/iwind/TeaGo/assert" "github.com/iwind/TeaGo/maps" - stringutil "github.com/iwind/TeaGo/utils/string" "net/http" "testing" ) @@ -35,30 +34,6 @@ func TestHTTPAuthBasicMethodUser_Validate(t *testing.T) { } a.IsFalse(b) } - - { - user := &HTTPAuthBasicMethodUser{ - Password: stringutil.Md5("123456"), - Encoder: "md5", - } - b, err := user.Validate("123456") - if err != nil { - t.Fatal(err) - } - a.IsTrue(b) - } - - { - user := &HTTPAuthBasicMethodUser{ - Password: base64.StdEncoding.EncodeToString([]byte("123456")), - Encoder: "base64", - } - b, err := user.Validate("123456") - if err != nil { - t.Fatal(err) - } - a.IsTrue(b) - } } func TestHTTPAuthBasicMethod_Filter(t *testing.T) { diff --git a/pkg/serverconfigs/http_auth_methods.go b/pkg/serverconfigs/http_auth_methods.go new file mode 100644 index 0000000..b27faf0 --- /dev/null +++ b/pkg/serverconfigs/http_auth_methods.go @@ -0,0 +1,31 @@ +// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. + +package serverconfigs + +type HTTPAuthType = string + +const ( + HTTPAuthTypeBasicAuth HTTPAuthType = "basicAuth" // BasicAuth + HTTPAuthTypeSubRequest HTTPAuthType = "subRequest" // 子请求 +) + +type HTTPAuthTypeDefinition struct { + Name string `json:"name"` + Code string `json:"code"` + Description string `json:"description"` +} + +func FindAllHTTPAuthTypes() []*HTTPAuthTypeDefinition { + return []*HTTPAuthTypeDefinition{ + { + Name: "基本认证", + Code: HTTPAuthTypeBasicAuth, + Description: "BasicAuth,最简单的HTTP请求认证方式,通过传递Authorization: Basic xxx Header认证。", + }, + { + Name: "子请求", + Code: HTTPAuthTypeSubRequest, + Description: "通过自定义的URL子请求来认证请求。", + }, + } +} diff --git a/pkg/serverconfigs/http_auth_policy.go b/pkg/serverconfigs/http_auth_policy.go index 8fdc9d3..bc1ca16 100644 --- a/pkg/serverconfigs/http_auth_policy.go +++ b/pkg/serverconfigs/http_auth_policy.go @@ -7,13 +7,6 @@ import ( "net/http" ) -type HTTPAuthType = string - -const ( - HTTPAuthTypeBasicAuth HTTPAuthType = "basicAuth" // BasicAuth - HTTPAuthTypeSubRequest HTTPAuthType = "subRequest" // 子请求 -) - // HTTPAuthPolicy HTTP认证策略 type HTTPAuthPolicy struct { Id int64 `json:"id"` @@ -53,3 +46,8 @@ func (this *HTTPAuthPolicy) Filter(req *http.Request, subReqFunc func(subReq *ht } return this.method.Filter(req, subReqFunc, formatter) } + +// Method 获取认证实例 +func (this *HTTPAuthPolicy) Method() HTTPAuthMethodInterface { + return this.method +} diff --git a/pkg/serverconfigs/http_auth_sub_request_method.go b/pkg/serverconfigs/http_auth_sub_request_method.go index a511a25..a612a02 100644 --- a/pkg/serverconfigs/http_auth_sub_request_method.go +++ b/pkg/serverconfigs/http_auth_sub_request_method.go @@ -65,15 +65,25 @@ func (this *HTTPAuthSubRequestMethod) Filter(req *http.Request, doSubReq func(su } var url = formatter(this.URL) + var scheme = "http" + if req.TLS != nil { + scheme = "https" + } + var host = req.URL.Host + if len(host) > 0 { + host = req.Host + } if !this.isFullURL { - url = req.URL.Scheme + "://" + req.URL.Host + url + url = scheme + "://" + host + url } newReq, err := http.NewRequest(method, url, nil) if err != nil { return false, err } for k, v := range req.Header { - newReq.Header[k] = v + if k != "Connection" { + newReq.Header[k] = v + } } if !this.isFullURL { @@ -84,6 +94,7 @@ func (this *HTTPAuthSubRequestMethod) Filter(req *http.Request, doSubReq func(su return status >= 200 && status < 300, nil } + newReq.Header.Set("Referer", req.URL.String()) resp, err := httpAuthSubRequestHTTPClient.Do(newReq) if err != nil { return false, err