123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package discordgo
- import (
- "bytes"
- "crypto/ed25519"
- "encoding/hex"
- "io"
- "io/ioutil"
- "net/http"
- )
- func VerifyInteraction(r *http.Request, key ed25519.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) != ed25519.SignatureSize {
- return false
- }
- timestamp := r.Header.Get("X-Signature-Timestamp")
- if timestamp == "" {
- return false
- }
- msg.WriteString(timestamp)
- defer r.Body.Close()
- 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 ed25519.Verify(key, msg.Bytes(), sig)
- }
|