2019-10-03 12:45:06 +13:00
|
|
|
<?php
|
|
|
|
|
2020-03-25 06:56:32 +13:00
|
|
|
namespace Appwrite\Auth\OAuth2;
|
2019-10-03 12:45:06 +13:00
|
|
|
|
2020-03-25 06:56:32 +13:00
|
|
|
use Appwrite\Auth\OAuth2;
|
2022-02-02 06:13:54 +13:00
|
|
|
use Utopia\Exception;
|
2019-10-03 12:45:06 +13:00
|
|
|
|
2020-02-17 00:41:03 +13:00
|
|
|
class Slack extends OAuth2
|
2019-10-03 12:45:06 +13:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $user = [];
|
2022-02-01 23:42:11 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $tokens = [];
|
2019-10-03 12:45:06 +13:00
|
|
|
|
2020-01-19 08:44:08 +13:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-01-19 09:12:41 +13:00
|
|
|
protected $scopes = [
|
2020-06-25 09:05:16 +12:00
|
|
|
'identity.avatar',
|
|
|
|
'identity.basic',
|
2020-01-19 09:12:41 +13:00
|
|
|
'identity.email',
|
|
|
|
'identity.team'
|
|
|
|
];
|
2020-01-19 08:44:08 +13:00
|
|
|
|
2019-10-03 12:45:06 +13:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName():string
|
|
|
|
{
|
|
|
|
return 'slack';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLoginURL():string
|
|
|
|
{
|
2019-10-04 05:19:39 +13:00
|
|
|
// https://api.slack.com/docs/oauth#step_1_-_sending_users_to_authorize_and_or_install
|
2020-06-20 23:05:43 +12:00
|
|
|
return 'https://slack.com/oauth/authorize?'.\http_build_query([
|
2020-01-19 08:44:08 +13:00
|
|
|
'client_id'=> $this->appID,
|
2020-06-20 23:05:43 +12:00
|
|
|
'scope' => \implode(' ', $this->getScopes()),
|
2020-01-19 08:44:08 +13:00
|
|
|
'redirect_uri' => $this->callback,
|
2020-06-20 23:05:43 +12:00
|
|
|
'state' => \json_encode($this->state)
|
2020-01-19 08:44:08 +13:00
|
|
|
]);
|
2019-10-03 12:45:06 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $code
|
|
|
|
*
|
2022-02-01 09:20:17 +13:00
|
|
|
* @return array
|
2019-10-03 12:45:06 +13:00
|
|
|
*/
|
2022-02-02 05:47:19 +13:00
|
|
|
protected function getTokens(string $code): array
|
2019-10-03 12:45:06 +13:00
|
|
|
{
|
2022-02-01 23:42:11 +13:00
|
|
|
if(empty($this->tokens)) {
|
|
|
|
// https://api.slack.com/docs/oauth#step_3_-_exchanging_a_verification_code_for_an_access_token
|
|
|
|
$this->tokens = \json_decode($this->request(
|
|
|
|
'GET',
|
|
|
|
'https://slack.com/api/oauth.access?' . \http_build_query([
|
|
|
|
'client_id' => $this->appID,
|
|
|
|
'client_secret' => $this->appSecret,
|
|
|
|
'code' => $code,
|
|
|
|
'redirect_uri' => $this->callback
|
|
|
|
])
|
|
|
|
), true);
|
|
|
|
}
|
2019-10-03 12:45:06 +13:00
|
|
|
|
2022-02-01 23:42:11 +13:00
|
|
|
return $this->tokens;
|
2019-10-03 12:45:06 +13:00
|
|
|
}
|
|
|
|
|
2022-02-02 04:54:20 +13:00
|
|
|
/**
|
|
|
|
* @param string $refreshToken
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function refreshTokens(string $refreshToken):array
|
|
|
|
{
|
2022-02-02 06:13:54 +13:00
|
|
|
$this->tokens = \json_decode($this->request(
|
|
|
|
'GET',
|
|
|
|
'https://slack.com/api/oauth.access?' . \http_build_query([
|
|
|
|
'client_id' => $this->appID,
|
|
|
|
'client_secret' => $this->appSecret,
|
|
|
|
'refresh_token' => $refreshToken,
|
|
|
|
'grant_type' => 'refresh_token'
|
|
|
|
])
|
|
|
|
), true);
|
2022-02-02 04:54:20 +13:00
|
|
|
|
2022-02-04 05:05:06 +13:00
|
|
|
if(empty($this->tokens['refresh_token'])) {
|
|
|
|
$this->tokens['refresh_token'] = $refreshToken;
|
|
|
|
}
|
|
|
|
|
2022-02-02 04:54:20 +13:00
|
|
|
return $this->tokens;
|
|
|
|
}
|
|
|
|
|
2019-10-03 12:45:06 +13:00
|
|
|
/**
|
|
|
|
* @param string $accessToken
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUserID(string $accessToken):string
|
|
|
|
{
|
|
|
|
$user = $this->getUser($accessToken);
|
|
|
|
|
|
|
|
if (isset($user['user']['id'])) {
|
|
|
|
return $user['user']['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $accessToken
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUserEmail(string $accessToken):string
|
|
|
|
{
|
|
|
|
$user = $this->getUser($accessToken);
|
|
|
|
|
|
|
|
if (isset($user['user']['email'])) {
|
|
|
|
return $user['user']['email'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2022-04-28 07:08:32 +12:00
|
|
|
/**
|
2022-04-28 07:14:09 +12:00
|
|
|
* Check if the OAuth email is verified
|
|
|
|
*
|
|
|
|
* @param $accessToken
|
2022-04-28 07:08:32 +12:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-04-28 07:14:09 +12:00
|
|
|
public function isEmailVerififed(string $accessToken): bool
|
2022-04-28 07:08:32 +12:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-03 12:45:06 +13:00
|
|
|
/**
|
|
|
|
* @param string $accessToken
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUserName(string $accessToken):string
|
|
|
|
{
|
|
|
|
$user = $this->getUser($accessToken);
|
|
|
|
|
|
|
|
if (isset($user['user']['name'])) {
|
|
|
|
return $user['user']['name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $accessToken
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getUser(string $accessToken):array
|
|
|
|
{
|
|
|
|
if (empty($this->user)) {
|
2019-10-04 05:19:39 +13:00
|
|
|
// https://api.slack.com/methods/users.identity
|
2019-10-03 12:45:06 +13:00
|
|
|
$user = $this->request(
|
|
|
|
'GET',
|
2020-06-20 23:05:43 +12:00
|
|
|
'https://slack.com/api/users.identity?token='.\urlencode($accessToken)
|
2019-10-04 05:19:39 +13:00
|
|
|
);
|
2019-10-03 12:45:06 +13:00
|
|
|
|
2020-06-20 23:05:43 +12:00
|
|
|
$this->user = \json_decode($user, true);
|
2019-10-03 12:45:06 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
}
|