Skip to content

Commit

Permalink
Wrap errors idiomatically (Thanks @Blquinn )
Browse files Browse the repository at this point in the history
  • Loading branch information
monde committed Feb 17, 2022
1 parent 4519c6d commit ee6b3cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion adaptors/lestrratGoJwx/lestrratGoJwx.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (lgj *LestrratGoJwx) Decode(jwt string, jwkUri string) (interface{}, error)

var claims interface{}
if err := json.Unmarshal(token, &claims); err != nil {
return nil, fmt.Errorf("could not unmarshal claims: %s", err.Error())
return nil, fmt.Errorf("could not unmarshal claims: %w", err)
}

return claims, nil
Expand Down
28 changes: 14 additions & 14 deletions jwtverifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Jwt struct {
func fetchMetaData(url string) (interface{}, error) {
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("request for metadata was not successful: %s", err.Error())
return nil, fmt.Errorf("request for metadata was not successful: %w", err)
}
defer resp.Body.Close()

Expand Down Expand Up @@ -103,7 +103,7 @@ func (j *JwtVerifier) SetLeeway(duration string) {
func (j *JwtVerifier) VerifyAccessToken(jwt string) (*Jwt, error) {
validJwt, err := j.isValidJwt(jwt)
if !validJwt {
return nil, fmt.Errorf("token is not valid: %s", err.Error())
return nil, fmt.Errorf("token is not valid: %w", err)
}

resp, err := j.decodeJwt(jwt)
Expand All @@ -119,27 +119,27 @@ func (j *JwtVerifier) VerifyAccessToken(jwt string) (*Jwt, error) {

err = j.validateIss(token["iss"])
if err != nil {
return &myJwt, fmt.Errorf("the `Issuer` was not able to be validated. %s", err.Error())
return &myJwt, fmt.Errorf("the `Issuer` was not able to be validated. %w", err)
}

err = j.validateAudience(token["aud"])
if err != nil {
return &myJwt, fmt.Errorf("the `Audience` was not able to be validated. %s", err.Error())
return &myJwt, fmt.Errorf("the `Audience` was not able to be validated. %w", err)
}

err = j.validateClientId(token["cid"])
if err != nil {
return &myJwt, fmt.Errorf("the `Client Id` was not able to be validated. %s", err.Error())
return &myJwt, fmt.Errorf("the `Client Id` was not able to be validated. %w", err)
}

err = j.validateExp(token["exp"])
if err != nil {
return &myJwt, fmt.Errorf("the `Expiration` was not able to be validated. %s", err.Error())
return &myJwt, fmt.Errorf("the `Expiration` was not able to be validated. %w", err)
}

err = j.validateIat(token["iat"])
if err != nil {
return &myJwt, fmt.Errorf("the `Issued At` was not able to be validated. %s", err.Error())
return &myJwt, fmt.Errorf("the `Issued At` was not able to be validated. %w", err)
}

return &myJwt, nil
Expand All @@ -156,7 +156,7 @@ func (j *JwtVerifier) decodeJwt(jwt string) (interface{}, error) {
}
resp, err := j.Adaptor.Decode(jwt, jwksURI)
if err != nil {
return nil, fmt.Errorf("could not decode token: %s", err.Error())
return nil, fmt.Errorf("could not decode token: %w", err)
}

return resp, nil
Expand All @@ -165,7 +165,7 @@ func (j *JwtVerifier) decodeJwt(jwt string) (interface{}, error) {
func (j *JwtVerifier) VerifyIdToken(jwt string) (*Jwt, error) {
validJwt, err := j.isValidJwt(jwt)
if !validJwt {
return nil, fmt.Errorf("token is not valid: %s", err.Error())
return nil, fmt.Errorf("token is not valid: %w", err)
}

resp, err := j.decodeJwt(jwt)
Expand All @@ -181,27 +181,27 @@ func (j *JwtVerifier) VerifyIdToken(jwt string) (*Jwt, error) {

err = j.validateIss(token["iss"])
if err != nil {
return &myJwt, fmt.Errorf("the `Issuer` was not able to be validated. %s", err.Error())
return &myJwt, fmt.Errorf("the `Issuer` was not able to be validated. %w", err)
}

err = j.validateAudience(token["aud"])
if err != nil {
return &myJwt, fmt.Errorf("the `Audience` was not able to be validated. %s", err.Error())
return &myJwt, fmt.Errorf("the `Audience` was not able to be validated. %w", err)
}

err = j.validateExp(token["exp"])
if err != nil {
return &myJwt, fmt.Errorf("the `Expiration` was not able to be validated. %s", err.Error())
return &myJwt, fmt.Errorf("the `Expiration` was not able to be validated. %w", err)
}

err = j.validateIat(token["iat"])
if err != nil {
return &myJwt, fmt.Errorf("the `Issued At` was not able to be validated. %s", err.Error())
return &myJwt, fmt.Errorf("the `Issued At` was not able to be validated. %w", err)
}

err = j.validateNonce(token["nonce"])
if err != nil {
return &myJwt, fmt.Errorf("the `Nonce` was not able to be validated. %s", err.Error())
return &myJwt, fmt.Errorf("the `Nonce` was not able to be validated. %w", err)
}

return &myJwt, nil
Expand Down

0 comments on commit ee6b3cf

Please sign in to comment.