APIgoat_API_wordpress_plugin/classes/APIgoatFetchAPI.php

108 lines
3.5 KiB
PHP
Raw Normal View History

2020-09-03 22:10:06 +00:00
<?php
use BernardoSilva\JWTAPIClient\APIClient;
use BernardoSilva\JWTAPIClient\AccessTokenCredentials;
use \Firebase\JWT\JWT;
include_once plugin_dir_path(dirname(__FILE__)) . 'includes/html_helper.php';
class APIgoatFetchAPI
{
2020-12-06 22:21:31 +00:00
private $username = 'sysadmin';
private $password = 'vezvez';
private $baseURI = 'https://goat.local/p/goatcheese/api/v1/';
2020-09-03 22:10:06 +00:00
private $jwt_pubkey = '9sKjdjuue8sSjwh6';
private $jwt_alg = ['HS256'];
private $client;
private $credentials;
public function __construct()
{
$this->client = new APIClient($this->baseURI);
$this->clientOptions = [
2020-12-06 22:21:31 +00:00
'verify' => false,
2020-09-03 22:10:06 +00:00
'content-Type' => 'application/json',
2020-10-27 00:06:31 +00:00
'accept' => 'application/json',
2020-12-06 22:21:31 +00:00
'debug' => false,
'timeout' => 10
2020-09-03 22:10:06 +00:00
];
2020-12-06 22:21:31 +00:00
//unset($_SESSION['APIgoat']);
2020-09-03 22:10:06 +00:00
if (!$this->authenticationValid()) {
$this->authenticate();
$this->saveCredentials();
$this->client = new APIClient($this->baseURI, $this->credentials);
} else {
$this->credentials = new AccessTokenCredentials($_SESSION['APIgoat']['API_jwt_token']);
$this->client->setCredentials($this->credentials);
}
}
public function fetchBehaviors()
{
$clientOptions = $this->clientOptions;
$clientOptions['query'] = [
2020-10-27 00:06:31 +00:00
"query" => [
2020-09-03 22:10:06 +00:00
"select" => [
2020-12-06 22:21:31 +00:00
["behavior.name", "name"], ["code", "title"], ["description", "text"], "value", "example", "type", ["behavior_category.name", "category_name"], "group"
2020-09-03 22:10:06 +00:00
],
"filter" => [
2020-12-06 22:21:31 +00:00
["status", "Active"]
2020-09-03 22:10:06 +00:00
],
2020-12-06 22:21:31 +00:00
"join" => ["behavior_category"]
]
2020-09-03 22:10:06 +00:00
];
2020-12-06 22:21:31 +00:00
//$clientOptions['debug'] = true;
2020-09-03 22:10:06 +00:00
$response = $this->client->get('Behavior', $clientOptions);
$body = json_decode($response->getBody()->getContents(), true);
2020-12-06 22:21:31 +00:00
//$body['debug'][] = $clientOptions['query'];
2020-09-03 22:10:06 +00:00
if ($response->getStatusCode() == 200) {
return $body;
} else {
$body = json_decode($response->getBody(), true);
return $body;
}
}
private function saveCredentials()
{
if (!empty($this->jwt_pubkey)) {
$decoded = JWT::decode($this->credentials->getAccessToken(), $this->jwt_pubkey, $this->jwt_alg);
$_SESSION['APIgoat']['API_jwt_expire'] = $decoded->exp;
$_SESSION['APIgoat']['API_jwt_token'] = $this->credentials->getAccessToken();
}
}
private function authenticate()
{
$options = [
'verify' => false, // might need this if API uses self signed certificate
'form_params' => [
'u' => $this->username,
'pw' => md5($this->password)
],
2020-12-06 22:21:31 +00:00
'debug' => false,
'timeout' => 10
2020-09-03 22:10:06 +00:00
];
// authenticate on API to get token
$response = $this->client->post('Authy/auth', $options);
$loginResponseDecoded = json_decode($response->getBody()->getContents(), true);
2020-11-02 00:14:51 +00:00
/*print_r($loginResponseDecoded);
die();*/
2020-09-03 22:10:06 +00:00
$this->credentials = new AccessTokenCredentials($loginResponseDecoded['token']);
}
private function authenticationValid()
{
if (isset($_SESSION['APIgoat']) && !empty($_SESSION['APIgoat']['API_jwt_token']) && $_SESSION['APIgoat']['API_jwt_expire'] > time()) {
return true;
}
return false;
}
}