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