From 0885393ce7493b0aef19ef922b7fbf7d721f4b40 Mon Sep 17 00:00:00 2001 From: Teodor Talov Date: Fri, 8 May 2015 02:30:49 -0400 Subject: [PATCH 1/3] Refactored authentication to suport multiple clients - Direct, OAuth, etc. --- README.md | 10 +- examples.php | 19 +-- src/Citrix/Authentication/Client.php | 14 +++ src/Citrix/Authentication/Direct.php | 14 +++ src/Citrix/Citrix.php | 171 +-------------------------- src/Citrix/Entity/Consumer.php | 4 +- src/Citrix/Entity/EntityAbstract.php | 4 +- src/Citrix/Entity/Webinar.php | 4 +- src/Citrix/GoToWebinar.php | 17 ++- 9 files changed, 72 insertions(+), 185 deletions(-) create mode 100644 src/Citrix/Authentication/Client.php create mode 100644 src/Citrix/Authentication/Direct.php diff --git a/README.md b/README.md index 07f75a6..d1951b3 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Citrix API - PHP warpper around GoToWebinar APIs - 2014 +Citrix API - PHP warpper around GoToWebinar APIs - 2015 ====== Install via Composer @@ -37,6 +37,14 @@ In order to get all the upcoming webinars, you have to do this: $webinars = $goToWebinar->getUpcoming(); var_dump($webinars) //this gives you all upcoming webinars +Getting past webinars +-- + +In order to get all the past webinars, you have to do this: + + $goToWebinar = new \Citrix\GoToWebinar($client); //@see $client definition above + $webinars = $goToWebinar->getUpcoming(); + var_dump($webinars) //this gives you all upcoming webinars If you would like to get the registration/join URL for a webinar you can do so like this: ```php diff --git a/examples.php b/examples.php index 6f63752..1b1d104 100644 --- a/examples.php +++ b/examples.php @@ -5,20 +5,17 @@ require_once 'vendor/autoload.php'; -use Citrix\Citrix; -use Citrix\GoToWebinar; -use Citrix\Entity\Consumer; -//authenticate -$client = new Citrix('CONSUMER_KEY'); +//authenticate with direct authentication +$client = new \Citrix\Authentication\Direct('CONSUMER_KEY'); $client->auth('USERNAME', 'PASSWORD'); //get upcoming weibnars -$goToWebinar = new GoToWebinar($client); +$goToWebinar = new \Citrix\GoToWebinar($client); $webinars = $goToWebinar->getUpcoming(); //get info for a single webinar -/* @var $webinar Citrix\Enttiy\Webinar */ +/* @var $webinar \Citrix\Entity\Webinar */ $webinar = reset($webinars); //get registraion/join url @@ -32,7 +29,7 @@ //register a user for a webinar $data = array('email' => 'teodor@talov.com', 'firstName' => 'Teodor', 'lastName' => 'Talov'); -$consumer = new Consumer($client); +$consumer = new \Citrix\Entity\Consumer($client); $consumer->setData($data)->populate(); $registration = $webinar->registerConsumer($consumer); @@ -42,3 +39,9 @@ } var_dump('You just registered!'); + +//get past weibnars +$goToWebinar = new \Citrix\GoToWebinar($client); +$webinars = $goToWebinar->getPast(); + + diff --git a/src/Citrix/Authentication/Client.php b/src/Citrix/Authentication/Client.php new file mode 100644 index 0000000..5eff471 --- /dev/null +++ b/src/Citrix/Authentication/Client.php @@ -0,0 +1,14 @@ +setApiKey($apiKey); - } - - /** - * Authenticate by passing username and password. Those are - * the same username and password that you use to login to - * www.gotowebinar.com - * - * @param String $username - * @param String $password - * @return \Citrix\Citrix - */ - public function auth($username, $password) - { - $params = array( - 'grant_type' => 'password', - 'user_id' => $username, - 'password' => $password, - 'client_id' => $this->getApiKey() - ); - - $this->setHttpMethod('GET') - ->setUrl($this->authorizeUrl) - ->setParams($params) - ->sendRequest() - ->processResponse(); - - return $this; - } - - /** - * - * @return the $apiKey - */ - public function getApiKey() - { - return $this->apiKey; - } - - /** - * - * @param String $apiKey - */ - public function setApiKey($apiKey) - { - $this->apiKey = $apiKey; - - return $this; - } - - /* (non-PHPdoc) - * @see \Citrix\CitrixApiAware::processResponse() - */ - public function processResponse() - { - $response = $this->getResponse(); - - if (empty($response)) { - return $this; - } - - if (isset($response['int_err_code'])) { - $this->addError($response['msg']); - return $this; - } - - $this->setAccessToken($response['access_token']); - $this->setOrganizerKey($response['organizer_key']); - return $this; - } - - /** - * - * @return the $accessToken - */ - public function getAccessToken() - { - return $this->accessToken; - } - - /** - * - * @param String $accessToken - */ - public function setAccessToken($accessToken) - { - $this->accessToken = $accessToken; - - return $this; - } - - /** - * - * @return the $authorizeUrl - */ - public function getAuthorizeUrl() - { - return $this->authorizeUrl; - } - - /** - * - * @param string $authorizeUrl - */ - public function setAuthorizeUrl($authorizeUrl) - { - $this->authorizeUrl = $authorizeUrl; - - return $this; - } - - /** - * - * @return the $organizerKey - */ - public function getOrganizerKey() - { - return $this->organizerKey; - } - - /** - * - * @param int $organizerKey - */ - public function setOrganizerKey($organizerKey) - { - $this->organizerKey = $organizerKey; - - return $this; - } } \ No newline at end of file diff --git a/src/Citrix/Entity/Consumer.php b/src/Citrix/Entity/Consumer.php index 2138c19..b883ad5 100644 --- a/src/Citrix/Entity/Consumer.php +++ b/src/Citrix/Entity/Consumer.php @@ -74,9 +74,9 @@ class Consumer extends EntityAbstract implements EntityAware /** * Begin here by injecting authentication object. * - * @param \Citrix\Citrix $client + * @param $client */ - public function __construct(\Citrix\Citrix $client) + public function __construct($client) { $this->setClient($client); } diff --git a/src/Citrix/Entity/EntityAbstract.php b/src/Citrix/Entity/EntityAbstract.php index 3799a4d..efbc313 100644 --- a/src/Citrix/Entity/EntityAbstract.php +++ b/src/Citrix/Entity/EntityAbstract.php @@ -33,9 +33,9 @@ protected function getClient() /** * - * @param \Citrix\Citrix $client + * @param $client */ - protected function setClient(\Citrix\Citrix $client) + protected function setClient($client) { $this->client = $client; diff --git a/src/Citrix/Entity/Webinar.php b/src/Citrix/Entity/Webinar.php index eec54f5..9ce9016 100644 --- a/src/Citrix/Entity/Webinar.php +++ b/src/Citrix/Entity/Webinar.php @@ -75,9 +75,9 @@ class Webinar extends EntityAbstract implements EntityAware /** * Beging here by injecting an authentication object. * - * @param \Citrix\Citrix $client + * @param $client */ - public function __construct(\Citrix\Citrix $client) + public function __construct($client) { $this->setClient($client); $this->consumers = new \ArrayObject(); diff --git a/src/Citrix/GoToWebinar.php b/src/Citrix/GoToWebinar.php index 08bdbac..698165a 100644 --- a/src/Citrix/GoToWebinar.php +++ b/src/Citrix/GoToWebinar.php @@ -1,6 +1,7 @@ setClient($client); } @@ -63,11 +64,21 @@ public function getWebinars(){ } /** - * Get all webinars. + * Get past webinars. * + * @deprecated - Use GoToWebinar::getPast() instead2 * @return \ArrayObject - Processed response */ public function getPastWebinars(){ + return $this->getPast(); + } + + /** + * Get all past webinars. + * @todo - add date range + * @return \ArrayObject - Processed response + */ + public function getPast(){ $since = date(DATE_ISO8601, mktime(0, 0, 0, 7, 1, 2000)); $until = date(DATE_ISO8601); $url = 'https://api.citrixonline.com/G2W/rest/organizers/' . $this->getClient()->getOrganizerKey() . '/historicalWebinars'; From 76c528b4685e12523589c80832a73cc56d0acd27 Mon Sep 17 00:00:00 2001 From: Teodor Talov Date: Fri, 8 May 2015 02:31:05 -0400 Subject: [PATCH 2/3] Refactored authentication to suport multiple clients - Direct, OAuth, etc. --- src/Citrix/Authentication/Client.php | 32 ++++- src/Citrix/Authentication/Direct.php | 186 ++++++++++++++++++++++++++- 2 files changed, 216 insertions(+), 2 deletions(-) diff --git a/src/Citrix/Authentication/Client.php b/src/Citrix/Authentication/Client.php index 5eff471..33272a8 100644 --- a/src/Citrix/Authentication/Client.php +++ b/src/Citrix/Authentication/Client.php @@ -8,7 +8,37 @@ namespace Citrix\Authentication; - +/* + * Wrapper class for all authentication methods. + * It could be used for a single point of entry foo authentication functionality. + */ class Client { + private $authentication; + public function __construct($authentication = 'Direct'){ + + $this->setAuthentication($authentication); + } + + public function auth(){ + return $this->getAuthentication()->auth(); + } + /** + * @return mixed + */ + public function getAuthentication() + { + return $this->authentication; + } + + /** + * @param string $authentication + */ + public function setAuthentication($authentication) + { + $class = '\\Citrix\\Authentication\\' . $authentication; + $this->authentication = new $class(); + return $this; + } + } \ No newline at end of file diff --git a/src/Citrix/Authentication/Direct.php b/src/Citrix/Authentication/Direct.php index 915f2fa..3647105 100644 --- a/src/Citrix/Authentication/Direct.php +++ b/src/Citrix/Authentication/Direct.php @@ -8,7 +8,191 @@ namespace Citrix\Authentication; +use Citrix\ServiceAbstract; +use Citrix\CitrixApiAware; -class Direct { +/** + * Citrix Authentication + * + * Use this class to authenticate into Citrix APIs. + * + * @uses \Citrix\ServiceAbstract + * @uses \Citrix\CitrixApiAware + */ + +class Direct extends ServiceAbstract implements CitrixApiAware +{ + + /** + * Authentication URL + * @var String + */ + private $authorizeUrl = 'https://api.citrixonline.com/oauth/access_token'; + + /** + * API key or Secret Key in Citrix's Developer Portal + * @var String + */ + private $apiKey; + + /** + * Access Token + * @var String + */ + private $accessToken; + + /** + * Organizer Key + * @var int + */ + private $organizerKey; + + /** + * Being here bu passing the api key + * + * @param String $apiKey + */ + public function __construct($apiKey = null) + { + $this->setApiKey($apiKey); + } + + /** + * Authenticate by passing username and password. Those are + * the same username and password that you use to login to + * www.gotowebinar.com + * + * @param String $username + * @param String $password + * @return \Citrix\Citrix + */ + public function auth($username, $password) + { + + if(is_null($this->getApiKey())){ + $this->addError('Direct Authentication requires API key. Please provide API key.'); + return $this; + } + + if(is_null($username) || is_null($password)){ + $this->addError('Direct Authentication requires username and password. Please provide username and password.'); + return $this; + } + + $params = array( + 'grant_type' => 'password', + 'user_id' => $username, + 'password' => $password, + 'client_id' => $this->getApiKey() + ); + + $this->setHttpMethod('GET') + ->setUrl($this->authorizeUrl) + ->setParams($params) + ->sendRequest() + ->processResponse(); + + return $this; + } + + /** + * + * @return the $apiKey + */ + public function getApiKey() + { + return $this->apiKey; + } + + /** + * + * @param String $apiKey + */ + public function setApiKey($apiKey) + { + $this->apiKey = $apiKey; + + return $this; + } + + /* (non-PHPdoc) + * @see \Citrix\CitrixApiAware::processResponse() + */ + public function processResponse() + { + $response = $this->getResponse(); + + if (empty($response)) { + return $this; + } + + if (isset($response['int_err_code'])) { + $this->addError($response['msg']); + return $this; + } + + $this->setAccessToken($response['access_token']); + $this->setOrganizerKey($response['organizer_key']); + return $this; + } + + /** + * + * @return the $accessToken + */ + public function getAccessToken() + { + return $this->accessToken; + } + + /** + * + * @param String $accessToken + */ + public function setAccessToken($accessToken) + { + $this->accessToken = $accessToken; + + return $this; + } + + /** + * + * @return the $authorizeUrl + */ + public function getAuthorizeUrl() + { + return $this->authorizeUrl; + } + + /** + * + * @param string $authorizeUrl + */ + public function setAuthorizeUrl($authorizeUrl) + { + $this->authorizeUrl = $authorizeUrl; + + return $this; + } + + /** + * + * @return the $organizerKey + */ + public function getOrganizerKey() + { + return $this->organizerKey; + } + + /** + * + * @param int $organizerKey + */ + public function setOrganizerKey($organizerKey) + { + $this->organizerKey = $organizerKey; + return $this; + } } \ No newline at end of file From 75ba2a92b414e57b9b3c83d0e680aa60cd2885de Mon Sep 17 00:00:00 2001 From: Teodor Talov Date: Fri, 8 May 2015 02:34:45 -0400 Subject: [PATCH 3/3] Updated README --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d1951b3..34f373d 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,13 @@ Authenticate and Get Going in 15 seconds All you need in order to authenticate with Citrix's API is consumer key, which you can obtain by registering at [Citrix Developer Center][1]. After registering and adding your application, you will be given *Consumer Key*, *Consumer Secret*, and *Callback URL*. You need the *Consumer Key* in order for your application to authenticate with Citrix. -In addition to the *Consumer Key*, you need your *username* and *password*, which is the one that you use to login to say [GoToWebinar.com][2]. +## Direct Authentication ## +In addition to the *Consumer Key*, for **Direct Authentication** you need your *username* and *password*, which is the one that you use to login to say [GoToWebinar.com][2]. You can authenticate to Citrix, and your GoToWebinar account respectively, like so: - $client = new \Citrix\Citrix('CONSUMER_KEY'); + $client = new \Citrix\Authentication\Direct('CONSUMER_KEY'); $client->auth('USERNAME', 'PASSWORD');