1
0
Fork 0
mirror of synced 2024-06-29 11:40:45 +12:00
appwrite/src/Appwrite/Auth/OAuth2/Yammer.php

169 lines
3.9 KiB
PHP
Raw Normal View History

2021-10-20 18:39:02 +13:00
<?php
namespace Appwrite\Auth\OAuth2;
use Appwrite\Auth\OAuth2;
// Reference Material
// https://developer.yammer.com/docs/oauth-2
class Yammer extends OAuth2
{
/**
* @var string
*/
2022-05-13 03:56:20 +12:00
private string $endpoint = 'https://www.yammer.com/oauth2/';
2021-10-20 18:39:02 +13:00
/**
* @var array
*/
2022-05-13 03:56:20 +12:00
protected array $user = [];
2022-02-01 23:42:11 +13:00
/**
* @var array
*/
2022-05-13 03:56:20 +12:00
protected array $tokens = [];
2021-10-20 18:39:02 +13:00
/**
* @return string
*/
public function getName(): string
{
return 'yammer';
}
/**
* @return string
*/
public function getLoginURL(): string
{
2022-05-13 03:56:20 +12:00
return $this->endpoint . 'oauth2/authorize?' .
\http_build_query([
'client_id' => $this->appID,
'response_type' => 'code',
'redirect_uri' => $this->callback,
'state' => \json_encode($this->state)
]);
2021-10-20 18:39:02 +13:00
}
/**
* @param string $code
*
2022-02-01 09:20:17 +13:00
* @return array
2021-10-20 18:39:02 +13:00
*/
2022-02-02 05:47:19 +13:00
protected function getTokens(string $code): array
2021-10-20 18:39:02 +13:00
{
2022-05-13 03:56:20 +12:00
if (empty($this->tokens)) {
2022-02-01 23:42:11 +13:00
$headers = ['Content-Type: application/x-www-form-urlencoded'];
$this->tokens = \json_decode($this->request(
'POST',
$this->endpoint . 'access_token?',
$headers,
\http_build_query([
'client_id' => $this->appID,
'client_secret' => $this->appSecret,
'code' => $code,
'grant_type' => 'authorization_code'
])
), true);
}
2022-02-01 09:20:17 +13:00
2022-02-01 23:42:11 +13:00
return $this->tokens;
2021-10-20 18:39:02 +13:00
}
2022-02-02 04:54:20 +13:00
/**
* @param string $refreshToken
*
* @return array
*/
2022-05-13 03:56:20 +12:00
public function refreshTokens(string $refreshToken): array
2022-02-02 04:54:20 +13:00
{
$headers = ['Content-Type: application/x-www-form-urlencoded'];
$this->tokens = \json_decode($this->request(
'POST',
$this->endpoint . 'access_token?',
$headers,
\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-05-13 03:56:20 +12:00
if (empty($this->tokens['refresh_token'])) {
$this->tokens['refresh_token'] = $refreshToken;
}
2022-02-02 04:54:20 +13:00
return $this->tokens;
}
2021-10-20 18:39:02 +13:00
/**
* @param string $accessToken
*
* @return string
*/
public function getUserID(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
return $user['id'] ?? '';
2021-10-20 18:39:02 +13:00
}
/**
* @param string $accessToken
*
* @return string
*/
public function getUserEmail(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
return $user['email'] ?? '';
2021-10-20 18:39:02 +13:00
}
/**
* Check if the OAuth email is verified
2022-05-24 02:54:50 +12:00
*
* If present, the email is verified. This was verfied through a manual Yammer sign up process
2022-05-24 02:54:50 +12:00
*
2022-05-13 03:56:20 +12:00
* @param string $accessToken
2022-05-24 02:54:50 +12:00
*
* @return bool
*/
2022-04-28 08:27:21 +12:00
public function isEmailVerified(string $accessToken): bool
{
2022-04-28 08:50:03 +12:00
$email = $this->getUserEmail($accessToken);
return !empty($email);
}
2021-10-20 18:39:02 +13:00
/**
* @param string $accessToken
*
* @return string
*/
public function getUserName(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
return $user['full_name'] ?? '';
2021-10-20 18:39:02 +13:00
}
/**
* @param string $accessToken
*
* @return array
*/
protected function getUser(string $accessToken): array
{
if (empty($this->user)) {
2022-05-13 03:56:20 +12:00
$headers = ['Authorization: Bearer ' . \urlencode($accessToken)];
2021-10-20 18:39:02 +13:00
$user = $this->request('GET', 'https://www.yammer.com/api/v1/users/current.json', $headers);
$this->user = \json_decode($user, true);
}
return $this->user;
}
}