Skip to content

Commit

Permalink
Optimized issue validation
Browse files Browse the repository at this point in the history
  • Loading branch information
corbadoman committed Sep 30, 2024
1 parent c9321f3 commit bd6b832
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions internal/services/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,43 @@ func (i *Impl) ValidateToken(shortSession string) (*entities.User, error) {
}
}

return nil, validationerror.New(err.Error(), code)
return nil, newValidationError(err.Error(), shortSession, code)
}

claims := token.Claims.(*entities.Claims)
if claims.Issuer != i.Config.JWTIssuer {
return nil, validationerror.New(
fmt.Sprintf("JWT issuer mismatch (configured: '%s', actual JWT: '%s')", i.Config.JWTIssuer, claims.Issuer),
validationerror.CodeJWTIssuerMismatch,
)
if err := i.validateIssuer(claims.Issuer, shortSession); err != nil {
return nil, err
}

return &entities.User{
UserID: claims.Subject,
FullName: claims.Name,
}, nil
}

func (i *Impl) validateIssuer(jwtIssuer string, shortSession string) error {
// Compare to old Frontend API (without .cloud.) to make our Frontend API host name change downwards compatible
if jwtIssuer == fmt.Sprintf("https://%s.frontendapi.corbado.io", i.Config.ProjectID) {
return nil
}

// Compare to new Frontend API (with .cloud.)
if jwtIssuer == fmt.Sprintf("https://%s.frontendapi.cloud.corbado.io", i.Config.ProjectID) {
return nil
}

// Compare to configured issuer (from FrontendAPI), needed if you set a CNAME for example
if jwtIssuer != i.Config.JWTIssuer {
return newValidationError(
fmt.Sprintf("JWT issuer mismatch (configured trough FrontendAPI: '%s', JWT issuer: '%s')", i.Config.JWTIssuer, jwtIssuer),
shortSession,
validationerror.CodeJWTIssuerMismatch,
)
}

return nil
}

func newValidationError(message string, jwt string, code validationerror.Code) error {
return validationerror.New(fmt.Sprintf("JWT validation failed: '%s' (JWT: '%s')", message, jwt), code)
}

0 comments on commit bd6b832

Please sign in to comment.