appwrite/src/Appwrite/Auth/OAuth2/Slack.php

173 lines
4 KiB
PHP
Raw Normal View History

<?php
namespace Appwrite\Auth\OAuth2;
use Appwrite\Auth\OAuth2;
2020-02-16 11:41:03 +00:00
class Slack extends OAuth2
{
/**
* @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 = [];
2020-01-18 19:44:08 +00:00
/**
* @var array
*/
2022-05-12 15:56:20 +00:00
protected array $scopes = [
2020-06-24 21:05:16 +00:00
'identity.avatar',
'identity.basic',
2020-01-18 20:12:41 +00:00
'identity.email',
'identity.team'
];
2020-01-18 19:44:08 +00:00
/**
* @return string
*/
2022-05-12 15:56:20 +00:00
public function getName(): string
{
return 'slack';
}
/**
* @return string
*/
2022-05-12 15:56:20 +00:00
public function getLoginURL(): string
{
// https://api.slack.com/docs/oauth#step_1_-_sending_users_to_authorize_and_or_install
2022-05-12 15:56:20 +00:00
return 'https://slack.com/oauth/authorize?' . \http_build_query([
'client_id' => $this->appID,
'scope' => \implode(' ', $this->getScopes()),
2020-01-18 19:44:08 +00:00
'redirect_uri' => $this->callback,
'state' => \json_encode($this->state)
2020-01-18 19:44:08 +00:00
]);
}
/**
* @param string $code
*
2022-01-31 20:20:17 +00:00
* @return array
*/
2022-02-01 16:47:19 +00:00
protected function getTokens(string $code): array
{
2022-05-12 15:56:20 +00:00
if (empty($this->tokens)) {
2022-02-01 10:42:11 +00:00
// 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);
}
2022-02-01 10:42:11 +00:00
return $this->tokens;
}
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
{
$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-01 15:54:20 +00:00
2022-05-12 15:56:20 +00:00
if (empty($this->tokens['refresh_token'])) {
$this->tokens['refresh_token'] = $refreshToken;
}
2022-02-01 15:54:20 +00:00
return $this->tokens;
}
/**
* @param string $accessToken
*
* @return string
*/
2022-05-12 15:56:20 +00:00
public function getUserID(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-12 15:56:20 +00:00
return $user['user']['id'] ?? '';
}
/**
* @param string $accessToken
*
* @return string
*/
2022-05-12 15:56:20 +00:00
public function getUserEmail(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-12 15:56:20 +00:00
return $user['user']['email'] ?? '';
}
/**
* 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
*
* @return bool
*/
2022-04-27 20:27:21 +00:00
public function isEmailVerified(string $accessToken): bool
{
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);
}
/**
* @param string $accessToken
*
* @return string
*/
2022-05-12 15:56:20 +00:00
public function getUserName(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-12 15:56:20 +00:00
return $user['user']['name'] ?? '';
}
/**
2022-04-27 19:57:36 +00:00
* @link https://api.slack.com/methods/users.identity
2022-05-23 14:54:50 +00:00
*
* @param string $accessToken
*
* @return array
*/
2022-05-12 15:56:20 +00:00
protected function getUser(string $accessToken): array
{
if (empty($this->user)) {
$user = $this->request(
'GET',
2022-05-12 15:56:20 +00:00
'https://slack.com/api/users.identity?token=' . \urlencode($accessToken)
);
$this->user = \json_decode($user, true);
}
return $this->user;
}
}