44
55namespace bizley \jwt ;
66
7- use Closure ;
87use Lcobucci \JWT \Builder ;
98use Lcobucci \JWT \ClaimsFormatter ;
109use Lcobucci \JWT \Configuration ;
2423use function count ;
2524use function in_array ;
2625use function is_array ;
26+ use function is_callable ;
2727use function is_string ;
2828use function reset ;
2929use function strpos ;
@@ -49,7 +49,7 @@ class Jwt extends Component
4949 public const EDDSA = 'EdDSA ' ;
5050
5151 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
5353
5454 public const METHOD_PLAIN = 'plain ' ;
5555 public const METHOD_BASE64 = 'base64 ' ;
@@ -68,28 +68,28 @@ class Jwt extends Component
6868 * This can be a simple string, an instance of Key, or a configuration array.
6969 * The configuration takes the following array keys:
7070 * - '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.
7373 * - 'method' => `Jwt::METHOD_PLAIN`, `Jwt::METHOD_BASE64`, or `Jwt::METHOD_FILE` - whether the key is a plain
7474 * 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.
7777 * - 'passphrase' => Key's passphrase.
7878 * In case a simple string is provided (and it does not start with 'file://' or '@') the following configuration
7979 * is assumed:
8080 * [
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' => '',
8585 * ]
8686 * In case a simple string is provided and it does start with 'file://' (direct file path) or '@' (Yii alias)
8787 * the following configuration is assumed:
8888 * [
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' => '',
9393 * ]
9494 * If you want to override the assumed configuration, you must provide it directly.
9595 * @since 3.0.0
@@ -108,13 +108,13 @@ class Jwt extends Component
108108 /**
109109 * @var string|Signer|null Signer ID or Signer instance to be used for signing/verifying.
110110 * 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.
112112 * @since 3.0.0
113113 */
114114 public $ signer ;
115115
116116 /**
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
118118 * spread into `Yii::createObject($type, array $params = [])` method so the first array element is $type, and
119119 * the second is $params.
120120 * Since 3.0.0 configuration is done using arrays.
@@ -171,9 +171,9 @@ class Jwt extends Component
171171 public $ decoder ;
172172
173173 /**
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.
177177 * For the constraints you can use instances of Lcobucci\JWT\Validation\Constraint or configuration arrays to be
178178 * resolved as such.
179179 * @since 3.0.0
@@ -218,7 +218,7 @@ public function init(): void
218218 }
219219
220220 /**
221- * @param array<mixed> $config
221+ * @param array<array< mixed>|(callable(): mixed)|string > $config
222222 * @return object
223223 * @throws InvalidConfigException
224224 */
@@ -325,6 +325,9 @@ private function prepareKey($key): Signer\Key
325325 }
326326
327327 if (is_string ($ key )) {
328+ if ($ key === '' ) {
329+ throw new InvalidConfigException ('Empty string used as a key configuration! ' );
330+ }
328331 if (strpos ($ key , '@ ' ) === 0 ) {
329332 $ keyConfig = [
330333 self ::KEY => Yii::getAlias ($ key ),
@@ -434,8 +437,9 @@ private function prepareValidationConstraints(): array
434437 return $ constraints ;
435438 }
436439
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 );
439443 }
440444
441445 return [];
0 commit comments