Skip to content

Commit

Permalink
Break out timestamping from signing logic for people making their own…
Browse files Browse the repository at this point in the history
… signed URL's.
  • Loading branch information
James D. Nurmi committed Jun 24, 2011
1 parent 01a7f2c commit a153fcd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
15 changes: 5 additions & 10 deletions signer.go
Expand Up @@ -9,8 +9,6 @@ import (
"http"
"os"
"strings"
"strconv"
"time"
)

// A signer simply holds the access & secret access keys
Expand Down Expand Up @@ -82,12 +80,10 @@ func (self *Signer) SignRequestV2(req *http.Request, canon func(*http.Request) s
req.Form.Del("Signature")
req.Form.Del("Timestamp")
req.Form.Del("Expires")
if req.Form.Get("Timestamp") == "" && req.Form.Get("Expires") == "" {
if exp > 0 {
req.Form.Set("Expires", strconv.Itoa64(time.Seconds()+exp))
} else {
req.Form.Set("Expires", time.UTC().Format(ISO8601TimestampFormat))
}
if exp > 0 {
Expires(req, nil, exp)
} else {
Timestamp(req, nil)
}

var sig []byte
Expand Down Expand Up @@ -121,8 +117,7 @@ func (self *Signer) SignRequestV1(req *http.Request, canon func(*http.Request) s

req.Form.Set("AWSAccessKeyId", self.AccessKey)
req.Form.Del("Signature")
req.Form.Set("Expires", strconv.Itoa64(time.Seconds()+exp))

Expires(req, nil, exp)
var sig []byte
sig, err = self.SignEncoded(crypto.SHA1, canon(req), base64.StdEncoding)

Expand Down
28 changes: 28 additions & 0 deletions timeformats.go
@@ -1,8 +1,36 @@
package aws

import (
"http"
"strconv"
"time"
)
/*
Copyright (c) 2010, Abneptis LLC.
See COPYRIGHT and LICENSE for details.
*/

var SQSTimestampFormat = "2006-01-02T15:04:05MST"
var ISO8601TimestampFormat = "2006-01-02T15:04:05Z"

// Adds a current timestamp to the request (deleting any present timestamp).
// - you may optionally pass in a custom function to create the timestamp value,
// otherwise we default to time.UTC().

func Timestamp(req *http.Request, t func() *time.Time) {
if t == nil {
req.Form.Set("Timestamp", time.UTC().Format(ISO8601TimestampFormat))
} else {
req.Form.Set("Timestamp", t().Format(ISO8601TimestampFormat))
}
}

// Adds an expiration message to the request (as opposed to a timestamp)
// You may supply a custom time() function, else time.Seconds() is used.
func Expires(req *http.Request, t func() *time.Time, from_now int64) {
if t == nil {
req.Form.Set("Expires", strconv.Itoa64(time.Seconds() + from_now))
} else {
req.Form.Set("Expires", strconv.Itoa64(t().Seconds()+from_now))
}
}

0 comments on commit a153fcd

Please sign in to comment.