-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement NewJwtSigner and CreateAttestation #579
base: master
Are you sure you want to change the base?
Changes from all commits
eb317a2
abb84ee
ddd21e6
03f1b59
4a2807b
643fe9d
5df1560
237d9f7
960a6b8
f7a29da
13fbc74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -254,3 +254,58 @@ func TestVerifyDetached(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestCreateDetachedSignature(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
key []byte | ||
alg SignatureAlgorithm | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "create rsa signature success", | ||
key: []byte(rsa2048PrivateKey), | ||
alg: RsaSignPkcs12048Sha256, | ||
expectedError: false, | ||
}, { | ||
name: "create ecdsa signature success", | ||
key: []byte(ec256PrivateKey), | ||
alg: EcdsaP256Sha256, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "rsa key with ecdsa alg", | ||
key: []byte(rsa2048PrivateKey), | ||
alg: EcdsaP256Sha256, | ||
expectedError: true, | ||
}, { | ||
name: "ecdsa key with rsa alg", | ||
key: []byte(ec256PrivateKey), | ||
alg: RsaSignPkcs12048Sha256, | ||
expectedError: true, | ||
}, { | ||
name: "unknown singature algorithm", | ||
key: []byte(rsa2048PrivateKey), | ||
alg: UnknownSigningAlgorithm, | ||
expectedError: true, | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
privKey, err := parsePkixPrivateKeyPem(tc.key) | ||
if err != nil { | ||
t.Fatalf("failed to parse private key %v", err) | ||
} | ||
_, err = createDetachedSignature(privKey, []byte(payload), tc.alg) | ||
if tc.expectedError { | ||
if err == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: spacing around = There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
t.Errorf("createDetachedSignature(...)=nil, expected non-nil") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit confusing, can be understood as signature is nil. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. discussed during sync. |
||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("createDetachedSignature(...)=%v, expected nil", err) | ||
} | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,10 +96,69 @@ func TestVerifyJWT(t *testing.T) { | |
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("Unexpected error: %e", err) | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
|
||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNewJwtSigner(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
key []byte | ||
publicKeyId string | ||
alg SignatureAlgorithm | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "new jwt signer success", | ||
key: []byte(rsa2048PrivateKey), | ||
publicKeyId: "kid", | ||
alg: RsaSignPkcs12048Sha256, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "new jwt signer with no key id success", | ||
key: []byte(rsa2048PrivateKey), | ||
publicKeyId: "", | ||
alg: RsaSignPkcs12048Sha256, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "new jwt signer with bad key fails", | ||
key: []byte("some-key"), | ||
publicKeyId: "", | ||
alg: RsaSignPkcs12048Sha256, | ||
expectedError: true, | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
_, err := NewJwtSigner(tc.key, tc.alg, tc.publicKeyId) | ||
if tc.expectedError { | ||
if err == nil { | ||
t.Errorf("NewJwtSigner(...)=nil, expected non nil") | ||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("NewJwtSigner(...)=%v, expected nil", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: spacing around = There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCreateJwtAttestation(t *testing.T) { | ||
signer, err := NewJwtSigner([]byte(rsa2048PrivateKey), RsaSignPkcs12048Sha256, "kid") | ||
if err != nil { | ||
t.Fatalf("failed to create signer") | ||
} | ||
attestation, err := signer.CreateAttestation([]byte(payload)) | ||
if err != nil { | ||
t.Errorf("CreateAttestation(..)=%v, expected nil", err) | ||
} else if attestation.PublicKeyID != "kid" { | ||
t.Errorf("attestation.PublicKeyID=%v, expected kid", attestation.PublicKeyID) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add two bad cases where alg does not match the key passed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.