Skip to content

Commit 92ecd1e

Browse files
committed
Addressed staticcheck code smells and issues
1 parent 5d4d54c commit 92ecd1e

17 files changed

+78
-77
lines changed

claims.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import (
66
"time"
77
)
88

9-
// For a type to be a Claims object, it must just have a Valid method that determines
9+
// Claims must just have a Valid method that determines
1010
// if the token is invalid for any supported reason
1111
type Claims interface {
1212
Valid() error
1313
}
1414

15-
// Structured version of Claims Section, as referenced at
15+
// StandardClaims are a structured version of the Claims Section, as referenced at
1616
// https://tools.ietf.org/html/rfc7519#section-4.1
1717
// See examples for how to use this with your own claim types
1818
type StandardClaims struct {
@@ -25,8 +25,7 @@ type StandardClaims struct {
2525
Subject string `json:"sub,omitempty"`
2626
}
2727

28-
// Validates time based claims "exp, iat, nbf".
29-
// There is no accounting for clock skew.
28+
// Valid validates time based claims "exp, iat, nbf". There is no accounting for clock skew.
3029
// As well, if any of the above claims are not in the token, it will still
3130
// be considered a valid claim.
3231
func (c StandardClaims) Valid() error {
@@ -58,31 +57,31 @@ func (c StandardClaims) Valid() error {
5857
return vErr
5958
}
6059

61-
// Compares the aud claim against cmp.
60+
// VerifyAudience compares the aud claim against cmp.
6261
// If required is false, this method will return true if the value matches or is unset
6362
func (c *StandardClaims) VerifyAudience(cmp string, req bool) bool {
6463
return verifyAud([]string{c.Audience}, cmp, req)
6564
}
6665

67-
// Compares the exp claim against cmp.
66+
// VerifyExpiresAt compares the exp claim against cmp.
6867
// If required is false, this method will return true if the value matches or is unset
6968
func (c *StandardClaims) VerifyExpiresAt(cmp int64, req bool) bool {
7069
return verifyExp(c.ExpiresAt, cmp, req)
7170
}
7271

73-
// Compares the iat claim against cmp.
72+
// VerifyIssuedAt compares the iat claim against cmp.
7473
// If required is false, this method will return true if the value matches or is unset
7574
func (c *StandardClaims) VerifyIssuedAt(cmp int64, req bool) bool {
7675
return verifyIat(c.IssuedAt, cmp, req)
7776
}
7877

79-
// Compares the iss claim against cmp.
78+
// VerifyIssuer compares the iss claim against cmp.
8079
// If required is false, this method will return true if the value matches or is unset
8180
func (c *StandardClaims) VerifyIssuer(cmp string, req bool) bool {
8281
return verifyIss(c.Issuer, cmp, req)
8382
}
8483

85-
// Compares the nbf claim against cmp.
84+
// VerifyNotBefore compares the nbf claim against cmp.
8685
// If required is false, this method will return true if the value matches or is unset
8786
func (c *StandardClaims) VerifyNotBefore(cmp int64, req bool) bool {
8887
return verifyNbf(c.NotBefore, cmp, req)

ecdsa.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var (
1313
ErrECDSAVerification = errors.New("crypto/ecdsa: verification error")
1414
)
1515

16-
// Implements the ECDSA family of signing methods signing methods
16+
// SigningMethodECDSA implements the ECDSA family of signing methods.
1717
// Expects *ecdsa.PrivateKey for signing and *ecdsa.PublicKey for verification
1818
type SigningMethodECDSA struct {
1919
Name string
@@ -53,7 +53,7 @@ func (m *SigningMethodECDSA) Alg() string {
5353
return m.Name
5454
}
5555

56-
// Implements the Verify method from SigningMethod
56+
// Verify implements token verification for the SigningMethod.
5757
// For this verify method, key must be an ecdsa.PublicKey struct
5858
func (m *SigningMethodECDSA) Verify(signingString, signature string, key interface{}) error {
5959
var err error
@@ -95,7 +95,7 @@ func (m *SigningMethodECDSA) Verify(signingString, signature string, key interfa
9595
return ErrECDSAVerification
9696
}
9797

98-
// Implements the Sign method from SigningMethod
98+
// Sign implements token signing for the SigningMethod.
9999
// For this signing method, key must be an ecdsa.PrivateKey struct
100100
func (m *SigningMethodECDSA) Sign(signingString string, key interface{}) (string, error) {
101101
// Get the key

ecdsa_utils.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
)
99

1010
var (
11-
ErrNotECPublicKey = errors.New("Key is not a valid ECDSA public key")
12-
ErrNotECPrivateKey = errors.New("Key is not a valid ECDSA private key")
11+
ErrNotECPublicKey = errors.New("key is not a valid ECDSA public key")
12+
ErrNotECPrivateKey = errors.New("key is not a valid ECDSA private key")
1313
)
1414

15-
// Parse PEM encoded Elliptic Curve Private Key Structure
15+
// ParseECPrivateKeyFromPEM parses a PEM encoded Elliptic Curve Private Key Structure
1616
func ParseECPrivateKeyFromPEM(key []byte) (*ecdsa.PrivateKey, error) {
1717
var err error
1818

@@ -39,7 +39,7 @@ func ParseECPrivateKeyFromPEM(key []byte) (*ecdsa.PrivateKey, error) {
3939
return pkey, nil
4040
}
4141

42-
// Parse PEM encoded PKCS1 or PKCS8 public key
42+
// ParseECPublicKeyFromPEM parses a PEM encoded PKCS1 or PKCS8 public key
4343
func ParseECPublicKeyFromPEM(key []byte) (*ecdsa.PublicKey, error) {
4444
var err error
4545

ed25519.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var (
1010
ErrEd25519Verification = errors.New("ed25519: verification error")
1111
)
1212

13-
// Implements the EdDSA family
13+
// SigningMethodEd25519 implements the EdDSA family.
1414
// Expects ed25519.PrivateKey for signing and ed25519.PublicKey for verification
1515
type SigningMethodEd25519 struct{}
1616

@@ -30,7 +30,7 @@ func (m *SigningMethodEd25519) Alg() string {
3030
return "EdDSA"
3131
}
3232

33-
// Implements the Verify method from SigningMethod
33+
// Verify implements token verification for the SigningMethod.
3434
// For this verify method, key must be an ed25519.PublicKey
3535
func (m *SigningMethodEd25519) Verify(signingString, signature string, key interface{}) error {
3636
var err error
@@ -59,7 +59,7 @@ func (m *SigningMethodEd25519) Verify(signingString, signature string, key inter
5959
return nil
6060
}
6161

62-
// Implements the Sign method from SigningMethod
62+
// Sign implements token signing for the SigningMethod.
6363
// For this signing method, key must be an ed25519.PrivateKey
6464
func (m *SigningMethodEd25519) Sign(signingString string, key interface{}) (string, error) {
6565
var ed25519Key ed25519.PrivateKey

ed25519_utils.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
)
1010

1111
var (
12-
ErrNotEdPrivateKey = errors.New("Key is not a valid Ed25519 private key")
13-
ErrNotEdPublicKey = errors.New("Key is not a valid Ed25519 public key")
12+
ErrNotEdPrivateKey = errors.New("key is not a valid Ed25519 private key")
13+
ErrNotEdPublicKey = errors.New("key is not a valid Ed25519 public key")
1414
)
1515

16-
// Parse PEM-encoded Edwards curve private key
16+
// ParseEdPrivateKeyFromPEM parses a PEM-encoded Edwards curve private key
1717
func ParseEdPrivateKeyFromPEM(key []byte) (crypto.PrivateKey, error) {
1818
var err error
1919

@@ -38,7 +38,7 @@ func ParseEdPrivateKeyFromPEM(key []byte) (crypto.PrivateKey, error) {
3838
return pkey, nil
3939
}
4040

41-
// Parse PEM-encoded Edwards curve public key
41+
// ParseEdPublicKeyFromPEM parses a PEM-encoded Edwards curve public key
4242
func ParseEdPublicKeyFromPEM(key []byte) (crypto.PublicKey, error) {
4343
var err error
4444

errors.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ const (
2727
ValidationErrorClaimsInvalid // Generic claims validation error
2828
)
2929

30-
// Helper for constructing a ValidationError with a string error message
30+
// NewValidationError is a helper for constructing a ValidationError with a string error message
3131
func NewValidationError(errorText string, errorFlags uint32) *ValidationError {
3232
return &ValidationError{
3333
text: errorText,
3434
Errors: errorFlags,
3535
}
3636
}
3737

38-
// The error from Parse if token is not valid
38+
// ValidationError represents an error from Parse if token is not valid
3939
type ValidationError struct {
4040
Inner error // stores the error returned by external dependencies, i.e.: KeyFunc
4141
Errors uint32 // bitfield. see ValidationError... constants
4242
text string // errors that do not have a valid error just have text
4343
}
4444

45-
// Validation error is an error type
45+
// Error is the implementation of the err interface.
4646
func (e ValidationError) Error() string {
4747
if e.Inner != nil {
4848
return e.Inner.Error()

hmac.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"errors"
77
)
88

9-
// Implements the HMAC-SHA family of signing methods signing methods
9+
// SigningMethodHMAC implements the HMAC-SHA family of signing methods.
1010
// Expects key type of []byte for both signing and validation
1111
type SigningMethodHMAC struct {
1212
Name string
@@ -45,7 +45,7 @@ func (m *SigningMethodHMAC) Alg() string {
4545
return m.Name
4646
}
4747

48-
// Verify the signature of HSXXX tokens. Returns nil if the signature is valid.
48+
// Verify implements token verification for the SigningMethod. Returns nil if the signature is valid.
4949
func (m *SigningMethodHMAC) Verify(signingString, signature string, key interface{}) error {
5050
// Verify the key is the right type
5151
keyBytes, ok := key.([]byte)
@@ -77,7 +77,7 @@ func (m *SigningMethodHMAC) Verify(signingString, signature string, key interfac
7777
return nil
7878
}
7979

80-
// Implements the Sign method from SigningMethod for this signing method.
80+
// Sign implements token signing for the SigningMethod.
8181
// Key must be []byte
8282
func (m *SigningMethodHMAC) Sign(signingString string, key interface{}) (string, error) {
8383
if keyBytes, ok := key.([]byte); ok {

http_example_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ var (
3030
verifyKey *rsa.PublicKey
3131
signKey *rsa.PrivateKey
3232
serverPort int
33-
// storing sample username/password pairs
34-
// don't do this on a real server
35-
users = map[string]string{
36-
"test": "known",
37-
}
3833
)
3934

4035
// read the key files before starting http handlers
@@ -65,8 +60,6 @@ func init() {
6560
}()
6661
}
6762

68-
var start func()
69-
7063
func fatal(err error) {
7164
if err != nil {
7265
log.Fatal(err)
@@ -214,5 +207,4 @@ func restrictedHandler(w http.ResponseWriter, r *http.Request) {
214207

215208
// Token is valid
216209
fmt.Fprintln(w, "Welcome,", token.Claims.(*CustomClaimsExample).Name)
217-
return
218210
}

map_claims.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
// "fmt"
77
)
88

9-
// Claims type that uses the map[string]interface{} for JSON decoding
9+
// MapClaims is a claims type that uses the map[string]interface{} for JSON decoding.
1010
// This is the default claims type if you don't supply one
1111
type MapClaims map[string]interface{}
1212

@@ -31,7 +31,7 @@ func (m MapClaims) VerifyAudience(cmp string, req bool) bool {
3131
return verifyAud(aud, cmp, req)
3232
}
3333

34-
// Compares the exp claim against cmp.
34+
// VerifyExpiresAt compares the exp claim against cmp.
3535
// If required is false, this method will return true if the value matches or is unset
3636
func (m MapClaims) VerifyExpiresAt(cmp int64, req bool) bool {
3737
exp, ok := m["exp"]
@@ -48,7 +48,7 @@ func (m MapClaims) VerifyExpiresAt(cmp int64, req bool) bool {
4848
return false
4949
}
5050

51-
// Compares the iat claim against cmp.
51+
// VerifyIssuedAt compares the iat claim against cmp.
5252
// If required is false, this method will return true if the value matches or is unset
5353
func (m MapClaims) VerifyIssuedAt(cmp int64, req bool) bool {
5454
iat, ok := m["iat"]
@@ -65,14 +65,14 @@ func (m MapClaims) VerifyIssuedAt(cmp int64, req bool) bool {
6565
return false
6666
}
6767

68-
// Compares the iss claim against cmp.
68+
// VerifyIssuer compares the iss claim against cmp.
6969
// If required is false, this method will return true if the value matches or is unset
7070
func (m MapClaims) VerifyIssuer(cmp string, req bool) bool {
7171
iss, _ := m["iss"].(string)
7272
return verifyIss(iss, cmp, req)
7373
}
7474

75-
// Compares the nbf claim against cmp.
75+
// VerifyNotBefore compares the nbf claim against cmp.
7676
// If required is false, this method will return true if the value matches or is unset
7777
func (m MapClaims) VerifyNotBefore(cmp int64, req bool) bool {
7878
nbf, ok := m["nbf"]
@@ -89,7 +89,7 @@ func (m MapClaims) VerifyNotBefore(cmp int64, req bool) bool {
8989
return false
9090
}
9191

92-
// Validates time based claims "exp, iat, nbf".
92+
// Valid calidates time based claims "exp, iat, nbf".
9393
// There is no accounting for clock skew.
9494
// As well, if any of the above claims are not in the token, it will still
9595
// be considered a valid claim.

none.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package jwt
22

3-
// Implements the none signing method. This is required by the spec
3+
// SigningMethodNone implements the none signing method. This is required by the spec
44
// but you probably should never use it.
55
var SigningMethodNone *signingMethodNone
66

0 commit comments

Comments
 (0)