add basic oauth bounces

This commit is contained in:
41666 2020-11-24 22:27:56 -05:00
parent bebfc862e8
commit a23184efd2
13 changed files with 262 additions and 1 deletions

View file

@ -0,0 +1,10 @@
# Bot Join Bounce Service
This function sends a user to the necessary Discord.com Bot OAuth flow.
Two cases may be present:
- General: The user will be sent to allow any of their relevant servers to join
- This flow would be triggered from a generalized button
- Specific: The user will be sent to join one of their servers.
- This flow would be triggered from server picker

View file

@ -0,0 +1,45 @@
package botjoin
import (
"bytes"
"net/http"
"regexp"
"text/template"
"github.com/roleypoly/roleypoly/src/common"
"github.com/roleypoly/roleypoly/src/common/faas"
)
var (
validGuildID = regexp.MustCompile(`^[0-9]+$`)
redirectPathTemplate = template.Must(
template.New("redirect").Parse(
`https://discord.com/api/oauth2/authorize?client_id={{.ClientID}}&scope=bot&permissions={{.Permissions}}{{if .GuildID}}&guild_id={{.GuildID}}&disable_guild_select=true{{end}}`,
),
)
clientID = common.Getenv("BOT_CLIENT_ID").String()
)
type redirectPathData struct {
ClientID string
Permissions int
GuildID string
}
func BotJoin(rw http.ResponseWriter, r *http.Request) {
guildID := r.URL.Query().Get("guild")
if !validGuildID.MatchString(guildID) {
guildID = ""
}
pathData := redirectPathData{
ClientID: clientID,
Permissions: 268435456, // MANAGE_ROLES
GuildID: guildID,
}
pathBuffer := bytes.Buffer{}
redirectPathTemplate.Execute(&pathBuffer, pathData)
faas.Bounce(rw, pathBuffer.String())
}

View file

@ -0,0 +1,36 @@
package botjoin_test
import (
"net/http/httptest"
"testing"
"github.com/onsi/gomega"
botjoin "github.com/roleypoly/roleypoly/src/functions/bot-join"
)
func TestGeneral(t *testing.T) {
O := gomega.NewWithT(t)
req := httptest.NewRequest("GET", "/bot-join", nil)
resp := httptest.NewRecorder()
botjoin.BotJoin(resp, req)
result := resp.Result()
O.Expect(result.StatusCode).Should(gomega.BeIdenticalTo(303))
O.Expect(result.Header.Get("location")).ShouldNot(gomega.ContainSubstring("guild_id"))
}
func TestGeneralSpecific(t *testing.T) {
O := gomega.NewWithT(t)
req := httptest.NewRequest("GET", "/bot-join?guild=386659935687147521", nil)
resp := httptest.NewRecorder()
botjoin.BotJoin(resp, req)
result := resp.Result()
O.Expect(result.StatusCode).Should(gomega.BeIdenticalTo(303))
O.Expect(result.Header.Get("location")).Should(gomega.ContainSubstring("guild_id=386659935687147521"))
}