1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AtlassianConnectBundle\Service; |
6
|
|
|
|
7
|
|
|
class QSHGenerator |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Create Query String Hash. |
11
|
|
|
* |
12
|
|
|
* More details: |
13
|
|
|
* https://developer.atlassian.com/static/connect/docs/latest/concepts/understanding-jwt.html#creating-token |
14
|
|
|
* |
15
|
|
|
* @param string $url URL of the request |
16
|
|
|
* @param string $method HTTP method |
17
|
|
|
*/ |
18
|
|
|
public static function generate(string $url, string $method): string |
19
|
|
|
{ |
20
|
|
|
$parts = parse_url($url); |
21
|
|
|
|
22
|
|
|
// Remove "/wiki" part from the path for the Confluence |
23
|
|
|
// Really, I didn't find this part in the docs, but it works |
24
|
|
|
$path = str_replace('/wiki', '', $parts['path']); |
25
|
|
|
$canonicalQuery = ''; |
26
|
|
|
|
27
|
|
|
if (\array_key_exists('query', $parts) && null !== $parts['query'] && '' !== $parts['query']) { |
28
|
|
|
$query = $parts['query']; |
29
|
|
|
$queryParts = explode('&', $query); |
30
|
|
|
$queryArray = []; |
31
|
|
|
|
32
|
|
|
foreach ($queryParts as $queryPart) { |
33
|
|
|
$pieces = explode('=', $queryPart); |
34
|
|
|
$key = array_shift($pieces); |
35
|
|
|
$key = rawurlencode($key); |
36
|
|
|
$value = mb_substr($queryPart, mb_strlen($key) + 1); |
37
|
|
|
$value = rawurlencode($value); |
38
|
|
|
$queryArray[$key][] = $value; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
ksort($queryArray); |
42
|
|
|
|
43
|
|
|
foreach ($queryArray as $key => $pieceOfQuery) { |
44
|
|
|
$pieceOfQuery = implode(',', $pieceOfQuery); |
45
|
|
|
$canonicalQuery .= $key.'='.$pieceOfQuery.'&'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$canonicalQuery = rtrim($canonicalQuery, '&'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return hash('sha256', implode('&', [mb_strtoupper($method), $path, $canonicalQuery])); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|