1
0
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:
2018-02-09 23:58:23 -06:00
parent 1fdeb3783a
commit 33c6e8e3fc
10 changed files with 195 additions and 27 deletions

View File

@@ -9,6 +9,8 @@ import { NotFound } from './pages/NotFound/NotFound';
import { Downloader } from './pages/Downloader/Downloader';
import { Pubg } from './pages/Pubg/Pubg';
import { Clips } from './pages/Clips';
import { Oauth } from './pages/oauth/oauth';
import 'babel-polyfill';
ReactDOM.render(
<Router history={browserHistory}>
@@ -18,6 +20,7 @@ ReactDOM.render(
<Route path="/downloader" component={Downloader}/>
<Route path="/pubg" component={Pubg}/>
<Route path="/clips" component={Clips}/>
<Route path="/oauth" component={Oauth}/>
<Route path="*" component={NotFound}/>
</Route>
</Router>

View File

@@ -3,6 +3,10 @@ import { Link } from 'react-router';
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`;
interface Props {
}
@@ -22,6 +26,7 @@ export class Navbar extends React.Component<Props, State> {
<Link to="/downloader" className="Navbar__item" activeClassName="Navbar__item--active">Youtube Downloader</Link>
<Link to="/pubg" className="Navbar__item" activeClassName="Navbar__item--active">Pubg</Link>
<Link to="/clips" className="Navbar__item" activeClassName="Navbar__item--active">Clips</Link>
<a href={oauthUrl} className="Navbar__item">Login</a>
</div>
);
}

View File

@@ -0,0 +1,36 @@
import React from 'react';
import { get } from 'lodash';
import axios from 'axios';
interface Props {
}
interface State {
}
export class Oauth extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
}
componentDidMount() {
const code = get(this, 'props.location.query.code');
if (code) {
// do stuff here
this.fetchOauth(code as string);
}
}
private async fetchOauth(code: string) {
const res = await axios.post('/api/oauth', { code });
console.log(res);
}
render() {
return <div></div>
}
}

View File

@@ -9,7 +9,7 @@
},
"author": "Mitchell Gerber",
"license": "MIT",
"devDependencies": {
"dependencies": {
"@types/lodash": "^4.14.71",
"@types/react": "^16.0.0",
"@types/react-dom": "^15.5.1",
@@ -20,6 +20,7 @@
"babel-core": "^6.21.0",
"babel-loader": "^6.2.10",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",

View File

@@ -812,6 +812,14 @@ babel-plugin-transform-strict-mode@^6.22.0:
babel-runtime "^6.22.0"
babel-types "^6.22.0"
babel-polyfill@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-polyfill/-/babel-polyfill-6.26.0.tgz#379937abc67d7895970adc621f284cd966cf2153"
dependencies:
babel-runtime "^6.26.0"
core-js "^2.5.0"
regenerator-runtime "^0.10.5"
babel-preset-es2015@^6.18.0:
version "6.22.0"
resolved "https://registry.yarnpkg.com/babel-preset-es2015/-/babel-preset-es2015-6.22.0.tgz#af5a98ecb35eb8af764ad8a5a05eb36dc4386835"
@@ -907,6 +915,13 @@ babel-runtime@^6.18.0, babel-runtime@^6.22.0:
core-js "^2.4.0"
regenerator-runtime "^0.10.0"
babel-runtime@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe"
dependencies:
core-js "^2.4.0"
regenerator-runtime "^0.11.0"
babel-template@^6.22.0:
version "6.22.0"
resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.22.0.tgz#403d110905a4626b317a2a1fcb8f3b73204b2edb"
@@ -1406,6 +1421,10 @@ core-js@^2.4.0:
version "2.4.1"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.4.1.tgz#4de911e667b0eae9124e34254b53aea6fc618d3e"
core-js@^2.5.0:
version "2.5.3"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.5.3.tgz#8acc38345824f16d8365b7c9b4259168e8ed603e"
core-util-is@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
@@ -4034,6 +4053,14 @@ regenerator-runtime@^0.10.0:
version "0.10.1"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.10.1.tgz#257f41961ce44558b18f7814af48c17559f9faeb"
regenerator-runtime@^0.10.5:
version "0.10.5"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.10.5.tgz#336c3efc1220adcedda2c9fab67b5a7955a33658"
regenerator-runtime@^0.11.0:
version "0.11.1"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9"
regenerator-transform@0.9.8:
version "0.9.8"
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.9.8.tgz#0f88bb2bc03932ddb7b6b7312e68078f01026d6c"

View File

@@ -1,13 +1,16 @@
{
"Token": "",
"BotPrefix": "#",
"SoundsPath": "./sounds/",
"ClipsPath": "./clips/",
"UploadPassword": "",
"ServerAddr": ":80",
"token": "",
"client_id": "",
"client_secret": "",
"redirect_uri": "",
"bot_prefix": "#",
"sounds_path": "./sounds/",
"clips_path": "./clips/",
"upload_password": "",
"server_addr": "0.0.0.0:80",
"pubg": {
"apiKey": "",
"enabled": true,
"api_key": "",
"enabled": false,
"players": ["player1"]
}
}

14
main.go
View File

@@ -10,24 +10,18 @@ import (
)
func init() {
log.SetLevel(log.DebugLevel)
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
//read config file
config.Init()
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.JSONFormatter{})
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)
if config.Flags.Prod {
// prod
gin.SetMode(gin.ReleaseMode)
// Only log the warning severity or above.
log.SetLevel(log.WarnLevel)
} else {
// debug
log.SetLevel(log.DebugLevel)
}
}

View File

@@ -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"`
}

View 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
}

View File

@@ -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
}