-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the support of OpenDAX HMAC authentication method
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package auth | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// APIKeyHMAC contains API keys credential to authenticate to HTTP API using HMAC | ||
type APIKeyHMAC struct { | ||
AccessKey string | ||
SecretKey string | ||
} | ||
|
||
// NewAPIKeyHMAC creates an instance of APIKeyHMAC | ||
func NewAPIKeyHMAC(accessKey, secretKey string) *APIKeyHMAC { | ||
return &APIKeyHMAC{ | ||
AccessKey: accessKey, | ||
SecretKey: secretKey, | ||
} | ||
} | ||
|
||
// GetSignature return a signature for the given nonce, if nonce is zero it use the current time in millisecond | ||
func (key *APIKeyHMAC) GetSignature(nonce int64) string { | ||
if nonce == 0 { | ||
nonce = int64(time.Now().UnixNano() * 1000000) | ||
} | ||
mac := hmac.New(sha256.New, []byte(key.SecretKey)) | ||
mac.Write([]byte(fmt.Sprintf("%d%s", nonce, key.AccessKey))) | ||
return hex.EncodeToString(mac.Sum(nil)) | ||
} | ||
|
||
// GetSignedHeader returns a header with valid HMAC authorization fields | ||
func (key *APIKeyHMAC) GetSignedHeader(nonce int64) http.Header { | ||
if nonce == 0 { | ||
nonce = int64(time.Now().UnixNano() * 1000000) | ||
} | ||
|
||
return http.Header{ | ||
"X-Auth-Apikey": {key.AccessKey}, | ||
"X-Auth-Nonce": {fmt.Sprintf("%d", nonce)}, | ||
"X-Auth-Signature": {key.GetSignature(nonce)}, | ||
} | ||
} |
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,34 @@ | ||
package auth | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAPIKeyHMACGetSignature(t *testing.T) { | ||
accessKey := "61d025b8573501c2" | ||
secretKey := "2d0b4979c7fe6986daa8e21d1dc0644f" | ||
|
||
k := NewAPIKeyHMAC(accessKey, secretKey) | ||
nonce := int64(1584524005143) | ||
signature := k.GetSignature(nonce) | ||
assert.Equal(t, "bd42b945e095880e28d046846dbecf655fdf09d95a396a24fe6fe1df42f15d13", signature) | ||
} | ||
|
||
func TestAPIKeyHMACGetSignedHeader(t *testing.T) { | ||
accessKey := "61d025b8573501c2" | ||
secretKey := "2d0b4979c7fe6986daa8e21d1dc0644f" | ||
|
||
k := NewAPIKeyHMAC(accessKey, secretKey) | ||
nonce := int64(1584524005143) | ||
headers := k.GetSignedHeader(nonce) | ||
|
||
assert.Equal(t, | ||
http.Header{ | ||
"X-Auth-Apikey": {accessKey}, | ||
"X-Auth-Nonce": {"1584524005143"}, | ||
"X-Auth-Signature": {"bd42b945e095880e28d046846dbecf655fdf09d95a396a24fe6fe1df42f15d13"}, | ||
}, headers) | ||
} |