Fixed auth

This commit is contained in:
Jori Lallo 2016-07-25 22:49:32 -07:00
parent c1c8644578
commit f08087ebd1
5 changed files with 28 additions and 25 deletions

View File

@ -13,5 +13,10 @@
"config": "webpack.config.js"
}
}
}
},
"globals": {
__DEV__: true,
SLACK_KEY: true,
SLACK_REDIRECT_URI: true,
},
}

View File

@ -16,22 +16,20 @@ class SlackAuthLink extends React.Component {
'identity.basic',
'identity.avatar',
'identity.team',
]
],
}
slackUrl = () => {
const baseUrl = 'https://slack.com/oauth/authorize';
const params = {
client_id: '30086650419.30130733398',
scope: this.props.scopes.join(" "),
redirect_uri: __DEV__ ?
'http://localhost:3000/auth/slack/' :
'https://www.beautifulatlas.com/auth/slack/',
client_id: SLACK_KEY,
scope: this.props.scopes.join(' '),
redirect_uri: SLACK_REDIRECT_URI,
state: this.props.user.getOauthState(),
};
const urlParams = Object.keys(params).map(function(key) {
return key + '=' + encodeURIComponent(params[key]);
const urlParams = Object.keys(params).map((key) => {
return `${key}=${encodeURIComponent(params[key])}`;
}).join('&');
return `${baseUrl}?${urlParams}`;
@ -40,7 +38,7 @@ class SlackAuthLink extends React.Component {
render() {
return (
<a href={ this.slackUrl() } className={ styles.link }>Authorize /w Slack</a>
)
);
}
}

View File

@ -5,6 +5,7 @@ import { observer } from 'mobx-react';
class SlackAuth extends React.Component {
static propTypes = {
user: React.PropTypes.object.isRequired,
location: React.PropTypes.object.isRequired,
}
componentDidMount = () => {
@ -19,4 +20,4 @@ class SlackAuth extends React.Component {
}
}
export default SlackAuth;
export default SlackAuth;

View File

@ -1,7 +1,7 @@
import Router from 'koa-router';
import httpErrors from 'http-errors';
import fetch from 'isomorphic-fetch';
var querystring = require('querystring');
import querystring from 'querystring';
import { presentUser, presentTeam } from '../presenters';
import { User, Team } from '../models';
@ -15,31 +15,29 @@ router.post('auth.slack', async (ctx) => {
const body = {
client_id: process.env.SLACK_KEY,
client_secret: process.env.SLACK_SECRET,
code: code,
redirect_uri: process.env.SLACK_REDIRECT_URI,
}
code,
};
let data;
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();
} catch(e) {
} catch (e) {
throw httpErrors.BadRequest();
}
console.log(data);
if (!data.ok) throw httpErrors.BadRequest(data.error);
// Temp to block
let allowedSlackIds = process.env.ALLOWED_SLACK_IDS.split(',');
if (!allowedSlackIds.includes(data.team.id)) throw httpErrors.BadRequest("Invalid Slack team");
const allowedSlackIds = process.env.ALLOWED_SLACK_IDS.split(',');
if (!allowedSlackIds.includes(data.team.id)) throw httpErrors.BadRequest('Invalid Slack team');
// User
let userData;
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
let team = await Team.findOne({ where: { slackId: data.team.id } });
if (!team) {
@ -48,7 +46,7 @@ router.post('auth.slack', async (ctx) => {
slackId: data.team.id,
slackData: data.team,
});
const atlas = await team.createFirstAtlas();
await team.createFirstAtlas();
} else {
team.name = data.team.name;
team.slackData = data.team;
@ -62,7 +60,7 @@ router.post('auth.slack', async (ctx) => {
} else {
user = await team.createUser({
slackId: data.user.id,
username: authData.user,
username: data.user.name,
name: data.user.name,
email: data.user.email,
slackData: data.user,

View File

@ -8,7 +8,8 @@ require('dotenv').config();
var definePlugin = new webpack.DefinePlugin({
__DEV__: JSON.stringify(JSON.parse(process.env.NODE_ENV !== 'production')),
__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 = {