Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add webhook secret option #187

Merged
merged 3 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package cmd
import (
"embed"
"fmt"
"log"
"net/http"
"os"
"strings"

"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/helpers"
Expand All @@ -20,10 +25,6 @@ import (
"github.com/gofiber/template/html/v2"
_ "github.com/mattn/go-sqlite3"
"github.com/spf13/cobra"
"log"
"net/http"
"os"
"strings"
)

var (
Expand All @@ -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.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.WhatsappWebhookSecret, "webhook-secret", "", config.WhatsappWebhookSecret, `secure webhook request --webhook-secret <string> | example: --webhook-secret="super-secret-key"`)
}

func runRest(_ *cobra.Command, _ []string) {
Expand Down
1 change: 1 addition & 0 deletions src/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (

WhatsappAutoReplyMessage string
WhatsappWebhook string
WhatsappWebhookSecret string
aldinokemal marked this conversation as resolved.
Show resolved Hide resolved
WhatsappLogLevel = "ERROR"
WhatsappSettingMaxFileSize int64 = 50000000 // 50MB
WhatsappSettingMaxVideoSize int64 = 100000000 // 100MB
Expand Down
42 changes: 35 additions & 7 deletions src/pkg/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@ package whatsapp
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"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/internal/websocket"
pkgError "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/error"
Expand All @@ -19,13 +30,6 @@ import (
"go.mau.fi/whatsmeow/types/events"
waLog "go.mau.fi/whatsmeow/util/log"
"google.golang.org/protobuf/proto"
"mime"
"net/http"
"os"
"regexp"
"strings"
"sync/atomic"
"time"
)

var (
Expand Down Expand Up @@ -297,6 +301,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
func forwardToWebhook(evt *events.Message) error {
logrus.Info("Forwarding event to webhook:", config.WhatsappWebhook)
Expand Down Expand Up @@ -398,7 +411,22 @@ func forwardToWebhook(evt *events.Message) error {
if err != nil {
return pkgError.WebhookError(fmt.Sprintf("error when create http object %v", err))
}

var secretKey []byte
if config.WhatsappWebhookSecret != "" {
secretKey = []byte(config.WhatsappWebhookSecret)
} else {
secretKey = []byte("anything")
aldinokemal marked this conversation as resolved.
Show resolved Hide resolved
}

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("X-Hub-Signature-256", fmt.Sprintf("sha256=%s", signature))
aldinokemal marked this conversation as resolved.
Show resolved Hide resolved

if _, err = client.Do(req); err != nil {
return pkgError.WebhookError(fmt.Sprintf("error when submit webhook %v", err))
}
Expand Down