예제 #1
0
파일: Auth.php 프로젝트: akmetainfo/vkApi
 /**
  * Авторизация в соц сети и получение secure_token
  * позволяет миновать открытие браузера
  *
  * @param $client_id
  * @param array $scope
  * @throws AuthFailed
  */
 private function authorize($client_id, array $scope)
 {
     $response_form = $this->login_form($client_id, $scope);
     $dom = \nokogiri::fromHtml($response_form->body);
     $origin = $dom->get('input[name="_origin"]')->toArray();
     $ip_h = $dom->get('input[name="ip_h"]')->toArray();
     $lg_h = $dom->get('input[name="lg_h"]')->toArray();
     $to = $dom->get('input[name="to"]')->toArray();
     $this->session->headers['Referer'] = $response_form->url;
     $this->session->headers['Origin'] = 'https://oauth.vk.com';
     if (!$ip_h || !$lg_h || !$to || !$origin) {
         throw new AuthFailed('Wrong client id or scope');
     }
     $auth_response = $this->auth_post_request($ip_h[0]['value'], $lg_h[0]['value'], $to[0]['value'], $origin[0]['value']);
     // 405 status === OK
     if (!$auth_response->success) {
         $url = $auth_response->url;
     } else {
         $this->session->headers['Referer'] = $auth_response->url;
         $confirm_form = \nokogiri::fromHtml($auth_response->body);
         $confirm_form_data = $confirm_form->get('form[method="post"]')->toArray();
         $confirm_response = $this->post_confirm($confirm_form_data[0]['action']);
         $url = $confirm_response->url;
     }
     if (preg_match('|access_token=([^&]+)&expires_in=([^&]+)&user_id=([\\d]+)|ius', $url, $m)) {
         $this->token = array('token' => $m[1], 'expires' => $m[2] ? $m[2] + time() : 0, 'uid' => $m[3]);
         if ($this->store_path) {
             file_put_contents($this->store_path . '/token-' . md5($client_id . serialize($scope)) . '.json', json_encode($this->token));
         }
     } else {
         throw new AuthFailed('Wrong auth params!');
     }
 }