<repo_name>gcostaa/devapiss<file_sep>/api/get.php
<?php

require_once("app/controller/get/request.php");

#validates if the get parameter is set
if(empty($_GET["id"])){


    $msg = "Enter the credential id. Example: get.php?id=";
    echo "<script type='text/javascript'>alert('$msg');</script>";

}else{


    #executes the get method of the request.php class
    $id = $_GET["id"];
    get($id);
}


?>
<file_sep>/api/README.txt
Author: <NAME>
Date: 2021/04/10

Description: Integration with the senhasegura API for credentials password 
query (version 1.0)

How to use:

- File config.json

    In the file you must insert your access keys generated through the tool. 
    The keys are: Consumer Key and oAUTH Token

- Request

    After configuring the config.json file, just call the URL 
    https://IP_OR_DNS/api/get.php?id=credential_id

    or

    curl -X "GET" https://server/api/get.php?id=2<file_sep>/api/app/controller/get/request.php
<?php

#get local directory

if(empty($_SERVER["PWD"])){

    require_once $_SERVER["DOCUMENT_ROOT"].'/api/app/model/keys/accessKeys.php';
    require_once $_SERVER["DOCUMENT_ROOT"].'/api/app/view/view.php';
}else{

    require_once $_SERVER["PWD"].'/app/model/keys/accessKeys.php';
    require_once $_SERVER["PWD"].'/app/view/view.php';
}


function get(string $credentialId){

    try{

        /*
        
        The AcessKeys class will obtain the access keys defined
        in config.json and deliver the values ​​for the URL request
        
        */

        $keys = new AccessKeys();
    
        $url = "https://".$keys->getValtIp()."/iso/coe/senha/".$credentialId."?oauth_consumer_key=".$keys->getOauthConsumerKey().
        "&oauth_token=".$keys->getOauthToken()."&oauth_signature=-";
    
        #initialize the method curl
        $request = curl_init();
    
        #config URL
        curl_setopt($request, CURLOPT_URL, $url);
    
        #get response
        curl_setopt($request,CURLOPT_RETURNTRANSFER,1);

        #skip certificate
        curl_setopt($request, CURLOPT_SSL_VERIFYHOST, FALSE);
    
        curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE);
        
        #send request
        $response = curl_exec($request);
        
        /*

        The View class will return the password values, device in the
        form of text on the screen

        */

        $view = new View($response);

        #close connection
        curl_close($request);
    
    } catch(Exception $e){
    
        echo("Erro ".$e->getMessage()."\n");
    }
}

<file_sep>/api/app/model/keys/accessKeys.php
<?php

class AccessKeys{
    

    private $oauth_consumer_key = "";
    private $oauth_signature = "";
    private $oauth_token = "";
    private $vaultIP = "";


    function __construct()
    {

        #get values ​​from the configuration file

        $json = file_get_contents($_SERVER['DOCUMENT_ROOT']."/api/config.json");
        $data = json_decode($json,true);
       
        $this->oauth_consumer_key = $data["keys"]["oauth_consumer_key"];
        $this->oauth_signature = $data["keys"]["oauth_signature"];
        $this->oauth_token = $data["keys"]["oauth_token"];
        $this->vaultIP = $data["keys"]["vault"];

    }

    public function getOauthConsumerKey(){

        $key = $this->oauth_consumer_key;

        if (empty($key)){

            echo("Consumer key is not configured");
           
        }else{
            
            return $key;
        }

    }

    public function getOauthToken(){

        $key = $this->oauth_token;

        if (empty($key)){

            echo("Token key is not configured");
           
        }else{
            
            return $key;
        }

    }

    public function getOauthSignature(){

        $key = $this->oauth_signature;

        if (empty($key)){

            echo("Signature key is not configured");
           
        }else{
            
            return $key;
        }
    }

    public function getValtIp(){

        $key = $this->vaultIP;
    
         if (empty($key)){
    
            echo("Vault IP is not configured");
               
        }else{
                
            return $key;
        }

    }

}

?><file_sep>/api/app/view/view.php
<?php

class View{

    private $response = [];
    private $status;

    function __construct($responseRequest)
    {

        #get response from senhasegura API request and convert to array
        $this->response = json_decode($responseRequest,true);

        #get status code
        $this->status = $this->getHttpCode($this->response);

        #displays the values ​​on the screen
        $this->display($this->response,$this->status);
          
    }


    public function display(Array $response,$statusCode){

       
        if($statusCode == 200){

            
            echo (PHP_EOL."KEY VALUES"."<br><br>".PHP_EOL);

            echo("Device.... ".$response["senha"]["hostname"]
            ."-".$response["senha"]["ip"]."<br>".PHP_EOL);

            echo("Credential.... ".$response["senha"]["username"]."<br>".PHP_EOL);

            echo("Password.... ".$response["senha"]["senha"]."".PHP_EOL);


        }else{
            echo ($response["response"]["mensagem"]);
        }

    
    }

    
    private function getHttpCode(Array $response){

    
        $code = $response["response"]["status"];

        return $code;
        
    }
    
}

?>