Skip to content

Instantly share code, notes, and snippets.

@sylvainraye
Last active April 13, 2017 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sylvainraye/9716109 to your computer and use it in GitHub Desktop.
Save sylvainraye/9716109 to your computer and use it in GitHub Desktop.
WSSE Authentification for OroPlatform REST API
<?php
/**
* Diglin GmbH - Switzerland
*
* User: sylvainraye
* Date: 22.03.14
* Time: 17:11
*
* @category orocrm
* @package Diglin_Oro
* @copyright Copyright (c) 2011-2014 Diglin (http://www.diglin.com)
*/
namespace Diglin\Oro\Wsse;
class Authentication
{
protected $_username;
protected $_apiKey;
/**
* @param $username
* @param $apiUserKey
*/
public function __construct ($username, $apiUserKey)
{
$this->_username = $username;
$this->_apiKey = $apiUserKey;
}
/**
* @param $raw
* @param $salt
* @return string
*/
private function _encodePassword($raw, $salt)
{
$salted = $this->_mergePasswordAndSalt($raw, $salt);
$digest = hash('sha1', $salted, true);
return base64_encode($digest);
}
/**
* @param string $password
* @param string $salt
* @return string
* @throws \InvalidArgumentException
*/
private function _mergePasswordAndSalt($password, $salt)
{
if (empty($salt)) {
return $password;
}
if (false !== strrpos($salt, '{') || false !== strrpos($salt, '}')) {
throw new \InvalidArgumentException('Cannot use { or } in salt.');
}
return $password.'{'.$salt.'}';
}
/**
* @return array
*/
public function getHeaders ()
{
$prefix = gethostname();
$created = date('c');
$nonce = base64_encode(substr(md5(uniqid($prefix . '_', true), true), 0, 16));
$salt = ''; // do not use real salt here, because API key already encrypted enough
$passwordDigest = $this->_encodePassword(sprintf(
'%s%s%s',
base64_decode($nonce),
$created,
$this->_apiKey
),
$salt
);
$wsseProfile = sprintf(
'X-WSSE: UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"',
$this->_username,
$passwordDigest,
$nonce,
$created
);
return array(
'Authorization: WSSE profile="UsernameToken"',
$wsseProfile
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment