/libraries/joomla/linkedin/oauth.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 144 lines · 60 code · 16 blank · 68 comment · 7 complexity · 664abdeecce54e5fc86a2423ab9f7a35 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Linkedin
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die();
  10. /**
  11. * Joomla Platform class for generating Linkedin API access token.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Linkedin
  15. *
  16. * @since 13.1
  17. */
  18. class JLinkedinOauth extends JOAuth1Client
  19. {
  20. /**
  21. * @var JRegistry Options for the JLinkedinOauth object.
  22. * @since 13.1
  23. */
  24. protected $options;
  25. /**
  26. * Constructor.
  27. *
  28. * @param JRegistry $options JLinkedinOauth options object.
  29. * @param JHttp $client The HTTP client object.
  30. * @param JInput $input The input object
  31. *
  32. * @since 13.1
  33. */
  34. public function __construct(JRegistry $options = null, JHttp $client = null, JInput $input = null)
  35. {
  36. $this->options = isset($options) ? $options : new JRegistry;
  37. $this->options->def('accessTokenURL', 'https://www.linkedin.com/uas/oauth/accessToken');
  38. $this->options->def('authenticateURL', 'https://www.linkedin.com/uas/oauth/authenticate');
  39. $this->options->def('authoriseURL', 'https://www.linkedin.com/uas/oauth/authorize');
  40. $this->options->def('requestTokenURL', 'https://www.linkedin.com/uas/oauth/requestToken');
  41. // Call the JOauthV1aclient constructor to setup the object.
  42. parent::__construct($this->options, $client, $input);
  43. }
  44. /**
  45. * Method to verify if the access token is valid by making a request to an API endpoint.
  46. *
  47. * @return boolean Returns true if the access token is valid and false otherwise.
  48. *
  49. * @since 13.1
  50. */
  51. public function verifyCredentials()
  52. {
  53. $token = $this->getToken();
  54. // Set parameters.
  55. $parameters = array(
  56. 'oauth_token' => $token['key']
  57. );
  58. $data['format'] = 'json';
  59. // Set the API url.
  60. $path = 'https://api.linkedin.com/v1/people::(~)';
  61. // Send the request.
  62. $response = $this->oauthRequest($path, 'GET', $parameters, $data);
  63. // Verify response
  64. if ($response->code == 200)
  65. {
  66. return true;
  67. }
  68. else
  69. {
  70. return false;
  71. }
  72. }
  73. /**
  74. * Method to validate a response.
  75. *
  76. * @param string $url The request URL.
  77. * @param JHttpResponse $response The response to validate.
  78. *
  79. * @return void
  80. *
  81. * @since 13.1
  82. * @throws DomainException
  83. */
  84. public function validateResponse($url, $response)
  85. {
  86. if (!$code = $this->getOption('success_code'))
  87. {
  88. $code = 200;
  89. }
  90. if (strpos($url, '::(~)') === false && $response->code != $code)
  91. {
  92. if ($error = json_decode($response->body))
  93. {
  94. throw new DomainException('Error code ' . $error->errorCode . ' received with message: ' . $error->message . '.');
  95. }
  96. else
  97. {
  98. throw new DomainException($response->body);
  99. }
  100. }
  101. }
  102. /**
  103. * Method used to set permissions.
  104. *
  105. * @param mixed $scope String or an array of string containing permissions.
  106. *
  107. * @return JLinkedinOauth This object for method chaining
  108. *
  109. * @see https://developer.linkedin.com/documents/authentication
  110. * @since 13.1
  111. */
  112. public function setScope($scope)
  113. {
  114. $this->setOption('scope', $scope);
  115. return $this;
  116. }
  117. /**
  118. * Method to get the current scope
  119. *
  120. * @return string String or an array of string containing permissions.
  121. *
  122. * @since 13.1
  123. */
  124. public function getScope()
  125. {
  126. return $this->getOption('scope');
  127. }
  128. }