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==