Browse Source

add webhook secret option (#187)

* add webhook hmac

* set default value for WhatsappWebhookSecret
pull/188/head
Davi Nomoeh Dani 1 year ago
committed by GitHub
parent
commit
6d368de9e7
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 10
      src/cmd/root.go
  2. 1
      src/config/settings.go
  3. 36
      src/pkg/whatsapp/whatsapp.go

10
src/cmd/root.go

@ -3,6 +3,11 @@ package cmd
import ( import (
"embed" "embed"
"fmt" "fmt"
"log"
"net/http"
"os"
"strings"
"github.com/aldinokemal/go-whatsapp-web-multidevice/config" "github.com/aldinokemal/go-whatsapp-web-multidevice/config"
"github.com/aldinokemal/go-whatsapp-web-multidevice/internal/rest" "github.com/aldinokemal/go-whatsapp-web-multidevice/internal/rest"
"github.com/aldinokemal/go-whatsapp-web-multidevice/internal/rest/helpers" "github.com/aldinokemal/go-whatsapp-web-multidevice/internal/rest/helpers"
@ -20,10 +25,6 @@ import (
"github.com/gofiber/template/html/v2" "github.com/gofiber/template/html/v2"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"log"
"net/http"
"os"
"strings"
) )
var ( var (
@ -47,6 +48,7 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&config.AppBasicAuthCredential, "basic-auth", "b", config.AppBasicAuthCredential, "basic auth credential | -b=yourUsername:yourPassword") rootCmd.PersistentFlags().StringVarP(&config.AppBasicAuthCredential, "basic-auth", "b", config.AppBasicAuthCredential, "basic auth credential | -b=yourUsername:yourPassword")
rootCmd.PersistentFlags().StringVarP(&config.WhatsappAutoReplyMessage, "autoreply", "", config.WhatsappAutoReplyMessage, `auto reply when received message --autoreply <string> | example: --autoreply="Don't reply this message"`) rootCmd.PersistentFlags().StringVarP(&config.WhatsappAutoReplyMessage, "autoreply", "", config.WhatsappAutoReplyMessage, `auto reply when received message --autoreply <string> | example: --autoreply="Don't reply this message"`)
rootCmd.PersistentFlags().StringVarP(&config.WhatsappWebhook, "webhook", "w", config.WhatsappWebhook, `forward event to webhook --webhook <string> | example: --webhook="https://yourcallback.com/callback"`) rootCmd.PersistentFlags().StringVarP(&config.WhatsappWebhook, "webhook", "w", config.WhatsappWebhook, `forward event to webhook --webhook <string> | example: --webhook="https://yourcallback.com/callback"`)
rootCmd.PersistentFlags().StringVarP(&config.WhatsappWebhookSecret, "webhook-secret", "", config.WhatsappWebhookSecret, `secure webhook request --webhook-secret <string> | example: --webhook-secret="super-secret-key"`)
rootCmd.PersistentFlags().BoolVarP(&config.WhatsappAccountValidation, "account-validation", "", config.WhatsappAccountValidation, `enable or disable account validation --account-validation <true/false> | example: --account-validation=true`) rootCmd.PersistentFlags().BoolVarP(&config.WhatsappAccountValidation, "account-validation", "", config.WhatsappAccountValidation, `enable or disable account validation --account-validation <true/false> | example: --account-validation=true`)
} }

1
src/config/settings.go

@ -21,6 +21,7 @@ var (
WhatsappAutoReplyMessage string WhatsappAutoReplyMessage string
WhatsappWebhook string WhatsappWebhook string
WhatsappWebhookSecret = "secret"
WhatsappLogLevel = "ERROR" WhatsappLogLevel = "ERROR"
WhatsappSettingMaxFileSize int64 = 50000000 // 50MB WhatsappSettingMaxFileSize int64 = 50000000 // 50MB
WhatsappSettingMaxVideoSize int64 = 100000000 // 100MB WhatsappSettingMaxVideoSize int64 = 100000000 // 100MB

36
src/pkg/whatsapp/whatsapp.go

@ -3,8 +3,19 @@ package whatsapp
import ( import (
"bytes" "bytes"
"context" "context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"mime"
"net/http"
"os"
"regexp"
"strings"
"sync/atomic"
"time"
"github.com/aldinokemal/go-whatsapp-web-multidevice/config" "github.com/aldinokemal/go-whatsapp-web-multidevice/config"
"github.com/aldinokemal/go-whatsapp-web-multidevice/internal/websocket" "github.com/aldinokemal/go-whatsapp-web-multidevice/internal/websocket"
pkgError "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/error" pkgError "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/error"
@ -19,13 +30,6 @@ import (
"go.mau.fi/whatsmeow/types/events" "go.mau.fi/whatsmeow/types/events"
waLog "go.mau.fi/whatsmeow/util/log" waLog "go.mau.fi/whatsmeow/util/log"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"mime"
"net/http"
"os"
"regexp"
"strings"
"sync/atomic"
"time"
) )
var ( var (
@ -299,6 +303,15 @@ func handler(rawEvt interface{}) {
} }
} }
func getMessageDigestOrSignature(msg, key []byte) (string, error) {
mac := hmac.New(sha256.New, key)
_, err := mac.Write(msg)
if err != nil {
return "", err
}
return hex.EncodeToString(mac.Sum(nil)), nil
}
// forwardToWebhook is a helper function to forward event to webhook url // forwardToWebhook is a helper function to forward event to webhook url
func forwardToWebhook(evt *events.Message) error { func forwardToWebhook(evt *events.Message) error {
logrus.Info("Forwarding event to webhook:", config.WhatsappWebhook) logrus.Info("Forwarding event to webhook:", config.WhatsappWebhook)
@ -400,7 +413,16 @@ func forwardToWebhook(evt *events.Message) error {
if err != nil { if err != nil {
return pkgError.WebhookError(fmt.Sprintf("error when create http object %v", err)) return pkgError.WebhookError(fmt.Sprintf("error when create http object %v", err))
} }
secretKey := []byte(config.WhatsappWebhookSecret)
signature, err := getMessageDigestOrSignature(postBody, secretKey)
if err != nil {
return pkgError.WebhookError(fmt.Sprintf("error when create signature %v", err))
}
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Hub-Signature-256", fmt.Sprintf("sha256=%s", signature))
if _, err = client.Do(req); err != nil { if _, err = client.Do(req); err != nil {
return pkgError.WebhookError(fmt.Sprintf("error when submit webhook %v", err)) return pkgError.WebhookError(fmt.Sprintf("error when submit webhook %v", err))
} }

Loading…
Cancel
Save