Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't verify requests for Discord interactions URL properly #1526

Open
preslavmihaylov opened this issue Apr 28, 2024 · 0 comments
Open

I can't verify requests for Discord interactions URL properly #1526

preslavmihaylov opened this issue Apr 28, 2024 · 0 comments

Comments

@preslavmihaylov
Copy link

preslavmihaylov commented Apr 28, 2024

I'm trying to write a golang handler, which reacts to discord interactions. I've set it up as the interactions URL in my discord developer portal.

This is the code I've written:

var secrets struct {
	DiscordPublicKey string
}

type UnknownDiscordInteraction struct {
	InteractionType discordgo.InteractionType `json:"type"`
}

func DiscordWebhook(w http.ResponseWriter, r *http.Request) {
	body, err := io.ReadAll(r.Body)
	if err != nil {
		http.Error(w, "Error reading request body", http.StatusInternalServerError)
		return
	}

	publicKeyBytes, err := hex.DecodeString(secrets.DiscordPublicKey)
	if err != nil {
		http.Error(w, "Error decoding hex string", http.StatusInternalServerError)
		return
	}

	if !discordgo.VerifyInteraction(r, publicKeyBytes) {
		http.Error(w, "invalid request signature", http.StatusUnauthorized)
		return
	}

	var interaction UnknownDiscordInteraction
	if err := json.Unmarshal(body, &interaction); err != nil {
		http.Error(w, "Error unmarshalling request body", http.StatusInternalServerError)
		return
	}

	if interaction.InteractionType == discordgo.InteractionPing {
		pongResp, err := json.Marshal(discordgo.InteractionResponse{
			Type: discordgo.InteractionResponsePong,
		})
		if err != nil {
			http.Error(w, "Error marshalling response", http.StatusInternalServerError)
			return
		}

		// set content-type header
		w.Header().Set("Content-Type", "application/json")
		w.Write(pongResp)
		w.WriteHeader(http.StatusOK)
		return
	}

	w.WriteHeader(http.StatusOK)
}

I can't save the endpoint in the developer dashboard, because any challenge request that discord sends I fail to verify, it returns that the request is invalid.

I am passing the DiscordPublicKey as a string, copied from my discord developer dashboard. Anyone has a clue what I'm doing wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant