-
Notifications
You must be signed in to change notification settings - Fork 36
/
server.go
71 lines (56 loc) · 1.54 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
package httpserver
import (
"bytes"
"context"
"encoding/hex"
"io"
"io/ioutil"
"net/http"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/log"
)
type EventHandlerFunc func(responseChannel chan<- discord.InteractionResponse, payload io.Reader)
// Server is used for receiving an Interaction over httpserver
type Server interface {
Logger() log.Logger
PublicKey() PublicKey
EventHandlerFunc() EventHandlerFunc
Start()
Close(ctx context.Context)
}
// 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(logger log.Logger, 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() {
err = r.Body.Close()
if err != nil {
logger.Error("error while closing request body: ", err)
}
}()
var body bytes.Buffer
defer func() {
r.Body = ioutil.NopCloser(&body)
}()
_, err = io.Copy(&msg, io.TeeReader(r.Body, &body))
if err != nil {
return false
}
return Verify(key, msg.Bytes(), sig)
}