server/src/app/Libraries/Handle/HandleClient.php
author ymh <ymh.work@gmail.com>
Sat, 06 Aug 2016 21:29:33 +0700
changeset 261 02e2396bcbbc
parent 154 ded3cf22eef8
child 326 226d5b17a119
permissions -rw-r--r--
Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
namespace CorpusParole\Libraries\Handle;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
use Log;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
use Illuminate\Pagination\LengthAwarePaginator;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
use Illuminate\Pagination\Paginator;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
use phpseclib\Crypt\RSA;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
// current_date = datetime.now()
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
// current_date_format = unicode(current_date.strftime('%Y-%m-%dT%H:%M:%SZ'))
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
// handle_record = {u'values': [
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
//     {u'index': 1, u'ttl': 86400, u'type': u'URL', u'timestamp': current_date_format, u'data': {u'value': u'http://www.ribaenterprises.com', u'format': u'string'}},
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
//     {u'index': 2, u'ttl': 86400, u'type': u'EMAIL', u'timestamp': current_date_format, u'data': {u'value': u'info@ribaenterprises.com', u'format': u'string'}},
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
//     {u'index': 100, u'ttl': 86400, u'type': u'HS_ADMIN', u'timestamp': current_date_format, u'data': {u'value': {u'index': 200, u'handle': unicode(auth_id), u'permissions': u'011111110011'}, u'format': u'admin'}}
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
// ], u'handle': unicode(handle), u'responseCode': 1}
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
// class to handle communication with Handle server API.
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
// inspired by : https://github.com/theNBS/handleserver-samples
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
class HandleClient {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    public function __construct($privateKeyOrCert, $pkpass, $adminId, $handleHost, $handlePort, $httpClient) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
        $this->session = null;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        $this->cert = null;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
        $this->adminId = $adminId;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        $this->privateKeyRes = null;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
        $this->privateKeyOrCert = $privateKeyOrCert;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        $this->pkpass = $pkpass;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
        $this->handleHost = $handleHost;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        $this->handlePort = $handlePort;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        $this->httpClient = $httpClient;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        $this->guzzleOptions = ['verify' => false,];
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    public function __destruct () {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        $this->close();
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    public function close() {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        $this->deleteSession();
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    45
        $this->freeResources();
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    private function getBaseUrl() {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        return "https://$this->handleHost:$this->handlePort/api/";
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
    private function getSessionAuthHeader() {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        return "Handle sessionId=$this->session";
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
    private function generateClientNonce() {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        return openssl_random_pseudo_bytes(16);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
    private function getPrivateKeyRes() {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        if(is_null($this->privateKeyRes)) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
            $this->privateKeyRes = openssl_pkey_get_private($this->privateKeyOrCert, $this->pkpass);
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    63
        }
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
        return $this->privateKeyRes;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    private function freeResources() {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
        if(!empty($this->privateKeyRes)) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
            $privateKeyRes = $this->privateKeyRes;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
            $this->privateKeyRes = null;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
            openssl_free_key($privateKeyRes);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
    private function signBytesDsa($str) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        openssl_sign($str, $signature, $this->getPrivateKeyRes(), OPENSSL_ALGO_DSS1);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
        return $signature;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
    private function signBytesRsa($str) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        $rsa = new RSA();
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
        $rsa->setHash('sha256');
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
        if(!empty($this->pkpass)) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
            $rsa->setPassword($this->pkpass);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
        }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
        $keyContent = $this->privateKeyOrCert;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
        if(is_file($keyContent)) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
            $keyContent = file_get_contents($keyContent);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
        }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
        $rsa->loadKey($keyContent);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
        $rsa->setSignatureMode(RSA::SIGNATURE_PKCS1);
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
    96
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
        return $rsa->sign($str);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
    private function createAuthorisationHeaderFromJson($jsonresp) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
        # Unpick number once (nonce) and session id from server response (this is the challenge)
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
        $serverNonce = base64_decode($jsonresp['nonce']);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
        $this->sessionId = $jsonresp['sessionId'];
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
        # Generate a client number once (cnonce)
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
        $clientNonce = $this->generateClientNonce();
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
        $clientNonceStr = base64_encode($clientNonce);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
        # Our response has to be the signature of server nonce + client nonce
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
        $combinedNonce = $serverNonce . $clientNonce;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
        if($this->getPrivateKeyRes() === false) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
            throw new \Exception("HandleClient: can not read private res");
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
        }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
        $keyDetails = openssl_pkey_get_details($this->getPrivateKeyRes());
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
        if($keyDetails === false) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
            throw new \Exception("HandleClient: can not read private key");
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
        }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
        if($keyDetails['type']===OPENSSL_KEYTYPE_RSA) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
            $signature = $this->signBytesRsa($combinedNonce);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
            $signAlg = 'SHA256';
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
        } elseif ($keyDetails['type']===OPENSSL_KEYTYPE_DSA) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
            $signature = $this->signBytesDsa($combinedNonce);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
            $signAlg = 'SHA1';
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
        } else {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
            throw new \Exception("HandleClient: $keyDetails[type] Format unknown");
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
        }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
        $signatureStr = base64_encode($signature);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
        $this->freeResources();
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
        # Build the authorisation header to send with the request
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
        # Use SHA1 for DSA keys; SHA256 can be used for RSA keys
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
        return $this->buildComplexAuthorizationString($signatureStr, $signAlg, $clientNonceStr);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
    private function buildComplexAuthorizationString($signatureString, $signAlg, $clientNonceString) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
        return "Handle " .
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
              "version=\"0\", " .
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
              "sessionId=\"$this->sessionId\", " .
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
              "cnonce=\"$clientNonceString\", " .
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
              "id=\"$this->adminId\", " .
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
              "type=\"HS_PUBKEY\", " .
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
              "alg=\"$signAlg\", " .
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
              "signature=\"$signatureString\"";
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
    public function initSession() {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
        if(!empty($this->session) || !empty($this->cert)) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
            return;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
        }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
        $headers = key_exists('headers', $this->guzzleOptions)?$this->guzzleOptions['headers']:[];
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
        $headers = array_merge($headers, [
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
            'Content-Type' => 'application/json;charset=UTF-8',
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
        ]);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
        $certContent = $this->privateKeyOrCert;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
        if(is_file($certContent)) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
            $certContent = file_get_contents($certContent);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
        }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
        if(openssl_x509_parse($certContent) !== false) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
            if(!empty($this->pkpass)) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
                $this->cert = [$this->privateKeyOrCert, $this->pkpass];
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
            } else {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
                $this->cert = $this->privateKeyOrCert;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
            }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
            $headers['Authorization'] =  "Handle clientCert=\"true\"";
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
        } else {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
            $url = $this->getBaseUrl()."sessions/";
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
            $challengeRes = $this->httpClient->post($url, ['verify' => false]);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
            $jsonResp = json_decode($challengeRes->getBody(), true);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
            $pkheaders = [
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
                'Content-Type' => 'application/json;charset=UTF-8',
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
                'Authorization' => $this->createAuthorisationHeaderFromJson($jsonResp)
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
            ];
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
            # Send the request again with a valid correctly signed Authorization header
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
            $sessionResp = $this->httpClient->put($url.'this', ['headers' => $pkheaders, 'verify' => false]);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
            Log::debug('Create session with auth: '.$sessionResp->getStatusCode().' : '.$sessionResp->getReasonPhrase());
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
            $jsonResp = json_decode($sessionResp->getBody(), true);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
            $this->session = $jsonResp['authenticated']?$jsonResp['sessionId']:"";
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
            $headers['Authorization'] = "Handle version=\"0\", sessionId=\"$this->session\"";
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
        }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
        $this->guzzleOptions = array_merge($this->guzzleOptions, ['headers' => $headers, 'cert' => $this->cert]);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
    // will call a async method. Apart logging we do not really care in the result
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
    public function deleteSession() {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
        if(empty($this->session)) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
            return;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
        }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
        $headers = [
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
            'Content-Type' => 'application/json;charset=UTF-8',
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
            'Authorization' => $this->getSessionAuthHeader()
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
        ];
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
        $url = $this->getBaseUrl()."sessions/this";
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
        // Do not really care of the response...
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
        $this->httpClient->deleteAsync($url, ['headers' => $headers, 'verify' => false]);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
        $this->session = null;
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
    /**
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
     * Paginate all handle as a paginator.
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
     *
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
     * @param  int  $perPage
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
     * @param  string  $pageName
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
     */
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
    public function paginateAll($prefix, $perPage = 15, $pageName = 'page', $page = null) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
        $this->initSession();
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
        $url = $this->getBaseUrl()."handles";
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
        $params = [
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
            'prefix' => $prefix,
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
            'page' => is_null($page)?0:$page-1,
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
            'pageSize' => $perPage
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
        ];
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
        $paginateRes = $this->httpClient->get($url, array_merge($this->guzzleOptions, ['query' => $params]));
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
        $paginateJson = json_decode($paginateRes->getBody(), true);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
        $total = (int)$paginateJson['totalCount'];
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
        $results = $paginateJson['handles'];
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   239
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
        return new LengthAwarePaginator($results, $total, $perPage, $page, [
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
            'path' => Paginator::resolveCurrentPath(),
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
            'pageName' => $pageName,
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
        ]);
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   244
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
    public function deleteHandle($handle) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
        $this->initSession();
