This repository has been archived by the owner on Oct 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
oauthDiscord.go
111 lines (102 loc) · 3.72 KB
/
oauthDiscord.go
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
package main
import (
"context"
"encoding/json"
_ "fmt"
"io"
"log"
"net/http"
"time"
"github.com/davecgh/go-spew/spew"
discord "github.com/ravener/discord-oauth2"
"golang.org/x/oauth2"
)
var DiscordRedirectUrl = "https://wz2100-autohost.net/oauth/discord"
var discordOauthConfig = &oauth2.Config{
RedirectURL: DiscordRedirectUrl,
Scopes: []string{
"connections", "identify", "guilds", "email"},
Endpoint: discord.Endpoint,
}
// type DiscordUser struct {
// ID string `json:"id"`
// Avatar string `json:"avatar"`
// Username string `json:"username"`
// Discriminator string `json:"discriminator"`
// }
func DiscordVerifyEnv() {
var ok bool
discordOauthConfig.ClientID, ok = cfg.GetString("discord", "id")
if !ok {
log.Println("Discord client ID not set")
}
discordOauthConfig.ClientSecret, ok = cfg.GetString("discord", "secret")
if !ok {
log.Println("Discord client secret not set")
}
}
func DiscordGetUrl(state string) string {
return discordOauthConfig.AuthCodeURL(state, oauth2.AccessTypeOffline)
}
func DiscordGetUInfo(token *oauth2.Token) map[string]any {
res, err := discordOauthConfig.Client(context.Background(), token).Get("https://discord.com/api/accounts/@me")
if err != nil {
log.Printf("Unauthorized, resetting discord (%s)", spew.Sprintln(err))
token.AccessToken = ""
token.RefreshToken = ""
token.Expiry = time.Now()
return map[string]any{"DiscordError": "Error"}
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return map[string]any{"DiscordError": err.Error()}
}
jsonMap := make(map[string]any)
err = json.Unmarshal([]byte(body), &jsonMap)
if err != nil {
log.Println(err.Error())
}
return jsonMap
}
func DiscordCallbackHandler(w http.ResponseWriter, r *http.Request) {
if !sessionManager.Exists(r.Context(), "UserAuthorized") || sessionManager.Get(r.Context(), "UserAuthorized") != "True" {
log.Println("Not authorized")
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": 1, "msg": "Not authorized"})
return
}
if !sessionManager.Exists(r.Context(), "User.Username") {
log.Println("Not authorized (no username)")
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": 1, "msg": "Not authorized (no username)"})
return
}
code := r.FormValue("code")
if sessionManager.Get(r.Context(), "User.Discord.State") != r.FormValue("state") {
log.Println("Code missmatch")
st := sessionManager.GetString(r.Context(), "User.Discord.State")
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": 1, "msg": "State missmatch " + st})
return
}
token, err := discordOauthConfig.Exchange(r.Context(), code)
if err != nil {
log.Printf("Code exchange failed with error %s\n", err.Error())
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": 1, "msg": "Code exchange failed with error: " + err.Error()})
return
}
if !token.Valid() {
log.Println("Retreived invalid token")
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": 1, "msg": "Retreived invalid token"})
return
}
tag, derr := dbpool.Exec(r.Context(), "UPDATE accounts SET discord_token = $1, discord_refresh = $2, discord_refresh_date = $3 WHERE username = $4", token.AccessToken, token.RefreshToken, token.Expiry, sessionManager.Get(r.Context(), "User.Username"))
if derr != nil {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": 1, "msg": "Database call error: " + derr.Error()})
return
}
if tag.RowsAffected() != 1 {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msgred": 1, "msg": "Database insert error, rows affected " + string(tag)})
return
}
log.Println("Got token")
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msggreen": 1, "msg": "Discord linked."})
}