Merge branch 'master' of github.com:appwrite/appwrite

This commit is contained in:
Eldad Fux 2020-01-21 09:33:39 +02:00
commit 93cfcb3df4
7 changed files with 483 additions and 0 deletions

View file

@ -67,6 +67,12 @@ return [
'enabled' => true,
'mock' => false,
],
'salesforce' => [
'developers' => 'https://developer.salesforce.com/docs/',
'icon' => 'icon-salesforce',
'enabled' => true,
'mock' => false,
],
// 'apple' => [
// 'developers' => 'https://developer.apple.com/',
// 'icon' => 'icon-apple',
@ -109,12 +115,24 @@ return [
'enabled' => true,
'mock' => false,
],
'yandex' => [
'developers' => 'https://tech.yandex.com/oauth/',
'icon' => 'icon-yandex',
'enabled' => true,
'mock' => false,
],
'twitter' => [
'developers' => 'https://developer.twitter.com/',
'icon' => 'icon-twitter',
'enabled' => false,
'mock' => false
],
'paypal' => [
'developers' => 'https://developer.paypal.com/docs/api/overview/',
'icon' => 'icon-paypal',
'enabled' => true,
'mock' => false
],
// Keep Last
'mock' => [
'developers' => 'https://appwrite.io',

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

164
src/Auth/OAuth/Paypal.php Normal file
View file

@ -0,0 +1,164 @@
<?php
namespace Auth\OAuth;
use Auth\OAuth;
// Reference Material
// https://developer.paypal.com/docs/api/overview/
class Paypal extends OAuth
{
/**
* @var string
*/
private $endpoint= [
'sandbox' => 'https://www.sandbox.paypal.com/',
'live' => 'https://www.paypal.com/',
];
private $resourceEndpoint = [
'sandbox' => 'https://api.sandbox.paypal.com/v1/',
'live' => 'https://api.paypal.com/v1/',
];
private $environment = 'live';
/**
* @var array
*/
protected $user = [];
protected $scopes = [
'openid',
'profile',
'email'
];
/**
* @return string
*/
public function getName(): string
{
return 'paypal';
}
/**
* @return string
*/
public function getLoginURL(): string
{
$url = $this->endpoint[$this->environment] . 'connect/?'.
http_build_query([
'flowEntry' => 'static',
'response_type' => 'code',
'client_id' => $this->appID,
'scope' => implode(' ', $this->getScopes()),
// paypal is not accepting localhost string into return uri
'redirect_uri' => str_replace("localhost", "127.0.0.1", $this->callback),
'state' => json_encode($this->state),
]);
return $url;
}
/**
* @param string $code
*
* @return string
*/
public function getAccessToken(string $code): string
{
$accessToken = $this->request(
'POST',
$this->resourceEndpoint[$this->environment] . 'oauth2/token',
['Authorization: Basic ' . base64_encode($this->appID . ':' . $this->appSecret)],
http_build_query([
'code' => $code,
'grant_type' => 'authorization_code',
])
);
$accessToken = json_decode($accessToken, true);
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['payer_id'])) {
return $user['payer_id'];
}
return '';
}
/**
* @param string $accessToken
*
* @return string
*/
public function getUserEmail(string $accessToken): string
{
$user = $this->getUser($accessToken);
if (isset($user['emails'])) {
return $user['emails'][0]['value'];
}
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
{
$header = [
'Content-Type: application/json',
'Authorization: Bearer '.urlencode($accessToken),
];
if (empty($this->user)) {
$user = $this->request(
'GET',
$this->resourceEndpoint[$this->environment] . 'identity/oauth2/userinfo?schema=paypalv1.1',
$header
);
$this->user = json_decode($user, true);
}
return $this->user;
}
}

View file

