2021-11-26 13:49:12 +01:00
|
|
|
package signer
|
|
|
|
|
|
|
|
|
|
import (
|
2022-12-03 07:10:57 +05:30
|
|
|
"context"
|
2021-11-26 13:49:12 +01:00
|
|
|
"errors"
|
|
|
|
|
"net/http"
|
2023-01-24 20:50:20 +05:30
|
|
|
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/types"
|
2021-11-26 13:49:12 +01:00
|
|
|
)
|
|
|
|
|
|
2022-12-03 07:10:57 +05:30
|
|
|
// An Argument that can be passed to Signer
|
|
|
|
|
type SignerArg string
|
|
|
|
|
|
2021-11-26 13:49:12 +01:00
|
|
|
type Signer interface {
|
2022-12-03 07:10:57 +05:30
|
|
|
SignHTTP(ctx context.Context, request *http.Request) error
|
2021-11-26 13:49:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SignerArgs interface {
|
|
|
|
|
Validate() error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewSigner(args SignerArgs) (signer Signer, err error) {
|
|
|
|
|
switch signerArgs := args.(type) {
|
2022-12-03 07:10:57 +05:30
|
|
|
case *AWSOptions:
|
2021-11-26 13:49:12 +01:00
|
|
|
awsSigner, err := NewAwsSigner(signerArgs)
|
|
|
|
|
if err != nil {
|
2022-12-03 07:10:57 +05:30
|
|
|
awsSigner, err = NewAwsSignerFromConfig(signerArgs)
|
2021-11-26 13:49:12 +01:00
|
|
|
if err != nil {
|
2022-12-03 07:10:57 +05:30
|
|
|
return nil, err
|
2021-11-26 13:49:12 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return awsSigner, err
|
|
|
|
|
default:
|
|
|
|
|
return nil, errors.New("unknown signature arguments type")
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-24 20:50:20 +05:30
|
|
|
|
|
|
|
|
// GetCtxWithArgs creates and returns context with signature args
|
|
|
|
|
func GetCtxWithArgs(maps ...map[string]interface{}) context.Context {
|
|
|
|
|
var region, service string
|
|
|
|
|
for _, v := range maps {
|
|
|
|
|
for key, val := range v {
|
|
|
|
|
if key == "region" && region == "" {
|
|
|
|
|
region = types.ToString(val)
|
|
|
|
|
}
|
|
|
|
|
if key == "service" && service == "" {
|
|
|
|
|
service = types.ToString(val)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// type ctxkey string
|
|
|
|
|
ctx := context.WithValue(context.Background(), SignerArg("service"), service)
|
|
|
|
|
return context.WithValue(ctx, SignerArg("region"), region)
|
|
|
|
|
}
|