Add PHP implmentation of WCS signature algorithm

This commit is contained in:
Benjamin Dauvergne 2014-09-18 10:55:41 +02:00
commit e2fc0a278c
1 changed files with 34 additions and 0 deletions

34
php/signature.php Normal file
View File

@ -0,0 +1,34 @@
<?php
function wcs_unparse_url($parsed_url) {
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
$user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
$pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
$pass = ($user || $pass) ? "$pass@" : '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
return "$scheme$user$pass$host$port$path$query$fragment";
}
function wcs_sign($query, $algo, $key) {
return $query . '&signature=' . urlencode(base64_encode(hash_hmac($algo, $query, $key, True)));
}
function wcs_signature($url, $algo, $key) {
$parsed_url = parse_url($url);
$signed_data = $parsed_url['query'];
if ($signed_data != "") {
$signed_data .= "&";
}
$signed_data .= 'hash=' . urlencode($algo);
$signed_data .= '&timestamp=' . urlencode(str_replace('+0000', 'Z', gmdate(DATE_ISO8601)));
echo str_replace('+0000', 'Z', gmdate(DATE_ISO8601)) . "\n";
$signed_data .= '&nonce=' . rand();
echo $signed_data . "\n";
$parsed_url['query'] = wcs_sign($signed_data, $algo, $key);
return wcs_unparse_url($parsed_url);
}
# $url = 'https://eservices.montpellier-agglo.com/user?format=json&NameID=_1234&orig=compte-citoyen.montpellier-agglo.com';
# echo $url . "\n";
# echo wcs_signature($url, 'sha256', 'coin') . "\n";