代码拉取完成,页面将自动刷新
package apollo
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"net/url"
"strconv"
"time"
)
// HTTPAuth http 授权
type HTTPAuth interface {
// HTTPHeaders 根据 @url 获取 http 授权请求头
HTTPHeaders(url string, appID string, secret string) map[string][]string
}
const (
httpHeaderAuthorization = "Authorization"
httpHeaderTimestamp = "Timestamp"
authorizationFormat = "Apollo %s:%s"
delimiter = "\n"
question = "?"
)
// AuthSignature apollo 授权
type AuthSignature struct {
}
// HTTPHeaders HTTPHeaders
func (t *AuthSignature) HTTPHeaders(url string, appID string, secret string) map[string][]string {
ms := time.Now().UnixNano() / int64(time.Millisecond)
timestamp := strconv.FormatInt(ms, 10)
pathWithQuery := url2PathWithQuery(url)
stringToSign := timestamp + delimiter + pathWithQuery
signature := signString(stringToSign, secret)
headers := make(map[string][]string, 2)
signatures := make([]string, 0, 1)
signatures = append(signatures, fmt.Sprintf(authorizationFormat, appID, signature))
headers[httpHeaderAuthorization] = signatures
timestamps := make([]string, 0, 1)
timestamps = append(timestamps, timestamp)
headers[httpHeaderTimestamp] = timestamps
return headers
}
func signString(stringToSign string, accessKeySecret string) string {
key := []byte(accessKeySecret)
mac := hmac.New(sha1.New, key)
mac.Write([]byte(stringToSign))
return base64.StdEncoding.EncodeToString(mac.Sum(nil))
}
func url2PathWithQuery(rawURL string) string {
u, err := url.Parse(rawURL)
if err != nil {
return ""
}
pathWithQuery := u.Path
if len(u.RawQuery) > 0 {
pathWithQuery += question + u.RawQuery
}
return pathWithQuery
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。