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 | * To implement an OAuth 1 based service provider, Hybrid_Provider_Model_OAuth1 |
---|
10 | * can be used to save the hassle of the authentication flow. |
---|
11 | * |
---|
12 | * Each class that inherit from Hybrid_Provider_Model_OAuth1 have to implemenent |
---|
13 | * at least 2 methods: |
---|
14 | * Hybrid_Providers_{provider_name}::initialize() to setup the provider api end-points urls |
---|
15 | * Hybrid_Providers_{provider_name}::getUserProfile() to grab the user profile |
---|
16 | * |
---|
17 | * Hybrid_Provider_Model_OAuth1 use OAuth1Client v0.1 which can be found on |
---|
18 | * Hybrid/thirdparty/OAuth/OAuth1Client.php |
---|
19 | */ |
---|
20 | class Hybrid_Provider_Model_OAuth1 extends Hybrid_Provider_Model |
---|
21 | { |
---|
22 | public $request_tokens_raw = null; // request_tokens as recived from provider |
---|
23 | public $access_tokens_raw = null; // access_tokens as recived from provider |
---|
24 | |
---|
25 | /** |
---|
26 | * try to get the error message from provider api |
---|
27 | */ |
---|
28 | function errorMessageByStatus( $code = null ) { |
---|
29 | $http_status_codes = ARRAY( |
---|
30 | 200 => "OK: Success!", |
---|
31 | 304 => "Not Modified: There was no new data to return.", |
---|
32 | 400 => "Bad Request: The request was invalid.", |
---|
33 | 401 => "Unauthorized.", |
---|
34 | 403 => "Forbidden: The request is understood, but it has been refused.", |
---|
35 | 404 => "Not Found: The URI requested is invalid or the resource requested does not exists.", |
---|
36 | 406 => "Not Acceptable.", |
---|
37 | 500 => "Internal Server Error: Something is broken.", |
---|
38 | 502 => "Bad Gateway.", |
---|
39 | 503 => "Service Unavailable." |
---|
40 | ); |
---|
41 | |
---|
42 | if( ! $code && $this->api ) |
---|
43 | $code = $this->api->http_code; |
---|
44 | |
---|
45 | if( isset( $http_status_codes[ $code ] ) ) |
---|
46 | return $code . " " . $http_status_codes[ $code ]; |
---|
47 | } |
---|
48 | |
---|
49 | // -------------------------------------------------------------------- |
---|
50 | |
---|
51 | /** |
---|
52 | * adapter initializer |
---|
53 | */ |
---|
54 | function initialize() |
---|
55 | { |
---|
56 | // 1 - check application credentials |
---|
57 | if ( ! $this->config["keys"]["key"] || ! $this->config["keys"]["secret"] ){ |
---|
58 | throw new Exception( "Your application key and secret are required in order to connect to {$this->providerId}.", 4 ); |
---|
59 | } |
---|
60 | |
---|
61 | // 2 - include OAuth lib and client |
---|
62 | require_once Hybrid_Auth::$config["path_libraries"] . "OAuth/OAuth.php"; |
---|
63 | require_once Hybrid_Auth::$config["path_libraries"] . "OAuth/OAuth1Client.php"; |
---|
64 | |
---|
65 | // 3.1 - setup access_token if any stored |
---|
66 | if( $this->token( "access_token" ) ){ |
---|
67 | $this->api = new OAuth1Client( |
---|
68 | $this->config["keys"]["key"], $this->config["keys"]["secret"], |
---|
69 | $this->token( "access_token" ), $this->token( "access_token_secret" ) |
---|
70 | ); |
---|
71 | } |
---|
72 | |
---|
73 | // 3.2 - setup request_token if any stored, in order to exchange with an access token |
---|
74 | elseif( $this->token( "request_token" ) ){ |
---|
75 | $this->api = new OAuth1Client( |
---|
76 | $this->config["keys"]["key"], $this->config["keys"]["secret"], |
---|
77 | $this->token( "request_token" ), $this->token( "request_token_secret" ) |
---|
78 | ); |
---|
79 | } |
---|
80 | |
---|
81 | // 3.3 - instanciate OAuth client with client credentials |
---|
82 | else{ |
---|
83 | $this->api = new OAuth1Client( $this->config["keys"]["key"], $this->config["keys"]["secret"] ); |
---|
84 | } |
---|
85 | |
---|
86 | // Set curl proxy if exist |
---|
87 | if( isset( Hybrid_Auth::$config["proxy"] ) ){ |
---|
88 | $this->api->curl_proxy = Hybrid_Auth::$config["proxy"]; |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | // -------------------------------------------------------------------- |
---|
93 | |
---|
94 | /** |
---|
95 | * begin login step |
---|
96 | */ |
---|
97 | function loginBegin() |
---|
98 | { |
---|
99 | $tokens = $this->api->requestToken( $this->endpoint ); |
---|
100 | |
---|
101 | // request tokens as recived from provider |
---|
102 | $this->request_tokens_raw = $tokens; |
---|
103 | |
---|
104 | // check the last HTTP status code returned |
---|
105 | if ( $this->api->http_code != 200 ){ |
---|
106 | throw new Exception( "Authentication failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ), 5 ); |
---|
107 | } |
---|
108 | |
---|
109 | if ( ! isset( $tokens["oauth_token"] ) ){ |
---|
110 | throw new Exception( "Authentication failed! {$this->providerId} returned an invalid oauth token.", 5 ); |
---|
111 | } |
---|
112 | |
---|
113 | $this->token( "request_token" , $tokens["oauth_token"] ); |
---|
114 | $this->token( "request_token_secret", $tokens["oauth_token_secret"] ); |
---|
115 | |
---|
116 | # redirect the user to the provider authentication url |
---|
117 | Hybrid_Auth::redirect( $this->api->authorizeUrl( $tokens ) ); |
---|
118 | } |
---|
119 | |
---|
120 | // -------------------------------------------------------------------- |
---|
121 | |
---|
122 | /** |
---|
123 | * finish login step |
---|
124 | */ |
---|
125 | function loginFinish() |
---|
126 | { |
---|
127 | $oauth_token = (array_key_exists('oauth_token',$_REQUEST))?$_REQUEST['oauth_token']:""; |
---|
128 | $oauth_verifier = (array_key_exists('oauth_verifier',$_REQUEST))?$_REQUEST['oauth_verifier']:""; |
---|
129 | |
---|
130 | if ( ! $oauth_token || ! $oauth_verifier ){ |
---|
131 | throw new Exception( "Authentication failed! {$this->providerId} returned an invalid oauth verifier.", 5 ); |
---|
132 | } |
---|
133 | |
---|
134 | // request an access token |
---|
135 | $tokens = $this->api->accessToken( $oauth_verifier ); |
---|
136 | |
---|
137 | // access tokens as recived from provider |
---|
138 | $this->access_tokens_raw = $tokens; |
---|
139 | |
---|
140 | // check the last HTTP status code returned |
---|
141 | if ( $this->api->http_code != 200 ){ |
---|
142 | throw new Exception( "Authentication failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ), 5 ); |
---|
143 | } |
---|
144 | |
---|
145 | // we should have an access_token, or else, something has gone wrong |
---|
146 | if ( ! isset( $tokens["oauth_token"] ) ){ |
---|
147 | throw new Exception( "Authentication failed! {$this->providerId} returned an invalid access token.", 5 ); |
---|
148 | } |
---|
149 | |
---|
150 | // we no more need to store requet tokens |
---|
151 | $this->deleteToken( "request_token" ); |
---|
152 | $this->deleteToken( "request_token_secret" ); |
---|
153 | |
---|
154 | // sotre access_token for later user |
---|
155 | $this->token( "access_token" , $tokens['oauth_token'] ); |
---|
156 | $this->token( "access_token_secret" , $tokens['oauth_token_secret'] ); |
---|
157 | |
---|
158 | // set user as logged in to the current provider |
---|
159 | $this->setUserConnected(); |
---|
160 | } |
---|
161 | } |
---|