2019-10-02 23:45:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-03-24 17:56:32 +00:00
|
|
|
namespace Appwrite\Auth\OAuth2;
|
2019-10-02 23:45:06 +00:00
|
|
|
|
2020-03-24 17:56:32 +00:00
|
|
|
use Appwrite\Auth\OAuth2;
|
2019-10-02 23:45:06 +00:00
|
|
|
|
2020-02-16 11:41:03 +00:00
|
|
|
class Slack extends OAuth2
|
2019-10-02 23:45:06 +00:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2022-05-12 15:56:20 +00:00
|
|
|
protected array $user = [];
|
|
|
|
|
|
2022-02-01 10:42:11 +00:00
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2022-05-12 15:56:20 +00:00
|
|
|
protected array $tokens = [];
|
2019-10-02 23:45:06 +00:00
|
|
|
|
2020-01-18 19:44:08 +00:00
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2022-05-12 15:56:20 +00:00
|
|
|
protected array $scopes = [
|
2025-01-17 07:05:31 +00:00
|
|
|
'openid',
|
|
|
|
|
'email',
|
|
|
|
|
'profile',
|
2020-01-18 20:12:41 +00:00
|
|
|
];
|
2020-01-18 19:44:08 +00:00
|
|
|
|
2019-10-02 23:45:06 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2022-05-12 15:56:20 +00:00
|
|
|
public function getName(): string
|
2019-10-02 23:45:06 +00:00
|
|
|
{
|
|
|
|
|
return 'slack';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2022-05-12 15:56:20 +00:00
|
|
|
public function getLoginURL(): string
|
2019-10-02 23:45:06 +00:00
|
|
|
{
|
2025-01-15 14:49:26 +00:00
|
|
|
// https://api.slack.com/authentication/oauth-v2
|
|
|
|
|
return 'https://slack.com/oauth/v2/authorize?' . \http_build_query([
|
2022-05-12 15:56:20 +00:00
|
|
|
'client_id' => $this->appID,
|
2025-01-17 07:05:31 +00:00
|
|
|
'user_scope' => \implode(',', $this->getScopes()),
|
2020-01-18 19:44:08 +00:00
|
|
|
'redirect_uri' => $this->callback,
|
2020-06-20 11:05:43 +00:00
|
|
|
'state' => \json_encode($this->state)
|
2020-01-18 19:44:08 +00:00
|
|
|
]);
|
2019-10-02 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $code
|
|
|
|
|
*
|
2022-01-31 20:20:17 +00:00
|
|
|
* @return array
|
2019-10-02 23:45:06 +00:00
|
|
|
*/
|
2022-02-01 16:47:19 +00:00
|
|
|
protected function getTokens(string $code): array
|
2019-10-02 23:45:06 +00:00
|
|
|
{
|
2022-05-12 15:56:20 +00:00
|
|
|
if (empty($this->tokens)) {
|
2022-02-01 10:42:11 +00:00
|
|
|
$this->tokens = \json_decode($this->request(
|
|
|
|
|
'GET',
|
2025-01-15 14:49:26 +00:00
|
|
|
'https://slack.com/api/oauth.v2.access?' . \http_build_query([
|
2022-02-01 10:42:11 +00:00
|
|
|
'client_id' => $this->appID,
|
|
|
|
|
'client_secret' => $this->appSecret,
|
|
|
|
|
'code' => $code,
|
|
|
|
|
'redirect_uri' => $this->callback
|
|
|
|
|
])
|
|
|
|
|
), true);
|
2025-01-17 07:05:31 +00:00
|
|
|
|
|
|
|
|
if (!$this->tokens['ok']) {
|
|
|
|
|
throw new \Exception('Error in fetching access token.');
|
|
|
|
|
}
|
2022-02-01 10:42:11 +00:00
|
|
|
}
|
2019-10-02 23:45:06 +00:00
|
|
|
|
2022-02-01 10:42:11 +00:00
|
|
|
return $this->tokens;
|
2019-10-02 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-01 15:54:20 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $refreshToken
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2022-05-12 15:56:20 +00:00
|
|
|
public function refreshTokens(string $refreshToken): array
|
2022-02-01 15:54:20 +00:00
|
|
|
{
|
2022-02-01 17:13:54 +00:00
|
|
|
$this->tokens = \json_decode($this->request(
|
|
|
|
|
'GET',
|
2025-01-15 14:49:26 +00:00
|
|
|
'https://slack.com/api/oauth.v2.access?' . \http_build_query([
|
2022-02-01 17:13:54 +00:00
|
|
|
'client_id' => $this->appID,
|
|
|
|
|
'client_secret' => $this->appSecret,
|
|
|
|
|
'refresh_token' => $refreshToken,
|
|
|
|
|
'grant_type' => 'refresh_token'
|
|
|
|
|
])
|
|
|
|
|
), true);
|
2022-02-01 15:54:20 +00:00
|
|
|
|
2022-05-12 15:56:20 +00:00
|
|
|
if (empty($this->tokens['refresh_token'])) {
|
2022-02-03 16:05:06 +00:00
|
|
|
$this->tokens['refresh_token'] = $refreshToken;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-01 15:54:20 +00:00
|
|
|
return $this->tokens;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-02 23:45:06 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $accessToken
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2022-05-12 15:56:20 +00:00
|
|
|
public function getUserID(string $accessToken): string
|
2019-10-02 23:45:06 +00:00
|
|
|
{
|
|
|
|
|
$user = $this->getUser($accessToken);
|
|
|
|
|
|
2022-05-12 15:56:20 +00:00
|
|
|
return $user['user']['id'] ?? '';
|
2019-10-02 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $accessToken
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2022-05-12 15:56:20 +00:00
|
|
|
public function getUserEmail(string $accessToken): string
|
2019-10-02 23:45:06 +00:00
|
|
|
{
|
|
|
|
|
$user = $this->getUser($accessToken);
|
|
|
|
|
|
2022-05-12 15:56:20 +00:00
|
|
|
return $user['user']['email'] ?? '';
|
2019-10-02 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
2022-04-27 19:08:32 +00:00
|
|
|
/**
|
2022-04-27 19:14:09 +00:00
|
|
|
* Check if the OAuth email is verified
|
2022-05-23 14:54:50 +00:00
|
|
|
*
|
2022-04-27 20:25:28 +00:00
|
|
|
* If present, the email is verified. This was verfied through a manual Slack sign up process
|
2022-05-23 14:54:50 +00:00
|
|
|
*
|
2022-04-27 19:57:36 +00:00
|
|
|
* @link https://slack.com/help/articles/207262907-Change-your-email-address
|
2022-05-23 14:54:50 +00:00
|
|
|
*
|
2022-05-12 15:56:20 +00:00
|
|
|
* @param string $accessToken
|
2022-05-23 14:54:50 +00:00
|
|
|
*
|
2022-04-27 19:08:32 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2022-04-27 20:27:21 +00:00
|
|
|
public function isEmailVerified(string $accessToken): bool
|
2022-04-27 19:08:32 +00:00
|
|
|
{
|
2022-04-27 20:10:48 +00:00
|
|
|
$email = $this->getUserEmail($accessToken);
|
2022-04-27 19:57:36 +00:00
|
|
|
|
2022-04-27 20:10:48 +00:00
|
|
|
return !empty($email);
|
2022-04-27 19:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-02 23:45:06 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $accessToken
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2022-05-12 15:56:20 +00:00
|
|
|
public function getUserName(string $accessToken): string
|
2019-10-02 23:45:06 +00:00
|
|
|
{
|
|
|
|
|
$user = $this->getUser($accessToken);
|
|
|
|
|
|
2022-05-12 15:56:20 +00:00
|
|
|
return $user['user']['name'] ?? '';
|
2019-10-02 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-04-27 19:57:36 +00:00
|
|
|
* @link https://api.slack.com/methods/users.identity
|
2022-05-23 14:54:50 +00:00
|
|
|
*
|
2019-10-02 23:45:06 +00:00
|
|
|
* @param string $accessToken
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2022-05-12 15:56:20 +00:00
|
|
|
protected function getUser(string $accessToken): array
|
2019-10-02 23:45:06 +00:00
|
|
|
{
|
|
|
|
|
if (empty($this->user)) {
|
|
|
|
|
$user = $this->request(
|
|
|
|
|
'GET',
|
2025-01-17 07:05:31 +00:00
|
|
|
'https://slack.com/api/users.identity',
|
|
|
|
|
[
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Authorization' => 'Bearer ' . $accessToken
|
|
|
|
|
]
|
|
|
|
|
]
|
2019-10-03 16:19:39 +00:00
|
|
|
);
|
2019-10-02 23:45:06 +00:00
|
|
|
|
2020-06-20 11:05:43 +00:00
|
|
|
$this->user = \json_decode($user, true);
|
2019-10-02 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->user;
|
|
|
|
|
}
|
|
|
|
|
}
|