-
Notifications
You must be signed in to change notification settings - Fork 187
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
Showing
9 changed files
with
400 additions
and
1 deletion.
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
58 changes: 58 additions & 0 deletions
58
drivers/plugins/extra-params_v2/dynamic-params/hmac-sha256/executor.go
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,58 @@ | ||
package hmac_sha256 | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"errors" | ||
"strings" | ||
|
||
"github.com/eolinker/eosc/log" | ||
|
||
dynamic_params "github.com/eolinker/apinto/drivers/plugins/extra-params_v2/dynamic-params" | ||
|
||
http_service "github.com/eolinker/eosc/eocontext/http-context" | ||
) | ||
|
||
type executor struct { | ||
*dynamic_params.Param | ||
secretKey string | ||
} | ||
|
||
func NewExecutor(name string, value []string) *executor { | ||
secretKey := "" | ||
v := value | ||
if len(value) >= 1 { | ||
secretKey = value[0] | ||
v = v[1:] | ||
} | ||
return &executor{ | ||
Param: dynamic_params.NewParam(name, v), | ||
secretKey: secretKey, | ||
} | ||
} | ||
|
||
func (m *executor) Generate(ctx http_service.IHttpContext, contentType string, args ...interface{}) (interface{}, error) { | ||
result, err := m.Param.Generate(ctx, contentType, args...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
v, ok := result.(string) | ||
if !ok { | ||
return nil, errors.New("hmac-sha256 value is not string") | ||
} | ||
value := hMacBySHA256(m.secretKey, v) | ||
if !strings.HasPrefix(m.Name(), "__") { | ||
value = strings.ToUpper(value) | ||
} | ||
log.DebugF("hmac-sha256 value before: %s,after: %s", v, value) | ||
return value, nil | ||
} | ||
|
||
func hMacBySHA256(secretKey, toSign string) string { | ||
// 创建对应的sha256哈希加密算法 | ||
hm := hmac.New(sha256.New, []byte(secretKey)) | ||
//写入加密数据 | ||
hm.Write([]byte(toSign)) | ||
return hex.EncodeToString(hm.Sum(nil)) | ||
} |
199 changes: 199 additions & 0 deletions
199
drivers/plugins/extra-params_v2/dynamic-params/hmac-sha256/executor_test.go
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,199 @@ | ||
package hmac_sha256 | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"testing" | ||
"time" | ||
|
||
"github.com/eolinker/eosc" | ||
"github.com/eolinker/eosc/eocontext" | ||
http_service "github.com/eolinker/eosc/eocontext/http-context" | ||
) | ||
|
||
func TestExecutor(t *testing.T) { | ||
e := NewExecutor("sign", []string{ | ||
"111111", | ||
"appKey", | ||
"1111111", | ||
"format", | ||
"JSON", | ||
"idcard", | ||
"111111111111111111", | ||
"method", | ||
"realid.idcard.verify", | ||
"nonce", | ||
"1111111", | ||
"realname", | ||
"张三", | ||
"signMethod", | ||
"HMAC-SHA256", | ||
"signVersion", | ||
"1", | ||
"timestamp", | ||
"2018-02-07 02:50:21", | ||
"version", | ||
"1", | ||
}) | ||
ctx := &Context{} | ||
v, err := e.Generate(ctx, "") | ||
t.Log(v, err) | ||
} | ||
|
||
type Context struct { | ||
} | ||
|
||
func (c *Context) RequestId() string { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) AcceptTime() time.Time { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) Context() context.Context { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) Value(key interface{}) interface{} { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) WithValue(key, val interface{}) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) Scheme() string { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) Assert(i interface{}) error { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) SetLabel(name, value string) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) GetLabel(name string) string { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) Labels() map[string]string { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) GetComplete() eocontext.CompleteHandler { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) SetCompleteHandler(handler eocontext.CompleteHandler) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) GetFinish() eocontext.FinishHandler { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) SetFinish(handler eocontext.FinishHandler) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) GetBalance() eocontext.BalanceHandler { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) SetBalance(handler eocontext.BalanceHandler) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) GetUpstreamHostHandler() eocontext.UpstreamHostHandler { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) SetUpstreamHostHandler(handler eocontext.UpstreamHostHandler) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) RealIP() string { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) LocalIP() net.IP { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) LocalAddr() net.Addr { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) LocalPort() int { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) IsCloneable() bool { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) Clone() (eocontext.EoContext, error) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) Request() http_service.IRequestReader { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) Proxy() http_service.IRequest { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) Response() http_service.IResponse { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) SendTo(scheme string, node eocontext.INode, timeout time.Duration) error { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) Proxies() []http_service.IProxy { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) FastFinish() { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (c *Context) GetEntry() eosc.IEntry { | ||
//TODO implement me | ||
panic("implement me") | ||
} |
20 changes: 20 additions & 0 deletions
20
drivers/plugins/extra-params_v2/dynamic-params/hmac-sha256/factory.go
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,20 @@ | ||
package hmac_sha256 | ||
|
||
import dynamic_params "github.com/eolinker/apinto/drivers/plugins/extra-params_v2/dynamic-params" | ||
|
||
const name = "$hmac-sha256" | ||
|
||
func Register() { | ||
dynamic_params.Register(name, NewFactory()) | ||
} | ||
|
||
func NewFactory() *Factory { | ||
return &Factory{} | ||
} | ||
|
||
type Factory struct { | ||
} | ||
|
||
func (f *Factory) Create(name string, value []string) (dynamic_params.IDynamicDriver, error) { | ||
return NewExecutor(name, value), nil | ||
} |
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,32 @@ | ||
package request_interception | ||
|
||
import ( | ||
"github.com/eolinker/apinto/drivers" | ||
"github.com/eolinker/eosc" | ||
) | ||
|
||
type Config struct { | ||
Status int `json:"status" label:"响应状态码" minimum:"100" default:"200" description:"最小值:100"` | ||
Body string `json:"body" label:"响应体"` | ||
ContentType string `json:"content_type" label:"响应体类型" default:"application/json" enum:"text/plain,text/html,application/json"` | ||
Headers []*Header `json:"headers" label:"响应头"` | ||
} | ||
|
||
type Header struct { | ||
Key string `json:"key" label:"响应头Key"` | ||
Value []string `json:"value" label:"响应头Value"` | ||
} | ||
|
||
func Create(id, name string, conf *Config, workers map[eosc.RequireId]eosc.IWorker) (eosc.IWorker, error) { | ||
contentType := conf.ContentType | ||
if contentType == "" { | ||
contentType = "application/json" | ||
} | ||
return &executor{ | ||
WorkerBase: drivers.Worker(id, name), | ||
status: conf.Status, | ||
body: conf.Body, | ||
headers: conf.Headers, | ||
contentType: conf.ContentType, | ||
}, nil | ||
} |
Oops, something went wrong.