4
4
5
5
namespace bizley \jwt ;
6
6
7
- use Closure ;
8
7
use Lcobucci \JWT \Builder ;
9
8
use Lcobucci \JWT \ClaimsFormatter ;
10
9
use Lcobucci \JWT \Configuration ;
24
23
use function count ;
25
24
use function in_array ;
26
25
use function is_array ;
26
+ use function is_callable ;
27
27
use function is_string ;
28
28
use function reset ;
29
29
use function strpos ;
@@ -49,7 +49,7 @@ class Jwt extends Component
49
49
public const EDDSA = 'EdDSA ' ;
50
50
51
51
public const STORE_IN_MEMORY = 'in_memory ' ;
52
- public const STORE_LOCAL_FILE_REFERENCE = 'local_file_reference ' ;
52
+ public const STORE_LOCAL_FILE_REFERENCE = 'local_file_reference ' ; // deprecated since 3.2.0, will be removed in 4.0.0
53
53
54
54
public const METHOD_PLAIN = 'plain ' ;
55
55
public const METHOD_BASE64 = 'base64 ' ;
@@ -68,28 +68,28 @@ class Jwt extends Component
68
68
* This can be a simple string, an instance of Key, or a configuration array.
69
69
* The configuration takes the following array keys:
70
70
* - 'key' => Key's value or path to the key file.
71
- * - 'store' => Either `Jwt::STORE_IN_MEMORY` or `Jwt::STORE_LOCAL_FILE_REFERENCE` - whether to keep the key in
72
- * the memory or as a reference to a local file.
71
+ * - 'store' => Either `Jwt::STORE_IN_MEMORY` or `Jwt::STORE_LOCAL_FILE_REFERENCE` (deprecated) -
72
+ * whether to keep the key in the memory or as a reference to a local file.
73
73
* - 'method' => `Jwt::METHOD_PLAIN`, `Jwt::METHOD_BASE64`, or `Jwt::METHOD_FILE` - whether the key is a plain
74
74
* text, base64 encoded text, or a file.
75
- * In case the 'store' is set to `Jwt::STORE_LOCAL_FILE_REFERENCE`, only `Jwt::METHOD_FILE` method
76
- * is available.
75
+ * In case the 'store' is set to `Jwt::STORE_LOCAL_FILE_REFERENCE` (deprecated) , only
76
+ * `Jwt::METHOD_FILE` method is available.
77
77
* - 'passphrase' => Key's passphrase.
78
78
* In case a simple string is provided (and it does not start with 'file://' or '@') the following configuration
79
79
* is assumed:
80
80
* [
81
- * 'key' => // the original given value,
82
- * 'store' => Jwt::STORE_IN_MEMORY,
83
- * 'method' => Jwt::METHOD_PLAIN,
84
- * 'passphrase' => '',
81
+ * 'key' => // the original given value,
82
+ * 'store' => Jwt::STORE_IN_MEMORY,
83
+ * 'method' => Jwt::METHOD_PLAIN,
84
+ * 'passphrase' => '',
85
85
* ]
86
86
* In case a simple string is provided and it does start with 'file://' (direct file path) or '@' (Yii alias)
87
87
* the following configuration is assumed:
88
88
* [
89
- * 'key' => // the original given value,
90
- * 'store' => Jwt::STORE_IN_MEMORY,
91
- * 'method' => Jwt::METHOD_FILE,
92
- * 'passphrase' => '',
89
+ * 'key' => // the original given value,
90
+ * 'store' => Jwt::STORE_IN_MEMORY,
91
+ * 'method' => Jwt::METHOD_FILE,
92
+ * 'passphrase' => '',
93
93
* ]
94
94
* If you want to override the assumed configuration, you must provide it directly.
95
95
* @since 3.0.0
@@ -108,13 +108,13 @@ class Jwt extends Component
108
108
/**
109
109
* @var string|Signer|null Signer ID or Signer instance to be used for signing/verifying.
110
110
* See $signers for available values. In case it's not set, no algorithm will be used, which may be handy if you
111
- * want to do some testing but it's NOT recommended for production environments.
111
+ * want to do some testing, but it's NOT recommended for production environments.
112
112
* @since 3.0.0
113
113
*/
114
114
public $ signer ;
115
115
116
116
/**
117
- * @var array<string, array<mixed> > Default signers configuration. When instantiated it will use selected array to
117
+ * @var array<string, string[] > Default signers configuration. When instantiated it will use selected array to
118
118
* spread into `Yii::createObject($type, array $params = [])` method so the first array element is $type, and
119
119
* the second is $params.
120
120
* Since 3.0.0 configuration is done using arrays.
@@ -171,9 +171,9 @@ class Jwt extends Component
171
171
public $ decoder ;
172
172
173
173
/**
174
- * @var array<array<mixed>>|Validation\Constraint[]|Closure| null List of constraints that will be used to validate
175
- * against or an anonymous function that can be resolved as such list. The signature of the function should be
176
- * `function(\bizley\jwt\Jwt $jwt)` where $jwt will be an instance of this component.
174
+ * @var array<array<mixed>|(callable(): mixed)|string>|(callable(): mixed)| null List of constraints that
175
+ * will be used to validate against or an anonymous function that can be resolved as such list. The signature of
176
+ * the function should be `function(\bizley\jwt\Jwt $jwt)` where $jwt will be an instance of this component.
177
177
* For the constraints you can use instances of Lcobucci\JWT\Validation\Constraint or configuration arrays to be
178
178
* resolved as such.
179
179
* @since 3.0.0
@@ -218,7 +218,7 @@ public function init(): void
218
218
}
219
219
220
220
/**
221
- * @param array<mixed> $config
221
+ * @param array<array< mixed>|(callable(): mixed)|string > $config
222
222
* @return object
223
223
* @throws InvalidConfigException
224
224
*/
@@ -325,6 +325,9 @@ private function prepareKey($key): Signer\Key
325
325
}
326
326
327
327
if (is_string ($ key )) {
328
+ if ($ key === '' ) {
329
+ throw new InvalidConfigException ('Empty string used as a key configuration! ' );
330
+ }
328
331
if (strpos ($ key , '@ ' ) === 0 ) {
329
332
$ keyConfig = [
330
333
self ::KEY => Yii::getAlias ($ key ),
@@ -434,8 +437,9 @@ private function prepareValidationConstraints(): array
434
437
return $ constraints ;
435
438
}
436
439
437
- if ($ this ->validationConstraints instanceof Closure) {
438
- return ($ this ->validationConstraints )($ this );
440
+ if (is_callable ($ this ->validationConstraints )) {
441
+ /** @phpstan-ignore-next-line */
442
+ return call_user_func ($ this ->validationConstraints , $ this );
439
443
}
440
444
441
445
return [];
0 commit comments