mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
e43ef0a979
Earlier this evening I tried to run the Go [modernize](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize) analyzer on OPA. That didn't go as planned: - https://github.com/golang/go/issues/73661 - https://github.com/golang/go/issues/73663 While we wait for that to be fixed, I figured an old-fashioned search-and-replace across the repo may work for at least the `interface{}` to `any` conversion. That should help make it easier to see the other fixes as applied by the modernize tool once it has had those issues resolved. Signed-off-by: Anders Eknert <anders@styra.com>
57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package verify
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/open-policy-agent/opa/internal/jwx/jwa"
|
|
)
|
|
|
|
// New creates a new JWS verifier using the specified algorithm
|
|
// and the public key
|
|
func New(alg jwa.SignatureAlgorithm) (Verifier, error) {
|
|
switch alg {
|
|
case jwa.RS256, jwa.RS384, jwa.RS512, jwa.PS256, jwa.PS384, jwa.PS512:
|
|
return newRSA(alg)
|
|
case jwa.ES256, jwa.ES384, jwa.ES512:
|
|
return newECDSA(alg)
|
|
case jwa.HS256, jwa.HS384, jwa.HS512:
|
|
return newHMAC(alg)
|
|
default:
|
|
return nil, fmt.Errorf(`unsupported signature algorithm: %s`, alg)
|
|
}
|
|
}
|
|
|
|
// GetSigningKey returns a *rsa.PublicKey or *ecdsa.PublicKey typically encoded in PEM blocks of type "PUBLIC KEY",
|
|
// for RSA and ECDSA family of algorithms.
|
|
// For HMAC family, it return a []byte value
|
|
func GetSigningKey(key string, alg jwa.SignatureAlgorithm) (any, error) {
|
|
switch alg {
|
|
case jwa.RS256, jwa.RS384, jwa.RS512, jwa.PS256, jwa.PS384, jwa.PS512, jwa.ES256, jwa.ES384, jwa.ES512:
|
|
block, _ := pem.Decode([]byte(key))
|
|
if block == nil {
|
|
return nil, errors.New("failed to parse PEM block containing the key")
|
|
}
|
|
|
|
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch pub := pub.(type) {
|
|
case *rsa.PublicKey, *ecdsa.PublicKey:
|
|
return pub, nil
|
|
default:
|
|
return nil, fmt.Errorf("invalid key type %T", pub)
|
|
}
|
|
case jwa.HS256, jwa.HS384, jwa.HS512:
|
|
return []byte(key), nil
|
|
default:
|
|
return nil, fmt.Errorf("unsupported signature algorithm: %s", alg)
|
|
}
|
|
}
|