123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace FD3\Command;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Input\InputArgument;
- use Symfony\Component\Console\Input\InputOption;
- 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;
- use GuzzleHttp\Cookie\CookieJar;
- class ImportONUCommand extends Command
- {
- protected function configure()
- {
- $this
- ->setName('import:ftth:onu')
- ->setDescription('Import ONU entities from json file')
- ->setHelp('ONU.json shuld be a valid json file, probably looking like the following example:
- [
- {"clientId":1,"ponSerialNumber":"1","transitionState":"success","tenancyId":3},
- {"clientId":1,"ponSerialNumber":"2","transitionState":"success","tenancyId":3},
- {"clientId":1,"ponSerialNumber":"1","transitionState":"success","tenancyId":3}
- ]
- ')
- ->addArgument('base_url', InputArgument::REQUIRED, 'The base url of the FD3 Client.')
- ->addArgument('data_file.json', InputArgument::OPTIONAL, 'The data source from where to get data.', "ONU.json")
- //->addOption('oauth_token', null, InputOption::VALUE_REQUIRED, 'The FD3 OAuth Client ID.', 'http://base.galvez.flowdat.com/oauth/v2/token')
- //->addOption('client_id', null, InputOption::VALUE_REQUIRED, 'The FD3 OAuth Client ID.', '2_2giq1sosvyxwsgwswcw4s44w0o0go0wc0oo4ck4kgo008w80k0')
- //->addOption('client_secret', null, InputOption::VALUE_REQUIRED, 'The FD3 OAuth Client Secret.', 'kuu06cynskgkc0swokkkg4gg8k84w0ks00cw00wk4ow480okg')
- ->addOption('uri', null, InputOption::VALUE_REQUIRED, 'uri after the base_url for the complete URL', '/api/onus.json')
- ->addOption('user', null, InputOption::VALUE_REQUIRED, 'The FD3 Username.', 'admin')
- ->addOption('pass', null, InputOption::VALUE_REQUIRED, 'The FD3 Password.', 'adminpass')
- ;
- }
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $base_url = $input->getArgument("base_url");
- $uri = $input->getOption("uri");
- $username = $input->getOption('user');
- $password = $input->getOption('pass');
- $file = $input->getArgument("data_file.json");
-
- $url = $base_url . $uri;
- $client = new GuzzClient();
- $parser = new \JsonCollectionParser\Parser();
- //$cookieJar = $this->getCookieJar($username, $token);
-
- $parser->parse($file, function($data) use ($client, $url, $username, $password) {
- try {
- $res = $client->request("POST", $url.'?filters[disableTenancy]', [
- // 'headers' => ['Authorization' => "Bearer {$token['access_token']}"]
- 'auth'=> [$username, $password],
- 'body' => json_encode($data)
- ]);
- echo $res->getBody();
- } catch (RequestException $e) {
- echo Psr7\str($e->getRequest());
- if ($e->hasResponse()) {
- echo Psr7\str($e->getResponse());
- }
- }
- }
- );
- }
-
- /**
- * 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 getRefreshToken($token, $client_id, $client_secret, $url)
- {
- $body = ['grant_type' => 'refresh_token',
- 'refresh_token' => $token['refresh_token']
- ];
- $client = new GuzzClient();
- $res = $client->request("POST", $url, [
- 'auth' => [$client_id, $client_secret],
- 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
- 'body' => http_build_query($body),
- ]);
- $body = $res->getBody();
- return json_decode($body, true);
- }
-
- private function getAccessToken($username, $password, $client_id, $client_secret, $url = 'http://base.fd3.flowdat.com/oauth/v2/token')
- {
- $body = ['grant_type' => 'password',
- 'client_id' => $client_id,
- 'client_secret' => $client_secret,
- 'username' => $username,
- 'password' => $password];
-
- $client = new GuzzClient();
- $res = $client->request("POST", $url, [
- 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
- 'body' => http_build_query($body),
- ]);
-
- return json_decode($res->getBody(), true);
- }
-
- /**
- * 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');
- $session->start();
- // the firewall context (defaults to the firewall name)
- $firewall = 'secured_area';
- $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();
- $cookie = new Cookie($session->getName(), $session->getId());
-
- return CookieJar::fromArray([
- $cookie->getName() => $cookie->getValue(),
- ], 'flowdat.com');
- }
- }
|