Skip to content

Commit

Permalink
Merge branch 'v4.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubboucek committed Feb 9, 2023
2 parents 78f325c + bb05dbc commit 8e5ed05
Show file tree
Hide file tree
Showing 18 changed files with 359 additions and 99 deletions.
25 changes: 17 additions & 8 deletions .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ name: Code Analysis
on:
pull_request:
push:
branches:
- master


jobs:
Expand All @@ -23,10 +21,20 @@ jobs:
php:
- "7.4"
- "8.0"
- "8.1"
- "8.2"

name: ${{ matrix.actions.name }} on PHP ${{ matrix.php }}
runs-on: ubuntu-latest
jwt:
- name: JWT 5
key: jtw5
arg: '"firebase/php-jwt:^5.0"'

- name: JWT 6
key: jwt6
arg: '"firebase/php-jwt:^6.0"'

name: ${{ matrix.actions.name }} on PHP ${{ matrix.php }} with ${{ matrix.jwt.name }}
runs-on: ubuntu-latest

steps:
- name: Checkout
Expand All @@ -53,18 +61,19 @@ jobs:
with:
path: |
${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-data-${{ hashFiles('composer.json') }}-php-${{ matrix.php }}
key: ${{ runner.os }}-composer-data-${{ hashFiles('composer.json') }}-php-${{ matrix.php }}-${{ matrix.jwt.name }}


- uses: actions/cache@v2
with:
path: |
**/composer.lock
key: ${{ runner.os }}-composer-lock-${{ hashFiles('composer.json') }}-php-${{ matrix.php }}
key: ${{ runner.os }}-composer-lock-${{ hashFiles('composer.json') }}-php-${{ matrix.php }}-${{ matrix.jwt.key }}


- name: Install Composer
run: composer install --no-progress
run: composer update --no-progress --with ${{ matrix.jwt.arg }}


- run: ${{ matrix.actions.run }}
- name: Run job
run: ${{ matrix.actions.run }}
7 changes: 3 additions & 4 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php
/**
* The MIT License (MIT)
* Copyright (c) 2022 Redbit s.r.o., Jakub Bouček
* Copyright (c) 2023 Redbit s.r.o., Jakub Bouček
*/

namespace PHPSTORM_META {

expectedArguments(
\Redbitcz\DebugMode\Detector::__construct(),
0,
Expand All @@ -29,7 +28,7 @@
);

expectedArguments(
\Redbitcz\DebugMode\Detector::detectProductionMode(),
\Redbitcz\DebugMode\Detector::detectProduction(),
0,
\Redbitcz\DebugMode\Detector::MODE_FULL,
\Redbitcz\DebugMode\Detector::MODE_SIMPLE,
Expand All @@ -40,7 +39,7 @@
);

expectedArguments(
\Redbitcz\DebugMode\Plugin\SignedUrl::__construct(),
\Redbitcz\DebugMode\Plugin\SignedUrl::create(),
1,
'ES384',
'ES256',
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2022 Redbit s.r.o., Jakub Bouček
Copyright (c) 2023 Redbit s.r.o., Jakub Bouček

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ Package is optimized for invoking in very early lifecycle phase of your App
## Requirements
Package requires:

- PHP version at least 7.4
- PHP version 7.4, 8.0, 8.1 or 8.2

Enabler requires:

- Temporary directory with writable access

SignUrl plugin requires:

- [Firebase JWT](https://github.com/firebase/php-jwt) v5 or v6

## Installation
```shell
composer require redbitcz/debug-mode-enabler
Expand Down Expand Up @@ -165,7 +169,7 @@ $detector->isDebugMode(); // <---- this invoke all Plugins
`SignUrl` plugin provide secure way to share link with activated Debug Mode.

```php
$plugin = new \Redbitcz\DebugMode\Plugin\SignedUrl('secretkey', 'HS256', 'https://myapp.cz');
$plugin = \Redbitcz\DebugMode\Plugin\SignedUrl::create('secretkey', 'HS256', 'https://myapp.cz');
$detector->appendPlugin($plugin);
$signedUrl = $plugin->signUrl('https://myapp.cz/failingPage', '+1 hour');
Expand Down
25 changes: 16 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"name": "redbitcz/debug-mode-enabler",
"description": "Debug mode enabler - safe and clean way to manage Debug Mode in your App",
"keywords": ["debug"],
"license": ["MIT"],
"homepage": "https://github.com/redbitcz/php-debug-mode-enabler",
"license": [
"MIT"
],
"keywords": [
"debug"
],
"authors": [
{
"name": "Redbit s.r.o.",
Expand All @@ -14,18 +17,19 @@
"homepage": "https://www.jakub-boucek.cz/"
}
],
"homepage": "https://github.com/redbitcz/php-debug-mode-enabler",
"require": {
"php": ">=7.4",
"php": "~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0",
"ext-json": "*",
"nette/utils": "^3.0"
},
"require-dev": {
"firebase/php-jwt": "^5.0",
"nette/tester": "^2.4",
"phpstan/phpstan": "^0.12.98"
"firebase/php-jwt": "^5.0 || ^6.0",
"nette/tester": "2.4.3",
"phpstan/phpstan": "1.9.14"
},
"suggest": {
"firebase/php-jwt": "Optional, required for SignedUrl plugin"
"firebase/php-jwt": "Optional, required for SignedUrl plugin, compatible with version 5.x and 6.x"
},
"autoload": {
"psr-4": {
Expand All @@ -37,8 +41,11 @@
"Redbitcz\\DebugModeTests\\": "tests/"
}
},
"config": {
"sort-packages": true
},
"scripts": {
"phpstan": "phpstan analyze src -c phpstan.neon --level 8",
"phpstan": "phpstan analyze -c phpstan.neon --level 5",
"tester": "tester tests"
}
}
22 changes: 21 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
parameters:
paths:
- src
excludePaths:
analyse:
- src/Plugin/JWT/*

ignoreErrors:
-
- # Too variants of parameters signature between PHP versions
message: '#Parameter \#3 \$options of function setcookie expects .+#'
reportUnmatched: false
path: src/Enabler.php
count: 2

- # Weird bug by PhpStan - the `path` fiealt is still nullable
message: '#Offset ''path'' on array.+ on left side of \?\? always exists and is not nullable\.#'
path: src/Plugin/SignedUrl.php
count: 1

-
message: '#.+ has unknown class (OpenSSLAsymmetricKey|OpenSSLCertificate) as its type.#' # PHP 7 compatibility
reportUnmatched: false

-
message: '#.+ has invalid type (OpenSSLAsymmetricKey|OpenSSLCertificate).#' # PHP 7 compatibility
reportUnmatched: false
6 changes: 3 additions & 3 deletions src/Detector.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* The MIT License (MIT)
* Copyright (c) 2022 Redbit s.r.o., Jakub Bouček
* Copyright (c) 2023 Redbit s.r.o., Jakub Bouček
*/

declare(strict_types=1);
Expand Down Expand Up @@ -254,10 +254,10 @@ public static function detect(
* @param string|null $tempDir Path to temp directory. Optional, but required when Enabler mode is enabled
* @param bool|null $default Default value when no method matches
*/
public static function detectProductionMode(
public static function detectProduction(
int $mode = self::MODE_SIMPLE,
?string $tempDir = null,
?bool $default = false
?bool $default = true
): ?bool {
if (is_bool($default)) {
$default = !$default;
Expand Down
2 changes: 1 addition & 1 deletion src/Enabler.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* The MIT License (MIT)
* Copyright (c) 2022 Redbit s.r.o., Jakub Bouček
* Copyright (c) 2023 Redbit s.r.o., Jakub Bouček
*/

declare(strict_types=1);
Expand Down
2 changes: 1 addition & 1 deletion src/InconsistentEnablerModeException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* The MIT License (MIT)
* Copyright (c) 2022 Redbit s.r.o., Jakub Bouček
* Copyright (c) 2023 Redbit s.r.o., Jakub Bouček
*/

declare(strict_types=1);
Expand Down
67 changes: 67 additions & 0 deletions src/Plugin/JWT/JWTFirebaseV5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* The MIT License (MIT)
* Copyright (c) 2023 Redbit s.r.o., Jakub Bouček
*
* @noinspection PhpUndefinedClassInspection OpenSSL only optional dependency
*/

declare(strict_types=1);

namespace Redbitcz\DebugMode\Plugin\JWT;

use Firebase\JWT\JWT;
use ReflectionMethod;
use stdClass;

class JWTFirebaseV5 implements JWTImpl
{
/** @var OpenSSLAsymmetricKey|OpenSSLCertificate|resource|string */
protected $key;
protected string $algorithm;

/**
* @param OpenSSLAsymmetricKey|OpenSSLCertificate|resource|string $key
* @param string $algorithm
*/
public function __construct($key, string $algorithm = 'HS256')
{
$this->key = $key;
$this->algorithm = $algorithm;
}

public static function isAvailable(): bool
{
if (class_exists(JWT::class) === false) {
return false;
}

$params = (new ReflectionMethod(JWT::class, 'decode'))->getParameters();

// JWT v5 has second parameter named `$key`
if ($params[1]->getName() === 'key') {
return true;
}

// JWT v5.5.0 already second parameter named `$keyOrKeyArray`, detect by third param (future compatibility)
return $params[1]->getName() === 'keyOrKeyArray'
&& isset($params[2])
&& $params[2]->getName() === 'allowed_algs';
}

public function decode(string $jwt): stdClass
{
return JWT::decode($jwt, $this->key, [$this->algorithm]);
}

public function encode(array $payload): string
{
return JWT::encode($payload, $this->key, $this->algorithm);
}

public function setTimestamp(?int $timestamp): void
{
JWT::$timestamp = $timestamp;
}
}
44 changes: 44 additions & 0 deletions src/Plugin/JWT/JWTFirebaseV6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* The MIT License (MIT)
* Copyright (c) 2023 Redbit s.r.o., Jakub Bouček
*
* @noinspection PhpUndefinedClassInspection Library support JWT 5.0
*/

declare(strict_types=1);

namespace Redbitcz\DebugMode\Plugin\JWT;

use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use ReflectionMethod;
use stdClass;

class JWTFirebaseV6 extends JWTFirebaseV5
{
public static function isAvailable(): bool
{
if (class_exists(JWT::class) === false) {
return false;
}

$params = (new ReflectionMethod(JWT::class, 'decode'))->getParameters();

// JWT v6 has always second parameter named `$keyOrKeyArray`
if ($params[1]->getName() !== 'keyOrKeyArray') {
return false;
}

// JWT v5.5.0 already second parameter named `$keyOrKeyArray`, detect by third param (future compatibility)
return isset($params[2]) === false || $params[2]->getName() !== 'allowed_algs';
}

public function decode(string $jwt): stdClass
{
return JWT::decode($jwt, new Key($this->key, $this->algorithm));
}


}
23 changes: 23 additions & 0 deletions src/Plugin/JWT/JWTImpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* The MIT License (MIT)
* Copyright (c) 2023 Redbit s.r.o., Jakub Bouček
*/

declare(strict_types=1);

namespace Redbitcz\DebugMode\Plugin\JWT;

use stdClass;

interface JWTImpl
{
public static function isAvailable(): bool;

public function decode(string $jwt): stdClass;

public function encode(array $payload): string;

public function setTimestamp(?int $timestamp): void;
}
2 changes: 1 addition & 1 deletion src/Plugin/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* The MIT License (MIT)
* Copyright (c) 2022 Redbit s.r.o., Jakub Bouček
* Copyright (c) 2023 Redbit s.r.o., Jakub Bouček
*/

declare(strict_types=1);
Expand Down
Loading

0 comments on commit 8e5ed05

Please sign in to comment.