jws: minor stylistic changes and dead code removal
diff --git a/crypto/ecdsa.go b/crypto/ecdsa.go
index c518c9d..3ef12ba 100644
--- a/crypto/ecdsa.go
+++ b/crypto/ecdsa.go
@@ -101,7 +101,9 @@
}
// Hasher implements the Hasher method from SigningMethod.
-func (m *SigningMethodECDSA) Hasher() crypto.Hash { return m.Hash }
+func (m *SigningMethodECDSA) Hasher() crypto.Hash {
+ return m.Hash
+}
// MarshalJSON is in case somebody decides to place SigningMethodECDSA
// inside the Header, presumably because they (wrongly) decided it was a good
diff --git a/crypto/none.go b/crypto/none.go
index 2b27af4..db3d139 100644
--- a/crypto/none.go
+++ b/crypto/none.go
@@ -7,24 +7,28 @@
"io"
)
-func init() { crypto.RegisterHash(crypto.Hash(0), h) }
+func init() {
+ crypto.RegisterHash(crypto.Hash(0), h)
+}
// h is passed to crypto.RegisterHash.
-func h() hash.Hash { return &f{Writer: nil} }
+func h() hash.Hash {
+ return &f{Writer: nil}
+}
type f struct{ io.Writer }
// Sum helps implement the hash.Hash interface.
-func (f *f) Sum(b []byte) []byte { return nil }
+func (_ *f) Sum(b []byte) []byte { return nil }
// Reset helps implement the hash.Hash interface.
-func (f *f) Reset() {}
+func (_ *f) Reset() {}
// Size helps implement the hash.Hash interface.
-func (f *f) Size() int { return -1 }
+func (_ *f) Size() int { return -1 }
// BlockSize helps implement the hash.Hash interface.
-func (f *f) BlockSize() int { return -1 }
+func (_ *f) BlockSize() int { return -1 }
// Unsecured is the default "none" algorithm.
var Unsecured = &SigningMethodNone{
@@ -40,20 +44,24 @@
}
// Verify helps implement the SigningMethod interface.
-func (m *SigningMethodNone) Verify(_ []byte, _ Signature, _ interface{}) error {
+func (_ *SigningMethodNone) Verify(_ []byte, _ Signature, _ interface{}) error {
return nil
}
// Sign helps implement the SigningMethod interface.
-func (m *SigningMethodNone) Sign(_ []byte, _ interface{}) (Signature, error) {
+func (_ *SigningMethodNone) Sign(_ []byte, _ interface{}) (Signature, error) {
return nil, nil
}
// Alg helps implement the SigningMethod interface.
-func (m *SigningMethodNone) Alg() string { return m.Name }
+func (m *SigningMethodNone) Alg() string {
+ return m.Name
+}
// Hasher helps implement the SigningMethod interface.
-func (m *SigningMethodNone) Hasher() crypto.Hash { return m.Hash }
+func (m *SigningMethodNone) Hasher() crypto.Hash {
+ return m.Hash
+}
// MarshalJSON implements json.Marshaler.
// See SigningMethodECDSA.MarshalJSON() for information.
diff --git a/header.go b/header.go
index 4bf64c5..4499a76 100644
--- a/header.go
+++ b/header.go
@@ -32,7 +32,7 @@
// MarshalJSON implements json.Marshaler for Header.
func (h Header) MarshalJSON() ([]byte, error) {
- if h == nil || len(h) == 0 {
+ if len(h) == 0 {
return nil, nil
}
b, err := json.Marshal(map[string]interface{}(h))
@@ -52,23 +52,11 @@
if b == nil {
return nil
}
-
b, err := DecodeEscaped(b)
if err != nil {
return err
}
-
- // Since json.Unmarshal calls UnmarshalJSON,
- // calling json.Unmarshal on *p would be infinitely recursive
- // A temp variable is needed because &map[string]interface{}(*p) is
- // invalid Go.
-
- tmp := map[string]interface{}(*h)
- if err = json.Unmarshal(b, &tmp); err != nil {
- return err
- }
- *h = tmp
- return nil
+ return json.Unmarshal(b, (*map[string]interface{})(h))
}
// Protected Headers are base64-encoded after they're marshaled into
@@ -131,4 +119,6 @@
var (
_ json.Marshaler = (Protected)(nil)
_ json.Unmarshaler = (*Protected)(nil)
+ _ json.Marshaler = (Header)(nil)
+ _ json.Unmarshaler = (*Header)(nil)
)
diff --git a/jws/jws.go b/jws/jws.go
index a0b929d..29ae67c 100644
--- a/jws/jws.go
+++ b/jws/jws.go
@@ -16,17 +16,21 @@
Payload() interface{}
// SetPayload sets the payload with the given value.
- SetPayload(interface{})
+ SetPayload(p interface{})
// Protected returns the JWS' Protected Header.
+ Protected() jose.Protected
+
+ // ProtectedAt returns the JWS' Protected Header.
// i represents the index of the Protected Header.
- // Left empty, it defaults to 0.
- Protected(...int) jose.Protected
+ ProtectedAt(i int) jose.Protected
// Header returns the JWS' unprotected Header.
- // i represents the index of the Protected Header.
- // Left empty, it defaults to 0.
- Header(...int) jose.Header
+ Header() jose.Header
+
+ // HeaderAt returns the JWS' unprotected Header.
+ // i represents the index of the unprotected Header.
+ HeaderAt(i int) jose.Header
// Verify validates the current JWS' signature as-is. Refer to
// ValidateMulti for more information.
@@ -74,29 +78,36 @@
}
// Payload returns the jws' payload.
-func (j *jws) Payload() interface{} { return j.payload.v }
+func (j *jws) Payload() interface{} {
+ return j.payload.v
+}
// SetPayload sets the jws' raw, unexported payload.
-func (j *jws) SetPayload(val interface{}) { j.payload.v = val }
+func (j *jws) SetPayload(val interface{}) {
+ j.payload.v = val
+}
+
+// Protected returns the JWS' Protected Header.
+func (j *jws) Protected() jose.Protected {
+ return j.sb[0].protected
+}
// Protected returns the JWS' Protected Header.
// i represents the index of the Protected Header.
// Left empty, it defaults to 0.
-func (j *jws) Protected(i ...int) jose.Protected {
- if len(i) == 0 {
- return j.sb[0].protected
- }
- return j.sb[i[0]].protected
+func (j *jws) ProtectedAt(i int) jose.Protected {
+ return j.sb[i].protected
}
// Header returns the JWS' unprotected Header.
-// i represents the index of the Protected Header.
-// Left empty, it defaults to 0.
-func (j *jws) Header(i ...int) jose.Header {
- if len(i) == 0 {
- return j.sb[0].unprotected
- }
- return j.sb[i[0]].unprotected
+func (j *jws) Header() jose.Header {
+ return j.sb[0].unprotected
+}
+
+// HeaderAt returns the JWS' unprotected Header.
+// |i| is the index of the unprotected Header.
+func (j *jws) HeaderAt(i int) jose.Header {
+ return j.sb[i].unprotected
}
// sigHead represents the 'signatures' member of the jws' "general"
@@ -121,10 +132,7 @@
if err := s.protected.UnmarshalJSON(s.Protected); err != nil {
return err
}
- if err := s.unprotected.UnmarshalJSON(s.Unprotected); err != nil {
- return err
- }
- return nil
+ return s.unprotected.UnmarshalJSON(s.Unprotected)
}
// New creates a JWS with the provided crypto.SigningMethods.
@@ -155,7 +163,6 @@
if sm == nil {
return ErrNoAlgorithm
}
-
s.method = sm
return nil
}
@@ -236,10 +243,10 @@
if err := g.Signatures[i].assignMethod(g.Signatures[i].protected); err != nil {
return nil, err
}
-
- g.clean = true
}
+ g.clean = len(g.Signatures) != 0
+
return &jws{
payload: &p,
plcache: g.Payload,
@@ -390,11 +397,12 @@
Compact
)
-var parseJumpTable = [^Format(0)]func([]byte, ...json.Unmarshaler) (JWS, error){
- Unknown: Parse,
- Flat: ParseFlat,
- General: ParseGeneral,
- Compact: ParseCompact,
+var parseJumpTable = [...]func([]byte, ...json.Unmarshaler) (JWS, error){
+ Unknown: Parse,
+ Flat: ParseFlat,
+ General: ParseGeneral,
+ Compact: ParseCompact,
+ 1<<8 - 1: Parse, // Max uint8.
}
func init() {
diff --git a/jws/jws_serialize.go b/jws/jws_serialize.go
index 9cb53af..923fdc2 100644
--- a/jws/jws_serialize.go
+++ b/jws/jws_serialize.go
@@ -100,36 +100,28 @@
}
// cache marshals the payload, but only if it's changed since the last cache.
-func (j *jws) cache() error {
+func (j *jws) cache() (err error) {
if !j.clean {
- var err error
j.plcache, err = j.payload.Base64()
j.clean = err == nil
- return err
}
- return nil
+ return err
}
// cache marshals the protected and unprotected headers, but only if
// they've changed since their last cache.
-func (s *sigHead) cache() error {
+func (s *sigHead) cache() (err error) {
if !s.clean {
- var err error
-
s.Protected, err = s.protected.Base64()
if err != nil {
- goto err_return
+ return err
}
-
s.Unprotected, err = s.unprotected.Base64()
if err != nil {
- goto err_return
+ return err
}
-
- err_return:
- s.clean = err == nil
- return err
}
+ s.clean = true
return nil
}
diff --git a/jws/jwt.go b/jws/jwt.go
index 9720613..f98edf1 100644
--- a/jws/jwt.go
+++ b/jws/jwt.go
@@ -64,7 +64,9 @@
}
// IsJWT returns true if the JWS is a JWT.
-func (j *jws) IsJWT() bool { return j.isJWT }
+func (j *jws) IsJWT() bool {
+ return j.isJWT
+}
func (j *jws) Validate(key interface{}, m crypto.SigningMethod, v ...*jwt.Validator) error {
if j.isJWT {
diff --git a/jws/payload.go b/jws/payload.go
index 34ba6d8..58bfd06 100644
--- a/jws/payload.go
+++ b/jws/payload.go
@@ -37,13 +37,11 @@
if err != nil {
return err
}
-
if p.u != nil {
err := p.u.UnmarshalJSON(b2)
p.v = p.u
return err
}
-
return json.Unmarshal(b2, &p.v)
}
diff --git a/jws/signing_methods.go b/jws/signing_methods.go
index 1b6665f..525806f 100644
--- a/jws/signing_methods.go
+++ b/jws/signing_methods.go
@@ -7,7 +7,7 @@
)
var (
- mu = &sync.RWMutex{}
+ mu sync.RWMutex
signingMethods = map[string]crypto.SigningMethod{
crypto.SigningMethodES256.Alg(): crypto.SigningMethodES256,
@@ -33,7 +33,8 @@
// RegisterSigningMethod registers the crypto.SigningMethod in the global map.
// This is typically done inside the caller's init function.
func RegisterSigningMethod(sm crypto.SigningMethod) {
- if GetSigningMethod(sm.Alg()) != nil {
+ alg := sm.Alg()
+ if GetSigningMethod(alg) != nil {
panic("jose/jws: cannot duplicate signing methods")
}
@@ -42,7 +43,7 @@
}
mu.Lock()
- signingMethods[sm.Alg()] = sm
+ signingMethods[alg] = sm
mu.Unlock()
}
@@ -54,8 +55,9 @@
}
// GetSigningMethod retrieves a crypto.SigningMethod from the global map.
-func GetSigningMethod(alg string) crypto.SigningMethod {
+func GetSigningMethod(alg string) (method crypto.SigningMethod) {
mu.RLock()
- defer mu.RUnlock()
- return signingMethods[alg]
+ method = signingMethods[alg]
+ mu.RUnlock()
+ return method
}
diff --git a/jwt/claims.go b/jwt/claims.go
index d4f94b8..ae41e7d 100644
--- a/jwt/claims.go
+++ b/jwt/claims.go
@@ -61,7 +61,7 @@
return json.Marshal(map[string]interface{}(c))
}
-// Base64 implements the Encoder interface.
+// Base64 implements the jose.Encoder interface.
func (c Claims) Base64() ([]byte, error) {
b, err := c.MarshalJSON()
if err != nil {