@ -0,0 +1,151 @@
<?php
namespace Auth\OAuth;
use Auth\OAuth;
// Reference Material
// https://help.salesforce.com/articleView?id=remoteaccess_oauth_endpoints.htm&type=5
// https://help.salesforce.com/articleView?id=remoteaccess_oauth_tokens_scopes.htm&type=5
// https://help.salesforce.com/articleView?id=remoteaccess_oauth_web_server_flow.htm&type=5
class Salesforce extends OAuth
{
/**
* @var array
*/
protected $user = [];
/**
* @var array
*/
protected $scopes = [
"openid"
];
/**
* @return string
*/
public function getName(): string
{
return 'Salesforce';
}
/**
* @param $state
*
* @return json
*/
public function parseState(string $state)
{
return json_decode(html_entity_decode($state), true);
}
/**
* @return string
*/
public function getLoginURL(): string
{
return 'https://login.salesforce.com/services/oauth2/authorize?'.http_build_query([
'response_type' => 'code',
'client_id' => $this->appID,
'redirect_uri'=> $this->callback,
'scope'=> implode(' ', $this->getScopes()),
'state' => json_encode($this->state)
]);
}
/**
* @param string $code
*
* @return string
*/
public function getAccessToken(string $code): string
{
$headers = [
"Authorization: Basic " . base64_encode($this->appID . ":" . $this->appSecret),
"Content-Type: application/x-www-form-urlencoded",
];
$accessToken = $this->request(
'POST',
'https://login.salesforce.com/services/oauth2/token',
$headers,
http_build_query([
'code' => $code,
'redirect_uri' => $this->callback ,
'grant_type' => 'authorization_code'
])
);
$accessToken = json_decode($accessToken, true);
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['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['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', 'https://login.salesforce.com/services/oauth2/userinfo?access_token='.urlencode($accessToken));
$this->user = json_decode($user, true);
}
return $this->user;
}
}

150
src/Auth/OAuth/Yandex.php Normal file
View file

@ -0,0 +1,150 @@
<?php
namespace Auth\OAuth;
use Auth\OAuth;
// Reference Material
// https://tech.yandex.com/passport/doc/dg/reference/request-docpage/
// https://tech.yandex.com/oauth/doc/dg/reference/web-client-docpage/
class Yandex extends OAuth
{
/**
* @var array
*/
protected $user = [];
/**
* @var array
*/
protected $scopes = [];
/**
* @return string
*/
public function getName(): string
{
return 'Yandex';
}
/**
* @param $state
*
* @return json
*/
public function parseState(string $state)
{
return json_decode(html_entity_decode($state), true);
}
/**
* @return string
*/
public function getLoginURL(): string
{
return 'https://oauth.yandex.com/authorize?'.http_build_query([
'response_type' => 'code',
'client_id' => $this->appID,
'scope'=> implode(' ', $this->getScopes()),
'state' => json_encode($this->state)
]);
}
/**
* @param string $code
*
* @return string
*/
public function getAccessToken(string $code): string
{
$headers = [
"Authorization: Basic " . base64_encode($this->appID . ":" . $this->appSecret),
"Content-Type: application/x-www-form-urlencoded",
];
$accessToken = $this->request(
'POST',
'https://oauth.yandex.com/token',
$headers,
http_build_query([
'code' => $code,
'grant_type' => 'authorization_code'
])
);
$accessToken = json_decode($accessToken, true);
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['default_email'])) {
return $user['default_email'];
}
return '';
}
/**
* @param string $accessToken
*
* @return string
*/
public function getUserName(string $accessToken): string
{
$user = $this->getUser($accessToken);
if (isset($user['display_name'])) {
return $user['display_name'];
}
return '';
}
/**
* @param string $accessToken
*
* @return array
*/
protected function getUser(string $accessToken): array
{
if (empty($this->user)) {
$user = $this->request('GET', 'https://login.yandex.ru/info?'.http_build_query([
'format' => 'json',
'oauth_token' => $accessToken
]));
$this->user = json_decode($user, true);
}
return $this->user;
}
}