Skip to content

Commit

Permalink
Merge pull request #13 from skynet2/rsa-jwk-support
Browse files Browse the repository at this point in the history
feat: add rsa support
  • Loading branch information
fqutishat authored Feb 19, 2024
2 parents 850e27c + fd0f63f commit f4260af
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 14 deletions.
33 changes: 27 additions & 6 deletions pkg/document/jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,42 @@ func (jwk JWK) Y() string {
return stringEntry(jwk["y"])
}

// N is n.
func (jwk JWK) N() string {
return stringEntry(jwk["n"])
}

// E is e.
func (jwk JWK) E() string {
return stringEntry(jwk["e"])
}

// Validate will validate JWK properties.
func (jwk JWK) Validate() error {
// TODO: validation of the JWK fields depends on the algorithm (issue-409)
// For now check required fields for currently supported algorithms secp256k1, P-256, P-384, P-512 and Ed25519

if jwk.Crv() == "" {
return errors.New("JWK crv is missing")
}

if jwk.Kty() == "" {
return errors.New("JWK kty is missing")
}

if jwk.X() == "" {
return errors.New("JWK x is missing")
if jwk.Kty() == "RSA" {
if jwk.N() == "" {
return errors.New("JWK n is missing")
}

if jwk.E() == "" {
return errors.New("JWK e is missing")
}
} else {
if jwk.Crv() == "" {
return errors.New("JWK crv is missing")
}

if jwk.X() == "" {
return errors.New("JWK x is missing")
}

}

return nil
Expand Down
21 changes: 21 additions & 0 deletions pkg/document/jwk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,25 @@ func TestValidate(t *testing.T) {
require.Error(t, err)
require.Contains(t, err.Error(), "JWK x is missing")
})

t.Run("missing n", func(t *testing.T) {
jwk := JWK{
"kty": "RSA",
}

err := jwk.Validate()
require.Error(t, err)
require.Contains(t, err.Error(), "JWK n is missing")
})

t.Run("missing e", func(t *testing.T) {
jwk := JWK{
"kty": "RSA",
"n": "nn",
}

err := jwk.Validate()
require.Error(t, err)
require.Contains(t, err.Error(), "JWK e is missing")
})
}
22 changes: 16 additions & 6 deletions pkg/jws/jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,26 @@ type JWK struct {

// Validate validates JWK.
func (jwk *JWK) Validate() error {
if jwk.Crv == "" {
return errors.New("JWK crv is missing")
}

if jwk.Kty == "" {
return errors.New("JWK kty is missing")
}

if jwk.X == "" {
return errors.New("JWK x is missing")
if jwk.Kty == "RSA" {
if jwk.N == "" {
return errors.New("JWK n is missing")
}

if jwk.E == "" {
return errors.New("JWK e is missing")
}
} else {
if jwk.Crv == "" {
return errors.New("JWK crv is missing")
}

if jwk.X == "" {
return errors.New("JWK x is missing")
}
}

return nil
Expand Down
21 changes: 21 additions & 0 deletions pkg/jws/jwk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,25 @@ func TestValidate(t *testing.T) {
require.Error(t, err)
require.Contains(t, err.Error(), "x is missing")
})

t.Run("missing n", func(t *testing.T) {
jwk := JWK{
Kty: "RSA",
}

err := jwk.Validate()
require.Error(t, err)
require.Contains(t, err.Error(), "n is missing")
})

t.Run("missing e", func(t *testing.T) {
jwk := JWK{
Kty: "RSA",
N: "something",
}

err := jwk.Validate()
require.Error(t, err)
require.Contains(t, err.Error(), "e is missing")
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestValidatePublicKeysErrors(t *testing.T) {

err = validatePublicKeys(doc.PublicKeys())
require.Error(t, err)
require.Contains(t, err.Error(), "JWK crv is missing")
require.Contains(t, err.Error(), "JWK kty is missing")
})

t.Run("pkB58 key with jwk type", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/versions/1_0/operationparser/recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestParseRecoverOperation(t *testing.T) {

op, err := parser.ParseRecoverOperation(request, false)
require.Error(t, err)
require.Contains(t, err.Error(), "validate signed data for recovery: signing key validation failed: JWK crv is missing")
require.Contains(t, err.Error(), "validate signed data for recovery: signing key validation failed: JWK kty is missing")
require.Nil(t, op)
})

Expand Down

0 comments on commit f4260af

Please sign in to comment.