github.com/nats-io/jwt/v2@v2.5.6/v1compat/validation.go (about)

     1  /*
     2   * Copyright 2018 The NATS Authors
     3   * Licensed under the Apache License, Version 2.0 (the "License");
     4   * you may not use this file except in compliance with the License.
     5   * You may obtain a copy of the License at
     6   *
     7   * http://www.apache.org/licenses/LICENSE-2.0
     8   *
     9   * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15  
    16  package jwt
    17  
    18  import (
    19  	"errors"
    20  	"fmt"
    21  )
    22  
    23  // ValidationIssue represents an issue during JWT validation, it may or may not be a blocking error
    24  type ValidationIssue struct {
    25  	Description string
    26  	Blocking    bool
    27  	TimeCheck   bool
    28  }
    29  
    30  func (ve *ValidationIssue) Error() string {
    31  	return ve.Description
    32  }
    33  
    34  // ValidationResults is a list of ValidationIssue pointers
    35  type ValidationResults struct {
    36  	Issues []*ValidationIssue
    37  }
    38  
    39  // CreateValidationResults creates an empty list of validation issues
    40  func CreateValidationResults() *ValidationResults {
    41  	issues := []*ValidationIssue{}
    42  	return &ValidationResults{
    43  		Issues: issues,
    44  	}
    45  }
    46  
    47  // Add appends an issue to the list
    48  func (v *ValidationResults) Add(vi *ValidationIssue) {
    49  	v.Issues = append(v.Issues, vi)
    50  }
    51  
    52  // AddError creates a new validation error and adds it to the list
    53  func (v *ValidationResults) AddError(format string, args ...interface{}) {
    54  	v.Add(&ValidationIssue{
    55  		Description: fmt.Sprintf(format, args...),
    56  		Blocking:    true,
    57  		TimeCheck:   false,
    58  	})
    59  }
    60  
    61  // AddTimeCheck creates a new validation issue related to a time check and adds it to the list
    62  func (v *ValidationResults) AddTimeCheck(format string, args ...interface{}) {
    63  	v.Add(&ValidationIssue{
    64  		Description: fmt.Sprintf(format, args...),
    65  		Blocking:    false,
    66  		TimeCheck:   true,
    67  	})
    68  }
    69  
    70  // AddWarning creates a new validation warning and adds it to the list
    71  func (v *ValidationResults) AddWarning(format string, args ...interface{}) {
    72  	v.Add(&ValidationIssue{
    73  		Description: fmt.Sprintf(format, args...),
    74  		Blocking:    false,
    75  		TimeCheck:   false,
    76  	})
    77  }
    78  
    79  // IsBlocking returns true if the list contains a blocking error
    80  func (v *ValidationResults) IsBlocking(includeTimeChecks bool) bool {
    81  	for _, i := range v.Issues {
    82  		if i.Blocking {
    83  			return true
    84  		}
    85  
    86  		if includeTimeChecks && i.TimeCheck {
    87  			return true
    88  		}
    89  	}
    90  	return false
    91  }
    92  
    93  // IsEmpty returns true if the list is empty
    94  func (v *ValidationResults) IsEmpty() bool {
    95  	return len(v.Issues) == 0
    96  }
    97  
    98  // Errors returns only blocking issues as errors
    99  func (v *ValidationResults) Errors() []error {
   100  	var errs []error
   101  	for _, v := range v.Issues {
   102  		if v.Blocking {
   103  			errs = append(errs, errors.New(v.Description))
   104  		}
   105  	}
   106  	return errs
   107  }
   108  
   109  // Warnings returns only non blocking issues as strings
   110  func (v *ValidationResults) Warnings() []string {
   111  	var errs []string
   112  	for _, v := range v.Issues {
   113  		if !v.Blocking {
   114  			errs = append(errs, v.Description)
   115  		}
   116  	}
   117  	return errs
   118  }