func ModifyJWSHeaders(jws string, ctx ContextInterface, opts ...JWSHeaderOpt) (string, error) {
	if len(opts) == 0 {
		return jws, nil
	}

	// decode the headers
	segments := strings.Split(jws, ".")
	if len(segments) == 0 {
		return "", fmt.Errorf("failed to modify JWS: received invalid JWS as input")
	}

	// assuming b64 encoded body, meaning that this can't be used in tests prior 3.1.4 (!)
	b64Encoded := true
	body := []byte{}
	if len(segments) > 1 {
		body = []byte(segments[1])
	}

	headersB64Decoded, err := base64.RawURLEncoding.DecodeString(segments[0])
	if err != nil {
		return "", fmt.Errorf("failed to modify JWS: %w", err)
	}
	header := map[string]interface{}{}
	err = json.Unmarshal(headersB64Decoded, &header)
	if err != nil {
		return "", fmt.Errorf("failed to modify JWS: %w", err)
	}

	// apply mutators (header opts)
	for _, opt := range opts {
		header = opt(header)
	}

	// The new signature is using the alg specified in the token;
	// if alg is not set then the signature can't be created
	alg, ok := header["alg"].(string)
	if !ok {
		return "", fmt.Errorf("failed to modify JWS: signing alg is undefined, 'alg' key is not set")
	}

	signingMethod, err := GetSigningAlg(alg)
	if err != nil {
		return "", fmt.Errorf("failed to modify JWS: %w", err)
	}

	token := &jwt.Token{
		Header: header,
		Method: signingMethod,
	}

	// sign anew and return the produce
	cert, err := SigningCertFromContext(ctx)
	if err != nil {
		return "", fmt.Errorf("failed to modify JWS: %w", err)
	}

	privKey := cert.PrivateKey()
	tokenString, err := CreateSignature(token, privKey, string(body), b64Encoded)
	if err != nil {
		return "", fmt.Errorf("failed to modify JWS: %w", err)
	}
	return SplitJWSWithBody(tokenString), nil
}
// ModifyJWSHeaders allows the caller to mutate an existing JWS for testing purposes, re-signed with the new contents
ModifyJWSHeaders allows the caller to mutate an existing JWS for testing purposes, re-signed with the new contents
// ModifyJWSHeaders allows the caller to mutate an existing JWS for testing purposes re - signed with the new contents
func ModifyJWSHeaders(jws string, ctx ContextInterface, opts ...JWSHeaderOpt) (string, error) {
	if len(opts) == 0 {
		return jws, nil
	}
	segments := strings.Split(jws, ".")
	if len(segments) == 0 {
		return "", fmt.Errorf("failed to modify JWS: received invalid JWS as input")
	}
	b64Encoded := true
	body := []byte{}
	if len(segments) > 1 {
		body = []byte(segments[1])
	}
	headersB64Decoded, err := base64.RawURLEncoding.DecodeString(segments[0])
	if err != nil {
		return "", fmt.Errorf("failed to modify JWS: %w", err)
	}
	header := map[string]interface{}{}
	err = json.Unmarshal(headersB64Decoded, &header)
	if err != nil {
		return "", fmt.Errorf("failed to modify JWS: %w", err)
	}
	for _, opt := range opts {
		header = opt(header)
	}
	alg, ok := header["alg"].(string)
	if !ok {
		return "", fmt.Errorf("failed to modify JWS: signing alg is undefined, 'alg' key is not set")
	}
	signingMethod, err := GetSigningAlg(alg)
	if err != nil {
		return "", fmt.Errorf("failed to modify JWS: %w", err)
	}
	token := &jwt.Token{
		Header: header,
		Method: signingMethod,
	}
	cert, err := SigningCertFromContext(ctx)
	if err != nil {
		return "", fmt.Errorf("failed to modify JWS: %w", err)
	}
	privKey := cert.PrivateKey()
	tokenString, err := CreateSignature(token, privKey, string(body), b64Encoded)
	if err != nil {
		return "", fmt.Errorf("failed to modify JWS: %w", err)
	}
	return SplitJWSWithBody(tokenString), nil
}
func ModifyJWSHeaders ( jws string , ctx ContextInterface , opts ... JWSHeaderOpt ) ( string , error ) { if len ( opts ) == 0 { return jws , nil } segments := strings . Split ( jws , " " ) if len ( segments ) == 0 { return " " , fmt . Errorf ( " " ) } b64Encoded := true body := [ ] byte { } if len ( segments ) > 1 { body = [ ] byte ( segments [ 1 ] ) } headersB64Decoded , err := base64 . RawURLEncoding . DecodeString ( segments [ 0 ] ) if err != nil { return " " , fmt . Errorf ( " " , err ) } header := map [ string ] interface { } { } err = json . Unmarshal ( headersB64Decoded , & header ) if err != nil { return " " , fmt . Errorf ( " " , err ) } for _ , opt := range opts { header = opt ( header ) } alg , ok := header [ " " ] . ( string ) if ! ok { return " " , fmt . Errorf ( " " ) } signingMethod , err := GetSigningAlg ( alg ) if err != nil { return " " , fmt . Errorf ( " " , err ) } token := & jwt . Token { Header : header , Method : signingMethod , } cert , err := SigningCertFromContext ( ctx ) if err != nil { return " " , fmt . Errorf ( " " , err ) } privKey := cert . PrivateKey ( ) tokenString , err := CreateSignature ( token , privKey , string ( body ) , b64Encoded ) if err != nil { return " " , fmt . Errorf ( " " , err ) } return SplitJWSWithBody ( tokenString ) , nil }
ModifyJWSHeaders allows the caller to mutate an existing JWS for testing purposes, re-signed with the new contents
ModifyJWSHeaders allows the caller to mutate an existing JWS for testing purposes re - signed with the new contents
// decode the headers // assuming b64 encoded body, meaning that this can't be used in tests prior 3.1.4 (!) // apply mutators (header opts) // The new signature is using the alg specified in the token; // if alg is not set then the signature can't be created // sign anew and return the produce