|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
|
9
|
|
|
* |
|
10
|
|
|
* This software may be modified and distributed under the terms |
|
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\Server\MetadataEndpoint; |
|
15
|
|
|
|
|
16
|
|
|
use Http\Message\ResponseFactory; |
|
17
|
|
|
use Interop\Http\Server\RequestHandlerInterface; |
|
18
|
|
|
use Interop\Http\Server\MiddlewareInterface; |
|
19
|
|
|
use Jose\Component\Core\Converter\StandardConverter; |
|
20
|
|
|
use Jose\Component\Core\JWK; |
|
21
|
|
|
use Jose\Component\Signature\JWSBuilder; |
|
22
|
|
|
use Jose\Component\Signature\Serializer\CompactSerializer; |
|
23
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
24
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
25
|
|
|
|
|
26
|
|
|
final class MetadataEndpoint implements MiddlewareInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ResponseFactory |
|
30
|
|
|
*/ |
|
31
|
|
|
private $responseFactory; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var Metadata |
|
35
|
|
|
*/ |
|
36
|
|
|
private $metadata; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var null|JWK |
|
40
|
|
|
*/ |
|
41
|
|
|
private $signatureKey = null; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var null|string |
|
45
|
|
|
*/ |
|
46
|
|
|
private $signatureAlgorithm = null; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var null|JWSBuilder |
|
50
|
|
|
*/ |
|
51
|
|
|
private $jwsBuilder = null; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* MetadataEndpoint constructor. |
|
55
|
|
|
* |
|
56
|
|
|
* @param ResponseFactory $responseFactory |
|
57
|
|
|
* @param Metadata $metadata |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct(ResponseFactory $responseFactory, Metadata $metadata) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->responseFactory = $responseFactory; |
|
62
|
|
|
$this->metadata = $metadata; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param JWSBuilder $jwsBuilder |
|
67
|
|
|
* @param JWK $signatureKey |
|
68
|
|
|
* @param string $signatureAlgorithm |
|
69
|
|
|
*/ |
|
70
|
|
|
public function enableSignature(JWSBuilder $jwsBuilder, string $signatureAlgorithm, JWK $signatureKey) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->jwsBuilder = $jwsBuilder; |
|
73
|
|
|
$this->signatureKey = $signatureKey; |
|
74
|
|
|
$this->signatureAlgorithm = $signatureAlgorithm; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
81
|
|
|
{ |
|
82
|
|
|
$data = $this->metadata->jsonSerialize(); |
|
83
|
|
|
if (null !== $this->jwsBuilder) { |
|
84
|
|
|
$data['signed_metadata'] = $this->sign($data); |
|
85
|
|
|
} |
|
86
|
|
|
$response = $this->responseFactory->createResponse(); |
|
87
|
|
|
$response->getBody()->write(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); |
|
88
|
|
|
$response = $response->withHeader('Content-Type', 'application/json; charset=UTF-8'); |
|
89
|
|
|
|
|
90
|
|
|
return $response; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param array $metadata |
|
95
|
|
|
* |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
private function sign(array $metadata): string |
|
99
|
|
|
{ |
|
100
|
|
|
$jsonConverter = new StandardConverter(); |
|
101
|
|
|
$header = [ |
|
102
|
|
|
'alg' => $this->signatureAlgorithm, |
|
103
|
|
|
]; |
|
104
|
|
|
$jws = $this->jwsBuilder |
|
105
|
|
|
->create() |
|
106
|
|
|
->withPayload($jsonConverter->encode($metadata)) |
|
107
|
|
|
->addSignature($this->signatureKey, $header) |
|
108
|
|
|
->build(); |
|
109
|
|
|
$serializer = new CompactSerializer($jsonConverter); |
|
110
|
|
|
$assertion = $serializer->serialize($jws, 0); |
|
111
|
|
|
|
|
112
|
|
|
return $assertion; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|