Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

utility.php
Go to the documentation of this file.
1 <?php
17 class ezpOauthUtility extends ezpRestModel
18 {
19 const AUTH_HEADER_NAME = 'Authorization';
20 const AUTH_CGI_HEADER_NAME = 'HTTP_AUTHORIZATION';
21
38 public static function getToken( ezcMvcRequest $request )
39 {
40 // 1. Should first extract required token from the request object
41 // as we know that the request parser does not support this at the
42 // moment we, will skip to the fallback right away. That is to say,
43 // ideally the request parser would make this header available to us,
44 // when available, automatically.
45
46 $token = null;
47 $checkStack = array( 'header', 'get', 'post' );
48
49 foreach ( $checkStack as $step )
50 {
51 switch ( $step )
52 {
53 case 'header':
54 $token = self::getTokenFromAuthorizationHeader();
55 break;
56
57 case 'get':
58 $token = self::getTokenFromQueryComponent( $request );
59 break;
60
61 case 'post':
62 $token = self::getTokenFromHttpBody( $request );
63 break;
64 }
65
66 if ( isset( $token ) ) // Go out of the loop if we find the token during the iteration
67 {
68 break;
69 }
70 }
71
72 return $token;
73 }
74
75
92 protected static function getTokenFromAuthorizationHeader()
93 {
94 $token = null;
95 $authHeader = null;
96 if ( function_exists( 'apache_request_headers' ) )
97 {
98 $apacheHeaders = apache_request_headers();
99 if ( isset( $apacheHeaders[self::AUTH_HEADER_NAME] ) )
100 $authHeader = $apacheHeaders[self::AUTH_HEADER_NAME];
101 }
102 else
103 {
104 if ( isset( $_SERVER[self::AUTH_CGI_HEADER_NAME] ) )
105 $authHeader = $_SERVER[self::AUTH_CGI_HEADER_NAME];
106 }
107
108 if ( isset( $authHeader ) )
109 {
110 $tokenPattern = "/^(?P<authscheme>OAuth)\s(?P<token>[a-zA-Z0-9]+)$/";
111 $match = preg_match( $tokenPattern, $authHeader, $m );
112 if ( $match > 0 )
113 {
114 $token = $m['token'];
115 }
116 }
117
118
119 return $token;
120 }
121
131 protected static function getTokenFromQueryComponent( ezpRestRequest $request )
132 {
133 $token = null;
134 if( isset( $request->get['oauth_token'] ) )
135 {
136 //throw new ezpOauthInvalidRequestException( "OAuth token not found in query component." );
137 $token = $request->get['oauth_token'];
138 }
139
140 return $token;
141 }
142
150 protected static function getTokenFromHttpBody( ezpRestRequest $request )
151 {
152 $token = null;
153 if ( isset( $request->post['oauth_token'] ) )
154 {
155 $token = $request->post['oauth_token'];
156 }
157
158 return $token;
159 }
160
170 public static function doRefreshToken( $clientId, $clientSecret, $refreshToken )
171 {
172 $client = ezpRestClient::fetchByClientId( $clientId );
173 $tokenTTL = (int)eZINI::instance( 'rest.ini' )->variable( 'OAuthSettings', 'TokenTTL' );
174
175 if (! ( $client instanceof ezpRestClient ) )
176 throw new ezpOauthInvalidRequestException( ezpOauthTokenEndpointErrorType::INVALID_CLIENT );
177
178 if ( !$client->validateSecret( $clientSecret ) )
179 throw new ezpOauthInvalidRequestException( ezpOauthTokenEndpointErrorType::INVALID_CLIENT );
180
181 $session = ezcPersistentSessionInstance::get();
182
183 $q = $session->createFindQuery( 'ezpRestToken' );
184 $q->where( $q->expr->eq( 'refresh_token', $q->bindValue( $refreshToken ) ) );
185 $refreshInfo = $session->find( $q, 'ezpRestToken' );
186
187 if ( empty( $refreshInfo) )
188 {
189 throw new ezpOauthInvalidRequestException( "Specified refresh-token does not exist." );
190 }
191
192 $refreshInfo = array_shift( $refreshInfo );
193
194 // Validate client is still authorized, then validate code is not expired
195 $authorized = ezpRestAuthorizedClient::fetchForClientUser( $client, eZUser::fetch( $refreshInfo->user_id ) );
196
197 if ( !($authorized instanceof ezpRestAuthorizedClient ) )
198 {
199 throw new ezpOauthInvalidRequestException( ezpOauthTokenEndpointErrorType::INVALID_CLIENT );
200 }
201
202
203 // Ideally there should be a separate expiry for refresh tokens here, for now allow.
204 $newToken = new ezpRestToken();
205 $newToken->id = ezpRestToken::generateToken( '' );
206 $newToken->refresh_token = ezpRestToken::generateToken( '' );
207 $newToken->client_id = $clientId;
208 $newToken->user_id = $refreshInfo->user_id;
209 $newToken->expirytime = time() + $tokenTTL;
210
211 $session->save( $newToken );
212 $session->delete( $refreshInfo );
213
214 return $newToken;
215 }
216
230 public static function doRefreshTokenWithAuthorizationCode( $clientId, $clientSecret, $authCode, $redirectUri )
231 {
232 $client = ezpRestClient::fetchByClientId( $clientId );
233 $tokenTTL = (int)eZINI::instance( 'rest.ini' )->variable( 'OAuthSettings', 'TokenTTL' );
234
235 if (! ( $client instanceof ezpRestClient ) )
236 throw new ezpOauthInvalidRequestException( ezpOauthTokenEndpointErrorType::INVALID_CLIENT );
237
238 if ( !$client->validateSecret( $clientSecret ) )
239 throw new ezpOauthInvalidRequestException( ezpOauthTokenEndpointErrorType::INVALID_CLIENT );
240
241 if ( !$client->isEndPointValid( $redirectUri ) )
242 throw new ezpOauthInvalidRequestException( ezpOauthTokenEndpointErrorType::INVALID_REQUEST );
243
244 $session = ezcPersistentSessionInstance::get();
245
246 $q = $session->createFindQuery( 'ezpRestAuthcode' );
247 $q->where( $q->expr->eq( 'id', $q->bindValue( $authCode ) ) );
248 $codeInfo = $session->find( $q, 'ezpRestAuthcode' );
249
250 if ( empty( $codeInfo ) )
251 {
252 throw new ezpOauthInvalidTokenException( "Specified authorization code does not exist." );
253 }
254 $codeInfo = array_shift( $codeInfo );
255
256 // Validate client is still authorized, then validate code is not expired
257 $authorized = ezpRestAuthorizedClient::fetchForClientUser( $client, eZUser::fetch( $codeInfo->user_id ) );
258
259 if ( !($authorized instanceof ezpRestAuthorizedClient ) )
260 {
261 throw new ezpOauthInvalidRequestException( ezpOauthTokenEndpointErrorType::INVALID_CLIENT );
262 }
263
264 // Check expiry of authorization_code
265 if ( $codeInfo->expirytime != 0 )
266 {
267 if ( $codeInfo->expirytime < time() )
268 {
269 $d = date( "c", $codeInfo->expirytime );
270 throw new ezpOauthExpiredTokenException( "Authorization code expired on {$d}" );
271 }
272 }
273
274 // code ok, create access and refresh tokens
275 $accessToken = ezpRestToken::generateToken( '' );
276 $refreshToken = ezpRestToken::generateToken( '' );
277
278 $token = new ezpRestToken();
279 $token->id = $accessToken;
280 $token->refresh_token = $refreshToken;
281 $token->client_id = $clientId;
282 $token->user_id = $codeInfo->user_id;
283 $token->expirytime = time() + $tokenTTL;
284
285 $session = ezcPersistentSessionInstance::get();
286 $session->save( $token );
287
288 // After an auth code is used, we'll remove it so it is not abused
289 $session->delete( $codeInfo );
290
291 return $token;
292 }
293 }
294 ?>
$session
$session
Definition: admin/articlelist.php:44
$tokenTTL
if(! $application->isAuthorizedByUser( $pScope, eZUser::currentUser())) $tokenTTL
Definition: authorize.php:137
ezpOauthExpiredTokenException
Definition: expired_token.php:19
ezpOauthInvalidRequestException
Definition: invalid_request.php:22
ezpOauthInvalidTokenException
Definition: invalid_token.php:20
ezpOauthTokenEndpointErrorType\INVALID_CLIENT
const INVALID_CLIENT
Definition: oauth_token_endpoint_error.php:15
ezpOauthTokenEndpointErrorType\INVALID_REQUEST
const INVALID_REQUEST
Definition: oauth_token_endpoint_error.php:14
ezpOauthUtility
Definition: utility.php:18
ezpOauthUtility\AUTH_CGI_HEADER_NAME
const AUTH_CGI_HEADER_NAME
Definition: utility.php:20
ezpOauthUtility\getToken
static getToken(ezcMvcRequest $request)
Definition: utility.php:38
ezpOauthUtility\getTokenFromHttpBody
static getTokenFromHttpBody(ezpRestRequest $request)
Definition: utility.php:150
ezpOauthUtility\doRefreshToken
static doRefreshToken( $clientId, $clientSecret, $refreshToken)
Definition: utility.php:170
ezpOauthUtility\AUTH_HEADER_NAME
const AUTH_HEADER_NAME
Definition: utility.php:19
ezpOauthUtility\getTokenFromAuthorizationHeader
static getTokenFromAuthorizationHeader()
Definition: utility.php:92
ezpOauthUtility\doRefreshTokenWithAuthorizationCode
static doRefreshTokenWithAuthorizationCode( $clientId, $clientSecret, $authCode, $redirectUri)
Definition: utility.php:230
ezpOauthUtility\getTokenFromQueryComponent
static getTokenFromQueryComponent(ezpRestRequest $request)
Definition: utility.php:131
ezpRestAuthorizedClient
Definition: restauthorizedclient.php:17
ezpRestAuthorizedClient\fetchForClientUser
static fetchForClientUser(ezpRestClient $client, eZUser $user)
Definition: restauthorizedclient.php:61
ezpRestClient
Definition: restclient.php:16
ezpRestClient\fetchByClientId
static fetchByClientId( $clientId)
Definition: restclient.php:195
ezpRestModel
Definition: rest_model.php:16
ezpRestRequest
Definition: rest_request.php:21
ezpRestToken
Definition: ezprest_token.php:17
ezpRestToken\generateToken
static generateToken( $vary)
Definition: ezprest_token.php:93
$q
$q
Definition: modules/oauthadmin/list.php:17
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.