diff --git a/signature/jws/envelope.go b/signature/jws/envelope.go index 3dbb1e17..970f9b28 100644 --- a/signature/jws/envelope.go +++ b/signature/jws/envelope.go @@ -45,24 +45,14 @@ func ParseEnvelope(envelopeBytes []byte) (signature.Envelope, error) { // Sign signs the envelope and return the encoded message func (e *envelope) Sign(req *signature.SignRequest) ([]byte, error) { - // check signer type - var ( - method signingMethod - err error - ) - if localSigner, ok := req.Signer.(signature.LocalSigner); ok { - // for local signer - method, err = newLocalSigningMethod(localSigner) - } else { - // for remote signer - method, err = newRemoteSigningMethod(req.Signer) - } + // get signingMethod for JWT package + method, err := getSigningMethod(req.Signer) if err != nil { return nil, &signature.MalformedSignRequestError{Msg: err.Error()} } // get all attributes ready to be signed - signedAttrs, err := getSignedAttrs(req, method.Alg()) + signedAttrs, err := getSignedAttributes(req, method.Alg()) if err != nil { return nil, err } diff --git a/signature/jws/jws.go b/signature/jws/jws.go index 8afb9baf..03cd4d66 100644 --- a/signature/jws/jws.go +++ b/signature/jws/jws.go @@ -176,8 +176,8 @@ func generateJWS(compact string, req *signature.SignRequest, certs []*x509.Certi }, nil } -// getSignerAttrs merge extended signed attributes and protected header to be signed attributes -func getSignedAttrs(req *signature.SignRequest, algorithm string) (map[string]interface{}, error) { +// getSignerAttributes merge extended signed attributes and protected header to be signed attributes +func getSignedAttributes(req *signature.SignRequest, algorithm string) (map[string]interface{}, error) { extAttrs := make(map[string]interface{}) crit := []string{headerKeySigningScheme} diff --git a/signature/jws/jwt.go b/signature/jws/jwt.go index a0c9f3c2..f9463e85 100644 --- a/signature/jws/jwt.go +++ b/signature/jws/jwt.go @@ -107,6 +107,16 @@ func (s *localSigningMethod) PrivateKey() crypto.PrivateKey { return s.signer.PrivateKey() } +// getSigningMethod return signingMethod for the given signer +func getSigningMethod(signer signature.Signer) (signingMethod, error) { + if localSigner, ok := signer.(signature.LocalSigner); ok { + // for local signer + return newLocalSigningMethod(localSigner) + } + // for remote signer + return newRemoteSigningMethod(signer) +} + // verifyJWT verifies the JWT token against the specified verification key func verifyJWT(tokenString string, publicKey interface{}) error { parser := jwt.NewParser(