Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Commit 0c02049

Browse files
authored
Merge pull request #16 from Zaaksam/master
add `Algorithm.DecodeAndValidate` function
2 parents 859ddbc + 49f767f commit 0c02049

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

algorithms.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,32 @@ func (a *Algorithm) Decode(encoded string) (*Claims, error) {
110110

111111
// Validate verifies a tokens validity. It returns nil if it is valid, and an error if invalid.
112112
func (a *Algorithm) Validate(encoded string) error {
113-
claims, err := a.Decode(encoded)
113+
_, err := a.DecodeAndValidate(encoded)
114+
return err
115+
}
116+
117+
// DecodeAndValidate returns a map representing the token's claims, and it's valid.
118+
func (a *Algorithm) DecodeAndValidate(encoded string) (claims *Claims, err error) {
119+
claims, err = a.Decode(encoded)
114120
if err != nil {
115-
return err
121+
return
116122
}
117123

118-
if err := a.validateSignature(encoded); err != nil {
119-
return errors.Wrap(err, "failed to validate signature")
124+
if err = a.validateSignature(encoded); err != nil {
125+
err = errors.Wrap(err, "failed to validate signature")
126+
return
120127
}
121128

122-
if err := a.validateExp(claims); err != nil {
123-
return errors.Wrap(err, "failed to validate exp")
129+
if err = a.validateExp(claims); err != nil {
130+
err = errors.Wrap(err, "failed to validate exp")
131+
return
124132
}
125133

126-
if err := a.validateNbf(claims); err != nil {
127-
return errors.Wrap(err, "failed to validate nbf")
134+
if err = a.validateNbf(claims); err != nil {
135+
err = errors.Wrap(err, "failed to validate nbf")
128136
}
129137

130-
return nil
138+
return
131139
}
132140

133141
func (a *Algorithm) validateSignature(encoded string) error {
@@ -152,7 +160,6 @@ func (a *Algorithm) validateSignature(encoded string) error {
152160
return nil
153161
}
154162

155-
156163
func (a *Algorithm) validateExp(claims *Claims) error {
157164
if claims.HasClaim("exp") {
158165
exp, err := claims.GetTime("exp")

0 commit comments

Comments
 (0)