1
|
|
|
<?php defined('SYSPATH') OR die('No direct access allowed.'); |
2
|
|
|
|
3
|
|
|
use Facebook\Facebook; |
4
|
|
|
use Facebook\Exceptions\FacebookResponseException; |
5
|
|
|
use Facebook\Exceptions\FacebookSDKException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Jam Auth driver. |
9
|
|
|
* |
10
|
|
|
* @package Kohana/Auth |
11
|
|
|
* @author Ivan Kerin |
12
|
|
|
* @copyright (c) 2011-2012 Despark Ltd. |
13
|
|
|
* @license http://creativecommons.org/licenses/by-sa/3.0/legalcode |
14
|
|
|
*/ |
15
|
|
|
abstract class Kohana_Auth_Service_Facebook extends Auth_Service { |
16
|
|
|
|
17
|
|
|
protected $_service_field = 'facebook_uid'; |
18
|
|
|
|
19
|
|
|
protected $_type = 'facebook'; |
20
|
|
|
|
21
|
|
|
public function initialize() |
22
|
|
|
{ |
23
|
|
|
return new Facebook(Arr::get($this->_config, 'auth')); |
24
|
|
|
} |
25
|
|
|
|
26
|
2 |
|
public function logged_in() |
27
|
|
|
{ |
28
|
2 |
|
return (bool) $this->service_uid(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function login_url($back_url) |
32
|
|
|
{ |
33
|
|
|
$helper = $this->api()->getRedirectLoginHelper(); |
34
|
|
|
|
35
|
|
|
$permissions = ['email']; |
36
|
|
|
|
37
|
|
|
return $helper->getLoginUrl($back_url, $permissions); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function logout_service($request, $back_url) |
41
|
|
|
{ |
42
|
|
|
$helper = $this->api()->getRedirectLoginHelper(); |
43
|
|
|
|
44
|
|
|
$accessToken = $helper->getAccessToken(); |
45
|
|
|
|
46
|
|
|
$logoutUrl = $helper->getLogoutUrl($accessToken, $back_url); |
47
|
|
|
|
48
|
|
|
HTTP::redirect($logoutUrl); |
49
|
|
|
|
50
|
|
|
return FALSE; |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
public function get_user_node() |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
2 |
|
$result = $this->api()->get('/me?fields=id,first_name,last_name,email,name'); |
57
|
|
|
|
58
|
|
|
} catch(FacebookResponseException $exception) { |
59
|
|
|
throw new Auth_Exception_Service($exception->getMessage(), [], 0, $exception); |
60
|
|
|
} catch(FacebookSDKException $exception) { |
61
|
|
|
throw new Auth_Exception_Service($exception->getMessage(), [], 0, $exception); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
return $result->getGraphObject(); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
public function service_user_info() |
68
|
|
|
{ |
69
|
1 |
|
return $this->get_user_node()->asArray(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function service_login_complete() |
73
|
|
|
{ |
74
|
|
|
$helper = $this->api()->getRedirectLoginHelper(); |
75
|
|
|
|
76
|
|
|
try { |
77
|
|
|
$accessToken = $helper->getAccessToken(); |
78
|
|
|
} catch(FacebookResponseException $exception) { |
79
|
|
|
throw new Auth_Exception_Service($exception->getMessage(), [], 0, $exception); |
80
|
|
|
} catch(FacebookSDKException $exception) { |
81
|
|
|
throw new Auth_Exception_Service($exception->getMessage(), [], 0, $exception); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->api()->setDefaultAccessToken($accessToken); |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
public function service_uid() |
88
|
|
|
{ |
89
|
2 |
|
if ($this->api()->getDefaultAccessToken()) { |
90
|
2 |
|
return $this->get_user_node()->getField('id'); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} // End Auth Jam |
95
|
|
|
|