mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-09 16:42:48 +00:00
wip - oauth
This commit is contained in:
@@ -3,8 +3,9 @@ package config
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Variables used for command line parameters
|
||||
@@ -14,15 +15,18 @@ var (
|
||||
)
|
||||
|
||||
type configFile struct {
|
||||
Token string `json:"Token"`
|
||||
BotPrefix string `json:"BotPrefix"` //prefix to use for bot commands
|
||||
SoundsPath string `json:"SoundsPath"`
|
||||
ClipsPath string `json:"ClipsPath"`
|
||||
UploadPassword string `json:"UploadPassword"`
|
||||
ServerAddr string `json:"ServerAddr`
|
||||
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"`
|
||||
UploadPassword string `json:"upload_password"`
|
||||
ServerAddr string `json:"server_addr"`
|
||||
Pubg struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
APIKey string `json:"apiKey"`
|
||||
APIKey string `json:"api_key"`
|
||||
Players []string `json:"players"`
|
||||
} `json:"pubg"`
|
||||
}
|
||||
|
||||
94
server/webserver/handlers/oauth.go
Normal file
94
server/webserver/handlers/oauth.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const discordApi = "https://discordapp.com/api/v6/oauth2/token"
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
func Oauth(c *gin.Context) {
|
||||
|
||||
var json oauthReq
|
||||
|
||||
err := c.ShouldBindJSON(&json)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
oauth, err := postReq(json.Code)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -25,6 +25,7 @@ func getRouter() *gin.Engine {
|
||||
api.GET("/soundlist", handlers.SoundList)
|
||||
api.GET("/cliplist", handlers.ClipList)
|
||||
api.POST("/upload", handlers.FileUpload)
|
||||
api.POST("/oauth", handlers.Oauth)
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user