forked from krishamoud/meteor-accounts-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack_client.js
63 lines (54 loc) · 2.11 KB
/
slack_client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Slack = {};
// Request Slack credentials for the user
// @param options {optional}
// @param credentialRequestCompleteCallback {Function} Callback function to call on
// completion. Takes one argument, credentialToken on success, or Error on
// error.
Slack.requestCredential = function (options, credentialRequestCompleteCallback) {
// support both (options, callback) and (callback).
if (!credentialRequestCompleteCallback && typeof options === 'function') {
credentialRequestCompleteCallback = options;
options = {};
}
var config = ServiceConfiguration.configurations.findOne({service: 'slack'});
if (!config) {
credentialRequestCompleteCallback && credentialRequestCompleteCallback(
new ServiceConfiguration.ConfigError());
return;
}
// For some reason, slack converts underscores to spaces in the state
// parameter when redirecting back to the client, so we use
// `Random.id()` here (alphanumerics) instead of `Random.secret()`
// (base 64 characters).
var credentialToken = Random.id();
var scope = (options && options.requestPermissions) || [];
var flatScope = _.map(scope, encodeURIComponent).join(',');
var loginStyle = OAuth._loginStyle('slack', config, options);
var loginUrl =
'https://slack.com/oauth/authorize' +
'?client_id=' + config.clientId +
((options && options.team) ? '&team=' + options.team : '') +
'&response_type=code' +
'&scope=' + flatScope +
'&redirect_uri=' + OAuth._redirectUri('slack', config) +
'&state=' + OAuth._stateParam(loginStyle, credentialToken);
// slack box gets taller when permissions requested.
var height = 620;
if (_.without(scope, 'basic').length)
height += 130;
OAuth.launchLogin({
loginService: 'slack',
loginStyle: loginStyle,
loginUrl: loginUrl,
credentialRequestCompleteCallback: credentialRequestCompleteCallback,
credentialToken: credentialToken,
popupOptions: {width: 900, height: height}
});
/*
OAuth.showPopup(
loginUrl,
_.bind(credentialRequestCompleteCallback, null, credentialToken),
{width: 900, height: height}
);
*/
};