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