package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"golang.org/x/oauth2"
"golang.org/x/oauth2/endpoints"
"git.sr.ht/~muirrum/gompei-verification/bot"
"github.com/bwmarrin/discordgo"
"github.com/gleich/logoru"
"github.com/joho/godotenv"
)
var ()
var azureConfig oauth2.Config
var discordConfig oauth2.Config
var verifyChan chan string
func LandingPage(w http.ResponseWriter, r *http.Request) {
logoru.Info("Starting auth process")
azureUrl := azureConfig.AuthCodeURL("xyz")
http.Redirect(w, r, azureUrl, http.StatusFound)
}
// Azure landing page
func AzureAuth(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
state := r.Form.Get("state")
if state != "xyz" {
http.Error(w, "Invalid state", http.StatusBadRequest)
return
}
code := r.Form.Get("code")
if code == "" {
http.Error(w, "Code not found", http.StatusBadRequest)
return
}
_, err := azureConfig.Exchange(context.Background(), code)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
discordUrl := discordConfig.AuthCodeURL("xyz")
http.Redirect(w, r, discordUrl, http.StatusFound)
}
// Discord landing page
func DiscordAuth(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
state := r.Form.Get("state")
if state != "xyz" {
http.Error(w, "Invalid state", http.StatusBadRequest)
return
}
code := r.Form.Get("code")
if code == "" {
http.Error(w, "Code not found", http.StatusBadRequest)
return
}
token, err := discordConfig.Exchange(context.Background(), code)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
req, err := http.NewRequest("GET", "https://discord.com/api/users/@me", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
token.SetAuthHeader(req)
res, err := http.DefaultClient.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
bytes := []byte(body)
var user discordgo.User
err = json.Unmarshal(bytes, &user)
verifyChan <- user.ID
http.Redirect(w, r, fmt.Sprintf("%s/success", os.Getenv("BASE_URL")), http.StatusFound)
}
func Success(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, os.Getenv("SUCCESS_URL"), http.StatusFound)
}
func main() {
err := godotenv.Load()
if err != nil {
logoru.Critical("Could not load environment file")
}
azureConfig = oauth2.Config{
ClientID: os.Getenv("AZURE_CLIENT_ID"),
ClientSecret: os.Getenv("AZURE_CLIENT_SECRET"),
Scopes: []string{".default"},
RedirectURL: fmt.Sprintf("%s/oauth/microsoft", os.Getenv("BASE_URL")),
Endpoint: endpoints.AzureAD(os.Getenv("AZURE_TENANT_ID")),
}
discordConfig = oauth2.Config{
ClientID: os.Getenv("DISCORD_CLIENT_ID"),
ClientSecret: os.Getenv("DISCORD_CLIENT_SECRET"),
Scopes: []string{"identify"},
RedirectURL: fmt.Sprintf("%s/oauth/discord", os.Getenv("BASE_URL")),
Endpoint: oauth2.Endpoint{
AuthURL: "https://discord.com/api/oauth2/authorize",
TokenURL: "https://discord.com/api/oauth2/token",
AuthStyle: 0,
},
}
http.HandleFunc("/", LandingPage)
http.HandleFunc("/oauth/microsoft", AzureAuth)
http.HandleFunc("/oauth/discord", DiscordAuth)
http.HandleFunc("/success", Success)
verifyChan = make(chan string)
go bot.StartDiscordBot(verifyChan)
logoru.Info("Starting client")
logoru.Critical(http.ListenAndServe(fmt.Sprintf(":%s", os.Getenv("LISTEN_PORT")), nil))
}