github.com/lestrrat-go/jwx/v2@v2.0.21/jwa/signature_gen.go (about)

     1  // Code generated by tools/cmd/genjwa/main.go. DO NOT EDIT.
     2  
     3  package jwa
     4  
     5  import (
     6  	"fmt"
     7  	"sort"
     8  	"sync"
     9  )
    10  
    11  // SignatureAlgorithm represents the various signature algorithms as described in https://tools.ietf.org/html/rfc7518#section-3.1
    12  type SignatureAlgorithm string
    13  
    14  // Supported values for SignatureAlgorithm
    15  const (
    16  	ES256       SignatureAlgorithm = "ES256"  // ECDSA using P-256 and SHA-256
    17  	ES256K      SignatureAlgorithm = "ES256K" // ECDSA using secp256k1 and SHA-256
    18  	ES384       SignatureAlgorithm = "ES384"  // ECDSA using P-384 and SHA-384
    19  	ES512       SignatureAlgorithm = "ES512"  // ECDSA using P-521 and SHA-512
    20  	EdDSA       SignatureAlgorithm = "EdDSA"  // EdDSA signature algorithms
    21  	HS256       SignatureAlgorithm = "HS256"  // HMAC using SHA-256
    22  	HS384       SignatureAlgorithm = "HS384"  // HMAC using SHA-384
    23  	HS512       SignatureAlgorithm = "HS512"  // HMAC using SHA-512
    24  	NoSignature SignatureAlgorithm = "none"
    25  	PS256       SignatureAlgorithm = "PS256" // RSASSA-PSS using SHA256 and MGF1-SHA256
    26  	PS384       SignatureAlgorithm = "PS384" // RSASSA-PSS using SHA384 and MGF1-SHA384
    27  	PS512       SignatureAlgorithm = "PS512" // RSASSA-PSS using SHA512 and MGF1-SHA512
    28  	RS256       SignatureAlgorithm = "RS256" // RSASSA-PKCS-v1.5 using SHA-256
    29  	RS384       SignatureAlgorithm = "RS384" // RSASSA-PKCS-v1.5 using SHA-384
    30  	RS512       SignatureAlgorithm = "RS512" // RSASSA-PKCS-v1.5 using SHA-512
    31  )
    32  
    33  var muSignatureAlgorithms sync.RWMutex
    34  var allSignatureAlgorithms map[SignatureAlgorithm]struct{}
    35  var listSignatureAlgorithm []SignatureAlgorithm
    36  
    37  func init() {
    38  	muSignatureAlgorithms.Lock()
    39  	defer muSignatureAlgorithms.Unlock()
    40  	allSignatureAlgorithms = make(map[SignatureAlgorithm]struct{})
    41  	allSignatureAlgorithms[ES256] = struct{}{}
    42  	allSignatureAlgorithms[ES256K] = struct{}{}
    43  	allSignatureAlgorithms[ES384] = struct{}{}
    44  	allSignatureAlgorithms[ES512] = struct{}{}
    45  	allSignatureAlgorithms[EdDSA] = struct{}{}
    46  	allSignatureAlgorithms[HS256] = struct{}{}
    47  	allSignatureAlgorithms[HS384] = struct{}{}
    48  	allSignatureAlgorithms[HS512] = struct{}{}
    49  	allSignatureAlgorithms[NoSignature] = struct{}{}
    50  	allSignatureAlgorithms[PS256] = struct{}{}
    51  	allSignatureAlgorithms[PS384] = struct{}{}
    52  	allSignatureAlgorithms[PS512] = struct{}{}
    53  	allSignatureAlgorithms[RS256] = struct{}{}
    54  	allSignatureAlgorithms[RS384] = struct{}{}
    55  	allSignatureAlgorithms[RS512] = struct{}{}
    56  	rebuildSignatureAlgorithm()
    57  }
    58  
    59  // RegisterSignatureAlgorithm registers a new SignatureAlgorithm so that the jwx can properly handle the new value.
    60  // Duplicates will silently be ignored
    61  func RegisterSignatureAlgorithm(v SignatureAlgorithm) {
    62  	muSignatureAlgorithms.Lock()
    63  	defer muSignatureAlgorithms.Unlock()
    64  	if _, ok := allSignatureAlgorithms[v]; !ok {
    65  		allSignatureAlgorithms[v] = struct{}{}
    66  		rebuildSignatureAlgorithm()
    67  	}
    68  }
    69  
    70  // UnregisterSignatureAlgorithm unregisters a SignatureAlgorithm from its known database.
    71  // Non-existentn entries will silently be ignored
    72  func UnregisterSignatureAlgorithm(v SignatureAlgorithm) {
    73  	muSignatureAlgorithms.Lock()
    74  	defer muSignatureAlgorithms.Unlock()
    75  	if _, ok := allSignatureAlgorithms[v]; ok {
    76  		delete(allSignatureAlgorithms, v)
    77  		rebuildSignatureAlgorithm()
    78  	}
    79  }
    80  
    81  func rebuildSignatureAlgorithm() {
    82  	listSignatureAlgorithm = make([]SignatureAlgorithm, 0, len(allSignatureAlgorithms))
    83  	for v := range allSignatureAlgorithms {
    84  		listSignatureAlgorithm = append(listSignatureAlgorithm, v)
    85  	}
    86  	sort.Slice(listSignatureAlgorithm, func(i, j int) bool {
    87  		return string(listSignatureAlgorithm[i]) < string(listSignatureAlgorithm[j])
    88  	})
    89  }
    90  
    91  // SignatureAlgorithms returns a list of all available values for SignatureAlgorithm
    92  func SignatureAlgorithms() []SignatureAlgorithm {
    93  	muSignatureAlgorithms.RLock()
    94  	defer muSignatureAlgorithms.RUnlock()
    95  	return listSignatureAlgorithm
    96  }
    97  
    98  // Accept is used when conversion from values given by
    99  // outside sources (such as JSON payloads) is required
   100  func (v *SignatureAlgorithm) Accept(value interface{}) error {
   101  	var tmp SignatureAlgorithm
   102  	if x, ok := value.(SignatureAlgorithm); ok {
   103  		tmp = x
   104  	} else {
   105  		var s string
   106  		switch x := value.(type) {
   107  		case fmt.Stringer:
   108  			s = x.String()
   109  		case string:
   110  			s = x
   111  		default:
   112  			return fmt.Errorf(`invalid type for jwa.SignatureAlgorithm: %T`, value)
   113  		}
   114  		tmp = SignatureAlgorithm(s)
   115  	}
   116  	if _, ok := allSignatureAlgorithms[tmp]; !ok {
   117  		return fmt.Errorf(`invalid jwa.SignatureAlgorithm value`)
   118  	}
   119  
   120  	*v = tmp
   121  	return nil
   122  }
   123  
   124  // String returns the string representation of a SignatureAlgorithm
   125  func (v SignatureAlgorithm) String() string {
   126  	return string(v)
   127  }