Skip to content

Commit

Permalink
test fialed secret reader
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Jan 1, 2019
1 parent b51c417 commit 8a9b27f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions token/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func (j *Service) Token(claims Claims) (string, error) {

token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)

if j.SecretReader == nil {
return "", errors.New("secretreader not defined")
}

secret, err := j.SecretReader.Get(claims.Audience) // get secret via consumer defined SecretReader
if err != nil {
return "", errors.Wrap(err, "can't get secret")
Expand Down
17 changes: 17 additions & 0 deletions token/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

jwt "github.com/dgrijalva/jwt-go"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -64,6 +65,14 @@ func TestJWT_Token(t *testing.T) {
res, err := j.Token(claims)
assert.Nil(t, err)
assert.Equal(t, testJwtValid, res)

j.SecretReader = nil
res, err = j.Token(claims)
assert.EqualError(t, err, "secretreader not defined")

j.SecretReader = SecretFunc(func(id string) (string, error) { return "", errors.New("err blah") })
res, err = j.Token(claims)
assert.EqualError(t, err, "can't get secret: err blah")
}

func TestJWT_Parse(t *testing.T) {
Expand Down Expand Up @@ -91,6 +100,14 @@ func TestJWT_Parse(t *testing.T) {
})
_, err = j.Parse(testJwtValid)
assert.NotNil(t, err, "bad token", "valid token parsed with wrong secret")

j = NewService(Opts{
SecretReader: SecretFunc(func(id string) (string, error) {
return "", errors.New("err blah")
}),
})
_, err = j.Parse(testJwtValid)
assert.EqualError(t, err, "can't get secret: err blah")
}

func TestJWT_Set(t *testing.T) {
Expand Down

0 comments on commit 8a9b27f

Please sign in to comment.