Compare commits

...

11 Commits

Author SHA1 Message Date
a-sansara
a8f0981377 composer.json dev 2017-03-21 03:45:09 +01:00
a-sansara
7d9da4ac73 version 1.3.3 - manage responseHeader 2017-03-21 03:43:25 +01:00
a-sansara
94f5dcd4e5 fix invalid server response 2017-03-21 03:41:54 +01:00
a-sansara
fb15b5652a amend response Header 2017-03-21 02:54:36 +01:00
a-sansara
20399c4ae7 manage headerResponse 2017-03-21 01:20:29 +01:00
a-sansara
8cecfa057f Merge branch 'master' of https://github.com/meta-tech/pws-client 2017-03-20 17:18:39 +01:00
a-sansara
a614f24061 fix style displaying in html mode 2017-03-20 17:17:57 +01:00
a-sansara
3e9f077187 add ssl options on config in README 2017-03-16 02:24:37 +01:00
a-sansara
44787b0f7d typo in README 2017-03-16 02:23:01 +01:00
a-sansara
e90ec32c2d remove yaml dependency 2017-03-15 23:45:15 +01:00
a-sansara
7f6fdfaf36 add verifypeer & verifyhost config options 2017-03-15 10:28:52 +01:00
5 changed files with 63 additions and 32 deletions

View File

@ -18,7 +18,7 @@ Or add the package to your `composer.json`.
``` ```
"require": { "require": {
"meta-tech/pws-client" : "~1.3" "meta-tech/pws-client" : "^1.3"
} }
``` ```
@ -50,7 +50,7 @@ if ($response->done) {
} }
// post example // post example
$client->post('/ws/person/222/update', [ 'firstname' => 'toto']); $response = $client->post('/ws/person/222/update', [ 'firstname' => 'toto']);
if ($response->done) { if ($response->done) {
// do stuff // do stuff
} }
@ -69,6 +69,9 @@ if ($response->done) {
debug : 1 debug : 1
protocol : https:// protocol : https://
hostname : pwsserver.docker hostname : pwsserver.docker
# ssl options
verifypeer : 0
verifyhost : 0
# file storing the server 's session id - must be out of DocumentRoot and read/writable by server # file storing the server 's session id - must be out of DocumentRoot and read/writable by server
store : wsess store : wsess
login : test login : test
@ -95,7 +98,7 @@ However, meta-tech always return this simple Json Structure :
`{ done : boolean, msg : 'string contextual msg', data : whatever }` `{ done : boolean, msg : 'string contextual msg', data : whatever }`
see [ meta-tech/pws-server ](https://github.com/meta-tech/pws-server) see [ meta-tech/pws-server ](https://github.com/meta-tech/pws-server)
and [ meta-tech/sile-core ](https://github.com/meta-tech/silex-core) and [ meta-tech/silex-core ](https://github.com/meta-tech/silex-core)
### License ### License

View File

@ -18,7 +18,7 @@
}, },
"require" : { "require" : {
"php" : ">=5.4", "php" : ">=5.4",
"symfony/yaml" : "^3.2", "meta-tech/pws-auth" : "@dev",
"meta-tech/pws-auth" : "~2.1" "symfony/yaml": "^3.2"
} }
} }

View File

@ -1,14 +1,16 @@
# pwsclient config # pwsclient config
# 0 : disable, 1 : verboose, 2 : most verboose # 0 : disable, 1 : verboose, 2 : most verboose
debug : 1 debug : 2
protocol : https:// protocol : http://
hostname : pwsserver.docker hostname : pwsserver.docker
verifypeer : 0
verifyhost : 0
# file storing the server 's session id - must be out of DocumentRoot and read/writable by server # file storing the server 's session id - must be out of DocumentRoot and read/writable by server
store : wsess store : wsess
login : test login : dev
password : test password : foo
key : test key : ed830045da9861d29c46f36b4f4b1a4d4b223408667c52428370e51b615e8769
# 0 : display cli, 1 : display html # 0 : display cli, 1 : display html
html_output : 0 html_output : 0
# http authentication # http authentication

