2024-07-31 03:09:22 +00:00
|
|
|
package discord
|
|
|
|
|
2024-07-31 03:21:52 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-07-31 03:09:22 +00:00
|
|
|
const API_ENDPOINT = "https://discord.com/api/v10"
|
|
|
|
const CLIENT_ID = "1268013769578119208"
|
2024-07-31 03:21:52 +00:00
|
|
|
|
2024-07-31 03:09:22 +00:00
|
|
|
// TODO: good GOD change this later please i beg you. we've already broken
|
|
|
|
// the rules by doing this at all
|
|
|
|
const CLIENT_SECRET = "JUEZnixhN7BxmLIHmbECiKETMP85VT0E"
|
|
|
|
const REDIRECT_URI = "https://discord.com/oauth2/authorize?client_id=1268013769578119208&response_type=code&redirect_uri=http%3A%2F%2F127.0.0.1%3A8080%2Fapi%2Fv1%2Fadmin%2Flogin&scope=identify"
|
2024-07-31 03:21:52 +00:00
|
|
|
|
2024-07-31 03:09:22 +00:00
|
|
|
// TODO: change before prod
|
|
|
|
const MY_REDIRECT_URI = "http://127.0.0.1:8080/api/v1/admin/login"
|
|
|
|
|
|
|
|
type (
|
|
|
|
AccessTokenResponse struct {
|
2024-07-31 03:21:52 +00:00
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
2024-07-31 03:09:22 +00:00
|
|
|
RefreshToken string `json:"refresh_token"`
|
2024-07-31 03:21:52 +00:00
|
|
|
Scope string `json:"scope"`
|
2024-07-31 03:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuthInfoResponse struct {
|
|
|
|
Application struct {
|
2024-07-31 03:21:52 +00:00
|
|
|
Id string
|
|
|
|
Name string
|
|
|
|
Icon string
|
|
|
|
Description string
|
|
|
|
Hook bool
|
|
|
|
BotPublic bool
|
2024-07-31 03:09:22 +00:00
|
|
|
botRequireCodeGrant bool
|
2024-07-31 03:21:52 +00:00
|
|
|
VerifyKey bool
|
2024-07-31 03:09:22 +00:00
|
|
|
}
|
2024-07-31 03:21:52 +00:00
|
|
|
Scopes []string
|
2024-07-31 03:09:22 +00:00
|
|
|
Expires string
|
2024-07-31 03:21:52 +00:00
|
|
|
User DiscordUser
|
|
|
|
}
|
|
|
|
|
|
|
|
DiscordUser struct {
|
|
|
|
Id string
|
|
|
|
Username string
|
|
|
|
Avatar string
|
|
|
|
Discriminator string
|
|
|
|
GlobalName string
|
|
|
|
PublicFlags int
|
2024-07-31 03:09:22 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-07-31 03:21:52 +00:00
|
|
|
func GetOAuthTokenFromCode(code string) (string, error) {
|
|
|
|
// let's get an oauth token!
|
|
|
|
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/oauth2/token", API_ENDPOINT),
|
|
|
|
strings.NewReader(url.Values{
|
|
|
|
"client_id": {CLIENT_ID},
|
|
|
|
"client_secret": {CLIENT_SECRET},
|
|
|
|
"grant_type": {"authorization_code"},
|
|
|
|
"code": {code},
|
|
|
|
"redirect_uri": {MY_REDIRECT_URI},
|
|
|
|
}.Encode()))
|
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.New(fmt.Sprintf("Failed while contacting discord API: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
oauth := AccessTokenResponse{}
|
|
|
|
|
|
|
|
err = json.NewDecoder(res.Body).Decode(&oauth)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.New(fmt.Sprintf("Failed to parse OAuth response data from discord: %s\n", err))
|
|
|
|
}
|
|
|
|
res.Body.Close()
|
|
|
|
|
|
|
|
return oauth.AccessToken, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetDiscordUserFromAuth(token string) (DiscordUser, error) {
|
|
|
|
// let's get authorisation information!
|
|
|
|
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/oauth2/@me", API_ENDPOINT), nil)
|
|
|
|
req.Header.Add("Authorization", "Bearer " + token)
|
|
|
|
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return DiscordUser{}, errors.New(fmt.Sprintf("Failed to retrieve discord auth information: %s\n", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
auth_info := AuthInfoResponse{}
|
|
|
|
|
|
|
|
err = json.NewDecoder(res.Body).Decode(&auth_info)
|
|
|
|
if err != nil {
|
|
|
|
return DiscordUser{}, errors.New(fmt.Sprintf("Failed to parse auth information from discord: %s\n", err))
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
return auth_info.User, nil
|
|
|
|
}
|