Blame view

common/components/nodge/eauth/src/services/FacebookOAuth2Service.php 2.62 KB
b0f143c3   Yarik   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  <?php
  /**
   * FacebookOAuth2Service class file.
   *
   * Register application: https://developers.facebook.com/apps/
   *
   * @author Maxim Zemskov <nodge@yandex.ru>
   * @link http://github.com/Nodge/yii2-eauth/
   * @license http://www.opensource.org/licenses/bsd-license.php
   */
  namespace common\components\nodge\eauth\src\services;
  use nodge\eauth\oauth2\Service;
  /**
   * Facebook provider class.
   *
   * @package application.extensions.eauth.services
   */
  class FacebookOAuth2Service extends Service
  {
  	/**
  	 * Full list of scopes may be found here:
  	 * https://developers.facebook.com/docs/authentication/permissions/
  	 */
  	const SCOPE_EMAIL = 'email';
  	const SCOPE_USER_BIRTHDAY = 'user_birthday';
  	const SCOPE_USER_HOMETOWN = 'user_hometown';
  	const SCOPE_USER_LOCATION = 'user_location';
  	const SCOPE_USER_PHOTOS = 'user_photos';
  	protected $name = 'facebook';
  	protected $title = 'Facebook';
  	protected $type = 'OAuth2';
  	protected $jsArguments = ['popup' => ['width' => 585, 'height' => 290]];
  	protected $scopes = [];
  	protected $providerOptions = [
  		'authorize' => 'https://www.facebook.com/dialog/oauth',
  		'access_token' => 'https://graph.facebook.com/oauth/access_token',
  	];
  	protected $baseApiUrl = 'https://graph.facebook.com/';
  	protected $errorParam = 'error_code';
  	protected $errorDescriptionParam = 'error_message';
  	protected function fetchAttributes()
  	{
  		$info = $this->makeSignedRequest('me');
  		$this->attributes['id'] = $info['id'];
  		$this->attributes['name'] = $info['name'];
  		$this->attributes['url'] = $info['link'];
  		return true;
  	}
  	/**
  	 * @return array
  	 */
  	public function getAccessTokenArgumentNames()
  	{
  		$names = parent::getAccessTokenArgumentNames();
  		$names['expires_in'] = 'expires';
  		return $names;
  	}
  	/**
  	 * @param string $response
  	 * @return array
  	 */
  	public function parseAccessTokenResponse($response)
  	{
  		// Facebook gives us a query string or json
  		if ($response[0] === '{') {
  			return json_decode($response, true);
  		}
  		else {
  			parse_str($response, $data);
  			return $data;
  		}
  	}
  	/**
  	 * Returns the error array.
  	 *
  	 * @param array $response
  	 * @return array the error array with 2 keys: code and message. Should be null if no errors.
  	 */
  	protected function fetchResponseError($response)
  	{
  		if (isset($response['error'])) {
  			return [
  				'code' => $response['error']['code'],
  				'message' => $response['error']['message'],
  			];
  		} else {
  			return null;
  		}
  	}
  	/**
  	 * @param array $data
  	 * @return string|null
  	 */
  	public function getAccessTokenResponseError($data)
  	{
  		$error = $this->fetchResponseError($data);
  		if (!$error) {
  			return null;
  		}
  		return $error['code'].': '.$error['message'];
  	}
  }