Example #1
0
 /**
  * Loads the number of allowed requests and the corresponding timestamp from a persistent storage.
  * @param \yii\web\Request $request the current request
  * @param \yii\base\Action $action the action to be executed
  * @return array an array of two elements. The first element is the number of allowed requests,
  * and the second element is the corresponding UNIX timestamp.
  */
 public function loadAllowance($request, $action)
 {
     AccessToken::checkAccessToken($request->post('access_token'));
     Client::checkClientId($request->post('client_id'));
     $access_token = \common\models\OauthAccessToken::findOne(['client_id' => $request->post('client_id'), 'access_token' => $request->post('access_token')]);
     if (!$access_token) {
         return [0, time()];
     }
     $endpoint = $action->controller->route;
     $api_ratelimiter = ApiRatelimiter::findOne(['client_id' => $request->post('client_id'), 'api_endpoint' => $endpoint, 'user_uuid' => $access_token->user_uuid]);
     if (!$api_ratelimiter) {
         $api_ratelimiter = new ApiRatelimiter(['client_id' => $request->post('client_id'), 'api_endpoint' => $endpoint, 'user_uuid' => $access_token->user_uuid, 'allowed_remaining' => $this->getRateLimit($request, $action)[0], 'last_timestamp' => time()]);
     }
     return [$api_ratelimiter->allowed_remaining, $api_ratelimiter->last_timestamp];
 }
Example #2
0
 public static function checkAccessByClientIdAndAccessToken($client_id, $access_token)
 {
     Client::checkClientId($client_id);
     AccessToken::checkAccessToken($access_token);
 }