소스 검색

Avances importador FTTH

Guillermo Espinoza 7 년 전
부모
커밋
cd0612e50f
1개의 변경된 파일41개의 추가작업 그리고 31개의 파일을 삭제
  1. 41 31
      tools/src/Command/ImportONUCommand.php

+ 41 - 31
tools/src/Command/ImportONUCommand.php

@@ -10,6 +10,7 @@ use Symfony\Component\Console\Output\OutputInterface;
 use Symfony\Component\HttpFoundation\Session\Session;
 use Symfony\Component\BrowserKit\Cookie;
 use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
+use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUser;
 use GuzzleHttp\Client as GuzzClient;
 use GuzzleHttp\Exception\ServerException as RequestException;
 use GuzzleHttp\Psr7;
@@ -53,35 +54,19 @@ class ImportONUCommand extends Command
         
         $client_id = $input->getOption('client_id');
         $client_secret = $input->getOption('client_secret');
-        
+                
         $token = $this->getAccessToken($username, $password, $client_id, $client_secret);
-
+        
         $url = $base_url . $uri;
-
-        $parser = new \JsonCollectionParser\Parser();
         $client = new GuzzClient();
-        $parser->parse($file, function($data) use ($client, $url, $token) {
+        $parser = new \JsonCollectionParser\Parser();
+        $cookieJar = $this->getCookieJar($username, $token);
+        
+        $parser->parse($file, function($data) use ($client, $url, $cookieJar, $token) {
             try {
-                
-
-//                $cookie = $this->logIn($user, $token['access_token'], $token['refresh_token'], $token['expires_in']);
-//                $cookieJar = CookieJar::fromArray([
-//                    $cookie->getName() => $cookie->getValue()
-//                ], 'ftth.fd3.flowdat.com');
-//                
-                
-//                $res = $client->request("POST", 'base.fd3.flowdat.com/oauth/v2/auth/login_check', [
-//                    'auth' => [$user, $pass],
-//                'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
-//                    'body' => http_build_query([
-//                        '_username'=>$user,
-//                        '_password'=>$pass,
-//                        '_csrf_token'=>'XqptzqyIjxEvmrp8kPb6-1AuTNmxYwH5qfhVR6QNiKg'
-//                            ])
-//                    ]);
-//                    
                 $res = $client->request("POST", $url, [
-//                    'cookies' => $cookieJar,
+                    'debug' => true,
+                    'cookies' => $cookieJar,
                     'headers' => ['Authorization' => "Bearer {$token['access_token']}"],
                     'body' => json_encode($data)
                     ]);
@@ -96,6 +81,17 @@ class ImportONUCommand extends Command
         );
     }
     
+    /**
+     * Retorna un token OAuth
+     * 
+     * @param string $username
+     * @param string $password
+     * @param string $client_id
+     * @param string $client_secret
+     * @param string $url
+     * 
+     * @return array
+     */
     private function getAccessToken($username, $password, $client_id, $client_secret, $url = 'http://base.fd3.flowdat.com/oauth/v2/token')
     {
         $body = ['grant_type' => 'password',
@@ -113,7 +109,16 @@ class ImportONUCommand extends Command
         return json_decode($res->getBody(), true);
     }
     
-    private function logIn($username, $access_token, $refresh_token, $expires_in)
+    /**
+     * Guardo en session al OAuthUser
+     * Retorno una cookie con la info de session
+     * 
+     * @param string $username
+     * @param array $token
+     * 
+     * @return CookieJar
+     */
+    private function getCookieJar($username, $token)
     {
         $session = new Session();
         $session->setName('ftth_session');
@@ -122,14 +127,19 @@ class ImportONUCommand extends Command
         // the firewall context (defaults to the firewall name)
         $firewall = 'secured_area';
 
-        $token = new OAuthToken($access_token, array('ROLE_USER'));
-        $token->setAccessToken($access_token);
-        $token->setRefreshToken($refresh_token);
-        $token->setExpiresIn($expires_in);
-        $session->set('_security_'.$firewall, serialize($token));
+        $OAuthToken = new OAuthToken($token['access_token'], array('ROLE_ADMIN', 'ROLE_USER'));
+        $OAuthToken->setRawToken($token);
+        $user = new OAuthUser($username);
+        $OAuthToken->setUser($user);
+        
+        $session->set('_security_'.$firewall, $OAuthToken->serialize());
         $session->save();
 
-        return new Cookie($session->getName(), $session->getId());
+        $cookie = new Cookie($session->getName(), $session->getId());
+        
+        return CookieJar::fromArray([
+                    $cookie->getName() => $cookie->getValue(),
+                ], 'ftth.fd3.flowdat.com'); 
     }
 
 }