mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-09 08:32:48 +00:00
use oauth for file uploads
This commit is contained in:
2
.editorconfig
Normal file
2
.editorconfig
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
[*]
|
||||||
|
indent_size = 2
|
||||||
@@ -1,52 +1,63 @@
|
|||||||
@import '../../scss/variables';
|
@import '../../scss/variables';
|
||||||
|
|
||||||
.Navbar {
|
.Navbar {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
top: 0;
|
top: 0;
|
||||||
left: 0;
|
left: 0;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
width: $navbarWidth;
|
width: $navbarWidth;
|
||||||
background-color: $gray2;
|
background-color: $gray2;
|
||||||
border-right: 1px solid darken($gray2, 2%);
|
border-right: 1px solid darken($gray2, 2%);
|
||||||
overflow-y: auto;
|
overflow-y: auto;
|
||||||
padding-bottom: 10px;
|
padding-bottom: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.Navbar__header {
|
.Navbar__header {
|
||||||
font-size: 25px;
|
font-size: 25px;
|
||||||
min-height: 100px;
|
min-height: 100px;
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
background-color: $primaryBlue;
|
background-color: $primaryBlue;
|
||||||
border-bottom: 1px solid $gray3;
|
border-bottom: 1px solid $gray3;
|
||||||
}
|
}
|
||||||
|
|
||||||
.Navbar__item {
|
.Navbar__item {
|
||||||
min-height: 50px;
|
min-height: 50px;
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
color: $white;
|
color: $white;
|
||||||
transition: background-color 0.1s linear, color 0.2s linear;
|
transition: background-color 0.1s linear, color 0.2s linear;
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
background-color: $gray1;
|
background-color: $gray1;
|
||||||
}
|
}
|
||||||
|
|
||||||
& + .Navbar__item {
|
& + .Navbar__item {
|
||||||
border-top: 1px solid $gray3;
|
border-top: 1px solid $gray3;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.Navbar__item--active {
|
.Navbar__item--active {
|
||||||
padding-left: 4px;
|
padding-left: 4px;
|
||||||
border-right: 4px solid $primaryBlue;
|
border-right: 4px solid $primaryBlue;
|
||||||
color: $primaryBlue !important;
|
color: $primaryBlue !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Navbar__email {
|
||||||
|
padding-top: 10px;
|
||||||
|
flex: 1;
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-end;
|
||||||
|
justify-content: center;
|
||||||
|
color: $primaryBlue;
|
||||||
|
bottom: 2;
|
||||||
|
left: 2;
|
||||||
|
font-size: 12px;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,33 +1,70 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Link } from 'react-router';
|
import { Link } from 'react-router';
|
||||||
|
import jwt_decode from 'jwt-decode';
|
||||||
|
|
||||||
import './Navbar.scss';
|
import './Navbar.scss';
|
||||||
|
import { storage } from '../../storage';
|
||||||
|
|
||||||
// TODO: change url for build
|
let oauthUrl: string;
|
||||||
const redirectUri = 'https://localhost/oauth';
|
|
||||||
const oauthUrl = `https://discordapp.com/api/oauth2/authorize?client_id=410818759746650140&redirect_uri=${redirectUri}&response_type=code&scope=guilds%20identify`;
|
|
||||||
|
|
||||||
interface Props {
|
|
||||||
|
|
||||||
|
if (!process.env.NODE_ENV) {
|
||||||
|
// dev
|
||||||
|
oauthUrl = `https://discordapp.com/api/oauth2/authorize?client_id=410818759746650140&redirect_uri=https%3A%2F%2Flocalhost%2Foauth&response_type=code&scope=identify%20guilds`;
|
||||||
|
} else {
|
||||||
|
// prod
|
||||||
|
oauthUrl = `https://discordapp.com/api/oauth2/authorize?client_id=271998875802402816&redirect_uri=https%3A%2F%2Fcashdiscord.com%2Foauth&response_type=code&scope=identify%20guilds%20email`;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface State {
|
|
||||||
|
|
||||||
|
interface Props {}
|
||||||
|
|
||||||
|
interface State {
|
||||||
|
token: string | null;
|
||||||
|
email?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Navbar extends React.Component<Props, State> {
|
export class Navbar extends React.Component<Props, State> {
|
||||||
|
|
||||||
render() {
|
constructor(props: Props) {
|
||||||
return (
|
super(props);
|
||||||
<div className="Navbar">
|
this.state = {
|
||||||
<div className="Navbar__header">Go Discord Bot</div>
|
token: null,
|
||||||
<Link to="/" className="Navbar__item" onlyActiveOnIndex activeClassName="Navbar__item--active">Home</Link>
|
};
|
||||||
<Link to="/soundboard" className="Navbar__item" activeClassName="Navbar__item--active">Soundboard</Link>
|
}
|
||||||
<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>
|
componentDidMount() {
|
||||||
<Link to="/clips" className="Navbar__item" activeClassName="Navbar__item--active">Clips</Link>
|
const token = storage.getJWT();
|
||||||
<a href={oauthUrl} className="Navbar__item">Login</a>
|
|
||||||
</div>
|
if (token) {
|
||||||
);
|
const claims: any = jwt_decode(token!);
|
||||||
|
const email = claims['email'];
|
||||||
|
this.setState({ token, email });
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private logout = () => {
|
||||||
|
localStorage.clear();
|
||||||
|
window.location.href = '/';
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<div className="Navbar">
|
||||||
|
<div className="Navbar__header">Go Discord Bot</div>
|
||||||
|
<Link to="/" className="Navbar__item" onlyActiveOnIndex activeClassName="Navbar__item--active">Home</Link>
|
||||||
|
<Link to="/soundboard" className="Navbar__item" activeClassName="Navbar__item--active">Soundboard</Link>
|
||||||
|
<Link to="/downloader" className="Navbar__item" activeClassName="Navbar__item--active">Youtube Downloader</Link>
|
||||||
|
{/* PUBG - DEPRECATED */}
|
||||||
|
{/* <Link to="/pubg" className="Navbar__item" activeClassName="Navbar__item--active">Pubg</Link> */}
|
||||||
|
<Link to="/clips" className="Navbar__item" activeClassName="Navbar__item--active">Clips</Link>
|
||||||
|
|
||||||
|
{ !this.state.token ?
|
||||||
|
<a href={oauthUrl} className="Navbar__item">Login</a> :
|
||||||
|
<a className="Navbar__item" onClick={this.logout}>Logout</a>
|
||||||
|
}
|
||||||
|
|
||||||
|
{this.state.email && <div className="Navbar__email">{this.state.email}</div>}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// DEPRECATED
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
|
|||||||
@@ -5,141 +5,139 @@ import axios, { AxiosRequestConfig } from 'axios';
|
|||||||
import { SoundList, SoundType } from '../../components/SoundList';
|
import { SoundList, SoundType } from '../../components/SoundList';
|
||||||
|
|
||||||
import './Soundboard.scss';
|
import './Soundboard.scss';
|
||||||
|
import { storage } from '../../storage';
|
||||||
|
|
||||||
let self: any;
|
let self: any;
|
||||||
|
|
||||||
interface Props {
|
interface Props {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
percentCompleted: number;
|
percentCompleted: number;
|
||||||
password: string;
|
uploaded: boolean;
|
||||||
uploaded: boolean;
|
uploadError: string;
|
||||||
uploadError: string;
|
soundList: SoundType[];
|
||||||
soundList: SoundType[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Soundboard extends React.Component<Props, State> {
|
export class Soundboard extends React.Component<Props, State> {
|
||||||
|
private config: AxiosRequestConfig;
|
||||||
private config: AxiosRequestConfig;
|
private soundListCache: any;
|
||||||
private soundListCache: any;
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.state = {
|
(this.state = {
|
||||||
percentCompleted: 0,
|
percentCompleted: 0,
|
||||||
password: "",
|
uploaded: false,
|
||||||
uploaded: false,
|
uploadError: ' ',
|
||||||
uploadError: " ",
|
soundList: [],
|
||||||
soundList: [],
|
}),
|
||||||
},
|
(self = this);
|
||||||
|
}
|
||||||
|
|
||||||
self = this;
|
componentDidMount() {
|
||||||
}
|
this.config = {
|
||||||
|
headers: {
|
||||||
componentDidMount() {
|
'Content-Type': 'multipart/form-data',
|
||||||
this.config = {
|
'Authorization': `Bearer ${storage.getJWT()}`
|
||||||
headers: {
|
},
|
||||||
'Content-Type': 'multipart/form-data',
|
onUploadProgress: progressEvent => {
|
||||||
},
|
|
||||||
onUploadProgress: (progressEvent) => {
|
|
||||||
this.setState({
|
|
||||||
percentCompleted: Math.round( (progressEvent.loaded * 100) / progressEvent.total ),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
this.getSoundList();
|
|
||||||
}
|
|
||||||
|
|
||||||
private getSoundList() {
|
|
||||||
if (!this.soundListCache) {
|
|
||||||
axios.get("/api/soundlist").then((response) => {
|
|
||||||
this.soundListCache = response.data;
|
|
||||||
this.setState({
|
|
||||||
soundList: response.data,
|
|
||||||
});
|
|
||||||
}).catch((error: any) => {
|
|
||||||
console.error(error.response.data);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this.setState({
|
|
||||||
soundList: this.soundListCache,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
onDrop(acceptedFiles: any) {
|
|
||||||
if (acceptedFiles.length > 0) {
|
|
||||||
self.uploadFile(acceptedFiles[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uploadFile(file: any) {
|
|
||||||
let formData = new FormData();
|
|
||||||
formData.append("name", file.name);
|
|
||||||
formData.append("file", file);
|
|
||||||
formData.append("password", this.state.password);
|
|
||||||
|
|
||||||
axios.post("/api/upload", formData, this.config)
|
|
||||||
.then(() => {
|
|
||||||
this.setState({
|
|
||||||
password: "",
|
|
||||||
percentCompleted: 0,
|
|
||||||
uploaded: true,
|
|
||||||
uploadError: " ",
|
|
||||||
});
|
|
||||||
|
|
||||||
this.soundListCache = undefined;
|
|
||||||
this.getSoundList();
|
|
||||||
}).catch((err) => {
|
|
||||||
this.setState({
|
|
||||||
percentCompleted: 0,
|
|
||||||
uploaded: false,
|
|
||||||
uploadError: err.response.data,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
passwordOnChange(event: any) {
|
|
||||||
this.setState({
|
this.setState({
|
||||||
password: event.target.value,
|
percentCompleted: Math.round(
|
||||||
|
progressEvent.loaded * 100 / progressEvent.total,
|
||||||
|
),
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
this.getSoundList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private getSoundList() {
|
||||||
|
if (!this.soundListCache) {
|
||||||
|
axios
|
||||||
|
.get('/api/soundlist')
|
||||||
|
.then(response => {
|
||||||
|
this.soundListCache = response.data;
|
||||||
|
this.setState({
|
||||||
|
soundList: response.data,
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
console.error(error.response.data);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.setState({
|
||||||
|
soundList: this.soundListCache,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
render() {
|
|
||||||
const { soundList } = this.state;
|
onDrop(acceptedFiles: any) {
|
||||||
return (
|
if (acceptedFiles.length > 0) {
|
||||||
<div className="Soundboard">
|
self.uploadFile(acceptedFiles[0]);
|
||||||
<div className="column">
|
|
||||||
<SoundList soundList={soundList} type="Sounds"/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="column">
|
|
||||||
<div>
|
|
||||||
<Dropzone className="Dropzone"
|
|
||||||
activeClassName="Dropzone--active"
|
|
||||||
onDrop={this.onDrop}
|
|
||||||
multiple={false}
|
|
||||||
disableClick={true}
|
|
||||||
maxSize={10000000000}
|
|
||||||
accept={"audio/*"}>
|
|
||||||
|
|
||||||
<input className="input Soundboard__input"
|
|
||||||
type="password"
|
|
||||||
placeholder="Password"
|
|
||||||
value={this.state.password}
|
|
||||||
onChange={this.passwordOnChange.bind(this)}/>
|
|
||||||
|
|
||||||
<div style={{fontSize: "20px"}}>Drop file here to upload.</div>
|
|
||||||
{this.state.percentCompleted > 0 ? <div>Uploading: {this.state.percentCompleted}</div> : ""}
|
|
||||||
{this.state.uploaded ? <div style={{color: 'green'}}>File uploded!</div> : ""}
|
|
||||||
<div style={{color: '#f95f59'}}>{this.state.uploadError}</div>
|
|
||||||
</Dropzone>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uploadFile(file: any) {
|
||||||
|
let formData = new FormData();
|
||||||
|
formData.append('name', file.name);
|
||||||
|
formData.append('file', file);
|
||||||
|
|
||||||
|
axios
|
||||||
|
.post('/api/upload', formData, this.config)
|
||||||
|
.then(() => {
|
||||||
|
this.setState({
|
||||||
|
percentCompleted: 0,
|
||||||
|
uploaded: true,
|
||||||
|
uploadError: ' ',
|
||||||
|
});
|
||||||
|
|
||||||
|
this.soundListCache = undefined;
|
||||||
|
this.getSoundList();
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
this.setState({
|
||||||
|
percentCompleted: 0,
|
||||||
|
uploaded: false,
|
||||||
|
uploadError: err.response.data,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const { soundList } = this.state;
|
||||||
|
return (
|
||||||
|
<div className="Soundboard">
|
||||||
|
<div className="column">
|
||||||
|
<SoundList soundList={soundList} type="Sounds" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="column">
|
||||||
|
<div>
|
||||||
|
<Dropzone
|
||||||
|
className="Dropzone"
|
||||||
|
activeClassName="Dropzone--active"
|
||||||
|
onDrop={this.onDrop}
|
||||||
|
multiple={false}
|
||||||
|
disableClick={true}
|
||||||
|
maxSize={10000000000}
|
||||||
|
accept={'audio/*'}
|
||||||
|
>
|
||||||
|
<div style={{ fontSize: '20px' }}>Drop file here to upload.</div>
|
||||||
|
{this.state.percentCompleted > 0 ? (
|
||||||
|
<div>Uploading: {this.state.percentCompleted}</div>
|
||||||
|
) : (
|
||||||
|
''
|
||||||
|
)}
|
||||||
|
{this.state.uploaded ? (
|
||||||
|
<div style={{ color: 'green' }}>File uploded!</div>
|
||||||
|
) : (
|
||||||
|
''
|
||||||
|
)}
|
||||||
|
<div style={{ color: '#f95f59' }}>{this.state.uploadError}</div>
|
||||||
|
</Dropzone>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,36 +1,37 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { get } from 'lodash';
|
import { get } from 'lodash';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import { storage } from '../../storage';
|
||||||
|
|
||||||
interface Props {
|
interface Props {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
interface State {
|
interface State {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Oauth extends React.Component<Props, State> {
|
export class Oauth extends React.Component<Props, State> {
|
||||||
|
constructor(props: Props) {
|
||||||
constructor(props: Props) {
|
super(props);
|
||||||
super(props);
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
const code = get(this, 'props.location.query.code');
|
||||||
|
|
||||||
|
if (code) {
|
||||||
|
// do stuff here
|
||||||
|
this.fetchOauth(code as string);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
componentDidMount() {
|
|
||||||
const code = get(this, 'props.location.query.code');
|
private async fetchOauth(code: string) {
|
||||||
|
try {
|
||||||
if (code) {
|
const res = await axios.post('/api/oauth', { code });
|
||||||
// do stuff here
|
storage.setJWT(res.data);
|
||||||
this.fetchOauth(code as string);
|
window.location.href = '/';
|
||||||
}
|
} catch (e) {
|
||||||
}
|
console.error(e);
|
||||||
|
|
||||||
private async fetchOauth(code: string) {
|
|
||||||
const res = await axios.post('/api/oauth', { code });
|
|
||||||
console.log(res);
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
|
||||||
return <div></div>
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return <div />;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
client/app/storage.ts
Normal file
12
client/app/storage.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
const setJWT = (token: string) => {
|
||||||
|
localStorage.setItem('jwt', token);
|
||||||
|
};
|
||||||
|
|
||||||
|
const getJWT = (): string | null => {
|
||||||
|
return localStorage.getItem('jwt');
|
||||||
|
};
|
||||||
|
|
||||||
|
export const storage = {
|
||||||
|
getJWT,
|
||||||
|
setJWT,
|
||||||
|
};
|
||||||
@@ -3,14 +3,16 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "A seed for a simple react application",
|
"description": "A seed for a simple react application",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "webpack -p --progress --colors",
|
"build": "NODE_ENV=prod webpack -p --progress --colors",
|
||||||
"dev": "webpack --watch",
|
"dev": "webpack --watch",
|
||||||
"c9": "webpack-dev-server --host 0.0.0.0 --port 8080 --inline --hot --history-api-fallback"
|
"c9": "webpack-dev-server --host 0.0.0.0 --port 8080 --inline --hot --history-api-fallback"
|
||||||
},
|
},
|
||||||
"author": "Mitchell Gerber",
|
"author": "Mitchell Gerber",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@types/jwt-decode": "^2.2.1",
|
||||||
"@types/lodash": "^4.14.71",
|
"@types/lodash": "^4.14.71",
|
||||||
|
"@types/node": "^9.4.6",
|
||||||
"@types/react": "^16.0.0",
|
"@types/react": "^16.0.0",
|
||||||
"@types/react-dom": "^15.5.1",
|
"@types/react-dom": "^15.5.1",
|
||||||
"@types/react-dropzone": "^3.13.1",
|
"@types/react-dropzone": "^3.13.1",
|
||||||
@@ -31,6 +33,7 @@
|
|||||||
"extract-text-webpack-plugin": "2.0.0-rc.1",
|
"extract-text-webpack-plugin": "2.0.0-rc.1",
|
||||||
"file-loader": "^0.10.0",
|
"file-loader": "^0.10.0",
|
||||||
"html-webpack-plugin": "^2.24.1",
|
"html-webpack-plugin": "^2.24.1",
|
||||||
|
"jwt-decode": "^2.2.0",
|
||||||
"lodash": "^4.17.4",
|
"lodash": "^4.17.4",
|
||||||
"node-sass": "^4.5.3",
|
"node-sass": "^4.5.3",
|
||||||
"postcss-loader": "^1.2.1",
|
"postcss-loader": "^1.2.1",
|
||||||
|
|||||||
@@ -4,66 +4,86 @@ const path = require('path');
|
|||||||
const webpack = require('webpack');
|
const webpack = require('webpack');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
entry: {
|
entry: {
|
||||||
app: './app/app.tsx',
|
app: './app/app.tsx',
|
||||||
vendor: ['react', 'react-dom']
|
vendor: ['react', 'react-dom'],
|
||||||
},
|
},
|
||||||
output: {
|
output: {
|
||||||
path: path.resolve(__dirname, '../dist'),
|
path: path.resolve(__dirname, '../dist'),
|
||||||
filename: './static/[name].[hash].js'
|
filename: './static/[name].[hash].js',
|
||||||
},
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
extensions: ['.ts', '.tsx', '.js']
|
extensions: ['.ts', '.tsx', '.js'],
|
||||||
},
|
},
|
||||||
module: {
|
module: {
|
||||||
rules: [{
|
rules: [
|
||||||
test: /\.(js|jsx)$/,
|
{
|
||||||
loaders: ['babel-loader']
|
test: /\.(js|jsx)$/,
|
||||||
}, {
|
loaders: ['babel-loader'],
|
||||||
test: /\.ts(x)?$/,
|
},
|
||||||
loaders: ['babel-loader', 'ts-loader']
|
{
|
||||||
},{
|
test: /\.ts(x)?$/,
|
||||||
test: /\.scss$/,
|
loaders: ['babel-loader', 'ts-loader'],
|
||||||
loader: ExtractTextPlugin.extract({
|
},
|
||||||
fallbackLoader: 'style-loader',
|
{
|
||||||
loader: 'css-loader!postcss-loader!sass-loader'
|
test: /\.scss$/,
|
||||||
})
|
loader: ExtractTextPlugin.extract({
|
||||||
}, {
|
fallbackLoader: 'style-loader',
|
||||||
test: /\.css$/,
|
loader: 'css-loader!postcss-loader!sass-loader',
|
||||||
loader: ExtractTextPlugin.extract({
|
|
||||||
fallbackLoader: 'style-loader',
|
|
||||||
loader: 'css-loader'
|
|
||||||
})
|
|
||||||
}, {
|
|
||||||
test: /\.svg$/,
|
|
||||||
loader: 'url-loader?limit=65000&mimetype=image/svg+xml&name=static/[name].[ext]&publicPath=../'
|
|
||||||
}, {
|
|
||||||
test: /\.woff$/,
|
|
||||||
loader: 'url-loader?limit=65000&mimetype=application/font-woff&name=static/[name].[ext]&publicPath=../'
|
|
||||||
}, {
|
|
||||||
test: /\.woff2$/,
|
|
||||||
loader: 'url-loader?limit=65000&mimetype=application/font-woff2&name=static/[name].[ext]&publicPath=../'
|
|
||||||
}, {
|
|
||||||
test: /\.[ot]tf$/,
|
|
||||||
loader: 'url-loader?limit=65000&mimetype=application/octet-stream&name=static/[name].[ext]&publicPath=../'
|
|
||||||
}, {
|
|
||||||
test: /\.eot$/,
|
|
||||||
loader: 'url-loader?limit=65000&mimetype=application/vnd.ms-fontobject&name=static/[name].[ext]&publicPath=../'
|
|
||||||
}]
|
|
||||||
},
|
|
||||||
plugins: [
|
|
||||||
new ExtractTextPlugin({
|
|
||||||
filename: '/static/[name].[hash].css',
|
|
||||||
disable: false,
|
|
||||||
allChunks: true
|
|
||||||
}),
|
}),
|
||||||
new HtmlWebpackPlugin({
|
},
|
||||||
filename: 'index.html',
|
{
|
||||||
template: './index.html'
|
test: /\.css$/,
|
||||||
|
loader: ExtractTextPlugin.extract({
|
||||||
|
fallbackLoader: 'style-loader',
|
||||||
|
loader: 'css-loader',
|
||||||
}),
|
}),
|
||||||
new webpack.optimize.CommonsChunkPlugin({
|
},
|
||||||
name: ['vendor', 'manifest'],
|
{
|
||||||
minChunks: 'Infinity'
|
test: /\.svg$/,
|
||||||
})
|
loader:
|
||||||
]
|
'url-loader?limit=65000&mimetype=image/svg+xml&name=static/[name].[ext]&publicPath=../',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: /\.woff$/,
|
||||||
|
loader:
|
||||||
|
'url-loader?limit=65000&mimetype=application/font-woff&name=static/[name].[ext]&publicPath=../',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: /\.woff2$/,
|
||||||
|
loader:
|
||||||
|
'url-loader?limit=65000&mimetype=application/font-woff2&name=static/[name].[ext]&publicPath=../',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: /\.[ot]tf$/,
|
||||||
|
loader:
|
||||||
|
'url-loader?limit=65000&mimetype=application/octet-stream&name=static/[name].[ext]&publicPath=../',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: /\.eot$/,
|
||||||
|
loader:
|
||||||
|
'url-loader?limit=65000&mimetype=application/vnd.ms-fontobject&name=static/[name].[ext]&publicPath=../',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
new ExtractTextPlugin({
|
||||||
|
filename: '/static/[name].[hash].css',
|
||||||
|
disable: false,
|
||||||
|
allChunks: true,
|
||||||
|
}),
|
||||||
|
new HtmlWebpackPlugin({
|
||||||
|
filename: 'index.html',
|
||||||
|
template: './index.html',
|
||||||
|
}),
|
||||||
|
new webpack.optimize.CommonsChunkPlugin({
|
||||||
|
name: ['vendor', 'manifest'],
|
||||||
|
minChunks: 'Infinity',
|
||||||
|
}),
|
||||||
|
new webpack.DefinePlugin({
|
||||||
|
'process.env': {
|
||||||
|
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
],
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,10 +6,18 @@
|
|||||||
version "3.2.1"
|
version "3.2.1"
|
||||||
resolved "https://registry.yarnpkg.com/@types/history/-/history-3.2.1.tgz#0039ab0e0be2a0cc22bac171d27a44588103d123"
|
resolved "https://registry.yarnpkg.com/@types/history/-/history-3.2.1.tgz#0039ab0e0be2a0cc22bac171d27a44588103d123"
|
||||||
|
|
||||||
|
"@types/jwt-decode@^2.2.1":
|
||||||
|
version "2.2.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/jwt-decode/-/jwt-decode-2.2.1.tgz#afdf5c527fcfccbd4009b5fd02d1e18241f2d2f2"
|
||||||
|
|
||||||
"@types/lodash@^4.14.71":
|
"@types/lodash@^4.14.71":
|
||||||
version "4.14.71"
|
version "4.14.71"
|
||||||
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.71.tgz#0dc383f78981216ac76e2f2c3afd998e0450e4c1"
|
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.71.tgz#0dc383f78981216ac76e2f2c3afd998e0450e4c1"
|
||||||
|
|
||||||
|
"@types/node@^9.4.6":
|
||||||
|
version "9.4.6"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.4.6.tgz#d8176d864ee48753d053783e4e463aec86b8d82e"
|
||||||
|
|
||||||
"@types/react-dom@^15.5.1":
|
"@types/react-dom@^15.5.1":
|
||||||
version "15.5.1"
|
version "15.5.1"
|
||||||
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-15.5.1.tgz#f3c3e14c682785923c7d64583537df319442dec1"
|
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-15.5.1.tgz#f3c3e14c682785923c7d64583537df319442dec1"
|
||||||
@@ -2900,6 +2908,10 @@ jsx-ast-utils@^1.3.4:
|
|||||||
dependencies:
|
dependencies:
|
||||||
object-assign "^4.1.0"
|
object-assign "^4.1.0"
|
||||||
|
|
||||||
|
jwt-decode@^2.2.0:
|
||||||
|
version "2.2.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/jwt-decode/-/jwt-decode-2.2.0.tgz#7d86bd56679f58ce6a84704a657dd392bba81a79"
|
||||||
|
|
||||||
kind-of@^3.0.2:
|
kind-of@^3.0.2:
|
||||||
version "3.1.0"
|
version "3.1.0"
|
||||||
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.1.0.tgz#475d698a5e49ff5e53d14e3e732429dc8bf4cf47"
|
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.1.0.tgz#475d698a5e49ff5e53d14e3e732429dc8bf4cf47"
|
||||||
|
|||||||
@@ -3,11 +3,17 @@
|
|||||||
"client_id": "",
|
"client_id": "",
|
||||||
"client_secret": "",
|
"client_secret": "",
|
||||||
"redirect_uri": "",
|
"redirect_uri": "",
|
||||||
"upload_password": "",
|
|
||||||
"bot_prefix": "#",
|
"bot_prefix": "#",
|
||||||
|
|
||||||
|
"admin_emails": ["mail@example.com"],
|
||||||
|
|
||||||
|
"jwt_key": "",
|
||||||
|
"server_addr": "0.0.0.0:80",
|
||||||
|
|
||||||
"sounds_path": "./sounds/",
|
"sounds_path": "./sounds/",
|
||||||
"clips_path": "./clips/",
|
"clips_path": "./clips/",
|
||||||
"server_addr": "0.0.0.0:80",
|
|
||||||
"pubg": {
|
"pubg": {
|
||||||
"api_key": "",
|
"api_key": "",
|
||||||
"enabled": false,
|
"enabled": false,
|
||||||
|
|||||||
@@ -20,13 +20,16 @@ type configFile struct {
|
|||||||
ClientSecret string `json:"client_secret"`
|
ClientSecret string `json:"client_secret"`
|
||||||
RedirectURI string `json:"redirect_uri"`
|
RedirectURI string `json:"redirect_uri"`
|
||||||
|
|
||||||
GuildID string `json:"guild_id"`
|
GuildID string `json:"guild_id"`
|
||||||
UploadPassword string `json:"upload_password"`
|
|
||||||
|
BotPrefix string `json:"bot_prefix"` //prefix to use for bot commands
|
||||||
|
|
||||||
BotPrefix string `json:"bot_prefix"` //prefix to use for bot commands
|
|
||||||
SoundsPath string `json:"sounds_path"`
|
SoundsPath string `json:"sounds_path"`
|
||||||
ClipsPath string `json:"clips_path"`
|
ClipsPath string `json:"clips_path"`
|
||||||
ServerAddr string `json:"server_addr"`
|
|
||||||
|
AdminEmails []string `json:"admin_emails"`
|
||||||
|
ServerAddr string `json:"server_addr"`
|
||||||
|
JWTKey string `json:"jwt_key"`
|
||||||
|
|
||||||
Pubg struct {
|
Pubg struct {
|
||||||
Enabled bool `json:"enabled"`
|
Enabled bool `json:"enabled"`
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/mgerb/go-discord-bot/server/webserver/discord"
|
"github.com/mgerb/go-discord-bot/server/webserver/discord"
|
||||||
|
"github.com/mgerb/go-discord-bot/server/webserver/middleware"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -27,6 +26,7 @@ func Oauth(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get users oauth code
|
||||||
oauth, err := discord.Oauth(json.Code)
|
oauth, err := discord.Oauth(json.Code)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -35,6 +35,7 @@ func Oauth(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// verify and grab user information
|
||||||
user, err := discord.GetUserInfo(oauth.AccessToken)
|
user, err := discord.GetUserInfo(oauth.AccessToken)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -43,8 +44,14 @@ func Oauth(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: generate jwt for user
|
// generate json web token
|
||||||
fmt.Println(user)
|
token, err := middleware.GetJWT(user)
|
||||||
|
|
||||||
c.JSON(200, oauth)
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
c.JSON(500, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, token)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,15 +11,12 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FileUpload
|
// FileUpload -
|
||||||
func FileUpload(c *gin.Context) {
|
func FileUpload(c *gin.Context) {
|
||||||
|
|
||||||
password := c.PostForm("password")
|
// originalClaims, _ := c.Get("claims")
|
||||||
|
// claims, _ := originalClaims.(*middleware.CustomClaims)
|
||||||
if string(password) != config.Config.UploadPassword {
|
// TODO: verify user for upload
|
||||||
c.JSON(http.StatusInternalServerError, "Invalid password.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := c.FormFile("file")
|
file, err := c.FormFile("file")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
90
server/webserver/middleware/jwt.go
Normal file
90
server/webserver/middleware/jwt.go
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"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"
|
||||||
|
"gopkg.in/dgrijalva/jwt-go.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CustomClaims -
|
||||||
|
type CustomClaims struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
Discriminator string `json:"discriminator"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
Permissions string `json:"permissions"`
|
||||||
|
jwt.StandardClaims
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetJWT - get json web token
|
||||||
|
func GetJWT(user discord.User) (string, error) {
|
||||||
|
|
||||||
|
permissions := "user"
|
||||||
|
|
||||||
|
// check if email is in config admin list
|
||||||
|
for _, email := range config.Config.AdminEmails {
|
||||||
|
if user.Email == email {
|
||||||
|
permissions = "admin"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
claims := CustomClaims{
|
||||||
|
user.ID,
|
||||||
|
user.Username,
|
||||||
|
user.Discriminator,
|
||||||
|
user.Email,
|
||||||
|
permissions,
|
||||||
|
jwt.StandardClaims{
|
||||||
|
ExpiresAt: time.Now().AddDate(0, 1, 0).Unix(), // one month
|
||||||
|
Issuer: "Go Discord Bot",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
|
return token.SignedString([]byte(config.Config.JWTKey))
|
||||||
|
}
|
||||||
|
|
||||||
|
// AuthorizedJWT - jwt middleware
|
||||||
|
func AuthorizedJWT() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
|
||||||
|
// grab token from authorization header: Bearer token
|
||||||
|
tokenString := strings.Split(c.GetHeader("Authorization"), " ")
|
||||||
|
|
||||||
|
if len(tokenString) != 2 {
|
||||||
|
c.JSON(401, "Unauthorized")
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse and verify token
|
||||||
|
token, err := jwt.ParseWithClaims(tokenString[1], &CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||||
|
return []byte(config.Config.JWTKey), nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
c.JSON(401, "Unauthorized")
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// get claims and set on gin context
|
||||||
|
if claims, ok := token.Claims.(*CustomClaims); ok && token.Valid {
|
||||||
|
c.Set("claims", claims)
|
||||||
|
} else {
|
||||||
|
log.Error(err)
|
||||||
|
c.JSON(401, "Unauthorized")
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/mgerb/go-discord-bot/server/config"
|
"github.com/mgerb/go-discord-bot/server/config"
|
||||||
"github.com/mgerb/go-discord-bot/server/webserver/handlers"
|
"github.com/mgerb/go-discord-bot/server/webserver/handlers"
|
||||||
|
"github.com/mgerb/go-discord-bot/server/webserver/middleware"
|
||||||
"github.com/mgerb/go-discord-bot/server/webserver/pubg"
|
"github.com/mgerb/go-discord-bot/server/webserver/pubg"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -24,9 +25,12 @@ func getRouter() *gin.Engine {
|
|||||||
api.GET("/ytdownloader", handlers.Downloader)
|
api.GET("/ytdownloader", handlers.Downloader)
|
||||||
api.GET("/soundlist", handlers.SoundList)
|
api.GET("/soundlist", handlers.SoundList)
|
||||||
api.GET("/cliplist", handlers.ClipList)
|
api.GET("/cliplist", handlers.ClipList)
|
||||||
api.POST("/upload", handlers.FileUpload)
|
|
||||||
api.POST("/oauth", handlers.Oauth)
|
api.POST("/oauth", handlers.Oauth)
|
||||||
|
|
||||||
|
authorizedAPI := router.Group("/api")
|
||||||
|
authorizedAPI.Use(middleware.AuthorizedJWT())
|
||||||
|
authorizedAPI.POST("/upload", handlers.FileUpload)
|
||||||
|
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user