-
Notifications
You must be signed in to change notification settings - Fork 35
/
server.go
82 lines (63 loc) · 1.92 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package httpserver
import (
"bytes"
"context"
"encoding/hex"
"io"
"net/http"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/log"
)
type (
// EventHandlerFunc is used to handle events from Discord's Outgoing Webhooks
EventHandlerFunc func(responseFunc RespondFunc, event gateway.EventInteractionCreate)
// RespondFunc is used to respond to Discord's Outgoing Webhooks
RespondFunc func(response discord.InteractionResponse) error
)
// Server is used for receiving Discord's interactions via Outgoing Webhooks
type Server interface {
// Logger returns the logger used by the Server
Logger() log.Logger
// PublicKey returns the public key used by the Server
PublicKey() PublicKey
// Start starts the Server
Start()
// Close closes the Server
Close(ctx context.Context)
// Handle passes a payload to the Server for processing
Handle(respondFunc RespondFunc, event gateway.EventInteractionCreate)
}
// VerifyRequest implements the verification side of the discord interactions api signing algorithm, as documented here: https://discord.com/developers/docs/interactions/slash-commands#security-and-authorization
// Credit: https://github.com/bsdlp/discord-interactions-go/blob/main/interactions/verify.go
func VerifyRequest(r *http.Request, key PublicKey) bool {
var msg bytes.Buffer
signature := r.Header.Get("X-Signature-Ed25519")
if signature == "" {
return false
}
sig, err := hex.DecodeString(signature)
if err != nil {
return false
}
if len(sig) != SignatureSize || sig[63]&224 != 0 {
return false
}
timestamp := r.Header.Get("X-Signature-Timestamp")
if timestamp == "" {
return false
}
msg.WriteString(timestamp)
defer func() {
_ = r.Body.Close()
}()
var body bytes.Buffer
defer func() {
r.Body = io.NopCloser(&body)
}()
_, err = io.Copy(&msg, io.TeeReader(r.Body, &body))
if err != nil {
return false
}
return Verify(key, msg.Bytes(), sig)
}