| 1 | package jwsscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 5 | "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/" |
| 6 | "syncscip-go gomod github.com/golang/go/src go1.24.0 sync/" |
| 7 | |
| 8 | "github.com/lestrrat-go/jwx/v3/jwascip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/" |
| 9 | ) |
| 10 | |
| 11 | // Signer2 is an interface that represents a per-signature algorithm signing |
| 12 | // operation. |
| 13 | type Signer2scip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/Signer2# interface { |
| 14 | Algorithmscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/Signer2#Algorithm.() jwascip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/.SignatureAlgorithmscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/SignatureAlgorithm# |
| 15 | |
| 16 | // Sign takes a key and a payload, and returns the signature for the payload. |
| 17 | // The key type is restricted by the signature algorithm that this |
| 18 | // signer is associated with. |
| 19 | // |
| 20 | // (Note to users of legacy Signer interface: the method signature |
| 21 | // is different from the legacy Signer interface) |
| 22 | Signscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/Signer2#Sign.(key any, payload []byte) ([]byte, error) |
| 23 | } |
| 24 | |
| 25 | var muSigner2DBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/muSigner2DB. syncscip-go gomod github.com/golang/go/src go1.24.0 sync/.RWMutexscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex# |
| 26 | var signer2DBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/signer2DB. = make(map[jwascip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/.SignatureAlgorithmscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/SignatureAlgorithm#]Signer2scip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/Signer2#) |
| 27 | |
| 28 | type SignerFactoryscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/SignerFactory# interface { |
| 29 | Createscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/SignerFactory#Create.() (Signerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/Signer#, error) |
| 30 | } |
| 31 | type SignerFactoryFnscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/SignerFactoryFn# func() (Signerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/Signer#, error) |
| 32 | |
| 33 | func (fn SignerFactoryFnscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/SignerFactoryFn#) Createscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/SignerFactoryFn#Create().() (Signerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/Signer#, error) { |
| 34 | return fn() |
| 35 | } |
| 36 | |
| 37 | func initscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/init().() { |
| 38 | // register the signers using jwsbb. These will be used by default. |
| 39 | for _, alg := range jwascip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/.SignatureAlgorithmsscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/SignatureAlgorithms().() { |
| 40 | if alg == jwascip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/.NoSignaturescip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/NoSignature().() { |
| 41 | continue |
| 42 | } |
| 43 | |
| 44 | if err := RegisterSignerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/RegisterSigner().(alg, defaultSignerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/defaultSigner#{algscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/defaultSigner#alg.: alg}); err != nil { |
| 45 | panic(fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("RegisterSigner failed: %v", err)) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // SignerFor returns a Signer2 for the given signature algorithm. |
| 51 | // |
| 52 | // Currently, this function will never fail. It will always return a |
| 53 | // valid Signer2 object. The heuristic is as follows: |
| 54 | // 1. If a Signer2 is registered for the given algorithm, it will return that. |
| 55 | // 2. If a legacy Signer(Factory) is registered for the given algorithm, it will |
| 56 | // return a Signer2 that wraps the legacy Signer. |
| 57 | // 3. If no Signer2 or legacy Signer(Factory) is registered, it will return a |
| 58 | // default signer that uses jwsbb.Sign. |
| 59 | // |
| 60 | // 1 and 2 will take care of 99% of the cases. The only time 3 will happen is |
| 61 | // when you are using a custom algorithm that is not supported out of the box. |
| 62 | // |
| 63 | // jwsbb.Sign knows how to handle a static set of algorithms, so if the |
| 64 | // algorithm is not supported, it will return an error when you call |
| 65 | // `Sign` on the default signer. |
| 66 | func SignerForscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/SignerFor().(alg jwascip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/.SignatureAlgorithmscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/SignatureAlgorithm#) (Signer2scip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/Signer2#, error) { |
| 67 | muSigner2DBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/muSigner2DB..RLockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#RLock().() |
| 68 | defer muSigner2DBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/muSigner2DB..RUnlockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#RUnlock().() |
| 69 | |
| 70 | signer, ok := signer2DBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/signer2DB.[alg] |
| 71 | if ok { |
| 72 | return signer, nil |
| 73 | } |
| 74 | |
| 75 | s1, err := legacySignerForscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/legacySignerFor().(alg) |
| 76 | if err == nil { |
| 77 | return signerAdapterscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/signerAdapter#{signerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/signerAdapter#signer.: s1}, nil |
| 78 | } |
| 79 | |
| 80 | return defaultSignerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/defaultSigner#{algscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/defaultSigner#alg.: alg}, nil |
| 81 | } |
| 82 | |
| 83 | var muSignerDBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/muSignerDB. syncscip-go gomod github.com/golang/go/src go1.24.0 sync/.RWMutexscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex# |
| 84 | var signerDBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/signerDB. = make(map[jwascip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/.SignatureAlgorithmscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/SignatureAlgorithm#]SignerFactoryscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/SignerFactory#) |
| 85 | |
| 86 | // RegisterSigner is used to register a signer for the given |
| 87 | // algorithm. |
| 88 | // |
| 89 | // Please note that this function is intended to be passed a |
| 90 | // signer object as its second argument, but due to historical |
| 91 | // reasons the function signature is defined as taking `any` type. |
| 92 | // |
| 93 | // You should create a signer object that implements the `Signer2` |
| 94 | // interface to register a signer, unless you have legacy code that |
| 95 | // plugged into the `SignerFactory` interface. |
| 96 | // |
| 97 | // Unlike the `UnregisterSigner` function, this function automatically |
| 98 | // calls `jwa.RegisterSignatureAlgorithm` to register the algorithm |
| 99 | // in this module's algorithm database. |
| 100 | // |
| 101 | // For backwards compatibility, this function also accepts |
| 102 | // `SignerFactory` implementations, but this usage is deprecated. |
| 103 | // You should use `Signer2` implementations instead. |
| 104 | // |
| 105 | // If you want to completely remove an algorithm, you must call |
| 106 | // `jwa.UnregisterSignatureAlgorithm` yourself after calling |
| 107 | // `UnregisterSigner`. |
| 108 | func RegisterSignerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/RegisterSigner().(alg jwascip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/.SignatureAlgorithmscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/SignatureAlgorithm#, f any) error { |
| 109 | jwascip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/.RegisterSignatureAlgorithmscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/RegisterSignatureAlgorithm().(alg) |
| 110 | switch s := f.(type) { |
| 111 | case Signer2scip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/Signer2#: |
| 112 | muSigner2DBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/muSigner2DB..Lockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#Lock().() |
| 113 | signer2DBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/signer2DB.[alg] = s |
| 114 | muSigner2DBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/muSigner2DB..Unlockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#Unlock().() |
| 115 | case SignerFactoryscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/SignerFactory#: |
| 116 | muSignerDBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/muSignerDB..Lockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#Lock().() |
| 117 | signerDBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/signerDB.[alg] = s |
| 118 | muSignerDBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/muSignerDB..Unlockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#Unlock().() |
| 119 | default: |
| 120 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().(`jws.RegisterSigner: unsupported type %T for algorithm %q`, f, alg) |
| 121 | } |
| 122 | return nil |
| 123 | } |
| 124 | |
| 125 | // UnregisterSigner removes the signer factory associated with |
| 126 | // the given algorithm, as well as the signer instance created |
| 127 | // by the factory. |
| 128 | // |
| 129 | // Note that when you call this function, the algorithm itself is |
| 130 | // not automatically unregistered from this module's algorithm database. |
| 131 | // This is because the algorithm may still be required for verification or |
| 132 | // some other operation (however unlikely, it is still possible). |
| 133 | // Therefore, in order to completely remove the algorithm, you must |
| 134 | // call `jwa.UnregisterSignatureAlgorithm` yourself. |
| 135 | func UnregisterSignerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/UnregisterSigner().(alg jwascip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/.SignatureAlgorithmscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/SignatureAlgorithm#) { |
| 136 | muSigner2DBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/muSigner2DB..Lockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#Lock().() |
| 137 | delete(signer2DBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/signer2DB., alg) |
| 138 | muSigner2DBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/muSigner2DB..Unlockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#Unlock().() |
| 139 | |
| 140 | muSignerDBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/muSignerDB..Lockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#Lock().() |
| 141 | delete(signerDBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/signerDB., alg) |
| 142 | muSignerDBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/muSignerDB..Unlockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#Unlock().() |
| 143 | // Remove previous signer |
| 144 | removeSignerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/removeSigner().(alg) |
| 145 | } |
| 146 | |
| 147 | // NewSigner creates a signer that signs payloads using the given signature algorithm. |
| 148 | // This function is deprecated, and will either be removed to re-purposed using |
| 149 | // a different signature. |
| 150 | // |
| 151 | // When you want to load a Signer object, you should use `SignerFor()` instead. |
| 152 | func NewSignerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/NewSigner().(alg jwascip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/.SignatureAlgorithmscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/SignatureAlgorithm#) (Signerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/Signer#, error) { |
| 153 | s, err := newLegacySignerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/newLegacySigner().(alg) |
| 154 | if err == nil { |
| 155 | return s, nil |
| 156 | } |
| 157 | |
| 158 | if stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.24.0 strings/HasPrefix().(err.Error(), `jws.NewSigner: unsupported signature algorithm`) { |
| 159 | // When newLegacySigner fails, automatically trigger to enable signers |
| 160 | enableLegacySignersOncescip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/enableLegacySignersOnce..Doscip-go gomod github.com/golang/go/src go1.24.0 sync/Once#Do().(enableLegacySignersscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/enableLegacySigners().) |
| 161 | return newLegacySignerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/newLegacySigner().(alg) |
| 162 | } |
| 163 | return nil, err |
| 164 | } |
| 165 | |
| 166 | func newLegacySignerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/newLegacySigner().(alg jwascip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/.SignatureAlgorithmscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/SignatureAlgorithm#) (Signerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/Signer#, error) { |
| 167 | muSignerDBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/muSignerDB..RLockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#RLock().() |
| 168 | f, ok := signerDBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/signerDB.[alg] |
| 169 | muSignerDBscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/muSignerDB..RUnlockscip-go gomod github.com/golang/go/src go1.24.0 sync/RWMutex#RUnlock().() |
| 170 | |
| 171 | if ok { |
| 172 | return f.Createscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/SignerFactory#Create.() |
| 173 | } |
| 174 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().(`jws.NewSigner: unsupported signature algorithm "%s"`, alg) |
| 175 | } |
| 176 | |
| 177 | type noneSignerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/noneSigner# struct{} |
| 178 | |
| 179 | func (noneSignerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/noneSigner#) Algorithmscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/noneSigner#Algorithm().() jwascip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/.SignatureAlgorithmscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/SignatureAlgorithm# { |
| 180 | return jwascip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/.NoSignaturescip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jwa`/NoSignature().() |
| 181 | } |
| 182 | |
| 183 | func (noneSignerscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/noneSigner#) Signscip-go gomod github.com/lestrrat-go/jwx/v3 . `github.com/lestrrat-go/jwx/v3/jws`/noneSigner#Sign().([]byte, any) ([]byte, error) { |
| 184 | return nil, nil |
| 185 | } |