Skip to content

Instantly share code, notes, and snippets.

@mvaragnat
Created November 29, 2016 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvaragnat/b835f089ee29e666c69a75c83df199e0 to your computer and use it in GitHub Desktop.
Save mvaragnat/b835f089ee29e666c69a75c83df199e0 to your computer and use it in GitHub Desktop.
/*
This gist shows a modification of the basic Oauth2 Passport strategy, to be suitable for Slack platform
Module dependencies.
*/
var util = require('util')
var OAuth2Strategy = require('passport-oauth2').Strategy
/**
* `Strategy` constructor.
*
* The Slack authentication strategy authenticates requests by delegating
* to Slack using the OAuth 2.0 protocol.
*
* Applications must supply a `verify` callback which accepts an `accessToken`,
* `refreshToken` and service-specific `profile`, and then calls the `done`
* callback supplying a `user`, which should be set to `false` if the
* credentials are not valid. If an exception occured, `err` should be set.
*
* Options:
* - `clientID` your Slack application's client id
* - `clientSecret` your Slack application's client secret
* - `callbackURL` URL to which Slack will redirect the user after granting authorization
* - `scope` array of permission scopes to request, for example:
* 'identify', 'channels:read', 'chat:write:user', 'client', or 'admin'
* full set of scopes: https://api.slack.com/docs/oauth-scopes
*
* Examples:
*
* passport.use(new SlackStrategy({
* clientID: '123-456-789',
* clientSecret: 'shhh-its-a-secret'
* callbackURL: 'https://www.example.net/auth/slack/callback',
* scope: 'identify channels:read chat:write:user client admin'
* },
* function(accessToken, refreshToken, profile, done) {
* User.findOrCreate(..., function (err, user) {
* done(err, user);
* });
* }
* ));
*
* @param {Object} options
* @param {Function} verify
* @api public
*/
function Strategy (options, verify) {
options = options || {}
options.authorizationURL = options.authorizationURL || 'https://slack.com/oauth/authorize'
options.tokenURL = options.tokenURL || 'https://slack.com/api/oauth.access'
options.scope = options.scope || 'identity.basic'
this.profileUrl = options.profileUrl || 'https://slack.com/api/users.identity'
this._team = options.team
OAuth2Strategy.call(this, options, verify)
this.name = options.name || 'slack'
this._oauth2.setAccessTokenName('token')
}
/**
* Inherit from `OAuth2Strategy`.
*/
util.inherits(Strategy, OAuth2Strategy)
/**
* Retrieve user profile from Slack.
*
* This function constructs a normalized profile, with the following properties:
*
* - `provider` always set to `slack`
* - `id` the user's ID
* - `displayName` the user's username
*
* as well as additional properties
* - `team_id` the team's ID
*
* @param {String} accessToken
* @param {Function} done
* @api protected
*/
Strategy.prototype.userProfile = function (accessToken, done) {
this._oauth2.get(this.profileUrl, accessToken, function (err, body) {
if (err) {
return done(err)
}
else {
try {
var json = JSON.parse(body)
// console.log("results from identity", json)
var profile = {
provider: 'slack'
}
if (json.ok) {
profile.team_id = json.team.id
profile.id = json.user.id
profile.displayName = json.user.name
}
else {
console.log(json)
}
return done(null, profile)
}
catch (e) {
console.log(e)
return done(e)
}
}
})
}
/**
* Expose `Strategy`.
*/
module.exports = Strategy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment