forked from apolloconfig/agollo
-
Notifications
You must be signed in to change notification settings - Fork 1
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
12 changed files
with
204 additions
and
6 deletions.
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
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 |
---|---|---|
|
@@ -12,4 +12,8 @@ type ConnectConfig struct { | |
URI string | ||
//是否重试 | ||
IsRetry bool | ||
//appID | ||
AppID string | ||
//密钥 | ||
Secret string | ||
} |
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,22 @@ | ||
package extension | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/tevid/gohamcrest" | ||
) | ||
|
||
type TestAuth struct{} | ||
|
||
func (a *TestAuth) HTTPHeaders(url string, appID string, secret string) map[string][]string { | ||
return nil | ||
} | ||
|
||
func TestSetHttpAuth(t *testing.T) { | ||
SetHTTPAuth(&TestAuth{}) | ||
|
||
a := GetHTTPAuth() | ||
|
||
b := a.(*TestAuth) | ||
Assert(t, b, NotNilVal()) | ||
} |
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,17 @@ | ||
package extension | ||
|
||
import ( | ||
"github.com/zouyx/agollo/v3/protocol/auth" | ||
) | ||
|
||
var authSign auth.HTTPAuth | ||
|
||
// SetHTTPAuth 设置HttpAuth | ||
func SetHTTPAuth(httpAuth auth.HTTPAuth) { | ||
authSign = httpAuth | ||
} | ||
|
||
// GetHTTPAuth 获取HttpAuth | ||
func GetHTTPAuth() auth.HTTPAuth { | ||
return authSign | ||
} |
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,7 @@ | ||
package auth | ||
|
||
// HTTPAuth http 授权 | ||
type HTTPAuth interface { | ||
// HTTPHeaders 根据 @url 获取 http 授权请求头 | ||
HTTPHeaders(url string, appID string, secret string) map[string][]string | ||
} |
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,70 @@ | ||
package sign | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/sha1" | ||
"encoding/base64" | ||
"fmt" | ||
"github.com/zouyx/agollo/v3/extension" | ||
"net/url" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
const ( | ||
httpHeaderAuthorization = "Authorization" | ||
httpHeaderTimestamp = "Timestamp" | ||
|
||
authorizationFormat = "Apollo %s:%s" | ||
|
||
delimiter = "\n" | ||
question = "?" | ||
) | ||
|
||
func init() { | ||
extension.SetHTTPAuth(&AuthSignature{}) | ||
} | ||
|
||
// 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 | ||
} |
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 sign | ||
|
||
import ( | ||
. "github.com/tevid/gohamcrest" | ||
"testing" | ||
) | ||
|
||
const ( | ||
rawURL = "http://baidu.com/a/b?key=1" | ||
secret = "6ce3ff7e96a24335a9634fe9abca6d51" | ||
appID = "testApplication_yang" | ||
) | ||
|
||
func TestSignString(t *testing.T) { | ||
s := signString(rawURL, secret) | ||
Assert(t, s, Equal("mcS95GXa7CpCjIfrbxgjKr0lRu8=")) | ||
} | ||
|
||
func TestUrl2PathWithQuery(t *testing.T) { | ||
|
||
pathWithQuery := url2PathWithQuery(rawURL) | ||
|
||
Assert(t, pathWithQuery, Equal("/a/b?key=1")) | ||
} | ||
|
||
func TestHttpHeaders(t *testing.T) { | ||
a := &AuthSignature{} | ||
headers := a.HTTPHeaders(rawURL, appID, secret) | ||
|
||
Assert(t, headers, HasMapValue("Authorization")) | ||
Assert(t, headers, HasMapValue("Timestamp")) | ||
} |
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