@@ -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
@@ -62,16 +61,28 @@ type validator struct {
6261 expectedSub string
6362}
6463
65- // newValidator can be used to create a stand-alone validator with the supplied
64+ // NewValidator can be used to create a stand-alone validator with the supplied
6665// options. This validator can then be used to validate already parsed claims.
67- func newValidator (opts ... ParserOption ) * validator {
66+ //
67+ // Note: Under normal circumstances, explicitly creating a validator is not
68+ // needed and can potentially be dangerous; instead functions of the [Parser]
69+ // class should be used.
70+ //
71+ // The [Validator] is only checking the *validity* of the claims, such as its
72+ // expiration time, but it does NOT perform *signature verification* of the
73+ // token.
74+ func NewValidator (opts ... ParserOption ) * Validator {
6875 p := NewParser (opts ... )
6976 return p .validator
7077}
7178
7279// Validate validates the given claims. It will also perform any custom
7380// validation if claims implements the [ClaimsValidator] interface.
74- func (v * validator ) Validate (claims Claims ) error {
81+ //
82+ // Note: It will NOT perform any *signature verification* on the token that
83+ // contains the claims and expects that the [Claim] was already successfully
84+ // verified.
85+ func (v * Validator ) Validate (claims Claims ) error {
7586 var (
7687 now time.Time
7788 errs []error = make ([]error , 0 , 6 )
@@ -149,7 +160,7 @@ func (v *validator) Validate(claims Claims) error {
149160//
150161// Additionally, if any error occurs while retrieving the claim, e.g., when its
151162// the wrong type, an ErrTokenUnverifiable error will be returned.
152- func (v * validator ) verifyExpiresAt (claims Claims , cmp time.Time , required bool ) error {
163+ func (v * Validator ) verifyExpiresAt (claims Claims , cmp time.Time , required bool ) error {
153164 exp , err := claims .GetExpirationTime ()
154165 if err != nil {
155166 return err
@@ -170,7 +181,7 @@ func (v *validator) verifyExpiresAt(claims Claims, cmp time.Time, required bool)
170181//
171182// Additionally, if any error occurs while retrieving the claim, e.g., when its
172183// the wrong type, an ErrTokenUnverifiable error will be returned.
173- func (v * validator ) verifyIssuedAt (claims Claims , cmp time.Time , required bool ) error {
184+ func (v * Validator ) verifyIssuedAt (claims Claims , cmp time.Time , required bool ) error {
174185 iat , err := claims .GetIssuedAt ()
175186 if err != nil {
176187 return err
@@ -191,7 +202,7 @@ func (v *validator) verifyIssuedAt(claims Claims, cmp time.Time, required bool)
191202//
192203// Additionally, if any error occurs while retrieving the claim, e.g., when its
193204// the wrong type, an ErrTokenUnverifiable error will be returned.
194- func (v * validator ) verifyNotBefore (claims Claims , cmp time.Time , required bool ) error {
205+ func (v * Validator ) verifyNotBefore (claims Claims , cmp time.Time , required bool ) error {
195206 nbf , err := claims .GetNotBefore ()
196207 if err != nil {
197208 return err
@@ -211,7 +222,7 @@ func (v *validator) verifyNotBefore(claims Claims, cmp time.Time, required bool)
211222//
212223// Additionally, if any error occurs while retrieving the claim, e.g., when its
213224// the wrong type, an ErrTokenUnverifiable error will be returned.
214- func (v * validator ) verifyAudience (claims Claims , cmp string , required bool ) error {
225+ func (v * Validator ) verifyAudience (claims Claims , cmp string , required bool ) error {
215226 aud , err := claims .GetAudience ()
216227 if err != nil {
217228 return err
@@ -247,7 +258,7 @@ func (v *validator) verifyAudience(claims Claims, cmp string, required bool) err
247258//
248259// Additionally, if any error occurs while retrieving the claim, e.g., when its
249260// the wrong type, an ErrTokenUnverifiable error will be returned.
250- func (v * validator ) verifyIssuer (claims Claims , cmp string , required bool ) error {
261+ func (v * Validator ) verifyIssuer (claims Claims , cmp string , required bool ) error {
251262 iss , err := claims .GetIssuer ()
252263 if err != nil {
253264 return err
@@ -267,7 +278,7 @@ func (v *validator) verifyIssuer(claims Claims, cmp string, required bool) error
267278//
268279// Additionally, if any error occurs while retrieving the claim, e.g., when its
269280// the wrong type, an ErrTokenUnverifiable error will be returned.
270- func (v * validator ) verifySubject (claims Claims , cmp string , required bool ) error {
281+ func (v * Validator ) verifySubject (claims Claims , cmp string , required bool ) error {
271282 sub , err := claims .GetSubject ()
272283 if err != nil {
273284 return err
0 commit comments