From 8af59d845be38f87b8255f1c65a393af02c5f01f Mon Sep 17 00:00:00 2001 From: dorlolo <428192774@qq.com> Date: Thu, 24 Mar 2022 00:15:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E4=BB=A4=E7=89=8C?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=9A=84=C3=A4=C2=B8=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- authorization.go | 41 +++++++++++++++++++++++++++++++++++++++++ simpleRequest.go | 13 ++++++++++++- test/base64_test.go | 21 +++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 authorization.go create mode 100644 test/base64_test.go diff --git a/authorization.go b/authorization.go new file mode 100644 index 0000000..eafa53e --- /dev/null +++ b/authorization.go @@ -0,0 +1,41 @@ +/* + * @FileName: auth.go + * @Author: JuneXu + * @CreateTime: 2022/3/24 上午12:09 + * @Description: + */ + +package simpleRequest + +import ( + "encoding/base64" + "fmt" +) + +type Authorization struct { + simpleReq *SimpleRequest +} + +// +// Basic +// @Description: 身份验证,使用bearer 令牌bearer 令牌 +// @receiver s +// @param username +// @param password +// +func (s *Authorization) Bearer(token string) { + s.simpleReq.headers.Set("Authorization", fmt.Sprintf("bearer %v", token)) +} + +// +// Basic +// @Description: 身份验证的基本验证方案 +// @receiver s +// @param username +// @param password +// +func (s *Authorization) Basic(username, password string) { + authStr := fmt.Sprintf("%v:%v", username, password) + data := base64.StdEncoding.EncodeToString([]byte(authStr)) + s.simpleReq.headers.Set("Authorization", fmt.Sprintf("basic %v", data)) +} diff --git a/simpleRequest.go b/simpleRequest.go index 8006010..836da6b 100644 --- a/simpleRequest.go +++ b/simpleRequest.go @@ -75,18 +75,29 @@ func (s *SimpleRequest) NewRequest() *SimpleRequest { //------------------------------------------------------ // // 数据准备 -// + +//Authorization 添加令牌的方法集合 +func (s *SimpleRequest) Authorization() *Authorization { + return &Authorization{ + simpleReq: s, + } +} + +//Headers 添加请求头 func (s *SimpleRequest) Headers() *HeadersConf { return &HeadersConf{ simpleReq: s, } } + +//Body 添加请求体 func (s *SimpleRequest) Body() *BodyConf { return &BodyConf{ simpleReq: s, } } +//QueryParams 添加url后面的参数 func (s *SimpleRequest) QueryParams() *QueryParams { return &QueryParams{ simpleReq: s, diff --git a/test/base64_test.go b/test/base64_test.go new file mode 100644 index 0000000..049c147 --- /dev/null +++ b/test/base64_test.go @@ -0,0 +1,21 @@ +/* + * @FileName: base64_test.go + * @Author: JuneXu + * @CreateTime: 2022/3/24 上午12:03 + * @Description: + */ + +package test + +import ( + "encoding/base64" + "fmt" + "testing" +) + +func TestBs(t *testing.T) { + authStr := fmt.Sprintf("%v:%v", "aaa", "bbb") + data := base64.StdEncoding.EncodeToString([]byte(authStr)) + t.Log(data) + +} //YWFhOmJiYg==