From 27327e0788c79928452d7abc7748d6f224c7f674 Mon Sep 17 00:00:00 2001 From: liangzy Date: Thu, 6 Aug 2020 17:29:29 +0800 Subject: [PATCH] delete sign value --- README.md | 55 +++++++++++++++++++++++++++++++++++++++ sdk/responses/response.go | 18 ++++++++++--- 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d2e5481..a70c17e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,57 @@ # gaore-common-sdk-go +### 目录架构 +``` +├── README.md +├── sdk +│   ├── auth `认证类` +│   ├── client.go `客户端主入口类` +│   ├── config.go +│   ├── requests `请求类` +│   ├── responses `响应类` +│   └── utils `工具类` +└── services + └── jedi +``` + +### 服务端认证调用事例 + +调用 `auth.UnSign` 方法,提供`*http.Request`请求实例,以及`signer.Signer` 签名器(签名器需要包括凭据) +即可完成整个验签过程 + +```go +package main + +import ( + "github.com/astaxie/beego" + "github.com/astaxie/beego/context" + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth" + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/credentials" + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/signers" + +) + +func init() { + // 过滤器 + beego.InsertFilter("/api/*", beego.BeforeRouter, func(context *context.Context) { + httpRequest := context.Request + err := auth.UnSign(httpRequest, signers.NewAccessKeySigner(&credentials.AccessKeyCredential{ + AccessKeyId: "aaaaaa", + AccessKeySecret: "bbbbbb", + AccessKeyFrom: context.Input.Param("access_from"), + })) + + if err != nil { + resp := response.NewJsonByDefaultFailed() + resp.Msg = err.Error() + resp.Code = 10086 + + var ( + hasIndent = beego.BConfig.RunMode != beego.PROD + ) + context.Output.Status = 500 + context.Output.JSON(resp, hasIndent, false) + } + }) +} +``` \ No newline at end of file diff --git a/sdk/responses/response.go b/sdk/responses/response.go index 3df729f..7125546 100644 --- a/sdk/responses/response.go +++ b/sdk/responses/response.go @@ -4,8 +4,10 @@ import ( "encoding/json" "errors" "fmt" + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests" "io/ioutil" "net/http" + "strings" ) type AcsResponse interface { @@ -86,12 +88,22 @@ func Unmarshal(response AcsResponse, httpResponse *http.Response, format string) if err != nil { return } - if !response.IsSuccess() { - err = errors.New(fmt.Sprintf("%d %s", response.GetHttpStatus(), response.GetHttpContentString())) + + if _, isCommonResponse := response.(CommonResponse); isCommonResponse { return } - if _, isCommonResponse := response.(CommonResponse); isCommonResponse { + if !response.IsSuccess() { + if contentType, ok := response.GetHttpHeaders()["Content-Type"]; ok { + for _, v := range contentType { + if strings.Contains(v, requests.Json) { + json.Unmarshal(response.GetHttpContentBytes(), response) + break + } + } + } + + err = errors.New(fmt.Sprintf("%d %s", response.GetHttpStatus(), response.GetHttpContentString())) return }