github.com/lestrrat-go/jwx/v2@v2.0.21/jwa/elliptic_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 // EllipticCurveAlgorithm represents the algorithms used for EC keys 12 type EllipticCurveAlgorithm string 13 14 // Supported values for EllipticCurveAlgorithm 15 const ( 16 Ed25519 EllipticCurveAlgorithm = "Ed25519" 17 Ed448 EllipticCurveAlgorithm = "Ed448" 18 InvalidEllipticCurve EllipticCurveAlgorithm = "P-invalid" 19 P256 EllipticCurveAlgorithm = "P-256" 20 P384 EllipticCurveAlgorithm = "P-384" 21 P521 EllipticCurveAlgorithm = "P-521" 22 X25519 EllipticCurveAlgorithm = "X25519" 23 X448 EllipticCurveAlgorithm = "X448" 24 ) 25 26 var muEllipticCurveAlgorithms sync.RWMutex 27 var allEllipticCurveAlgorithms map[EllipticCurveAlgorithm]struct{} 28 var listEllipticCurveAlgorithm []EllipticCurveAlgorithm 29 30 func init() { 31 muEllipticCurveAlgorithms.Lock() 32 defer muEllipticCurveAlgorithms.Unlock() 33 allEllipticCurveAlgorithms = make(map[EllipticCurveAlgorithm]struct{}) 34 allEllipticCurveAlgorithms[Ed25519] = struct{}{} 35 allEllipticCurveAlgorithms[Ed448] = struct{}{} 36 allEllipticCurveAlgorithms[P256] = struct{}{} 37 allEllipticCurveAlgorithms[P384] = struct{}{} 38 allEllipticCurveAlgorithms[P521] = struct{}{} 39 allEllipticCurveAlgorithms[X25519] = struct{}{} 40 allEllipticCurveAlgorithms[X448] = struct{}{} 41 rebuildEllipticCurveAlgorithm() 42 } 43 44 // RegisterEllipticCurveAlgorithm registers a new EllipticCurveAlgorithm so that the jwx can properly handle the new value. 45 // Duplicates will silently be ignored 46 func RegisterEllipticCurveAlgorithm(v EllipticCurveAlgorithm) { 47 muEllipticCurveAlgorithms.Lock() 48 defer muEllipticCurveAlgorithms.Unlock() 49 if _, ok := allEllipticCurveAlgorithms[v]; !ok { 50 allEllipticCurveAlgorithms[v] = struct{}{} 51 rebuildEllipticCurveAlgorithm() 52 } 53 } 54 55 // UnregisterEllipticCurveAlgorithm unregisters a EllipticCurveAlgorithm from its known database. 56 // Non-existentn entries will silently be ignored 57 func UnregisterEllipticCurveAlgorithm(v EllipticCurveAlgorithm) { 58 muEllipticCurveAlgorithms.Lock() 59 defer muEllipticCurveAlgorithms.Unlock() 60 if _, ok := allEllipticCurveAlgorithms[v]; ok { 61 delete(allEllipticCurveAlgorithms, v) 62 rebuildEllipticCurveAlgorithm() 63 } 64 } 65 66 func rebuildEllipticCurveAlgorithm() { 67 listEllipticCurveAlgorithm = make([]EllipticCurveAlgorithm, 0, len(allEllipticCurveAlgorithms)) 68 for v := range allEllipticCurveAlgorithms { 69 listEllipticCurveAlgorithm = append(listEllipticCurveAlgorithm, v) 70 } 71 sort.Slice(listEllipticCurveAlgorithm, func(i, j int) bool { 72 return string(listEllipticCurveAlgorithm[i]) < string(listEllipticCurveAlgorithm[j]) 73 }) 74 } 75 76 // EllipticCurveAlgorithms returns a list of all available values for EllipticCurveAlgorithm 77 func EllipticCurveAlgorithms() []EllipticCurveAlgorithm { 78 muEllipticCurveAlgorithms.RLock() 79 defer muEllipticCurveAlgorithms.RUnlock() 80 return listEllipticCurveAlgorithm 81 } 82 83 // Accept is used when conversion from values given by 84 // outside sources (such as JSON payloads) is required 85 func (v *EllipticCurveAlgorithm) Accept(value interface{}) error { 86 var tmp EllipticCurveAlgorithm 87 if x, ok := value.(EllipticCurveAlgorithm); ok { 88 tmp = x 89 } else { 90 var s string 91 switch x := value.(type) { 92 case fmt.Stringer: 93 s = x.String() 94 case string: 95 s = x 96 default: 97 return fmt.Errorf(`invalid type for jwa.EllipticCurveAlgorithm: %T`, value) 98 } 99 tmp = EllipticCurveAlgorithm(s) 100 } 101 if _, ok := allEllipticCurveAlgorithms[tmp]; !ok { 102 return fmt.Errorf(`invalid jwa.EllipticCurveAlgorithm value`) 103 } 104 105 *v = tmp 106 return nil 107 } 108 109 // String returns the string representation of a EllipticCurveAlgorithm 110 func (v EllipticCurveAlgorithm) String() string { 111 return string(v) 112 }