~muirrum/gompei-verification

ref: 81f954eb9963c633593c722d7172cbe2b6aa17f1 gompei-verification/main.go -rw-r--r-- 3.9 KiB
81f954eb — Cara Salter meta: Add privacy policy 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"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 Privacy(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, "Gompei-Verification is a project by Cara Salter (https://devcara.com) for the WPI '25 Discord Server.\n\nThe bot collects only the data strictly necessary for verification -- your Discord ID and Azure AD Home ID, and stores neither.\n\nData processing requests are irrelevant and will be ignored.")

}

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)
	http.HandleFunc("/privacy", Privacy)

	verifyChan = make(chan string)

	go bot.StartDiscordBot(verifyChan)

	logoru.Info("Starting client")
	logoru.Critical(http.ListenAndServe(fmt.Sprintf(":%s", os.Getenv("LISTEN_PORT")), nil))
}