Merge pull request #855 from armino-dev/tradeshift-oauth2-provider
Add Tradeshift OAuth2 provider for live and sandbox
This commit is contained in:
commit
7a06b4e4bf
|
@ -163,6 +163,24 @@ return [ // Ordered by ABC.
|
|||
'beta' => false,
|
||||
'mock' => false,
|
||||
],
|
||||
'tradeshift' => [
|
||||
'name' => 'Tradeshift',
|
||||
'developers' => 'https://developers.tradeshift.com/docs/api',
|
||||
'icon' => 'icon-tradeshift',
|
||||
'enabled' => true,
|
||||
'form' => false,
|
||||
'beta' => false,
|
||||
'mock' => false,
|
||||
],
|
||||
'tradeshiftBox' => [
|
||||
'name' => 'Tradeshift Sandbox',
|
||||
'developers' => 'https://developers.tradeshift.com/docs/api',
|
||||
'icon' => 'icon-tradeshiftbox',
|
||||
'enabled' => true,
|
||||
'form' => false,
|
||||
'beta' => false,
|
||||
'mock' => false,
|
||||
],
|
||||
'twitch' => [
|
||||
'name' => 'Twitch',
|
||||
'developers' => 'https://dev.twitch.tv/docs/authentication',
|
||||
|
@ -215,6 +233,15 @@ return [ // Ordered by ABC.
|
|||
// 'beta' => false,
|
||||
// 'mock' => false,
|
||||
// ],
|
||||
'wordpress' => [
|
||||
'name' => 'WordPress',
|
||||
'developers' => 'https://developer.wordpress.com/docs/oauth2/',
|
||||
'icon' => 'icon-wordpress',
|
||||
'enabled' => true,
|
||||
'form' => false,
|
||||
'beta' => false,
|
||||
'mock' => false
|
||||
],
|
||||
// Keep Last
|
||||
'mock' => [
|
||||
'name' => 'Mock',
|
||||
|
@ -225,13 +252,4 @@ return [ // Ordered by ABC.
|
|||
'beta' => false,
|
||||
'mock' => true,
|
||||
],
|
||||
'wordpress' => [
|
||||
'name' => 'WordPress',
|
||||
'developers' => 'https://developer.wordpress.com/docs/oauth2/',
|
||||
'icon' => 'icon-wordpress',
|
||||
'enabled' => true,
|
||||
'form' => false,
|
||||
'beta' => false,
|
||||
'mock' => false
|
||||
]
|
||||
];
|
||||
|
|
BIN
public/images/oauth2/tradeshift.png
Normal file
BIN
public/images/oauth2/tradeshift.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.8 KiB |
BIN
public/images/oauth2/tradeshiftbox.png
Normal file
BIN
public/images/oauth2/tradeshiftbox.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.8 KiB |
155
src/Appwrite/Auth/OAuth2/Tradeshift.php
Normal file
155
src/Appwrite/Auth/OAuth2/Tradeshift.php
Normal file
|
@ -0,0 +1,155 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Auth\OAuth2;
|
||||
|
||||
use Appwrite\Auth\OAuth2;
|
||||
|
||||
// Reference Material
|
||||
// https://developers.tradeshift.com/docs/api
|
||||
|
||||
class Tradeshift extends OAuth2
|
||||
{
|
||||
const TRADESHIFT_SANDBOX_API_DOMAIN = 'api-sandbox.tradeshift.com';
|
||||
const TRADESHIFT_API_DOMAIN = 'api.tradeshift.com';
|
||||
|
||||
private $apiDomain = [
|
||||
'sandbox' => self::TRADESHIFT_SANDBOX_API_DOMAIN,
|
||||
'live' => self::TRADESHIFT_API_DOMAIN,
|
||||
];
|
||||
|
||||
private $endpoint = [
|
||||
'sandbox' => 'https://' . self::TRADESHIFT_SANDBOX_API_DOMAIN . '/tradeshift/',
|
||||
'live' => 'https://' . self::TRADESHIFT_API_DOMAIN . '/tradeshift/',
|
||||
];
|
||||
|
||||
private $resourceEndpoint = [
|
||||
'sandbox' => 'https://' . self::TRADESHIFT_SANDBOX_API_DOMAIN . '/tradeshift/rest/external/',
|
||||
'live' => 'https://' . self::TRADESHIFT_API_DOMAIN . '/tradeshift/rest/external/',
|
||||
];
|
||||
|
||||
protected $environment = 'live';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $user = [];
|
||||
|
||||
|
||||
protected $scopes = [
|
||||
'openid',
|
||||
'offline',
|
||||
];
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return 'tradeshift';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLoginURL(): string
|
||||
{
|
||||
$httpQuery = \http_build_query([
|
||||
'response_type' => 'code',
|
||||
'client_id' => $this->appID,
|
||||
'scope' => \implode(' ', $this->getScopes()),
|
||||
'redirect_uri' => \str_replace("localhost", "127.0.0.1", $this->callback),
|
||||
'state' => \json_encode($this->state),
|
||||
]);
|
||||
|
||||
$url = $this->endpoint[$this->environment] . 'auth/login?' . $httpQuery;
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $code
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAccessToken(string $code): string
|
||||
{
|
||||
$response = $this->request(
|
||||
'POST',
|
||||
$this->endpoint[$this->environment] . 'auth/token',
|
||||
['Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret)],
|
||||
\http_build_query([
|
||||
'grant_type' => 'authorization_code',
|
||||
'code' => $code,
|
||||
])
|
||||
);
|
||||
|
||||
$accessToken = \json_decode($response, true);
|
||||
|
||||
return $accessToken['access_token'] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $accessToken
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUserID(string $accessToken): string
|
||||
{
|
||||
$user = $this->getUser($accessToken);
|
||||
|
||||
return $user['Id'] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $accessToken
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUserEmail(string $accessToken): string
|
||||
{
|
||||
$user = $this->getUser($accessToken);
|
||||
|
||||
return $user['Username'] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $accessToken
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUserName(string $accessToken): string
|
||||
{
|
||||
$user = $this->getUser($accessToken);
|
||||
|
||||
$firstName = $user['FirstName'] ?? '';
|
||||
$lastName = $user['LastName'] ?? '';
|
||||
|
||||
return $firstName . ' ' . $lastName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $accessToken
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getUser(string $accessToken): array
|
||||
{
|
||||
$header = [
|
||||
'Content-Type: application/json',
|
||||
'Accept: application/json',
|
||||
'Host: ' . urlencode($this->apiDomain[$this->environment]),
|
||||
'Authorization: Bearer ' . $accessToken,
|
||||
];
|
||||
|
||||
if (empty($this->user)) {
|
||||
$response = $this->request(
|
||||
'GET',
|
||||
$this->resourceEndpoint[$this->environment] . 'account/info/user',
|
||||
$header
|
||||
);
|
||||
$this->user = \json_decode($response, true);
|
||||
}
|
||||
|
||||
return $this->user;
|
||||
}
|
||||
}
|
18
src/Appwrite/Auth/OAuth2/TradeshiftBox.php
Normal file
18
src/Appwrite/Auth/OAuth2/TradeshiftBox.php
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Auth\OAuth2;
|
||||
|
||||
use Appwrite\Auth\OAuth2\Tradeshift;
|
||||
|
||||
class TradeshiftBox extends Tradeshift
|
||||
{
|
||||
protected $environment = 'sandbox';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return 'tradeshiftBox';
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue