Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
EOAuthService.php
1 <?php
2 /**
3  * EOAuthService class file.
4  *
5  * @author Maxim Zemskov <nodge@yandex.ru>
6  * @link http://code.google.com/p/yii-eauth/
7  * @license http://www.opensource.org/licenses/bsd-license.php
8  */
9 
10 require_once 'EAuthServiceBase.php';
11 
12 /**
13  * EOAuthService is a base class for all OAuth providers.
14  * @package application.extensions.eauth
15  */
16 abstract class EOAuthService extends EAuthServiceBase implements IAuthService {
17 
18  /**
19  * @var EOAuthUserIdentity the OAuth library instance.
20  */
21  private $auth;
22 
23 
24  /**
25  * @var string OAuth2 client id.
26  */
27  protected $key;
28 
29  /**
30  * @var string OAuth2 client secret key.
31  */
32  protected $secret;
33 
34  /**
35  * @var string OAuth scopes.
36  */
37  protected $scope = '';
38 
39  /**
40  * @var array Provider options. Must contain the keys: request, authorize, access.
41  */
42  protected $providerOptions = array(
43  'request' => '',
44  'authorize' => '',
45  'access' => '',
46  );
47 
48 
49  /**
50  * Initialize the component.
51  * @param EAuth $component the component instance.
52  * @param array $options properties initialization.
53  */
54  public function init($component, $options = array()) {
55  parent::init($component, $options);
56 
57  $this->auth = new EOAuthUserIdentity(array(
58  'scope' => $this->scope,
59  'key' => $this->key,
60  'secret' => $this->secret,
61  'provider' => $this->providerOptions,
62  ));
63  }
64 
65  /**
66  * Authenticate the user.
67  * @return boolean whether user was successfuly authenticated.
68  */
69  public function authenticate() {
70  $this->authenticated = $this->auth->authenticate();
71  return $this->getIsAuthenticated();
72  }
73 
74  /**
75  * Returns the OAuth consumer.
76  * @return object the consumer.
77  */
78  protected function getConsumer() {
79  return $this->auth->getProvider()->consumer;
80  }
81 
82  /**
83  * Returns the OAuth access token.
84  * @return string the token.
85  */
86  protected function getAccessToken() {
87  return $this->auth->getProvider()->token;
88  }
89 
90  /**
91  * Initializes a new session and return a cURL handle.
92  * @param string $url url to request.
93  * @param array $options HTTP request options. Keys: query, data, referer.
94  * @param boolean $parseJson Whether to parse response in json format.
95  * @return cURL handle.
96  */
97  protected function initRequest($url, $options = array()) {
98  $ch = parent::initRequest($url, $options);
99  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
100  if (isset($options['data'])) {
101  curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml","SOAPAction: \"/soap/action/query\"", "Content-length: ".strlen($options['data'])));
102  }
103  return $ch;
104  }
105 
106  /**
107  * Returns the protected resource.
108  * @param string $url url to request.
109  * @param array $options HTTP request options. Keys: query, data, referer.
110  * @param boolean $parseJson Whether to parse response in json format.
111  * @return string the response.
112  * @see makeRequest
113  */
114  public function makeSignedRequest($url, $options = array(), $parseJson = true) {
115  if (!$this->getIsAuthenticated())
116  throw new CHttpException(401, Yii::t('eauth', 'Unable to complete the request because the user was not authenticated.'));
117 
118  $consumer = $this->getConsumer();
119  $signatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
120  $token = $this->getAccessToken();
121 
122  $request = OAuthRequest::from_consumer_and_token($consumer, $token, isset($options['data']) ? 'POST' : 'GET', $url);
123  $request->sign_request($signatureMethod, $consumer, $token);
124  $url = $request->to_url();
125 
126  return $this->makeRequest($url, $options, $parseJson);
127  }
128 }