PHP Soap填充标头凭证 [英] PHP Soap fill header credentials

查看:117
本文介绍了PHP Soap填充标头凭证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Soap连接到Web服务,我需要填写标题凭据才能登录.

$user_id        = 'MyUserId';
$unique_key     = $this->getUniqueKey();
$base_password  = $this->getFieldBase('MyPassword', $uniqueKey);
$base_date      = $this->getFieldBase(gmdate('Y-m-d\TH:i:s\.00\Z'), $unique_key);
$nonce          = $this->getFieldNonce($unique_key, '.myPemFile.pem');

<wss:UsernameToken>
    <wss:Username>' . $user_id . '</wss:Username>
    <wss:Password>' . $base_password . '</wss:Password>
    <wss:Nonce>' . $nonce . '</wss:Nonce>
    <wss:Created>' . $base_date . '</wss:Created>
</wss:UsernameToken>

所有遵循结构的值(用户名除外).

我已经为5.6 PHP项目工作了,但是现在我需要将其改编成PHP 7项目,这意味着我不再可以使用mcrypt_encrypt(),因为它已被弃用,因此我需要使用openssl_encrypt()

我当前的功能是:

private function getFieldBase($data, $key)
{
    $ivsize         = openssl_cipher_iv_length('AES-128-ECB');
    $iv             = openssl_random_pseudo_bytes($ivsize);
    $ciphertext     = openssl_encrypt($data, 'AES-128-ECB', $key, 0, $iv);  

    return trim(base64_encode($ciphertext));
}

private function getFieldNonce($data, $pbkey)
{
    openssl_public_encrypt($data, $crypttext, openssl_pkey_get_public(file_get_contents($pbkey)));

    return base64_encode($crypttext);
}

private function getUniqueKey() 
{
   return substr(md5(uniqid(microtime())), 0, 16);
}

问题是,当连接到Web服务时,我收到错误消息:

已拒绝:错误:密钥会话无效.它不是 可以破译创建"字段

告诉我我的getFieldBase()函数是错误的.

解决方案

已解决.

在功能openssl_encrypt中,参数RAW_OUTPUT必须为true.

private function getFieldBase($data, $key)
{
    $ivsize     = openssl_cipher_iv_length('AES-128-ECB');
    $iv         = openssl_random_pseudo_bytes($ivsize);
    $ciphertext = openssl_encrypt($data, 'AES-128-ECB', $key, TRUE, $iv);   

    return base64_encode($ciphertext);
}

I'm connecting by Soap to a webservice and I need to fill the header credentials in order to login.

$user_id        = 'MyUserId';
$unique_key     = $this->getUniqueKey();
$base_password  = $this->getFieldBase('MyPassword', $uniqueKey);
$base_date      = $this->getFieldBase(gmdate('Y-m-d\TH:i:s\.00\Z'), $unique_key);
$nonce          = $this->getFieldNonce($unique_key, '.myPemFile.pem');

<wss:UsernameToken>
    <wss:Username>' . $user_id . '</wss:Username>
    <wss:Password>' . $base_password . '</wss:Password>
    <wss:Nonce>' . $nonce . '</wss:Nonce>
    <wss:Created>' . $base_date . '</wss:Created>
</wss:UsernameToken>

All values (except username) as to follow a structure.

I have this working for a 5.6 PHP project but now I need to adapt it to a PHP 7 project, and that means I can no longer use mcrypt_encrypt() because it's deprecated and therefore I need to use openssl_encrypt()

My current functions are:

private function getFieldBase($data, $key)
{
    $ivsize         = openssl_cipher_iv_length('AES-128-ECB');
    $iv             = openssl_random_pseudo_bytes($ivsize);
    $ciphertext     = openssl_encrypt($data, 'AES-128-ECB', $key, 0, $iv);  

    return trim(base64_encode($ciphertext));
}

private function getFieldNonce($data, $pbkey)
{
    openssl_public_encrypt($data, $crypttext, openssl_pkey_get_public(file_get_contents($pbkey)));

    return base64_encode($crypttext);
}

private function getUniqueKey() 
{
   return substr(md5(uniqid(microtime())), 0, 16);
}

The problem is that when connecting to the Webservice I receive the error:

Rejected: Error: The key session is invalid. It was not possible to decipher the field Created

Which tells me that my getFieldBase() function is wrong.

解决方案

Solved.

The parameter RAW_OUTPUT must be true within the function openssl_encrypt.

private function getFieldBase($data, $key)
{
    $ivsize     = openssl_cipher_iv_length('AES-128-ECB');
    $iv         = openssl_random_pseudo_bytes($ivsize);
    $ciphertext = openssl_encrypt($data, 'AES-128-ECB', $key, TRUE, $iv);   

    return base64_encode($ciphertext);
}

这篇关于PHP Soap填充标头凭证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