Skip to content

Okay Server Integration Example (PHP)

Paul Nest edited this page Jul 23, 2019 · 1 revision
<?php

class ProtectoriaApi
{

    public $url;

    public $tenantId;

    public $userExternalId;

    public $tenantSecret;

    public $type;

    public $guiHeader;

    public $guiText;

    public $sessionExternalId;

    public function __construct($data = [])
    {
        if(!empty($data)){
            $this->url = $data['url'] ?? null;
            $this->tenantId = $data['tenantId'] ?? null;
            $this->userExternalId = $data['userExternalId'] ?? null;
            $this->tenantSecret = $data['tenantSecret'] ?? null;
            $this->type = $data['type'] ?? null;
            $this->guiHeader = $data['guiHeader'] ?? null;
            $this->guiText = $data['guiText'] ?? null;
            $this->sessionExternalId = $data['guiText'] ?? null;
        }

    }

    public function sendLinkingRequst()
    {
        return $this->sendRequest($this->url, [
            'tenantId' => $this->tenantId,
            'userExternalId' => $this->userExternalId,
            'signature' => $this->getSignature($this->tenantId.$this->userExternalId.$this->tenantSecret)
        ]);
    }

    public function sendServerAuthRequst()
    {
        return $this->sendRequest($this->url, [
            'tenantId' => $this->tenantId,
            'userExternalId' => $this->userExternalId,
            'type' => $this->type,
            'authParams' => [
                'guiHeader' => $this->guiHeader,
                'guiText' => $this->guiText
            ],
            'signature' =>  $this->getSignature($this->tenantId.$this->userExternalId.$this->guiHeader.
                $this->guiText.$this->type.$this->tenantSecret)
        ]);
    }

    public function sendServerAuthCheckUserRequst()
    {
        return $this->sendRequest($this->url, [
            'tenantId' => $this->tenantId,
            'sessionExternalId' => $this->sessionExternalId,
            'signature' =>  $this->getSignature($this->tenantId.$this->userExternalId.$this->tenantSecret)
        ]);
    }

    /**
     * @param $request_body string
     * @return bool
     */
    public function checkLinkUserCallbackSignature($request_body)
    {
        $tmp_signature = $this->getSignature($request_body['userExternalId'].$request_body['status']['code'].
            $request_body['type'].$this->tenantSecret);

         return $tmp_signature === $request_body['signature'];
    }

    /**
     * @param $request_body string
     * @return bool
     */
    public function checkAuthUserCallbackSignature($request_body)
    {
        $tmp_signature = $this->getSignature($request_body['userExternalId'].$request_body['status']['code'].
            $request_body['type'].$this->tenantSecret);

        return $tmp_signature === $request_body['signature'];
    }

    /**
     * @param $request_body string
     * @return bool
     */
    public function checkRequestSignature($request_body){
        $request_body = json_decode($request_body, true);
        if (!empty($request_body['authResult']))
            return $this->checkAuthUserCallbackSignature($request_body);
        else
            return $this->checkLinkUserCallbackSignature($request_body);

    }

    /** Generate signature for specified string
     * @param $str
     * @return string
     */
    private function getSignature($str)
    {
        return base64_encode(hash('sha256', $str, true));
    }

    /**
     * @param $url
     * @param $data array of send values
     * @return array
     */
    private function sendRequest($url, $data)
    {
        $data = json_encode($data);

        $headers = array(                       //setting headers to send JSON
            "Content-type: application/json;",
            "Accept: application/json",
            "Content-length: ".strlen($data),
        );

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $result = curl_exec($curl);

        curl_close($curl);

        return json_decode($result, true);
    }
}
?>

Class usage example

<?php
$request_body = file_get_contents("php://input");

if (!empty($request_body)) {
    $object = new ProtectoriaApi([
        'tenantSecret' => 'my-super-secret-token'
    ]);
    $result = $object->checkRequestSignature($request_body);
} else {
    $object = new ProtectoriaApi([
        'url' => 'http://protdemo.demohoster.com/gateway/link',
        'tenantId' => 20001,
        'userExternalId' => strval(rand(1, 100000)),
        'tenantSecret' => 'my-super-secret-token',
    ]);
    $linkingResponse = $object->sendLinkingRequst();

    $object = new ProtectoriaApi([
        'url' => 'http://protdemo.demohoster.com/gateway/auth',
        'tenantId' => 20001,
        'userExternalId' => strval(rand(1, 100000)),
        'tenantSecret' => 'my-super-secret-token',
        'guiHeader' => 'guiHeader',
        'guiText' => 'guiText',
        'type' => 101,
    ]);
    $serverAuthResponse = $object->sendServerAuthRequst();

    $object = new ProtectoriaApi([
        'url' => 'http://protdemo.demohoster.com/gateway/check',
        'tenantId' => 20001,
        'sessionExternalId' => strval(rand(1, 100000)),
        'tenantSecret' => 'my-super-secret-token',
    ]);
    $serverAuthCheckUserResponse = $object->sendServerAuthCheckUserRequst();
}
?>