Fixed auth
This commit is contained in:
@ -13,5 +13,10 @@
|
|||||||
"config": "webpack.config.js"
|
"config": "webpack.config.js"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
"globals": {
|
||||||
|
__DEV__: true,
|
||||||
|
SLACK_KEY: true,
|
||||||
|
SLACK_REDIRECT_URI: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
@ -16,22 +16,20 @@ class SlackAuthLink extends React.Component {
|
|||||||
'identity.basic',
|
'identity.basic',
|
||||||
'identity.avatar',
|
'identity.avatar',
|
||||||
'identity.team',
|
'identity.team',
|
||||||
]
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
slackUrl = () => {
|
slackUrl = () => {
|
||||||
const baseUrl = 'https://slack.com/oauth/authorize';
|
const baseUrl = 'https://slack.com/oauth/authorize';
|
||||||
const params = {
|
const params = {
|
||||||
client_id: '30086650419.30130733398',
|
client_id: SLACK_KEY,
|
||||||
scope: this.props.scopes.join(" "),
|
scope: this.props.scopes.join(' '),
|
||||||
redirect_uri: __DEV__ ?
|
redirect_uri: SLACK_REDIRECT_URI,
|
||||||
'http://localhost:3000/auth/slack/' :
|
|
||||||
'https://www.beautifulatlas.com/auth/slack/',
|
|
||||||
state: this.props.user.getOauthState(),
|
state: this.props.user.getOauthState(),
|
||||||
};
|
};
|
||||||
|
|
||||||
const urlParams = Object.keys(params).map(function(key) {
|
const urlParams = Object.keys(params).map((key) => {
|
||||||
return key + '=' + encodeURIComponent(params[key]);
|
return `${key}=${encodeURIComponent(params[key])}`;
|
||||||
}).join('&');
|
}).join('&');
|
||||||
|
|
||||||
return `${baseUrl}?${urlParams}`;
|
return `${baseUrl}?${urlParams}`;
|
||||||
@ -40,7 +38,7 @@ class SlackAuthLink extends React.Component {
|
|||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<a href={ this.slackUrl() } className={ styles.link }>Authorize /w Slack</a>
|
<a href={ this.slackUrl() } className={ styles.link }>Authorize /w Slack</a>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import { observer } from 'mobx-react';
|
|||||||
class SlackAuth extends React.Component {
|
class SlackAuth extends React.Component {
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
user: React.PropTypes.object.isRequired,
|
user: React.PropTypes.object.isRequired,
|
||||||
|
location: React.PropTypes.object.isRequired,
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount = () => {
|
componentDidMount = () => {
|
||||||
@ -19,4 +20,4 @@ class SlackAuth extends React.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default SlackAuth;
|
export default SlackAuth;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import Router from 'koa-router';
|
import Router from 'koa-router';
|
||||||
import httpErrors from 'http-errors';
|
import httpErrors from 'http-errors';
|
||||||
import fetch from 'isomorphic-fetch';
|
import fetch from 'isomorphic-fetch';
|
||||||
var querystring = require('querystring');
|
import querystring from 'querystring';
|
||||||
|
|
||||||
import { presentUser, presentTeam } from '../presenters';
|
import { presentUser, presentTeam } from '../presenters';
|
||||||
import { User, Team } from '../models';
|
import { User, Team } from '../models';
|
||||||
@ -15,31 +15,29 @@ router.post('auth.slack', async (ctx) => {
|
|||||||
const body = {
|
const body = {
|
||||||
client_id: process.env.SLACK_KEY,
|
client_id: process.env.SLACK_KEY,
|
||||||
client_secret: process.env.SLACK_SECRET,
|
client_secret: process.env.SLACK_SECRET,
|
||||||
code: code,
|
|
||||||
redirect_uri: process.env.SLACK_REDIRECT_URI,
|
redirect_uri: process.env.SLACK_REDIRECT_URI,
|
||||||
}
|
code,
|
||||||
|
};
|
||||||
|
|
||||||
let data;
|
let data;
|
||||||
try {
|
try {
|
||||||
const response = await fetch('https://slack.com/api/oauth.access?' + querystring.stringify(body));
|
const response = await fetch(`https://slack.com/api/oauth.access?${querystring.stringify(body)}`);
|
||||||
data = await response.json();
|
data = await response.json();
|
||||||
} catch(e) {
|
} catch (e) {
|
||||||
throw httpErrors.BadRequest();
|
throw httpErrors.BadRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(data);
|
||||||
|
|
||||||
if (!data.ok) throw httpErrors.BadRequest(data.error);
|
if (!data.ok) throw httpErrors.BadRequest(data.error);
|
||||||
|
|
||||||
// Temp to block
|
// Temp to block
|
||||||
let allowedSlackIds = process.env.ALLOWED_SLACK_IDS.split(',');
|
const allowedSlackIds = process.env.ALLOWED_SLACK_IDS.split(',');
|
||||||
if (!allowedSlackIds.includes(data.team.id)) throw httpErrors.BadRequest("Invalid Slack team");
|
if (!allowedSlackIds.includes(data.team.id)) throw httpErrors.BadRequest('Invalid Slack team');
|
||||||
|
|
||||||
// User
|
// User
|
||||||
let userData;
|
|
||||||
let user = await User.findOne({ where: { slackId: data.user.id }});
|
let user = await User.findOne({ where: { slackId: data.user.id }});
|
||||||
|
|
||||||
const authResponse = await fetch(`https://slack.com/api/auth.test?token=${data.access_token}`);
|
|
||||||
const authData = await authResponse.json();
|
|
||||||
|
|
||||||
// Team
|
// Team
|
||||||
let team = await Team.findOne({ where: { slackId: data.team.id } });
|
let team = await Team.findOne({ where: { slackId: data.team.id } });
|
||||||
if (!team) {
|
if (!team) {
|
||||||
@ -48,7 +46,7 @@ router.post('auth.slack', async (ctx) => {
|
|||||||
slackId: data.team.id,
|
slackId: data.team.id,
|
||||||
slackData: data.team,
|
slackData: data.team,
|
||||||
});
|
});
|
||||||
const atlas = await team.createFirstAtlas();
|
await team.createFirstAtlas();
|
||||||
} else {
|
} else {
|
||||||
team.name = data.team.name;
|
team.name = data.team.name;
|
||||||
team.slackData = data.team;
|
team.slackData = data.team;
|
||||||
@ -62,7 +60,7 @@ router.post('auth.slack', async (ctx) => {
|
|||||||
} else {
|
} else {
|
||||||
user = await team.createUser({
|
user = await team.createUser({
|
||||||
slackId: data.user.id,
|
slackId: data.user.id,
|
||||||
username: authData.user,
|
username: data.user.name,
|
||||||
name: data.user.name,
|
name: data.user.name,
|
||||||
email: data.user.email,
|
email: data.user.email,
|
||||||
slackData: data.user,
|
slackData: data.user,
|
||||||
|
@ -8,7 +8,8 @@ require('dotenv').config();
|
|||||||
var definePlugin = new webpack.DefinePlugin({
|
var definePlugin = new webpack.DefinePlugin({
|
||||||
__DEV__: JSON.stringify(JSON.parse(process.env.NODE_ENV !== 'production')),
|
__DEV__: JSON.stringify(JSON.parse(process.env.NODE_ENV !== 'production')),
|
||||||
__PRERELEASE__: JSON.stringify(JSON.parse(process.env.BUILD_PRERELEASE || 'false')),
|
__PRERELEASE__: JSON.stringify(JSON.parse(process.env.BUILD_PRERELEASE || 'false')),
|
||||||
SLACK_REDIRECT_URI: process.env.SLACK_REDIRECT_URI,
|
SLACK_REDIRECT_URI: JSON.stringify(process.env.SLACK_REDIRECT_URI),
|
||||||
|
SLACK_KEY: JSON.stringify(process.env.SLACK_KEY),
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
Reference in New Issue
Block a user