1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-09 08:32:48 +00:00

wip - more oauth work

This commit is contained in:
2018-02-18 16:36:29 -06:00
parent 33c6e8e3fc
commit 79b4fecd3c
6 changed files with 157 additions and 71 deletions

View File

@@ -5,7 +5,7 @@ import './Navbar.scss';
// TODO: change url for build
const redirectUri = 'https://localhost/oauth';
const oauthUrl = `https://discordapp.com/api/oauth2/authorize?client_id=410818759746650140&redirect_uri=${redirectUri}&response_type=code&scope=email`;
const oauthUrl = `https://discordapp.com/api/oauth2/authorize?client_id=410818759746650140&redirect_uri=${redirectUri}&response_type=code&scope=guilds%20identify`;
interface Props {

View File

@@ -3,10 +3,10 @@
"client_id": "",
"client_secret": "",
"redirect_uri": "",
"upload_password": "",
"bot_prefix": "#",
"sounds_path": "./sounds/",
"clips_path": "./clips/",
"upload_password": "",
"server_addr": "0.0.0.0:80",
"pubg": {
"api_key": "",

View File

@@ -15,16 +15,20 @@ var (
)
type configFile struct {
Token string `json:"token"`
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RedirectUri string `json:"redirect_uri"`
BotPrefix string `json:"bot_prefix"` //prefix to use for bot commands
SoundsPath string `json:"sounds_path"`
ClipsPath string `json:"clips_path"`
Token string `json:"token"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RedirectURI string `json:"redirect_uri"`
GuildID string `json:"guild_id"`
UploadPassword string `json:"upload_password"`
ServerAddr string `json:"server_addr"`
Pubg struct {
BotPrefix string `json:"bot_prefix"` //prefix to use for bot commands
SoundsPath string `json:"sounds_path"`
ClipsPath string `json:"clips_path"`
ServerAddr string `json:"server_addr"`
Pubg struct {
Enabled bool `json:"enabled"`
APIKey string `json:"api_key"`
Players []string `json:"players"`

View File

@@ -0,0 +1,66 @@
package discord
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/mgerb/go-discord-bot/server/config"
)
const discordAPI = "https://discordapp.com/api/v6"
// OauthResp -
type OauthResp struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Scope string `json:"scope"`
}
// Oauth -
func Oauth(code string) (OauthResp, error) {
form := url.Values{}
form.Set("client_id", config.Config.ClientID)
form.Set("client_secret", config.Config.ClientSecret)
form.Set("grant_type", "authorization_code")
form.Set("code", code)
form.Set("redirect_uri", config.Config.RedirectURI)
req, err := http.NewRequest("POST", discordAPI+"/oauth2/token", strings.NewReader(form.Encode()))
if err != nil {
return OauthResp{}, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return OauthResp{}, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return OauthResp{}, err
}
var oauth OauthResp
err = json.Unmarshal(data, &oauth)
if err != nil {
return OauthResp{}, err
}
return oauth, nil
}

View File

@@ -0,0 +1,60 @@
package discord
import (
"encoding/json"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
)
// User -
type User struct {
Username string `json:"username"`
Verified bool `json:"verified"`
MFAEnabled bool `json:"mfa_enabled"`
ID string `json:"id"`
Avatar string `json:"avatar"`
Discriminator string `json:"discriminator"`
Email string `json:"email"`
}
// GetUserInfo - get user info
func GetUserInfo(accessToken string) (User, error) {
req, err := http.NewRequest("GET", discordAPI+"/users/@me", nil)
if err != nil {
log.Error(err)
return User{}, err
}
req.Header.Add("Authorization", "Bearer "+accessToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Error(err)
return User{}, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error(err)
return User{}, err
}
var userInfo User
err = json.Unmarshal(data, &userInfo)
if err != nil {
log.Error(err)
return User{}, err
}
// filter guild based on id
return userInfo, nil
}

View File

@@ -1,31 +1,20 @@
package handlers
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
"fmt"
"github.com/gin-gonic/gin"
"github.com/mgerb/go-discord-bot/server/config"
"github.com/mgerb/go-discord-bot/server/webserver/discord"
log "github.com/sirupsen/logrus"
)
const discordApi = "https://discordapp.com/api/v6/oauth2/token"
const cashGuildID = "101198129352691712"
type oauthReq struct {
Code string `json:"code"`
}
type oauthResp struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Scope string `json:"scope"`
}
// Oauth -
func Oauth(c *gin.Context) {
var json oauthReq
@@ -38,7 +27,7 @@ func Oauth(c *gin.Context) {
return
}
oauth, err := postReq(json.Code)
oauth, err := discord.Oauth(json.Code)
if err != nil {
log.Error(err)
@@ -46,49 +35,16 @@ func Oauth(c *gin.Context) {
return
}
user, err := discord.GetUserInfo(oauth.AccessToken)
if err != nil {
log.Error(err)
c.JSON(500, err)
return
}
// TODO: generate jwt for user
fmt.Println(user)
c.JSON(200, oauth)
}
func postReq(code string) (oauthResp, error) {
form := url.Values{}
form.Set("client_id", config.Config.ClientId)
form.Set("client_secret", config.Config.ClientSecret)
form.Set("grant_type", "authorization_code")
form.Set("code", code)
form.Set("redirect_uri", config.Config.RedirectUri)
req, err := http.NewRequest("POST", discordApi, strings.NewReader(form.Encode()))
if err != nil {
return oauthResp{}, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
c := &http.Client{}
resp, err := c.Do(req)
if err != nil {
return oauthResp{}, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return oauthResp{}, err
}
var oauth oauthResp
err = json.Unmarshal(data, &oauth)
if err != nil {
return oauthResp{}, err
}
return oauth, nil
}