| 10582 |
lgm |
1 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
2 |
/**
|
|
|
3 |
* Google OAuth2 Provider
|
|
|
4 |
*
|
|
|
5 |
* @package CodeIgniter/OAuth2
|
|
|
6 |
* @category Provider
|
|
|
7 |
* @author Phil Sturgeon
|
|
|
8 |
* @copyright (c) 2012 HappyNinjas Ltd
|
|
|
9 |
* @license http://philsturgeon.co.uk/code/dbad-license
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
class OAuth2_Provider_Google extends OAuth2_Provider
|
|
|
13 |
{
|
|
|
14 |
/**
|
|
|
15 |
* @var string the method to use when requesting tokens
|
|
|
16 |
*/
|
|
|
17 |
public $method = 'POST';
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* @var string scope separator, most use "," but some like Google are spaces
|
|
|
21 |
*/
|
|
|
22 |
public $scope_seperator = ' ';
|
|
|
23 |
|
|
|
24 |
public function url_authorize()
|
|
|
25 |
{
|
|
|
26 |
return 'https://accounts.google.com/o/oauth2/auth';
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
public function url_access_token()
|
|
|
30 |
{
|
|
|
31 |
return 'https://accounts.google.com/o/oauth2/token';
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
public function __construct(array $options = array())
|
|
|
35 |
{
|
|
|
36 |
// Now make sure we have the default scope to get user data
|
|
|
37 |
empty($options['scope']) and $options['scope'] = array(
|
|
|
38 |
'https://www.googleapis.com/auth/userinfo.profile',
|
|
|
39 |
'https://www.googleapis.com/auth/userinfo.email'
|
|
|
40 |
);
|
|
|
41 |
|
|
|
42 |
// Array it if its string
|
|
|
43 |
$options['scope'] = (array) $options['scope'];
|
|
|
44 |
|
|
|
45 |
parent::__construct($options);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/*
|
|
|
49 |
* Get access to the API
|
|
|
50 |
*
|
|
|
51 |
* @param string The access code
|
|
|
52 |
* @return object Success or failure along with the response details
|
|
|
53 |
*/
|
|
|
54 |
public function access($code, $options = array())
|
|
|
55 |
{
|
|
|
56 |
if ($code === null)
|
|
|
57 |
{
|
|
|
58 |
throw new OAuth2_Exception(array('message' => 'Expected Authorization Code from '.ucfirst($this->name).' is missing'));
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
return parent::access($code, $options);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
public function get_user_info(OAuth2_Token_Access $token)
|
|
|
65 |
{
|
|
|
66 |
$url = 'https://www.googleapis.com/oauth2/v1/userinfo?alt=json&'.http_build_query(array(
|
|
|
67 |
'access_token' => $token->access_token,
|
|
|
68 |
));
|
|
|
69 |
$ch = curl_init($url);
|
|
|
70 |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
|
|
|
71 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
72 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
73 |
//$response = curl_exec($ch);
|
|
|
74 |
$user = json_decode(curl_exec($ch), true);
|
|
|
75 |
return array(
|
|
|
76 |
'uid' => $user['id'],
|
|
|
77 |
'nickname' => url_title($user['name'], '_', true),
|
|
|
78 |
'name' => $user['name'],
|
|
|
79 |
'first_name' => $user['given_name'],
|
|
|
80 |
'last_name' => $user['family_name'],
|
|
|
81 |
'email' => $user['email'],
|
|
|
82 |
'location' => null,
|
|
|
83 |
'image' => (isset($user['picture'])) ? $user['picture'] : null,
|
|
|
84 |
'description' => null,
|
|
|
85 |
'urls' => array(),
|
|
|
86 |
'loginType'=>'google',
|
|
|
87 |
);
|
|
|
88 |
}
|
|
|
89 |
}
|