refactored out global
. long live AppState
This commit is contained in:
parent
3d674515ce
commit
384579ee5e
24 changed files with 350 additions and 375 deletions
|
@ -1,38 +1,17 @@
|
|||
package discord
|
||||
|
||||
import (
|
||||
"arimelody-web/model"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"arimelody-web/global"
|
||||
)
|
||||
|
||||
const API_ENDPOINT = "https://discord.com/api/v10"
|
||||
|
||||
var CREDENTIALS_PROVIDED = true
|
||||
var CLIENT_ID = func() string {
|
||||
id := global.Config.Discord.ClientID
|
||||
if id == "" {
|
||||
// fmt.Printf("WARN: Discord client ID (DISCORD_CLIENT) was not provided.\n")
|
||||
CREDENTIALS_PROVIDED = false
|
||||
}
|
||||
return id
|
||||
}()
|
||||
var CLIENT_SECRET = func() string {
|
||||
secret := global.Config.Discord.Secret
|
||||
if secret == "" {
|
||||
// fmt.Printf("WARN: Discord secret (DISCORD_SECRET) was not provided.\n")
|
||||
CREDENTIALS_PROVIDED = false
|
||||
}
|
||||
return secret
|
||||
}()
|
||||
var OAUTH_CALLBACK_URI = fmt.Sprintf("%s/admin/login", global.Config.BaseUrl)
|
||||
var REDIRECT_URI = fmt.Sprintf("https://discord.com/oauth2/authorize?client_id=%s&response_type=code&redirect_uri=%s&scope=identify", CLIENT_ID, OAUTH_CALLBACK_URI)
|
||||
|
||||
type (
|
||||
AccessTokenResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
|
@ -68,15 +47,15 @@ type (
|
|||
}
|
||||
)
|
||||
|
||||
func GetOAuthTokenFromCode(code string) (string, error) {
|
||||
func GetOAuthTokenFromCode(app *model.AppState, 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},
|
||||
"client_id": {app.Config.Discord.ClientID},
|
||||
"client_secret": {app.Config.Discord.Secret},
|
||||
"grant_type": {"authorization_code"},
|
||||
"code": {code},
|
||||
"redirect_uri": {OAUTH_CALLBACK_URI},
|
||||
"redirect_uri": {GetOAuthCallbackURI(app.Config.BaseUrl)},
|
||||
}.Encode()))
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
|
@ -115,3 +94,15 @@ func GetDiscordUserFromAuth(token string) (DiscordUser, error) {
|
|||
|
||||
return auth_info.User, nil
|
||||
}
|
||||
|
||||
func GetOAuthCallbackURI(baseURL string) string {
|
||||
return fmt.Sprintf("%s/admin/login", baseURL)
|
||||
}
|
||||
|
||||
func GetRedirectURI(app *model.AppState) string {
|
||||
return fmt.Sprintf(
|
||||
"https://discord.com/oauth2/authorize?client_id=%s&response_type=code&redirect_uri=%s&scope=identify",
|
||||
app.Config.Discord.ClientID,
|
||||
GetOAuthCallbackURI(app.Config.BaseUrl),
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue