forked from Spomky-Labs/jose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JWTLoader.php
150 lines (128 loc) · 4.65 KB
/
JWTLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2017 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Jose;
use Assert\Assertion;
final class JWTLoader implements JWTLoaderInterface
{
/**
* @var \Jose\LoaderInterface
*/
private $loader;
/**
* @var \Jose\Checker\CheckerManagerInterface
*/
private $checker_manager;
/**
* @var \Jose\DecrypterInterface|null
*/
private $decrypter = null;
/**
* @var \Jose\VerifierInterface
*/
private $verifier;
/**
* JWTLoader constructor.
*
* @param \Jose\Checker\CheckerManagerInterface $checker_manager
* @param \Jose\VerifierInterface $verifier
*/
public function __construct(Checker\CheckerManagerInterface $checker_manager, VerifierInterface $verifier)
{
$this->checker_manager = $checker_manager;
$this->verifier = $verifier;
$this->loader = new Loader();
}
/**
* {@inheritdoc}
*/
public function enableDecryptionSupport(DecrypterInterface $decrypter)
{
$this->decrypter = $decrypter;
}
/**
* {@inheritdoc}
*/
public function getSupportedSignatureAlgorithms()
{
return $this->verifier->getSupportedSignatureAlgorithms();
}
/**
* @return bool
*/
public function isDecryptionSupportEnabled()
{
return null !== $this->decrypter;
}
/**
* {@inheritdoc}
*/
public function getSupportedKeyEncryptionAlgorithms()
{
return false === $this->isDecryptionSupportEnabled() ? [] : $this->decrypter->getSupportedKeyEncryptionAlgorithms();
}
/**
* {@inheritdoc}
*/
public function getSupportedContentEncryptionAlgorithms()
{
return false === $this->isDecryptionSupportEnabled() ? [] : $this->decrypter->getSupportedContentEncryptionAlgorithms();
}
/**
* {@inheritdoc}
*/
public function getSupportedCompressionMethods()
{
return false === $this->isDecryptionSupportEnabled() ? [] : $this->decrypter->getSupportedCompressionMethods();
}
/**
* {@inheritdoc}
*/
public function load($assertion, Object\JWKSetInterface $encryption_key_set = null, $is_encryption_required = false)
{
Assertion::string($assertion);
Assertion::boolean($is_encryption_required);
$jwt = $this->loader->load($assertion);
if ($jwt instanceof Object\JWEInterface) {
Assertion::notNull($encryption_key_set, 'Encryption key set is not available.');
Assertion::true($this->isDecryptionSupportEnabled(), 'Encryption support is not enabled.');
Assertion::inArray($jwt->getSharedProtectedHeader('alg'), $this->getSupportedKeyEncryptionAlgorithms(), sprintf('The key encryption algorithm "%s" is not allowed.', $jwt->getSharedProtectedHeader('alg')));
Assertion::inArray($jwt->getSharedProtectedHeader('enc'), $this->getSupportedContentEncryptionAlgorithms(), sprintf('The content encryption algorithm "%s" is not allowed or not supported.', $jwt->getSharedProtectedHeader('enc')));
$jwt = $this->decryptAssertion($jwt, $encryption_key_set);
} elseif (true === $is_encryption_required) {
throw new \InvalidArgumentException('The assertion must be encrypted.');
}
return $jwt;
}
/**
* {@inheritdoc}
*/
public function verify(Object\JWSInterface $jws, Object\JWKSetInterface $signature_key_set, $detached_payload = null)
{
Assertion::inArray($jws->getSignature(0)->getProtectedHeader('alg'), $this->getSupportedSignatureAlgorithms(), sprintf('The signature algorithm "%s" is not supported or not allowed.', $jws->getSignature(0)->getProtectedHeader('alg')));
$index = null;
$this->verifier->verifyWithKeySet($jws, $signature_key_set, $detached_payload, $index);
Assertion::notNull($index, 'JWS signature(s) verification failed.');
$this->checker_manager->checkJWS($jws, $index);
return $index;
}
/**
* @param \Jose\Object\JWEInterface $jwe
* @param \Jose\Object\JWKSetInterface $encryption_key_set
*
* @return \Jose\Object\JWSInterface
*/
private function decryptAssertion(Object\JWEInterface $jwe, Object\JWKSetInterface $encryption_key_set)
{
$this->decrypter->decryptUsingKeySet($jwe, $encryption_key_set);
$jws = $this->loader->load($jwe->getPayload());
Assertion::isInstanceOf($jws, Object\JWSInterface::class, 'The encrypted assertion does not contain a JWS.');
return $jws;
}
}