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

167 lines
3.8 KiB
PHP
Raw Normal View History

2019-05-09 06:54:39 +00:00
<?php
namespace Appwrite\Auth\OAuth2;
2019-05-09 06:54:39 +00:00
use Appwrite\Auth\OAuth2;
2019-05-09 06:54:39 +00:00
2020-02-16 11:41:03 +00:00
class Facebook extends OAuth2
2019-05-09 06:54:39 +00:00
{
/**
* @var string
*/
2022-05-12 15:56:20 +00:00
protected string $version = 'v2.8';
2019-05-09 06:54:39 +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-05-09 06:54:39 +00:00
2020-01-18 14:33:39 +00:00
/**
* @var array
*/
2022-05-12 15:56:20 +00:00
protected array $scopes = [
2020-01-18 20:12:41 +00:00
'email'
];
2020-01-18 14:33:39 +00:00
2019-05-09 06:54:39 +00:00
/**
* @return string
*/
2022-05-12 15:56:20 +00:00
public function getName(): string
2019-05-09 06:54:39 +00:00
{
return 'facebook';
}
/**
* @return string
*/
2022-05-12 15:56:20 +00:00
public function getLoginURL(): string
2019-05-09 06:54:39 +00:00
{
2022-05-12 15:56:20 +00:00
return 'https://www.facebook.com/' . $this->version . '/dialog/oauth?' . \http_build_query([
'client_id' => $this->appID,
2020-01-18 14:33:39 +00:00
'redirect_uri' => $this->callback,
'scope' => \implode(' ', $this->getScopes()),
'state' => \json_encode($this->state)
2020-01-18 14:33:39 +00:00
]);
2019-05-09 06:54:39 +00:00
}
/**
* @param string $code
*
2022-01-31 20:20:17 +00:00
* @return array
2019-05-09 06:54:39 +00:00
*/
2022-02-01 16:47:19 +00:00
protected function getTokens(string $code): array
2019-05-09 06:54:39 +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',
'https://graph.facebook.com/' . $this->version . '/oauth/access_token?' . \http_build_query([
'client_id' => $this->appID,
'redirect_uri' => $this->callback,
'client_secret' => $this->appSecret,
'code' => $code
])
), true);
}
2019-05-09 06:54:39 +00:00
2022-02-01 10:42:11 +00:00
return $this->tokens;
2019-05-09 06:54:39 +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
{
$this->tokens = \json_decode($this->request(
'GET',
'https://graph.facebook.com/' . $this->version . '/oauth/access_token?' . \http_build_query([
'client_id' => $this->appID,
'redirect_uri' => $this->callback,
'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;
}
2019-05-09 06:54:39 +00:00
/**
* @param string $accessToken
*
2019-05-09 06:54:39 +00:00
* @return string
*/
2022-05-12 15:56:20 +00:00
public function getUserID(string $accessToken): string
2019-05-09 06:54:39 +00:00
{
$user = $this->getUser($accessToken);
2022-05-12 15:56:20 +00:00
return $user['id'] ?? '';
2019-05-09 06:54:39 +00:00
}
/**
* @param string $accessToken
*
2019-05-09 06:54:39 +00:00
* @return string
*/
2022-05-12 15:56:20 +00:00
public function getUserEmail(string $accessToken): string
2019-05-09 06:54:39 +00:00
{
$user = $this->getUser($accessToken);
2022-05-12 15:56:20 +00:00
return $user['email'] ?? '';
2019-05-09 06:54:39 +00:00
}
/**
* Check if the OAuth email is verified
2022-05-23 14:54:50 +00:00
*
2022-05-05 20:41:35 +00:00
* If present, the email is verified. This was verfied through a manual Facebook sign up process
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-05-05 20:41:35 +00:00
$email = $this->getUserEmail($accessToken);
return !empty($email);
}
2019-05-09 06:54:39 +00:00
/**
* @param string $accessToken
*
2019-05-09 06:54:39 +00:00
* @return string
*/
2022-05-12 15:56:20 +00:00
public function getUserName(string $accessToken): string
2019-05-09 06:54:39 +00:00
{
$user = $this->getUser($accessToken);
2022-05-12 15:56:20 +00:00
return $user['name'] ?? '';
2019-05-09 06:54:39 +00:00
}
/**
* @param string $accessToken
*
2019-05-09 06:54:39 +00:00
* @return array
*/
2022-05-12 15:56:20 +00:00
protected function getUser(string $accessToken): array
2019-05-09 06:54:39 +00:00
{
if (empty($this->user)) {
2022-05-12 15:56:20 +00:00
$user = $this->request('GET', 'https://graph.facebook.com/' . $this->version . '/me?fields=email,name&access_token=' . \urlencode($accessToken));
2019-05-09 06:54:39 +00:00
$this->user = \json_decode($user, true);
2019-05-09 06:54:39 +00:00
}
return $this->user;
}
}