From d21f85cfebeaee5335b0a8228f0c6f2653e942a0 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 3 Jun 2016 14:03:20 +0200 Subject: Encapsulate Signer in Provider --- signer.go | 49 +++++++++++++++++-------------------------------- 1 file changed, 17 insertions(+), 32 deletions(-) (limited to 'signer.go') diff --git a/signer.go b/signer.go index 04bfd85..f72cd3c 100644 --- a/signer.go +++ b/signer.go @@ -6,7 +6,7 @@ import ( "crypto/rsa" "encoding/base64" "errors" - "io" + "io/ioutil" "net/http" "strings" @@ -20,7 +20,7 @@ var errNoNonces = errors.New("out of nonces") // Signer ... type Signer struct { - signer jose.Signer + jose.Signer thumb string nonces chan string } @@ -41,8 +41,8 @@ func NewSigner(privKey crypto.PrivateKey) (*Signer, error) { if err != nil { return nil, err } - sig := &Signer{signer: s, thumb: t, nonces: make(chan string, 100)} - sig.signer.SetNonceSource(sig) + sig := &Signer{Signer: s, thumb: t, nonces: make(chan string, 100)} + sig.SetNonceSource(sig) return sig, nil case *ecdsa.PrivateKey: s, err := jose.NewSigner(jose.ES384, k) @@ -53,8 +53,8 @@ func NewSigner(privKey crypto.PrivateKey) (*Signer, error) { if err != nil { return nil, err } - sig := &Signer{signer: s, thumb: t, nonces: make(chan string, 100)} - sig.signer.SetNonceSource(sig) + sig := &Signer{Signer: s, thumb: t, nonces: make(chan string, 100)} + sig.SetNonceSource(sig) return sig, nil default: return nil, ErrKeyType @@ -73,20 +73,18 @@ func (s Signer) Nonce() (string, error) { // RoundTrip extracts nonces from HTTP response func (s Signer) RoundTrip(req *http.Request) (*http.Response, error) { - /* - if req.Method == "POST" { - body, err := ioutil.ReadAll(req.Body) - if err != nil { - return nil, err - } - req.Body.Close() - obj, err := s.signer.Sign(body) - if err != nil { - return nil, err - } - req.Body = ioutil.NopCloser(strings.NewReader(obj.FullSerialize())) + if req.Method == "POST" { + body, err := ioutil.ReadAll(req.Body) + if err != nil { + return nil, err } - */ + req.Body.Close() + obj, err := s.Sign(body) + if err != nil { + return nil, err + } + req.Body = ioutil.NopCloser(strings.NewReader(obj.FullSerialize())) + } resp, err := http.DefaultTransport.RoundTrip(req) if err != nil { return nil, err @@ -101,16 +99,3 @@ func (s Signer) RoundTrip(req *http.Request) (*http.Response, error) { s.nonces <- nonce return resp, nil } - -// Sign implements Signer interface -func (s Signer) Sign(msg []byte) (io.Reader, error) { - obj, err := s.signer.Sign(msg) - if err != nil { - return nil, err - } - return strings.NewReader(obj.FullSerialize()), nil -} - -func (s Signer) KeyAuth(token string) string { - return token + "." + s.thumb -} -- cgit v1.2.3