2022-08-22 09:18:00 +00:00
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/ed25519"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2023-09-19 15:25:48 +00:00
|
|
|
"errors"
|
2023-09-19 15:23:25 +00:00
|
|
|
"fmt"
|
2022-08-22 09:18:00 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/diamondburned/arikawa/v3/api"
|
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
|
|
)
|
|
|
|
|
|
|
|
func writeError(w http.ResponseWriter, code int, err error) {
|
|
|
|
var resp struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
resp.Error = err.Error()
|
|
|
|
} else {
|
|
|
|
resp.Error = http.StatusText(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln("cannot marshal error response:", err)
|
|
|
|
}
|
|
|
|
|
2022-08-22 10:21:12 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(code)
|
2022-08-22 09:18:00 +00:00
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InteractionHandler is a type whose method is called on every incoming event.
|
|
|
|
type InteractionHandler interface {
|
|
|
|
// HandleInteraction is expected to return a response synchronously, either
|
|
|
|
// to be followed-up later by deferring the response or to be responded
|
|
|
|
// immediately.
|
|
|
|
HandleInteraction(*discord.InteractionEvent) *api.InteractionResponse
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:42:29 +00:00
|
|
|
// InteractionHandlerFunc is a function type that implements the interface.
|
|
|
|
type InteractionHandlerFunc func(*discord.InteractionEvent) *api.InteractionResponse
|
|
|
|
|
|
|
|
var _ InteractionHandler = InteractionHandlerFunc(nil)
|
|
|
|
|
|
|
|
func (f InteractionHandlerFunc) HandleInteraction(ev *discord.InteractionEvent) *api.InteractionResponse {
|
|
|
|
return f(ev)
|
|
|
|
}
|
|
|
|
|
2022-08-22 09:18:00 +00:00
|
|
|
type alwaysDeferInteraction struct {
|
|
|
|
f func(*discord.InteractionEvent)
|
|
|
|
flags discord.MessageFlags
|
|
|
|
}
|
|
|
|
|
|
|
|
// AlwaysDeferInteraction always returns a DeferredMessageInteractionWithSource
|
|
|
|
// then invokes f in the background. This allows f to always use the follow-up
|
|
|
|
// functions.
|
|
|
|
func AlwaysDeferInteraction(flags discord.MessageFlags, f func(*discord.InteractionEvent)) InteractionHandler {
|
|
|
|
return alwaysDeferInteraction{f, flags}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f alwaysDeferInteraction) HandleInteraction(ev *discord.InteractionEvent) *api.InteractionResponse {
|
|
|
|
go f.f(ev)
|
|
|
|
return &api.InteractionResponse{
|
|
|
|
Type: api.DeferredMessageInteractionWithSource,
|
|
|
|
Data: &api.InteractionResponseData{
|
|
|
|
Flags: f.flags,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InteractionErrorFunc is called to write an error. err may be nil with a
|
|
|
|
// non-2xx code.
|
|
|
|
type InteractionErrorFunc func(w http.ResponseWriter, r *http.Request, code int, err error)
|
|
|
|
|
|
|
|
// InteractionServer provides a HTTP handler to verify and handle Interaction
|
|
|
|
// Create events sent by Discord into a HTTP endpoint..
|
|
|
|
type InteractionServer struct {
|
|
|
|
ErrorFunc InteractionErrorFunc
|
|
|
|
|
|
|
|
interactionHandler InteractionHandler
|
|
|
|
httpHandler http.Handler
|
|
|
|
pubkey ed25519.PublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewInteractionServer creates a new InteractionServer instance. pubkey should
|
|
|
|
// be hex-encoded.
|
|
|
|
func NewInteractionServer(pubkey string, handler InteractionHandler) (*InteractionServer, error) {
|
|
|
|
pubkeyB, err := hex.DecodeString(pubkey)
|
|
|
|
if err != nil {
|
2023-09-19 15:23:25 +00:00
|
|
|
return nil, fmt.Errorf("cannot decode hex pubkey: %w", err)
|
2022-08-22 09:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s := InteractionServer{
|
|
|
|
ErrorFunc: func(w http.ResponseWriter, r *http.Request, code int, err error) {
|
|
|
|
writeError(w, code, err)
|
|
|
|
},
|
|
|
|
interactionHandler: handler,
|
|
|
|
httpHandler: nil,
|
|
|
|
pubkey: pubkeyB,
|
|
|
|
}
|
|
|
|
|
|
|
|
s.httpHandler = http.HandlerFunc(s.handle)
|
2022-10-12 01:01:31 +00:00
|
|
|
if len(s.pubkey) != 0 {
|
|
|
|
s.httpHandler = s.withVerification(s.httpHandler)
|
|
|
|
}
|
2022-08-22 09:18:00 +00:00
|
|
|
|
|
|
|
return &s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP implements http.Handler.
|
|
|
|
func (s *InteractionServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2022-10-12 01:01:31 +00:00
|
|
|
s.httpHandler.ServeHTTP(w, r)
|
2022-08-22 09:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *InteractionServer) handle(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "POST":
|
|
|
|
var ev discord.InteractionEvent
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&ev); err != nil {
|
2023-09-19 15:23:25 +00:00
|
|
|
s.ErrorFunc(w, r, 400, fmt.Errorf("cannot decode interaction : %w", err))
|
2022-08-22 09:18:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ev.Data.(type) {
|
|
|
|
case *discord.PingInteraction:
|
2022-08-22 19:37:18 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2022-08-22 09:18:00 +00:00
|
|
|
json.NewEncoder(w).Encode(api.InteractionResponse{
|
|
|
|
Type: api.PongInteraction,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := s.interactionHandler.HandleInteraction(&ev)
|
|
|
|
if resp != nil && resp.Type != api.PongInteraction {
|
|
|
|
if resp.NeedsMultipart() {
|
|
|
|
body := multipart.NewWriter(w)
|
|
|
|
w.Header().Set("Content-Type", body.FormDataContentType())
|
|
|
|
resp.WriteMultipart(body)
|
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
s.ErrorFunc(w, r, http.StatusMethodNotAllowed, errors.New("method not allowed"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// withVerification was written thanks to @bsdlp and their code
|
|
|
|
// https://github.com/bsdlp/discord-interactions-go/blob/a2ba844/interactions/verify_example_test.go#L63.
|
|
|
|
func (s *InteractionServer) withVerification(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
signature := r.Header.Get("X-Signature-Ed25519")
|
|
|
|
if signature == "" {
|
|
|
|
s.ErrorFunc(w, r, 401, errors.New("missing header X-Signature-Ed25519"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sig, err := hex.DecodeString(signature)
|
|
|
|
if err != nil {
|
2023-09-19 15:23:25 +00:00
|
|
|
s.ErrorFunc(w, r, 400, fmt.Errorf("X-Signature-Ed25519 is not valid hex-encoded: %w", err))
|
2022-08-22 09:18:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sig) != ed25519.SignatureSize || sig[63]&224 != 0 {
|
|
|
|
s.ErrorFunc(w, r, 400, errors.New("invalid X-Signature-Ed25519 data"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
timestamp := r.Header.Get("X-Signature-Timestamp")
|
|
|
|
if timestamp == "" {
|
|
|
|
s.ErrorFunc(w, r, 401, errors.New("missing header X-Signature-Timestamp"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg bytes.Buffer
|
|
|
|
msg.Grow(int(r.ContentLength+1) + len(timestamp))
|
|
|
|
msg.WriteString(timestamp)
|
|
|
|
|
|
|
|
if _, err := io.Copy(&msg, r.Body); err != nil {
|
2023-09-19 15:23:25 +00:00
|
|
|
s.ErrorFunc(w, r, 500, fmt.Errorf("cannot read body: %w", err))
|
2022-08-22 09:18:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ed25519.Verify(s.pubkey, msg.Bytes(), sig) {
|
|
|
|
s.ErrorFunc(w, r, 401, errors.New("signature mismatch"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the request body for use.
|
|
|
|
body := msg.Bytes()[len(timestamp):]
|
|
|
|
r.Body = io.NopCloser(bytes.NewReader(body))
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|