source: extensions/oAuth/include/hybridauth/Hybrid/Provider_Model.php @ 20293

Last change on this file since 20293 was 20293, checked in by mistic100, 11 years ago

first commit of oAuth plugin, still in developpement

File size: 6.7 KB
Line 
1<?php
2/*!
3* HybridAuth
4* http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth
5* (c) 2009-2012, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html
6*/
7
8/**
9 * Hybrid_Provider_Model provide a common interface for supported IDps on HybridAuth.
10 *
11 * Basically, each provider adapter has to define at least 4 methods:
12 *   Hybrid_Providers_{provider_name}::initialize()
13 *   Hybrid_Providers_{provider_name}::loginBegin()
14 *   Hybrid_Providers_{provider_name}::loginFinish()
15 *   Hybrid_Providers_{provider_name}::getUserProfile()
16 *
17 * HybridAuth also come with three others models
18 *   Class Hybrid_Provider_Model_OpenID for providers that uses the OpenID 1 and 2 protocol.
19 *   Class Hybrid_Provider_Model_OAuth1 for providers that uses the OAuth 1 protocol.
20 *   Class Hybrid_Provider_Model_OAuth2 for providers that uses the OAuth 2 protocol.
21 */
22abstract class Hybrid_Provider_Model
23{
24        /* IDp ID (or unique name) */
25        public $providerId = NULL;
26
27        /* specific provider adapter config */
28        public $config     = NULL;
29
30        /* provider extra parameters */
31        public $params     = NULL;
32
33        /* Endpoint URL for that provider */
34        public $endpoint   = NULL; 
35
36        /* Hybrid_User obj, represents the current loggedin user */
37        public $user       = NULL;
38
39        /* the provider api client (optional) */
40        public $api        = NULL; 
41
42        /**
43        * common providers adapter constructor
44        */
45        function __construct( $providerId, $config, $params = NULL )
46        {
47                # init the IDp adapter parameters, get them from the cache if possible
48                if( ! $params ){
49                        $this->params = Hybrid_Auth::storage()->get( "hauth_session.$providerId.id_provider_params" );
50                }
51                else{
52                        $this->params = $params;
53                }
54
55                // idp id
56                $this->providerId = $providerId;
57
58                // set HybridAuth endpoint for this provider
59                $this->endpoint = Hybrid_Auth::storage()->get( "hauth_session.$providerId.hauth_endpoint" );
60
61                // idp config
62                $this->config = $config;
63
64                // new user instance
65                $this->user = new Hybrid_User();
66                $this->user->providerId = $providerId;
67
68                // initialize the current provider adapter
69                $this->initialize(); 
70
71                Hybrid_Logger::debug( "Hybrid_Provider_Model::__construct( $providerId ) initialized. dump current adapter instance: ", serialize( $this ) );
72        }
73
74        // --------------------------------------------------------------------
75
76        /**
77        * IDp wrappers initializer
78        *
79        * The main job of wrappers initializer is to performs (depend on the IDp api client it self):
80        *     - include some libs nedded by this provider,
81        *     - check IDp key and secret,
82        *     - set some needed parameters (stored in $this->params) by this IDp api client
83        *     - create and setup an instance of the IDp api client on $this->api
84        */
85        abstract protected function initialize(); 
86
87        // --------------------------------------------------------------------
88
89        /**
90        * begin login
91        */
92        abstract protected function loginBegin();
93
94        // --------------------------------------------------------------------
95
96        /**
97        * finish login
98        */
99        abstract protected function loginFinish();
100
101        // --------------------------------------------------------------------
102
103        /**
104        * generic logout, just erase current provider adapter stored data to let Hybrid_Auth all forget about it
105        */
106        function logout()
107        {
108                Hybrid_Logger::info( "Enter [{$this->providerId}]::logout()" );
109
110                $this->clearTokens();
111
112                return TRUE;
113        }
114
115        // --------------------------------------------------------------------
116
117        /**
118        * grab the user profile from the IDp api client
119        */
120        function getUserProfile()
121        {
122                Hybrid_Logger::error( "HybridAuth do not provide users contats list for {$this->providerId} yet." ); 
123               
124                throw new Exception( "Provider does not support this feature.", 8 ); 
125        }
126
127        // --------------------------------------------------------------------
128
129        /**
130        * load the current logged in user contacts list from the IDp api client 
131        */
132        function getUserContacts() 
133        {
134                Hybrid_Logger::error( "HybridAuth do not provide users contats list for {$this->providerId} yet." ); 
135               
136                throw new Exception( "Provider does not support this feature.", 8 ); 
137        }
138
139        // --------------------------------------------------------------------
140
141        /**
142        * return the user activity stream 
143        */
144        function getUserActivity( $stream ) 
145        {
146                Hybrid_Logger::error( "HybridAuth do not provide user's activity stream for {$this->providerId} yet." ); 
147               
148                throw new Exception( "Provider does not support this feature.", 8 ); 
149        }
150
151        // --------------------------------------------------------------------
152
153        /**
154        * return the user activity stream 
155        */ 
156        function setUserStatus( $status )
157        {
158                Hybrid_Logger::error( "HybridAuth do not provide user's activity stream for {$this->providerId} yet." ); 
159               
160                throw new Exception( "Provider does not support this feature.", 8 ); 
161        }
162
163        // --------------------------------------------------------------------
164
165        /**
166        * return true if the user is connected to the current provider
167        */ 
168        public function isUserConnected()
169        {
170                return (bool) Hybrid_Auth::storage()->get( "hauth_session.{$this->providerId}.is_logged_in" );
171        }
172
173        // --------------------------------------------------------------------
174
175        /**
176        * set user to connected
177        */ 
178        public function setUserConnected()
179        {
180                Hybrid_Logger::info( "Enter [{$this->providerId}]::setUserConnected()" );
181               
182                Hybrid_Auth::storage()->set( "hauth_session.{$this->providerId}.is_logged_in", 1 );
183        }
184
185        // --------------------------------------------------------------------
186
187        /**
188        * set user to unconnected
189        */ 
190        public function setUserUnconnected()
191        {
192                Hybrid_Logger::info( "Enter [{$this->providerId}]::setUserUnconnected()" );
193               
194                Hybrid_Auth::storage()->set( "hauth_session.{$this->providerId}.is_logged_in", 0 ); 
195        }
196
197        // --------------------------------------------------------------------
198
199        /**
200        * get or set a token
201        */ 
202        public function token( $token, $value = NULL )
203        {
204                if( $value === NULL ){
205                        return Hybrid_Auth::storage()->get( "hauth_session.{$this->providerId}.token.$token" );
206                }
207                else{
208                        Hybrid_Auth::storage()->set( "hauth_session.{$this->providerId}.token.$token", $value );
209                }
210        }
211
212        // --------------------------------------------------------------------
213
214        /**
215        * delete a stored token
216        */ 
217        public function deleteToken( $token )
218        {
219                Hybrid_Auth::storage()->delete( "hauth_session.{$this->providerId}.token.$token" );
220        }
221
222        // --------------------------------------------------------------------
223
224        /**
225        * clear all existen tokens for this provider
226        */ 
227        public function clearTokens()
228        { 
229                Hybrid_Auth::storage()->deleteMatch( "hauth_session.{$this->providerId}." );
230        }
231}
Note: See TracBrowser for help on using the repository browser.