View File

@ -86,11 +86,11 @@ class Formatter
if (!$this->embedStyle) { if (!$this->embedStyle) {
$this->embedStyle = true; $this->embedStyle = true;
$style .= '<style type="text/css">' $style .= '<style type="text/css">'
. '.meta-tech-of-line { white-space:pre !important; background-color:black !important; color:white !important; font-weight:bold !important; font-family:monospace !important; padding:10px !important; margin-top:0 !important }' . '.meta-tech-of-line { font-size:13px !important; background-color:black !important; color:white !important; white-space:pre !important; font-weight:bold !important; font-family:\'monospace\' !important; padding:10px !important; margin-top:0 !important }'
. '.meta-tech-of-tag1 { color:#FB4E4E !important; }' . '.meta-tech-of-tag1 { color:#FB4E4E !important; }'
. '.meta-tech-of-tag2 { color:#20FF93 !important; }' . '.meta-tech-of-tag2 { color:#20FF93 !important; }'
. '.meta-tech-of-tag3 { color:#FFDC58 !important; }' . '.meta-tech-of-tag3 { color:#FFDC58 !important; }'
. '.meta-tech-of-tag4 { color:#44A2D6 !important; }' . '.meta-tech-of-tag4 { color:#44A2D6 !important; }'
. '</style>'; . '</style>';
} }
return $style; return $style;
@ -128,7 +128,7 @@ class Formatter
{ {
$content = $value . ($newline ? self::LF : ''); $content = $value . ($newline ? self::LF : '');
if ($this->type == self::TYPE_HTML) { if ($this->type == self::TYPE_HTML) {
$content = $this->embedStyleIfNeeded() . '<div class"meta-tech-ofline">'.$content.'</div>'; $content = $this->embedStyleIfNeeded() . '<div class="meta-tech-of-line">'.$content.'</div>';
} }
echo $content; echo $content;
} }

View File

