@@ -28,13 +28,12 @@ type ClaimsValidator interface {
2828 Validate () error
2929}
3030
31- // validator is the core of the new Validation API. It is automatically used by
31+ // Validator is the core of the new Validation API. It is automatically used by
3232// a [Parser] during parsing and can be modified with various parser options.
3333//
34- // Note: This struct is intentionally not exported (yet) as we want to
35- // internally finalize its API. In the future, we might make it publicly
36- // available.
37- type validator struct {
34+ // The [NewValidator] function should be used to create an instance of this
35+ // struct.
36+ type Validator struct {
3837 // leeway is an optional leeway that can be provided to account for clock skew.
3938 leeway time.Duration
4039
@@ -65,16 +64,28 @@ type validator struct {
6564 expectedSub string
6665}
6766
68- // newValidator can be used to create a stand-alone validator with the supplied
67+ // NewValidator can be used to create a stand-alone validator with the supplied
6968// options. This validator can then be used to validate already parsed claims.
70- func newValidator (opts ... ParserOption ) * validator {
69+ //
70+ // Note: Under normal circumstances, explicitly creating a validator is not
71+ // needed and can potentially be dangerous; instead functions of the [Parser]
72+ // class should be used.
73+ //
74+ // The [Validator] is only checking the *validity* of the claims, such as its
75+ // expiration time, but it does NOT perform *signature verification* of the
76+ // token.
77+ func NewValidator (opts ... ParserOption ) * Validator {
7178 p := NewParser (opts ... )
7279 return p .validator
7380}
7481
7582// Validate validates the given claims. It will also perform any custom
7683// validation if claims implements the [ClaimsValidator] interface.
77- func (v * validator ) Validate (claims Claims ) error {
84+ //
85+ // Note: It will NOT perform any *signature verification* on the token that
86+ // contains the claims and expects that the [Claim] was already successfully
87+ // verified.
88+ func (v * Validator ) Validate (claims Claims ) error {
7889 var (
7990 now time.Time
8091 errs []error = make ([]error , 0 , 6 )
@@ -153,7 +164,7 @@ func (v *validator) Validate(claims Claims) error {
153164//
154165// Additionally, if any error occurs while retrieving the claim, e.g., when its
155166// the wrong type, an ErrTokenUnverifiable error will be returned.
156- func (v * validator ) verifyExpiresAt (claims Claims , cmp time.Time , required bool ) error {
167+ func (v * Validator ) verifyExpiresAt (claims Claims , cmp time.Time , required bool ) error {
157168 exp , err := claims .GetExpirationTime ()
158169 if err != nil {
159170 return err
@@ -174,7 +185,7 @@ func (v *validator) verifyExpiresAt(claims Claims, cmp time.Time, required bool)
174185//
175186// Additionally, if any error occurs while retrieving the claim, e.g., when its
176187// the wrong type, an ErrTokenUnverifiable error will be returned.
177- func (v * validator ) verifyIssuedAt (claims Claims , cmp time.Time , required bool ) error {
188+ func (v * Validator ) verifyIssuedAt (claims Claims , cmp time.Time , required bool ) error {
178189 iat , err := claims .GetIssuedAt ()
179190 if err != nil {
180191 return err
@@ -195,7 +206,7 @@ func (v *validator) verifyIssuedAt(claims Claims, cmp time.Time, required bool)
195206//
196207// Additionally, if any error occurs while retrieving the claim, e.g., when its
197208// the wrong type, an ErrTokenUnverifiable error will be returned.
198- func (v * validator ) verifyNotBefore (claims Claims , cmp time.Time , required bool ) error {
209+ func (v * Validator ) verifyNotBefore (claims Claims , cmp time.Time , required bool ) error {
199210 nbf , err := claims .GetNotBefore ()
200211 if err != nil {
201212 return err
@@ -215,7 +226,7 @@ func (v *validator) verifyNotBefore(claims Claims, cmp time.Time, required bool)
215226//
216227// Additionally, if any error occurs while retrieving the claim, e.g., when its
217228// the wrong type, an ErrTokenUnverifiable error will be returned.
218- func (v * validator ) verifyAudience (claims Claims , cmp string , required bool ) error {
229+ func (v * Validator ) verifyAudience (claims Claims , cmp string , required bool ) error {
219230 aud , err := claims .GetAudience ()
220231 if err != nil {
221232 return err
@@ -251,7 +262,7 @@ func (v *validator) verifyAudience(claims Claims, cmp string, required bool) err
251262//
252263// Additionally, if any error occurs while retrieving the claim, e.g., when its
253264// the wrong type, an ErrTokenUnverifiable error will be returned.
254- func (v * validator ) verifyIssuer (claims Claims , cmp string , required bool ) error {
265+ func (v * Validator ) verifyIssuer (claims Claims , cmp string , required bool ) error {
255266 iss , err := claims .GetIssuer ()
256267 if err != nil {
257268 return err
@@ -271,7 +282,7 @@ func (v *validator) verifyIssuer(claims Claims, cmp string, required bool) error
271282//
272283// Additionally, if any error occurs while retrieving the claim, e.g., when its
273284// the wrong type, an ErrTokenUnverifiable error will be returned.
274- func (v * validator ) verifySubject (claims Claims , cmp string , required bool ) error {
285+ func (v * Validator ) verifySubject (claims Claims , cmp string , required bool ) error {
275286 sub , err := claims .GetSubject ()
276287 if err != nil {
277288 return err
0 commit comments