Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #343 from sundeepgupta/redirect_params
Browse files Browse the repository at this point in the history
Add flexibility to Slack auth flow via custom redirect params
  • Loading branch information
Ben Brown authored Nov 7, 2016
2 parents 9aab9b6 + ab9ae8b commit 908171f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
29 changes: 23 additions & 6 deletions lib/SlackBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var Botkit = require(__dirname + '/CoreBot.js');
var request = require('request');
var express = require('express');
var bodyParser = require('body-parser');
var querystring = require('querystring');

function Slackbot(configuration) {

Expand Down Expand Up @@ -303,7 +304,7 @@ function Slackbot(configuration) {
};

// get a team url to redirect the user through oauth process
slack_botkit.getAuthorizeURL = function(team_id) {
slack_botkit.getAuthorizeURL = function(team_id, redirect_params) {

var scopes = slack_botkit.config.scopes;
var url = 'https://slack.com/oauth/authorize' + '?client_id=' +
Expand All @@ -312,8 +313,14 @@ function Slackbot(configuration) {
if (team_id)
url += '&team=' + team_id;

if (slack_botkit.config.redirectUri)
url += '&redirect_uri=' + slack_botkit.config.redirectUri;
if (slack_botkit.config.redirectUri) {
var redirect_query = '';
if (redirect_params)
redirect_query += encodeURIComponent(querystring.stringify(redirect_params));

var redirect_uri = slack_botkit.config.redirectUri + '?' + redirect_query;
url += '&redirect_uri=' + redirect_uri;
}

return url;

Expand Down Expand Up @@ -382,7 +389,17 @@ function Slackbot(configuration) {
code: code
};

if (slack_botkit.config.redirectUri) opts.redirect_uri = slack_botkit.config.redirectUri;
var redirect_params = {};
if (slack_botkit.config.redirectUri) {
Object.assign(redirect_params, req.query);
delete redirect_params.code;
delete redirect_params.state;

var redirect_query = querystring.stringify(redirect_params);
var redirect_uri = slack_botkit.config.redirectUri + '?' + redirect_query;

opts.redirect_uri = redirect_uri;
}

oauth_access(opts, function(err, auth) {

Expand Down Expand Up @@ -508,9 +525,9 @@ function Slackbot(configuration) {
slack_botkit.trigger('error', [err]);
} else {
if (isnew) {
slack_botkit.trigger('create_user', [bot, user]);
slack_botkit.trigger('create_user', [bot, user, redirect_params]);
} else {
slack_botkit.trigger('update_user', [bot, user]);
slack_botkit.trigger('update_user', [bot, user, redirect_params]);
}
if (callback) {
callback(null, req, res);
Expand Down
18 changes: 18 additions & 0 deletions readme-slack.md
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,24 @@ controller.setupWebserver(process.env.port,function(err,webserver) {

```

#### Custom auth flows
In addition to the Slack Button, you can send users through an auth flow via a Slack interaction.
The `getAuthorizeURL` provides the url. It requires the `team_id` and accepts an optional `redirect_params` argument.
```javascript
controller.getAuthorizeURL(team_id, redirect_params);
```

The `redirect_params` argument is passed back into the `create_user` and `update_user` events so you can handle
auth flows in different ways. For example:
```javascript
controller.on('create_user', function(bot, user, redirect_params) {
if (redirect_params.slash_command_id) {
// continue processing the slash command for the user
}
}
```
### How to identify what team your message came from
```javascript
var team = bot.identifyTeam() // returns team id
Expand Down

0 comments on commit 908171f

Please sign in to comment.