-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlackStrategy.php
executable file
·157 lines (131 loc) · 3.95 KB
/
SlackStrategy.php
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* Slack strategy for Opauth
*
* Based on work by U-Zyn Chua (http://uzyn.com)
*
* More information on Opauth: http://opauth.org
*
* @copyright Copyright © 2015 Timm Stokke (http://timm.stokke.me)
* @link http://opauth.org
* @package Opauth.BasecampStrategy
* @license MIT License
*/
/**
* Slack strategy for Opauth
*
* @package Opauth.Slack
*/
class SlackStrategy extends OpauthStrategy {
/**
* Compulsory config keys, listed as unassociative arrays
*/
public $expects = array('client_id', 'client_secret');
/**
* Optional config keys, without predefining any default values.
*/
public $optionals = array('redirect_uri', 'scope', 'state', 'team');
/**
* Optional config keys with respective default values, listed as associative arrays
* eg. array('scope' => 'post');
*/
public $defaults = array(
'redirect_uri' => '{complete_url_to_strategy}oauth2callback'
);
/**
* Auth request
*/
public function request() {
$url = 'https://slack.com/oauth/authorize';
$params = array(
'client_id' => $this->strategy['client_id'],
'redirect_uri' => $this->strategy['redirect_uri']
);
foreach ($this->optionals as $key) {
if (!empty($this->strategy[$key])) $params[$key] = $this->strategy[$key];
}
$this->clientGet($url, $params);
}
/**
* Internal callback, after OAuth
*/
public function oauth2callback() {
if (array_key_exists('code', $_GET) && !empty($_GET['code'])) {
$code = $_GET['code'];
$url = 'https://slack.com/api/oauth.access';
$params = array(
'code' => $code,
'client_id' => $this->strategy['client_id'],
'client_secret' => $this->strategy['client_secret'],
'redirect_uri' => $this->strategy['redirect_uri'],
);
if (!empty($this->strategy['state'])) $params['state'] = $this->strategy['state'];
$response = $this->serverPost($url, $params, null, $headers);
$results = json_decode($response,true);
if (!empty($results) && !empty($results['access_token'])) {
$user = $this->user($results['access_token']);
$this->auth = array(
'uid' => $user['basics']['user_id'],
'info' => array(),
'credentials' => array(
'token' => $results['access_token']
),
'raw' => $user
);
$this->mapProfile($user, 'user.real_name', 'info.name');
$this->mapProfile($user, 'user.name', 'info.nickname');
$this->mapProfile($user, 'user.profile.first_name', 'info.first_name');
$this->mapProfile($user, 'user.profile.last_name', 'info.last_name');
$this->mapProfile($user, 'user.profile.email', 'info.email');
$this->mapProfile($user, 'user.profile.image_48', 'info.image');
$this->callback();
}
else {
$error = array(
'code' => 'access_token_error',
'message' => 'Failed when attempting to obtain access token',
'raw' => array(
'response' => $response,
'headers' => $headers
)
);
$this->errorCallback($error);
}
}
else {
$error = array(
'code' => 'oauth2callback_error',
'raw' => $_GET
);
$this->errorCallback($error);
}
}
/**
* Queries Slack API for user info
*
* @param string $access_token
* @return array Parsed JSON results
*/
private function user($access_token) {
$user = $this->serverGet('https://slack.com/api/auth.test', array('token' => $access_token), null, $headers);
if (!empty($user)) {
$basics = $this->recursiveGetObjectVars(json_decode($user));
// Get detailed info:
$getDetails = $this->serverGet('https://slack.com/api/users.info', array('token' => $access_token, 'user' => $basics['user_id']), null, $headers);
$details = $this->recursiveGetObjectVars(json_decode($getDetails));
$details['basics'] = $basics;
return $details;
}
else {
$error = array(
'code' => 'userinfo_error',
'message' => 'Failed when attempting to query Slack API for user information',
'raw' => array(
'response' => $user,
'headers' => $headers
)
);
$this->errorCallback($error);
}
}
}