Make authenticate site look a lot nicer

This commit is contained in:
Jesper 2022-09-17 21:37:44 +02:00
parent 72563fffc5
commit d19c802027
3 changed files with 82 additions and 26 deletions

View File

@ -1,19 +1,32 @@
<html> <html>
<head><title>Steam OpenID</title></head>
<style>body{font-family: Inter, Avenir, Helvetica, Arial, sans-serif;font-size: 16px;line-height: 24px;font-weight: 400;color-scheme: light dark;color: rgba(255, 255, 255, 0.87);background-color: #242424;font-synthesis: none;text-rendering: optimizeLegibility;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;-webkit-text-size-adjust: 100%;}a {font-weight: 500;color: #646cff;text-decoration: inherit;}a:hover {color: #535bf2;}.login {margin-top: 5%;margin: 0;text-align: center;min-width: 320px;min-height: 100vh;}h1 {font-size: 3.2em;line-height: 1.1;}img {scale: 1.2;}</style> <head>
<body> <title>Steam OpenID</title>
<div class="login"> </head>
{{if .user}} <style>.login,li a{text-align:center}body{font-family:Inter,Avenir,Helvetica,Arial,sans-serif;font-size:16px;line-height:24px;font-weight:400;color-scheme:light dark;color:rgba(255,255,255,.87);background-color:#242424;font-synthesis:none;text-rendering:optimizeLegibility;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;-webkit-text-size-adjust:100%}a{font-weight:500;color:#646cff;text-decoration:inherit}a:hover{color:#535bf2}.login{margin:0;min-width:320px;min-height:100vh}h1{font-size:3.2em;line-height:1.1}img{scale:1.2}ul{list-style-type:none;margin:0;padding:0;overflow:hidden}li a{display:block;color:#fff;padding:14px 16px;text-decoration:none}.navbar img{border-radius:50%;scale:1.0}</style>
<p>{{.user}} Linked to your Discord account</p>
<body>
{{if .DiscordName}}
<div class="navbar">
<ul>
<li style="float:right"><img alt="avatar" src="https://cdn.discordapp.com/avatars/{{.DiscordAvatar}}.png?size=100" width="50" height="50px"></li>
<li style="float:right"><a class="active" href="#">{{.DiscordName}}</a></li>
</ul>
</div>
<div class="login">
<h1>Authenticate Steam</h1>
<p><b>Link {{.DiscordName}} with your Steam account</b></p>
<br>
<a href="/discover">
<img alt="Sign in through Steam"
src="http://steamcommunity-a.akamaihd.net/public/images/signinthroughsteam/sits_large_noborder.png" />
</a>
<div class="login">
{{else if .user}}
<p>{{.user}} Linked to your Discord account</p>
{{else}} {{else}}
<h1>Authenticate Steam</h1> <p>An error occured</p>
<a href="/discover"> {{end}}
<img alt="Sign in through Steam" src="http://steamcommunity-a.akamaihd.net/public/images/signinthroughsteam/sits_large_noborder.png" /> </div>
</a> </body>
{{end}} </html>
{{if .error}}
<p>An error occured</p>
{{end}}
</div>
</body>
</html>

17
main.go
View File

@ -24,12 +24,13 @@ type YAMLFile struct {
} }
type Config struct { type Config struct {
MYSQL_DB string `yaml:"MYSQL_DB"` MYSQL_DB string `yaml:"MYSQL_DB"`
MYSQL_USER string `yaml:"MYSQL_USER"` MYSQL_USER string `yaml:"MYSQL_USER"`
MYSQL_PASS string `yaml:"MYSQL_PASS"` MYSQL_PASS string `yaml:"MYSQL_PASS"`
MYSQL_HOST string `yaml:"MYSQL_HOST"` MYSQL_HOST string `yaml:"MYSQL_HOST"`
DOMAIN string `yaml:"DOMAIN"` DOMAIN string `yaml:"DOMAIN"`
PORT string `yaml:"PORT"` PORT string `yaml:"PORT"`
DISCORD_TOKEN string `yaml:"DISCORD_TOKEN"`
} }
func ReadConfig() (*Config, error) { func ReadConfig() (*Config, error) {
@ -176,6 +177,8 @@ func GenerateRandomString(n int) (string, error) {
return string(ret), nil return string(ret), nil
} }
var bearer string
var db *sql.DB var db *sql.DB
func main() { func main() {
@ -198,6 +201,8 @@ func main() {
defer db.Close() defer db.Close()
log.Println("Database connection established") log.Println("Database connection established")
bearer = "Bot " + config.DISCORD_TOKEN
router := mux.NewRouter() router := mux.NewRouter()
router.HandleFunc("/", indexHandler) router.HandleFunc("/", indexHandler)

View File

@ -1,8 +1,10 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"html/template" "html/template"
"io"
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
@ -33,23 +35,59 @@ func (n *NoOpDiscoveryCache) Get(id string) openid.DiscoveredInfo {
var nonceStore = openid.NewSimpleNonceStore() var nonceStore = openid.NewSimpleNonceStore()
var discoveryCache = &NoOpDiscoveryCache{} 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. // indexHandler serves up the index template with the "Sign in through STEAM" button.
func indexHandler(w http.ResponseWriter, r *http.Request) { func indexHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
token := vars["token"] token := vars["token"]
query := `SELECT token FROM tokens where token = BINARY ?` var discordId string
err := db.QueryRow(query, token).Scan(&token) query := `SELECT discord_id, token FROM tokens where token = BINARY ?`
err := db.QueryRow(query, token).Scan(&discordId, &token)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Bad request") fmt.Fprintf(w, "Bad request")
return 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) log.Println(token)
expiration := time.Now().Add(time.Hour) expiration := time.Now().Add(time.Hour)
cookie := http.Cookie{Name: "token", Value: token, Expires: expiration} cookie := http.Cookie{Name: "token", Value: token, Expires: expiration}
http.SetCookie(w, &cookie) http.SetCookie(w, &cookie)
indexTemplate.Execute(w, nil) indexTemplate.Execute(w, tmpl)
} }
// discoverHandler calls the Steam openid API and redirects to steam for login. // discoverHandler calls the Steam openid API and redirects to steam for login.