-
Notifications
You must be signed in to change notification settings - Fork 932
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf198d0
commit 7a0101d
Showing
9 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ services: | |
- name: "GetUser" | ||
retries: "1" | ||
loadbalance: "random" | ||
token: "true" | ||
|
||
protocols: | ||
"dubbo": | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
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 impl | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
import ( | ||
perrors "github.com/pkg/errors" | ||
) | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/common/constant" | ||
"github.com/apache/dubbo-go/common/extension" | ||
"github.com/apache/dubbo-go/filter" | ||
"github.com/apache/dubbo-go/protocol" | ||
) | ||
|
||
const ( | ||
token = "token" | ||
) | ||
|
||
func init() { | ||
extension.SetFilter(token, GetTokenFilter) | ||
} | ||
|
||
type TokenFilter struct{} | ||
|
||
func (tf *TokenFilter) Invoke(invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { | ||
invokerTkn := invoker.GetUrl().GetParam(constant.TOKEN_KEY, "") | ||
if len(invokerTkn) > 0 { | ||
attachs := invocation.Attachments() | ||
if len(attachs) > 0 { | ||
remoteTkn, exist := attachs[constant.TOKEN_KEY] | ||
if exist && strings.EqualFold(invokerTkn, remoteTkn) { | ||
return invoker.Invoke(invocation) | ||
} | ||
} | ||
return &protocol.RPCResult{Err: perrors.Errorf("Invalid token! Forbid invoke remote service %s method %s ", | ||
invoker, invocation.MethodName())} | ||
} | ||
|
||
return invoker.Invoke(invocation) | ||
} | ||
|
||
func (tf *TokenFilter) OnResponse(result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { | ||
return result | ||
} | ||
|
||
func GetTokenFilter() filter.Filter { | ||
return &TokenFilter{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
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 impl | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/common" | ||
"github.com/apache/dubbo-go/common/constant" | ||
"github.com/apache/dubbo-go/protocol" | ||
"github.com/apache/dubbo-go/protocol/invocation" | ||
) | ||
|
||
func TestTokenFilter_Invoke(t *testing.T) { | ||
filter := GetTokenFilter() | ||
|
||
url := common.NewURLWithOptions( | ||
common.WithParams(url.Values{}), | ||
common.WithParamsValue(constant.TOKEN_KEY, "ori_key")) | ||
attch := make(map[string]string, 0) | ||
attch[constant.TOKEN_KEY] = "ori_key" | ||
result := filter.Invoke(protocol.NewBaseInvoker(*url), | ||
invocation.NewRPCInvocation("MethodName", []interface{}{"OK"}, attch)) | ||
assert.Nil(t, result.Error()) | ||
assert.Nil(t, result.Result()) | ||
} | ||
|
||
func TestTokenFilter_InvokeEmptyToken(t *testing.T) { | ||
filter := GetTokenFilter() | ||
|
||
url := common.URL{} | ||
attch := make(map[string]string, 0) | ||
attch[constant.TOKEN_KEY] = "ori_key" | ||
result := filter.Invoke(protocol.NewBaseInvoker(url), | ||
invocation.NewRPCInvocation("MethodName", []interface{}{"OK"}, attch)) | ||
assert.Nil(t, result.Error()) | ||
assert.Nil(t, result.Result()) | ||
} | ||
|
||
func TestTokenFilter_InvokeEmptyAttach(t *testing.T) { | ||
filter := GetTokenFilter() | ||
|
||
url := common.NewURLWithOptions( | ||
common.WithParams(url.Values{}), | ||
common.WithParamsValue(constant.TOKEN_KEY, "ori_key")) | ||
attch := make(map[string]string, 0) | ||
result := filter.Invoke(protocol.NewBaseInvoker(*url), | ||
invocation.NewRPCInvocation("MethodName", []interface{}{"OK"}, attch)) | ||
assert.NotNil(t, result.Error()) | ||
} | ||
|
||
func TestTokenFilter_InvokeNotEqual(t *testing.T) { | ||
filter := GetTokenFilter() | ||
|
||
url := common.NewURLWithOptions( | ||
common.WithParams(url.Values{}), | ||
common.WithParamsValue(constant.TOKEN_KEY, "ori_key")) | ||
attch := make(map[string]string, 0) | ||
attch[constant.TOKEN_KEY] = "err_key" | ||
result := filter.Invoke(protocol.NewBaseInvoker(*url), | ||
invocation.NewRPCInvocation("MethodName", []interface{}{"OK"}, attch)) | ||
assert.NotNil(t, result.Error()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters