| 10582 |
lgm |
1 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
2 |
/**
|
|
|
3 |
* Yandex OAuth2 Provider
|
|
|
4 |
*
|
|
|
5 |
* @package CodeIgniter/OAuth2
|
|
|
6 |
* @category Provider
|
|
|
7 |
* @author Lavr Lyndin
|
|
|
8 |
*/
|
|
|
9 |
|
|
|
10 |
class OAuth2_Provider_Yandex extends OAuth2_Provider
|
|
|
11 |
{
|
|
|
12 |
public $method = 'POST';
|
|
|
13 |
|
|
|
14 |
public function url_authorize()
|
|
|
15 |
{
|
|
|
16 |
return 'https://oauth.yandex.ru/authorize';
|
|
|
17 |
}
|
|
|
18 |
|
|
|
19 |
public function url_access_token()
|
|
|
20 |
{
|
|
|
21 |
return 'https://oauth.yandex.ru/token';
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
public function get_user_info(OAuth2_Token_Access $token)
|
|
|
25 |
{
|
|
|
26 |
$opts = array(
|
|
|
27 |
'http' => array(
|
|
|
28 |
'method' => 'GET',
|
|
|
29 |
'header' => 'Authorization: OAuth '.$token->access_token
|
|
|
30 |
)
|
|
|
31 |
);
|
|
|
32 |
$_default_opts = stream_context_get_params(stream_context_get_default());
|
|
|
33 |
|
|
|
34 |
$opts = array_merge_recursive($_default_opts['options'], $opts);
|
|
|
35 |
$context = stream_context_create($opts);
|
|
|
36 |
$url = 'http://api-yaru.yandex.ru/me/?format=json';
|
|
|
37 |
|
|
|
38 |
$user = json_decode(file_get_contents($url,false,$context));
|
|
|
39 |
|
|
|
40 |
preg_match("/\d+$/",$user->id,$uid);
|
|
|
41 |
|
|
|
42 |
return array(
|
|
|
43 |
'uid' => $uid[0],
|
|
|
44 |
'nickname' => isset($user->name) ? $user->name : null,
|
|
|
45 |
'name' => isset($user->name) ? $user->name : null,
|
|
|
46 |
'first_name' => isset($user->first_name) ? $user->first_name : null,
|
|
|
47 |
'last_name' => isset($user->last_name) ? $user->last_name : null,
|
|
|
48 |
'email' => isset($user->email) ? $user->email : null,
|
|
|
49 |
'location' => isset($user->hometown->name) ? $user->hometown->name : null,
|
|
|
50 |
'description' => isset($user->bio) ? $user->bio : null,
|
|
|
51 |
'image' => $user->links->userpic,
|
|
|
52 |
);
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
public function access($code, $options = array())
|
|
|
56 |
{
|
|
|
57 |
$params = array(
|
|
|
58 |
'client_id' => $this->client_id,
|
|
|
59 |
'client_secret' => $this->client_secret,
|
|
|
60 |
'grant_type' => isset($options['grant_type']) ? $options['grant_type'] : 'authorization_code',
|
|
|
61 |
);
|
|
|
62 |
|
|
|
63 |
switch ($params['grant_type'])
|
|
|
64 |
{
|
|
|
65 |
case 'authorization_code':
|
|
|
66 |
$params['code'] = $code;
|
|
|
67 |
$params['redirect_uri'] = isset($options['redirect_uri']) ? $options['redirect_uri'] : $this->redirect_uri;
|
|
|
68 |
break;
|
|
|
69 |
|
|
|
70 |
case 'refresh_token':
|
|
|
71 |
$params['refresh_token'] = $code;
|
|
|
72 |
break;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
$response = null;
|
|
|
76 |
$url = $this->url_access_token();
|
|
|
77 |
|
|
|
78 |
$curl = curl_init($url);
|
|
|
79 |
|
|
|
80 |
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8;';
|
|
|
81 |
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
|
|
82 |
|
|
|
83 |
// curl_setopt($curl, CURLOPT_USERAGENT, 'yamolib-php');
|
|
|
84 |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
|
85 |
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
|
|
|
86 |
curl_setopt($curl, CURLOPT_TIMEOUT, 80);
|
|
|
87 |
curl_setopt($curl, CURLOPT_POST, true);
|
|
|
88 |
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
|
|
|
89 |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
90 |
// curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
|
|
|
91 |
// curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/../data/ca-certificate.crt');
|
|
|
92 |
|
|
|
93 |
$response = curl_exec($curl);
|
|
|
94 |
curl_close($curl);
|
|
|
95 |
|
|
|
96 |
$return = json_decode($response, true);
|
|
|
97 |
|
|
|
98 |
if ( ! empty($return['error']))
|
|
|
99 |
{
|
|
|
100 |
throw new OAuth2_Exception($return);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
switch ($params['grant_type'])
|
|
|
104 |
{
|
|
|
105 |
case 'authorization_code':
|
|
|
106 |
return OAuth2_Token::factory('access', $return);
|
|
|
107 |
break;
|
|
|
108 |
|
|
|
109 |
case 'refresh_token':
|
|
|
110 |
return OAuth2_Token::factory('refresh', $return);
|
|
|
111 |
break;
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
}
|