From d19c8020278cc782ea80b8240daf9fa972ace733 Mon Sep 17 00:00:00 2001 From: Jesper Handskemager Date: Sat, 17 Sep 2022 21:37:44 +0200 Subject: [PATCH] Make authenticate site look a lot nicer --- index.html | 47 ++++++++++++++++++++++++++++++----------------- main.go | 17 +++++++++++------ steam.go | 44 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 82 insertions(+), 26 deletions(-) diff --git a/index.html b/index.html index b22df5b..8e975c3 100644 --- a/index.html +++ b/index.html @@ -1,19 +1,32 @@ - Steam OpenID - - -
- {{if .user}} -

{{.user}} Linked to your Discord account

+ + + Steam OpenID + + + + + {{if .DiscordName}} + + + + \ No newline at end of file diff --git a/main.go b/main.go index 9c729d6..b61bfef 100644 --- a/main.go +++ b/main.go @@ -24,12 +24,13 @@ type YAMLFile struct { } type Config struct { - MYSQL_DB string `yaml:"MYSQL_DB"` - MYSQL_USER string `yaml:"MYSQL_USER"` - MYSQL_PASS string `yaml:"MYSQL_PASS"` - MYSQL_HOST string `yaml:"MYSQL_HOST"` - DOMAIN string `yaml:"DOMAIN"` - PORT string `yaml:"PORT"` + MYSQL_DB string `yaml:"MYSQL_DB"` + MYSQL_USER string `yaml:"MYSQL_USER"` + MYSQL_PASS string `yaml:"MYSQL_PASS"` + MYSQL_HOST string `yaml:"MYSQL_HOST"` + DOMAIN string `yaml:"DOMAIN"` + PORT string `yaml:"PORT"` + DISCORD_TOKEN string `yaml:"DISCORD_TOKEN"` } func ReadConfig() (*Config, error) { @@ -176,6 +177,8 @@ func GenerateRandomString(n int) (string, error) { return string(ret), nil } +var bearer string + var db *sql.DB func main() { @@ -198,6 +201,8 @@ func main() { defer db.Close() log.Println("Database connection established") + bearer = "Bot " + config.DISCORD_TOKEN + router := mux.NewRouter() router.HandleFunc("/", indexHandler) diff --git a/steam.go b/steam.go index 8dd2c7d..d449844 100644 --- a/steam.go +++ b/steam.go @@ -1,8 +1,10 @@ package main import ( + "encoding/json" "fmt" "html/template" + "io" "log" "net/http" "strconv" @@ -33,23 +35,59 @@ func (n *NoOpDiscoveryCache) Get(id string) openid.DiscoveredInfo { var nonceStore = openid.NewSimpleNonceStore() var discoveryCache = &NoOpDiscoveryCache{} +type IndexStruct struct { + DiscordName string `json:"DiscordName"` + DiscordAvatar string `json:"DiscordAvatar"` +} + +type DiscordUser struct { + Id string `json:"id"` + Username string `json:"username"` + Avatar string `json:"avatar"` + AvatarDecoration string `json:"avatar_decoration"` + Discriminator string `json:"discriminator"` + PublicFlags int `json:"public_flags"` + Banner string `json:"banner"` + BannerColor string `json:"banner_color"` + AccentColor string `json:"accent_color"` +} + // indexHandler serves up the index template with the "Sign in through STEAM" button. func indexHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) token := vars["token"] - query := `SELECT token FROM tokens where token = BINARY ?` - err := db.QueryRow(query, token).Scan(&token) + var discordId string + query := `SELECT discord_id, token FROM tokens where token = BINARY ?` + err := db.QueryRow(query, token).Scan(&discordId, &token) if err != nil { log.Print(err) w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "Bad request") return } + // 959336363172442152/1ce1214a9540ff02cedc0acd0ad37d1f.png + req, err := http.NewRequest("GET", "https://discord.com/api/v9/users/"+discordId, nil) + req.Header.Add("Authorization", bearer) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + log.Println("Error on response.\n[ERROR] -", err) + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Println("Error while reading the response bytes:", err) + } + var discord DiscordUser + json.Unmarshal(body, &discord) + tmpl := IndexStruct{DiscordName: discord.Username, DiscordAvatar: discord.Id + "/" + discord.Avatar} log.Println(token) expiration := time.Now().Add(time.Hour) cookie := http.Cookie{Name: "token", Value: token, Expires: expiration} http.SetCookie(w, &cookie) - indexTemplate.Execute(w, nil) + indexTemplate.Execute(w, tmpl) } // discoverHandler calls the Steam openid API and redirects to steam for login.