|
| 1 | +package hmac_auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/hmac" |
| 6 | + "crypto/md5" |
| 7 | + "crypto/sha1" |
| 8 | + "crypto/sha256" |
| 9 | + "crypto/sha512" |
| 10 | + "crypto/subtle" |
| 11 | + "encoding/base64" |
| 12 | + "encoding/hex" |
| 13 | + "errors" |
| 14 | + "fmt" |
| 15 | + "hash" |
| 16 | + |
| 17 | + "github.com/getkin/kin-openapi/openapi3" |
| 18 | + "github.com/webhookx-io/webhookx/db/entities" |
| 19 | + "github.com/webhookx-io/webhookx/pkg/http/response" |
| 20 | + "github.com/webhookx-io/webhookx/pkg/plugin" |
| 21 | +) |
| 22 | + |
| 23 | +var ( |
| 24 | + ErrInvalidHashMethod = errors.New("invalid hash method") |
| 25 | + ErrInvalidEncodingMethod = errors.New("invalid encoding method") |
| 26 | +) |
| 27 | + |
| 28 | +var hashes = map[string]func() hash.Hash{ |
| 29 | + "md5": md5.New, |
| 30 | + "sha-1": sha1.New, |
| 31 | + "sha-256": sha256.New, |
| 32 | + "sha-512": sha512.New, |
| 33 | +} |
| 34 | + |
| 35 | +func Hmac(algorithm string, key string, data string) []byte { |
| 36 | + fn, ok := hashes[algorithm] |
| 37 | + if !ok { |
| 38 | + panic(fmt.Errorf("%w: %s", ErrInvalidHashMethod, algorithm)) |
| 39 | + } |
| 40 | + h := hmac.New(fn, []byte(key)) |
| 41 | + h.Write([]byte(data)) |
| 42 | + return h.Sum(nil) |
| 43 | +} |
| 44 | + |
| 45 | +func encode(encoding string, data []byte) string { |
| 46 | + switch encoding { |
| 47 | + case "hex": |
| 48 | + return hex.EncodeToString(data) |
| 49 | + case "base64": |
| 50 | + return base64.StdEncoding.EncodeToString(data) |
| 51 | + case "base64url": |
| 52 | + return base64.RawURLEncoding.EncodeToString(data) |
| 53 | + default: |
| 54 | + panic(fmt.Errorf("%w: %s", ErrInvalidEncodingMethod, encoding)) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +type Config struct { |
| 59 | + Hash string `json:"hash"` |
| 60 | + Encoding string `json:"encoding"` |
| 61 | + SignatureHeader string `json:"signature_header"` |
| 62 | + Secret string `json:"secret"` |
| 63 | +} |
| 64 | + |
| 65 | +func (c Config) Schema() *openapi3.Schema { |
| 66 | + return entities.LookupSchema("HmacAuthPluginConfiguration") |
| 67 | +} |
| 68 | + |
| 69 | +type HmacAuthPlugin struct { |
| 70 | + plugin.BasePlugin[Config] |
| 71 | +} |
| 72 | + |
| 73 | +func (p *HmacAuthPlugin) Name() string { |
| 74 | + return "hmac-auth" |
| 75 | +} |
| 76 | + |
| 77 | +func (p *HmacAuthPlugin) ExecuteInbound(ctx context.Context, inbound *plugin.Inbound) (result plugin.InboundResult, err error) { |
| 78 | + matched := false |
| 79 | + signature := inbound.Request.Header.Get(p.Config.SignatureHeader) |
| 80 | + if len(signature) > 0 { |
| 81 | + bytes := Hmac(p.Config.Hash, p.Config.Secret, string(inbound.RawBody)) |
| 82 | + expectedSignature := encode(p.Config.Encoding, bytes) |
| 83 | + matched = subtle.ConstantTimeCompare([]byte(signature), []byte(expectedSignature)) == 1 |
| 84 | + } |
| 85 | + |
| 86 | + if !matched { |
| 87 | + response.JSON(inbound.Response, 401, `{"message":"Unauthorized"}`) |
| 88 | + result.Terminated = true |
| 89 | + } |
| 90 | + result.Payload = inbound.RawBody |
| 91 | + |
| 92 | + return |
| 93 | +} |
0 commit comments