service: prevent NPE in VerifyTokenSignature function

This commit adds next changes to VerifyTokenSignature:

  * returns ErrEmptyToken on nil token argument;

  * returns ErrEmptyPublicKey on nil public key argument.
This commit is contained in:
Leonard Lyubich 2020-04-29 10:57:07 +03:00
parent 82ffde253b
commit cce6566f1e
2 changed files with 19 additions and 0 deletions

View file

@ -203,7 +203,16 @@ func SignToken(token SessionToken, key *ecdsa.PrivateKey) error {
} }
// VerifyTokenSignature checks if token was signed correctly. // VerifyTokenSignature checks if token was signed correctly.
//
// If passed token is nil, ErrEmptyToken returns.
// If passed public key is nil, crypto.ErrEmptyPublicKey returns.
func VerifyTokenSignature(token SessionToken, key *ecdsa.PublicKey) error { func VerifyTokenSignature(token SessionToken, key *ecdsa.PublicKey) error {
if token == nil {
return ErrEmptyToken
} else if key == nil {
return crypto.ErrEmptyPublicKey
}
return crypto.Verify( return crypto.Verify(
key, key,
verificationTokenData(token), verificationTokenData(token),

View file

@ -96,6 +96,11 @@ func TestSignToken(t *testing.T) {
ErrEmptyToken.Error(), ErrEmptyToken.Error(),
) )
require.EqualError(t,
VerifyTokenSignature(nil, nil),
ErrEmptyToken.Error(),
)
var token SessionToken = new(Token) var token SessionToken = new(Token)
// nil key // nil key
@ -104,6 +109,11 @@ func TestSignToken(t *testing.T) {
crypto.ErrEmptyPrivateKey.Error(), crypto.ErrEmptyPrivateKey.Error(),
) )
require.EqualError(t,
VerifyTokenSignature(token, nil),
crypto.ErrEmptyPublicKey.Error(),
)
// create private key for signing // create private key for signing
sk := test.DecodeKey(0) sk := test.DecodeKey(0)
pk := &sk.PublicKey pk := &sk.PublicKey