From 7512128337656a8acd58e2710a00a9e9f1f4bbfb Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Tue, 16 Dec 2014 11:56:54 -0800 Subject: [PATCH] oauth2/jws: minor readability improvements Change-Id: I8bcf0bc88b3db49ac281531524e792e8547e1e07 Reviewed-on: https://go-review.googlesource.com/1671 Reviewed-by: Brad Fitzpatrick --- jws/jws.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/jws/jws.go b/jws/jws.go index 61cbfc5..362323c 100644 --- a/jws/jws.go +++ b/jws/jws.go @@ -104,7 +104,7 @@ func (h *Header) encode() (string, error) { } // Decode decodes a claim set from a JWS payload. -func Decode(payload string) (c *ClaimSet, err error) { +func Decode(payload string) (*ClaimSet, error) { // decode returned id token to get expiry s := strings.Split(payload, ".") if len(s) < 2 { @@ -115,31 +115,28 @@ func Decode(payload string) (c *ClaimSet, err error) { if err != nil { return nil, err } - c = &ClaimSet{} + c := &ClaimSet{} err = json.NewDecoder(bytes.NewBuffer(decoded)).Decode(c) return c, err } // Encode encodes a signed JWS with provided header and claim set. -func Encode(header *Header, c *ClaimSet, signature *rsa.PrivateKey) (payload string, err error) { - var encodedHeader, encodedClaimSet string - encodedHeader, err = header.encode() +func Encode(header *Header, c *ClaimSet, signature *rsa.PrivateKey) (string, error) { + head, err := header.encode() if err != nil { - return + return "", err } - encodedClaimSet, err = c.encode() + cs, err := c.encode() if err != nil { - return + return "", err } - - ss := fmt.Sprintf("%s.%s", encodedHeader, encodedClaimSet) + ss := fmt.Sprintf("%s.%s", head, cs) h := sha256.New() h.Write([]byte(ss)) b, err := rsa.SignPKCS1v15(rand.Reader, signature, crypto.SHA256, h.Sum(nil)) if err != nil { - return + return "", err } - sig := base64Encode(b) return fmt.Sprintf("%s.%s", ss, sig), nil }