@ -42,6 +42,9 @@ class Client
/*! @protected @var Mtc\Core\Auth\Authenticator $authenticator */ /*! @protected @var Mtc\Core\Auth\Authenticator $authenticator */
protected $authenticator; protected $authenticator;
/*! @protected @var str $responseToken */
protected $responseToken;
/*! /*!
* desc * desc
* *
@ -55,7 +58,7 @@ class Client
if (!is_array($config)) { if (!is_array($config)) {
throw new \Exception('bad rest config'); throw new \Exception('bad rest config');
} }
$typeFormatter = $this->config['html_output'] ? Formatter::TYPE_HTML : Formatter::TYPE_CLI; $typeFormatter = $config['html_output'] ? Formatter::TYPE_HTML : Formatter::TYPE_CLI;
$this->formatter = new Formatter($typeFormatter); $this->formatter = new Formatter($typeFormatter);
$this->config = $config; $this->config = $config;
$this->authenticator = $authenticator; $this->authenticator = $authenticator;
@ -173,6 +176,8 @@ class Client
private function _buildHeader($sessid=null) private function _buildHeader($sessid=null)
{ {
$header = $this->authenticator->generateHeader($this->config['login'], $this->config['key'], $sessid); $header = $this->authenticator->generateHeader($this->config['login'], $this->config['key'], $sessid);
$token = $this->authenticator->getToken($this->authenticator->readHeader($header));
$this->responseToken = $this->authenticator->generateResponseHeader($token, $this->config['login']);
return $header; return $header;
} }
@ -226,10 +231,9 @@ class Client
curl_setopt($curl, CURLOPT_HEADER , true); curl_setopt($curl, CURLOPT_HEADER , true);
curl_setopt($curl, CURLOPT_COOKIESESSION , false); curl_setopt($curl, CURLOPT_COOKIESESSION , false);
curl_setopt($curl, CURLOPT_USERAGENT , $this->config['key']); curl_setopt($curl, CURLOPT_USERAGENT , $this->config['key']);
if (preg_match('/^https:/i', $this->config['protocol'])) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->config['verifypeer']);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $this->config['verifyhost']);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
}
if (isset($this->config['http']) && isset($this->config['http']['user']) && isset($this->config['http']['password'])) { if (isset($this->config['http']) && isset($this->config['http']['user']) && isset($this->config['http']['password'])) {
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_USERPWD , $this->config['http']['user'] . ':'. $this->config['http']['password']); curl_setopt($curl, CURLOPT_USERPWD , $this->config['http']['user'] . ':'. $this->config['http']['password']);
@ -263,14 +267,31 @@ class Client
} }
if (count($header) > 0) curl_setopt($curl, CURLOPT_HTTPHEADER, $header); if (count($header) > 0) curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
// curl_setopt($curl, CURLOPT_HEADERFUNCTION, array($this, "HandleHeaderLine")); // curl_setopt($curl, CURLOPT_HEADERFUNCTION, array($this, "HandleHeaderLine"));
$rs = curl_exec($curl); $rs = curl_exec($curl);
$exectime = number_format(((microtime(true)-$stime)),5); $exectime = number_format(((microtime(true)-$stime)),5);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); $size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$respheader = substr($rs, 0, $size); $respheader = substr($rs, 0, $size);
$body = substr($rs, $size); $authresponse = false;
$response = json_decode($body); try {
$url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL); $lines = explode(PHP_EOL, $respheader);
$arrheader = [];
foreach($lines as $line) {
if (!empty($line)) {
$match = preg_split('/: /', $line, 2);
if (count($match)==2) {
$arrheader[$match[0]] = trim($match[1]);
}
}
}
$authresponse = isset($arrheader['Pws-Response']) && $arrheader['Pws-Response'] == $this->responseToken;
}
catch(\Exception $e) {
}
$body = substr($rs, $size);
$response = json_decode($body);
$url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
if ($status == 0) { if ($status == 0) {
throw new \Exception(curl_error($curl)); throw new \Exception(curl_error($curl));
} }
@ -286,6 +307,7 @@ class Client
} }
} }
if ($this->config['debug']) { if ($this->config['debug']) {
$responseToken = $this->responseToken;
$respcontent = null; $respcontent = null;
if (is_object($response)) { if (is_object($response)) {
$respcontent = clone $response; $respcontent = clone $response;
@ -317,7 +339,7 @@ class Client
'HEADER' => $this->authenticator->readHeader($header), 'HEADER' => $this->authenticator->readHeader($header),
'PARAMS' => $data, 'PARAMS' => $data,
'METHOD' => $method, 'METHOD' => $method,
'RESPONSE' => compact('date', 'uri', 'status') + ['curl' => $rs, 'response' => $respcontent] 'RESPONSE' => compact('date', 'uri', 'status', 'responseToken', 'authresponse') + ['curl' => $rs, 'response' => $respcontent]
], true) . Formatter::LF; ], true) . Formatter::LF;
array_unshift($tags, $traces); array_unshift($tags, $traces);
$this->formatter->writeTags($tags); $this->formatter->writeTags($tags);
@ -325,11 +347,15 @@ class Client
case self::VERBOOSE : case self::VERBOOSE :
array_unshift($tags, Formatter::LF); array_unshift($tags, Formatter::LF);
$tags[] = var_export(compact('status')+['response' => $respcontent], true); $tags[] = var_export(compact('status', 'authresponse')+['response' => $respcontent], true);
$this->formatter->writeTags($tags); $this->formatter->writeTags($tags);
break; break;
} }
return compact('date', 'uri', 'response', 'status', 'exectime'); if (!$authresponse) {
$response->done = false;
$response->msg = 'server response not authenticated !';
}
return compact('date', 'uri', 'response', 'status', 'exectime', 'authresponse');
} }
} }