Skip to content

Commit

Permalink
Refactor singature based auth logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yarik bratashchuk committed Sep 26, 2019
1 parent 25d06e6 commit 56c2111
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
6 changes: 3 additions & 3 deletions uploadcare/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func SimpleAuth(creds APICreds, req *http.Request) {
// For more info on how SHA1 signature is constructed see
// https://uploadcare.com/docs/api_reference/rest/requests_auth/
func SignBasedAuth(creds APICreds, req *http.Request) {
authParam := signBasedAuthParam(creds, req, time.Now())
authParam := signBasedAuthParam(creds, req)
setHeader(req, authHeaderKey, authParam)
}

Expand All @@ -62,7 +62,7 @@ func simpleAuthParam(creds APICreds) string {
return val
}

func signBasedAuthParam(creds APICreds, req *http.Request, t time.Time) string {
func signBasedAuthParam(creds APICreds, req *http.Request) string {
bodyData := new(bytes.Buffer)
var bodyReader io.Reader
bodyReader = req.Body
Expand All @@ -84,7 +84,7 @@ func signBasedAuthParam(creds APICreds, req *http.Request, t time.Time) string {
signData.WriteRune('\n')
signData.WriteString(req.Header.Get("Content-Type"))
signData.WriteRune('\n')
signData.WriteString(t.In(dateHeaderLocation).Format(dateHeaderFormat))
signData.WriteString(req.Header.Get("Date"))
signData.WriteRune('\n')
signData.WriteString(uri)

Expand Down
8 changes: 6 additions & 2 deletions uploadcare/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ func TestSignBasedAuthParam(t *testing.T) {
)

// taken from https://uploadcare.com/docs/api_reference/rest/requests_auth/
now := time.
Unix(1541423681, 0).
In(dateHeaderLocation).
Format(dateHeaderFormat)
req.Header.Set("Content-Type", "application/json")
now := time.Unix(1541423681, 0)
req.Header.Set("Date", now)

expected := "Uploadcare testpk:3cbc4d2cf91f80c1ba162b926f8a975e8bec7995"
authParam := signBasedAuthParam(creds, req, now)
authParam := signBasedAuthParam(creds, req)

assert.Equal(t, expected, authParam)
}

0 comments on commit 56c2111

Please sign in to comment.