Skip to content

Commit

Permalink
Rename constants per Go convention
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Lo-A-Foe committed Nov 19, 2018
1 parent 32c6782 commit e79eb81
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
44 changes: 25 additions & 19 deletions signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ import (
"time"
)

// Constants
const (
LogTimeFormat = "2006-01-02T15:04:05.000Z07:00"
TimeFormat = time.RFC3339
HeaderAuthorization = "hsdp-api-signature"
HeaderSignedDate = "SignedDate"
DefaultPrefix64 = "REhQV1M="
AlgorithmName = "HmacSHA256"
)

// Errors
var (
LOG_TIME_FORMAT = "2006-01-02T15:04:05.000Z07:00"
TIME_FORMAT = time.RFC3339
AUTHORIZATION_HEADER = "hsdp-api-signature"
SIGNED_DATE_HEADER = "SignedDate"
DEFAULT_PREFIX_64 = "REhQV1M="
ALGORITHM_NAME = "HmacSHA256"
ErrSignatureExpired = errors.New("signture expired")
ErrInvalidSignature = errors.New("invalid signature")
ErrInvalidCredential = errors.New("invalid credential")
Expand All @@ -33,6 +38,7 @@ type Signer struct {
nowFunc NowFunc
}

// NowFunc is a time source
type NowFunc func() time.Time

// New creates an instance of Signer
Expand All @@ -48,8 +54,8 @@ func NewWithPrefixAndNowFunc(sharedKey, sharedSecret, prefix string, nowFunc Now
prefix: prefix,
}
if signer.prefix == "" {
decoded := make([]byte, base64.StdEncoding.DecodedLen(len(DEFAULT_PREFIX_64)))
l, _ := base64.StdEncoding.Decode(decoded, []byte(DEFAULT_PREFIX_64))
decoded := make([]byte, base64.StdEncoding.DecodedLen(len(DefaultPrefix64)))
l, _ := base64.StdEncoding.Decode(decoded, []byte(DefaultPrefix64))
signer.prefix = string(decoded[:l])
}
if nowFunc != nil {
Expand All @@ -62,34 +68,34 @@ func NewWithPrefixAndNowFunc(sharedKey, sharedSecret, prefix string, nowFunc Now
return signer, nil
}

// SignsRequest signs a http.Request by
// SignRequest signs a http.Request by
// adding an Authorization and SignedDate header
func (s *Signer) SignRequest(request *http.Request) error {
signTime := s.nowFunc().UTC().Format(TIME_FORMAT)
func (s *Signer) SignRequest(request *http.Request, withHeaders ...string) error {
signTime := s.nowFunc().UTC().Format(TimeFormat)

seed1 := base64.StdEncoding.EncodeToString([]byte(signTime))

hashedSeed := hash([]byte(seed1), []byte(s.prefix+s.sharedSecret))

signature := base64.StdEncoding.EncodeToString(hashedSeed)

authorization := ALGORITHM_NAME + ";" +
authorization := AlgorithmName + ";" +
"Credential:" + s.sharedKey + ";" +
"SignedHeaders:SignedDate" + ";" +
"Signature:" + signature

request.Header.Set(AUTHORIZATION_HEADER, authorization)
request.Header.Set(SIGNED_DATE_HEADER, signTime)
request.Header.Set(HeaderAuthorization, authorization)
request.Header.Set(HeaderSignedDate, signTime)
return nil
}

// ValidateRequests validates a previously signed request
// ValidateRequest validates a previously signed request
func (s *Signer) ValidateRequest(request *http.Request) (bool, error) {
signature := request.Header.Get(AUTHORIZATION_HEADER)
signedDate := request.Header.Get(SIGNED_DATE_HEADER)
signature := request.Header.Get(HeaderAuthorization)
signedDate := request.Header.Get(HeaderSignedDate)

comps := strings.Split(signature, ";")
if len(comps) < 4 || comps[0] != ALGORITHM_NAME {
if len(comps) < 4 || comps[0] != AlgorithmName {
return false, ErrInvalidSignature
}
credential := strings.TrimPrefix(comps[1], "Credential:")
Expand Down Expand Up @@ -126,7 +132,7 @@ func (s *Signer) ValidateRequest(request *http.Request) (bool, error) {
}

now := s.nowFunc()
signed, err := time.Parse(TIME_FORMAT, signedDate)
signed, err := time.Parse(TimeFormat, signedDate)
if err != nil || now.Sub(signed).Seconds() > 900 {
return false, ErrSignatureExpired
}
Expand Down
12 changes: 6 additions & 6 deletions signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func TestSigner(t *testing.T) {

signer.SignRequest(req)

signedDate := req.Header.Get(SIGNED_DATE_HEADER)
signature := req.Header.Get(AUTHORIZATION_HEADER)
signedDate := req.Header.Get(HeaderSignedDate)
signature := req.Header.Get(HeaderAuthorization)

nowFormatted := fixedTime().UTC().Format(TIME_FORMAT)
nowFormatted := fixedTime().UTC().Format(TimeFormat)

if signedDate != nowFormatted {
t.Errorf("Signature mismatch: %s != %s", signedDate, nowFormatted)
Expand Down Expand Up @@ -69,15 +69,15 @@ func TestValidator(t *testing.T) {
}

signer.SignRequest(req)
authSig := req.Header.Get(AUTHORIZATION_HEADER)
req.Header.Set(AUTHORIZATION_HEADER, strings.Replace(authSig, ALGORITHM_NAME, "BogusAlg", 1))
authSig := req.Header.Get(HeaderAuthorization)
req.Header.Set(HeaderAuthorization, strings.Replace(authSig, AlgorithmName, "BogusAlg", 1))
valid, err = signer.ValidateRequest(req)
if valid {
t.Errorf("Expected validation to fail")
}
if err != ErrInvalidSignature {
t.Errorf("Expected ErrInvalidSignature: %v", err)
}
req.Header.Set(AUTHORIZATION_HEADER, authSig)
req.Header.Set(HeaderAuthorization, authSig)

}

0 comments on commit e79eb81

Please sign in to comment.