1
0
Fork 0
mirror of synced 2024-06-03 11:24:48 +12:00
appwrite/src/Appwrite/Auth/OAuth2/Mock.php

138 lines
2.8 KiB
PHP
Raw Normal View History

2020-01-14 01:23:39 +13:00
<?php
namespace Appwrite\Auth\OAuth2;
2020-01-14 01:23:39 +13:00
use Appwrite\Auth\OAuth2;
2020-01-14 01:23:39 +13:00
2020-02-17 00:41:03 +13:00
class Mock extends OAuth2
2020-01-14 01:23:39 +13:00
{
/**
* @var string
*/
protected $version = 'v1';
2020-01-19 09:12:41 +13:00
/**
* @var array
*/
protected $scopes = [
'email'
];
2020-01-14 01:23:39 +13:00
/**
* @var array
*/
protected $user = [];
/**
* @return string
*/
public function getName():string
{
return 'mock';
}
/**
* @return string
*/
public function getLoginURL():string
{
return 'http://localhost/'.$this->version.'/mock/tests/general/oauth2?'. \http_build_query([
2020-01-19 09:12:41 +13:00
'client_id' => $this->appID,
'redirect_uri' => $this->callback,
'scope' => \implode(' ', $this->getScopes()),
'state' => \json_encode($this->state)
2020-01-19 09:12:41 +13:00
]);
2020-01-14 01:23:39 +13:00
}
/**
* @param string $code
*
* @return string
*/
public function getAccessToken(string $code):string
{
$accessToken = $this->request(
'GET',
2020-02-17 00:41:03 +13:00
'http://localhost/'.$this->version.'/mock/tests/general/oauth2/token?'.
\http_build_query([
2020-01-19 09:12:41 +13:00
'client_id' => $this->appID,
'redirect_uri' => $this->callback,
'client_secret' => $this->appSecret,
'code' => $code
])
2020-01-14 01:23:39 +13:00
);
$accessToken = \json_decode($accessToken, true); //
2020-01-14 01:23:39 +13:00
if (isset($accessToken['access_token'])) {
return $accessToken['access_token'];
}
return '';
}
/**
* @param string $accessToken
*
* @return string
*/
public function getUserID(string $accessToken):string
{
$user = $this->getUser($accessToken);
if (isset($user['id'])) {
return $user['id'];
}
return '';
}
/**
* @param string $accessToken
*
* @return string
*/
public function getUserEmail(string $accessToken):string
{
$user = $this->getUser($accessToken);
if (isset($user['email'])) {
return $user['email'];
}
return '';
}
/**
* @param string $accessToken
*
* @return string
*/
public function getUserName(string $accessToken):string
{
$user = $this->getUser($accessToken);
if (isset($user['name'])) {
return $user['name'];
}
return '';
}
/**
* @param string $accessToken
*
* @return array
*/
protected function getUser(string $accessToken):array
{
if (empty($this->user)) {
$user = $this->request('GET', 'http://localhost/'.$this->version.'/mock/tests/general/oauth2/user?token='.\urlencode($accessToken));
2020-01-14 01:23:39 +13:00
$this->user = \json_decode($user, true);
2020-01-14 01:23:39 +13:00
}
return $this->user;
}
2020-06-25 09:05:16 +12:00
}