1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Slack |
4
|
|
|
* |
5
|
|
|
* @created 26.10.2017 |
6
|
|
|
* @author Smiley <[email protected]> |
7
|
|
|
* @copyright 2017 Smiley |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace chillerlan\OAuth\Providers; |
12
|
|
|
|
13
|
|
|
use chillerlan\HTTP\Utils\MessageUtil; |
14
|
|
|
use chillerlan\OAuth\Core\{CSRFToken, OAuth2Provider, ProviderException}; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
use function sprintf; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @see https://api.slack.com/docs/oauth |
20
|
|
|
* @see https://api.slack.com/docs/sign-in-with-slack |
21
|
|
|
* @see https://api.slack.com/docs/token-types |
22
|
|
|
*/ |
23
|
|
|
class Slack extends OAuth2Provider implements CSRFToken{ |
24
|
|
|
|
25
|
|
|
// bot token |
26
|
|
|
public const SCOPE_BOT = 'bot'; |
27
|
|
|
|
28
|
|
|
// user token |
29
|
|
|
public const SCOPE_ADMIN = 'admin'; |
30
|
|
|
public const SCOPE_CHAT_WRITE_BOT = 'chat:write:bot'; |
31
|
|
|
public const SCOPE_CLIENT = 'client'; |
32
|
|
|
public const SCOPE_DND_READ = 'dnd:read'; |
33
|
|
|
public const SCOPE_DND_WRITE = 'dnd:write'; |
34
|
|
|
public const SCOPE_FILES_READ = 'files:read'; |
35
|
|
|
public const SCOPE_FILES_WRITE_USER = 'files:write:user'; |
36
|
|
|
public const SCOPE_IDENTIFY = 'identify'; |
37
|
|
|
public const SCOPE_IDENTITY_AVATAR = 'identity.avatar'; |
38
|
|
|
public const SCOPE_IDENTITY_BASIC = 'identity.basic'; |
39
|
|
|
public const SCOPE_IDENTITY_EMAIL = 'identity.email'; |
40
|
|
|
public const SCOPE_IDENTITY_TEAM = 'identity.team'; |
41
|
|
|
public const SCOPE_INCOMING_WEBHOOK = 'incoming-webhook'; |
42
|
|
|
public const SCOPE_POST = 'post'; |
43
|
|
|
public const SCOPE_READ = 'read'; |
44
|
|
|
public const SCOPE_REMINDERS_READ = 'reminders:read'; |
45
|
|
|
public const SCOPE_REMINDERS_WRITE = 'reminders:write'; |
46
|
|
|
public const SCOPE_SEARCH_READ = 'search:read'; |
47
|
|
|
public const SCOPE_STARS_READ = 'stars:read'; |
48
|
|
|
public const SCOPE_STARS_WRITE = 'stars:write'; |
49
|
|
|
|
50
|
|
|
// user & workspace tokens |
51
|
|
|
public const SCOPE_CHANNELS_HISTORY = 'channels:history'; |
52
|
|
|
public const SCOPE_CHANNELS_READ = 'channels:read'; |
53
|
|
|
public const SCOPE_CHANNELS_WRITE = 'channels:write'; |
54
|
|
|
public const SCOPE_CHAT_WRITE_USER = 'chat:write:user'; |
55
|
|
|
public const SCOPE_COMMANDS = 'commands'; |
56
|
|
|
public const SCOPE_EMOJI_READ = 'emoji:read'; |
57
|
|
|
public const SCOPE_GROUPS_HISTORY = 'groups:history'; |
58
|
|
|
public const SCOPE_GROUPS_READ = 'groups:read'; |
59
|
|
|
public const SCOPE_GROUPS_WRITE = 'groups:write'; |
60
|
|
|
public const SCOPE_IM_HISTORY = 'im:history'; |
61
|
|
|
public const SCOPE_IM_READ = 'im:read'; |
62
|
|
|
public const SCOPE_IM_WRITE = 'im:write'; |
63
|
|
|
public const SCOPE_LINKS_READ = 'links:read'; |
64
|
|
|
public const SCOPE_LINKS_WRITE = 'links:write'; |
65
|
|
|
public const SCOPE_MPIM_HISTORY = 'mpim:history'; |
66
|
|
|
public const SCOPE_MPIM_READ = 'mpim:read'; |
67
|
|
|
public const SCOPE_MPIM_WRITE = 'mpim:write'; |
68
|
|
|
public const SCOPE_PINS_READ = 'pins:read'; |
69
|
|
|
public const SCOPE_PINS_WRITE = 'pins:write'; |
70
|
|
|
public const SCOPE_REACTIONS_READ = 'reactions:read'; |
71
|
|
|
public const SCOPE_REACTIONS_WRITE = 'reactions:write'; |
72
|
|
|
public const SCOPE_TEAM_READ = 'team:read'; |
73
|
|
|
public const SCOPE_USERGROUPS_READ = 'usergroups:read'; |
74
|
|
|
public const SCOPE_USERGROUPS_WRITE = 'usergroups:write'; |
75
|
|
|
public const SCOPE_USERS_PROFILE_READ = 'users.profile:read'; |
76
|
|
|
public const SCOPE_USERS_PROFILE_WRITE = 'users.profile:write'; |
77
|
|
|
public const SCOPE_USERS_READ = 'users:read'; |
78
|
|
|
public const SCOPE_USERS_READ_EMAIL = 'users:read.email'; |
79
|
|
|
public const SCOPE_USERS_WRITE = 'users:write'; |
80
|
|
|
|
81
|
|
|
protected string $authURL = 'https://slack.com/oauth/authorize'; |
82
|
|
|
protected string $accessTokenURL = 'https://slack.com/api/oauth.access'; |
83
|
|
|
protected string $apiURL = 'https://slack.com/api'; |
84
|
|
|
protected ?string $userRevokeURL = 'https://slack.com/apps/manage'; |
85
|
|
|
protected ?string $apiDocs = 'https://api.slack.com'; |
86
|
|
|
protected ?string $applicationURL = 'https://api.slack.com/apps'; |
87
|
|
|
|
88
|
|
|
protected array $defaultScopes = [ |
89
|
|
|
self::SCOPE_IDENTITY_AVATAR, |
90
|
|
|
self::SCOPE_IDENTITY_BASIC, |
91
|
|
|
self::SCOPE_IDENTITY_EMAIL, |
92
|
|
|
self::SCOPE_IDENTITY_TEAM, |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritDoc |
97
|
|
|
*/ |
98
|
|
|
public function me():ResponseInterface{ |
99
|
|
|
$response = $this->request('/users.identity'); |
100
|
|
|
$status = $response->getStatusCode(); |
101
|
|
|
$json = MessageUtil::decodeJSON($response); |
102
|
|
|
|
103
|
|
|
if($status === 200 && isset($json->ok) && $json->ok === true){ |
104
|
|
|
return $response; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if(isset($json->error)){ |
108
|
|
|
throw new ProviderException($json->error); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
throw new ProviderException(sprintf('user info error error HTTP/%s', $status)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|