1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package discordgo
- import (
- "bytes"
- "crypto/ed25519"
- "encoding/hex"
- "net/http/httptest"
- "strconv"
- "strings"
- "testing"
- "time"
- )
- func TestVerifyInteraction(t *testing.T) {
- pubkey, privkey, err := ed25519.GenerateKey(nil)
- if err != nil {
- t.Errorf("error generating signing keypair: %s", err)
- }
- timestamp := "1608597133"
- t.Run("success", func(t *testing.T) {
- body := "body"
- request := httptest.NewRequest("POST", "http://localhost/interaction", strings.NewReader(body))
- request.Header.Set("X-Signature-Timestamp", timestamp)
- var msg bytes.Buffer
- msg.WriteString(timestamp)
- msg.WriteString(body)
- signature := ed25519.Sign(privkey, msg.Bytes())
- request.Header.Set("X-Signature-Ed25519", hex.EncodeToString(signature[:ed25519.SignatureSize]))
- if !VerifyInteraction(request, pubkey) {
- t.Error("expected true, got false")
- }
- })
- t.Run("failure/modified body", func(t *testing.T) {
- body := "body"
- request := httptest.NewRequest("POST", "http://localhost/interaction", strings.NewReader("WRONG"))
- request.Header.Set("X-Signature-Timestamp", timestamp)
- var msg bytes.Buffer
- msg.WriteString(timestamp)
- msg.WriteString(body)
- signature := ed25519.Sign(privkey, msg.Bytes())
- request.Header.Set("X-Signature-Ed25519", hex.EncodeToString(signature[:ed25519.SignatureSize]))
- if VerifyInteraction(request, pubkey) {
- t.Error("expected false, got true")
- }
- })
- t.Run("failure/modified timestamp", func(t *testing.T) {
- body := "body"
- request := httptest.NewRequest("POST", "http://localhost/interaction", strings.NewReader("WRONG"))
- request.Header.Set("X-Signature-Timestamp", strconv.FormatInt(time.Now().Add(time.Minute).Unix(), 10))
- var msg bytes.Buffer
- msg.WriteString(timestamp)
- msg.WriteString(body)
- signature := ed25519.Sign(privkey, msg.Bytes())
- request.Header.Set("X-Signature-Ed25519", hex.EncodeToString(signature[:ed25519.SignatureSize]))
- if VerifyInteraction(request, pubkey) {
- t.Error("expected false, got true")
- }
- })
- }
|