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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
<?php
/*!
* HybridAuth
* http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth
* (c) 2009-2012, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html
*/
/**
* Hybrid_Provider_Model provide a common interface for supported IDps on HybridAuth.
*
* Basically, each provider adapter has to define at least 4 methods:
* Hybrid_Providers_{provider_name}::initialize()
* Hybrid_Providers_{provider_name}::loginBegin()
* Hybrid_Providers_{provider_name}::loginFinish()
* Hybrid_Providers_{provider_name}::getUserProfile()
*
* HybridAuth also come with three others models
* Class Hybrid_Provider_Model_OpenID for providers that uses the OpenID 1 and 2 protocol.
* Class Hybrid_Provider_Model_OAuth1 for providers that uses the OAuth 1 protocol.
* Class Hybrid_Provider_Model_OAuth2 for providers that uses the OAuth 2 protocol.
*/
abstract class Hybrid_Provider_Model
{
/* IDp ID (or unique name) */
public $providerId = NULL;
/* specific provider adapter config */
public $config = NULL;
/* provider extra parameters */
public $params = NULL;
/* Endpoint URL for that provider */
public $endpoint = NULL;
/* Hybrid_User obj, represents the current loggedin user */
public $user = NULL;
/* the provider api client (optional) */
public $api = NULL;
/**
* common providers adapter constructor
*/
function __construct( $providerId, $config, $params = NULL )
{
# init the IDp adapter parameters, get them from the cache if possible
if( ! $params ){
$this->params = Hybrid_Auth::storage()->get( "hauth_session.$providerId.id_provider_params" );
}
else{
$this->params = $params;
}
// idp id
$this->providerId = $providerId;
// set HybridAuth endpoint for this provider
$this->endpoint = Hybrid_Auth::storage()->get( "hauth_session.$providerId.hauth_endpoint" );
// idp config
$this->config = $config;
// new user instance
$this->user = new Hybrid_User();
$this->user->providerId = $providerId;
// initialize the current provider adapter
$this->initialize();
Hybrid_Logger::debug( "Hybrid_Provider_Model::__construct( $providerId ) initialized. dump current adapter instance: ", serialize( $this ) );
}
// --------------------------------------------------------------------
/**
* IDp wrappers initializer
*
* The main job of wrappers initializer is to performs (depend on the IDp api client it self):
* - include some libs nedded by this provider,
* - check IDp key and secret,
* - set some needed parameters (stored in $this->params) by this IDp api client
* - create and setup an instance of the IDp api client on $this->api
*/
abstract protected function initialize();
// --------------------------------------------------------------------
/**
* begin login
*/
abstract protected function loginBegin();
// --------------------------------------------------------------------
/**
* finish login
*/
abstract protected function loginFinish();
// --------------------------------------------------------------------
/**
* generic logout, just erase current provider adapter stored data to let Hybrid_Auth all forget about it
*/
function logout()
{
Hybrid_Logger::info( "Enter [{$this->providerId}]::logout()" );
$this->clearTokens();
return TRUE;
}
// --------------------------------------------------------------------
/**
* grab the user profile from the IDp api client
*/
function getUserProfile()
{
Hybrid_Logger::error( "HybridAuth do not provide users contats list for {$this->providerId} yet." );
throw new Exception( "Provider does not support this feature.", 8 );
}
// --------------------------------------------------------------------
/**
* load the current logged in user contacts list from the IDp api client
*/
function getUserContacts()
{
Hybrid_Logger::error( "HybridAuth do not provide users contats list for {$this->providerId} yet." );
throw new Exception( "Provider does not support this feature.", 8 );
}
// --------------------------------------------------------------------
/**
* return the user activity stream
*/
function getUserActivity( $stream )
{
Hybrid_Logger::error( "HybridAuth do not provide user's activity stream for {$this->providerId} yet." );
throw new Exception( "Provider does not support this feature.", 8 );
}
// --------------------------------------------------------------------
/**
* return the user activity stream
*/
function setUserStatus( $status )
{
Hybrid_Logger::error( "HybridAuth do not provide user's activity stream for {$this->providerId} yet." );
throw new Exception( "Provider does not support this feature.", 8 );
}
// --------------------------------------------------------------------
/**
* return true if the user is connected to the current provider
*/
public function isUserConnected()
{
return (bool) Hybrid_Auth::storage()->get( "hauth_session.{$this->providerId}.is_logged_in" );
}
// --------------------------------------------------------------------
/**
* set user to connected
*/
public function setUserConnected()
{
Hybrid_Logger::info( "Enter [{$this->providerId}]::setUserConnected()" );
Hybrid_Auth::storage()->set( "hauth_session.{$this->providerId}.is_logged_in", 1 );
}
// --------------------------------------------------------------------
/**
* set user to unconnected
*/
public function setUserUnconnected()
{
Hybrid_Logger::info( "Enter [{$this->providerId}]::setUserUnconnected()" );
Hybrid_Auth::storage()->set( "hauth_session.{$this->providerId}.is_logged_in", 0 );
}
// --------------------------------------------------------------------
/**
* get or set a token
*/
public function token( $token, $value = NULL )
{
if( $value === NULL ){
return Hybrid_Auth::storage()->get( "hauth_session.{$this->providerId}.token.$token" );
}
else{
Hybrid_Auth::storage()->set( "hauth_session.{$this->providerId}.token.$token", $value );
}
}
// --------------------------------------------------------------------
/**
* delete a stored token
*/
public function deleteToken( $token )
{
Hybrid_Auth::storage()->delete( "hauth_session.{$this->providerId}.token.$token" );
}
// --------------------------------------------------------------------
/**
* clear all existen tokens for this provider
*/
public function clearTokens()
{
Hybrid_Auth::storage()->deleteMatch( "hauth_session.{$this->providerId}." );
}
}
|