Skip to content
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

refector: add getSigningMethod #56

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions signature/jws/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions signature/jws/jws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
10 changes: 10 additions & 0 deletions signature/jws/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down