154
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   249
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   250
        if($handle === $this->adminId) {
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   251
            throw new \Exception("HandleClient: can not delete admin handle");
ded3cf22eef8 correct handle managing
ymh <ymh.work@gmail.com>
parents: 153
diff changeset
   252
        }
153
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
        $delUrl = $this->getBaseUrl()."handles/$handle";
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
        $delRes = $this->httpClient->delete($delUrl, $this->guzzleOptions);
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
        Log::debug('Delete Handle: '.$delRes->getStatusCode().': '.$delRes->getReasonPhrase());
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
    public function createHandleUrlRecord($handle, $url) {
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
        $this->initSession();
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
        $currentDate = gmstrftime('%Y-%m-%dT%H:%M:%SZ');
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
        $handleRecord = [
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
          'values' => [
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
            ['index' => 1, 'ttl' => 86400, 'type' => 'URL', 'timestamp' => $currentDate, 'data' => ['value'=> $url, 'format'=> 'string']],
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
            ['index' => 100, 'ttl' => 86400, 'type' => 'HS_ADMIN', 'timestamp' => $currentDate, 'data' => [
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
                'value' => ['index' => 200, 'handle' => $this->adminId],
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
                'permissions' => '011111110011',
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
                'format' => 'admin'
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
              ]
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
            ]
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
          ],
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
          'handle' => $handle,
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
        ];
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
        $submitUrl = $this->getBaseUrl()."handles/$handle";
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
        $submitRes = $this->httpClient->put($submitUrl, array_merge($this->guzzleOptions, ['json' => $handleRecord, ]));
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
        Log::debug('Create Handle Url: '.$submitRes->getStatusCode().' : '.$submitRes->getReasonPhrase());
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
    }
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
338bcc78d431 add HandleClient implementation + configuration + tests
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
}