Skip to content

Commit

Permalink
Remove signer instance
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Nov 18, 2023
1 parent 1a4e1ed commit 3d63537
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions jws/jws.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func (s *payloadSigner) PublicHeader() Headers {
var signers = make(map[jwa.SignatureAlgorithm]Signer)
var muSigner = &sync.Mutex{}

func removeSigner(alg jwa.SignatureAlgorithm) {
muSigner.Lock()
defer muSigner.Unlock()
delete(signers, alg)
}

func makeSigner(alg jwa.SignatureAlgorithm, key interface{}, public, protected Headers) (*payloadSigner, error) {
muSigner.Lock()
signer, ok := signers[alg]
Expand Down
13 changes: 13 additions & 0 deletions jws/jws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,19 @@ func TestGH910(t *testing.T) {
require.NoError(t, err, `jws.Verify should succeed`)

require.Equal(t, src, string(verified), `verified payload should match`)

jws.UnregisterSigner(sha256Algo)

// Now try after unregistering the signer for the algorithm
_, err = jws.Sign([]byte(src), jws.WithKey(sha256Algo, nil))
require.Error(t, err, `jws.Sign should succeed`)

jws.RegisterSigner(sha256Algo, jws.SignerFactoryFn(func() (jws.Signer, error) {
return s256SignerVerifier{}, nil
}))

_, err = jws.Sign([]byte(src), jws.WithKey(sha256Algo, nil))
require.NoError(t, err, `jws.Sign should succeed`)
}

func TestUnpaddedSignatureR(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions jws/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func RegisterSigner(alg jwa.SignatureAlgorithm, f SignerFactory) {
muSignerDB.Lock()
signerDB[alg] = f
muSignerDB.Unlock()

// Remove previous signer, if there was one
removeSigner(alg)
}

// UnregisterSigner removes the signer factory associated with
Expand All @@ -49,6 +52,8 @@ func UnregisterSigner(alg jwa.SignatureAlgorithm) {
muSignerDB.Lock()
delete(signerDB, alg)
muSignerDB.Unlock()
// Remove previous signer
removeSigner(alg)
}

func init() {
Expand Down

0 comments on commit 3d63537

Please sign in to comment.