Skip to content

Commit 3beeb8b

Browse files
authored
Merge pull request Strobotti#2 from Strobotti/formatting
Formatting code and other tweaks
2 parents 2073499 + 04bed66 commit 3beeb8b

17 files changed

+186
-123
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
.idea
2+
.DS_Store
23
.phpunit.result.cache
34
composer.lock
45
vendor
56
coverage.xml
7+
clover.xml
8+
.php_cs.cache

.php_cs.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
return \PhpCsFixer\Config::create()
4+
->setFinder(\PhpCsFixer\Finder::create()->in(__DIR__ . '/src'))
5+
->setRiskyAllowed(true)
6+
->setRules([
7+
'@PhpCsFixer' => true,
8+
'@PhpCsFixer:risky' => true,
9+
'@PHP71Migration' => true,
10+
'@PHP71Migration:risky' => true,
11+
'@DoctrineAnnotation' => true,
12+
'@PHPUnit60Migration:risky' => true,
13+
'native_function_invocation' => true,
14+
'header_comment' => ['header' => ''],
15+
'method_chaining_indentation' => false,
16+
'php_unit_test_class_requires_covers' => false,
17+
])
18+
;

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ language: php
33
php:
44
- 7.2
55
- 7.3
6-
7-
sudo: false
6+
- 7.4
87

98
before_script:
109
- composer install

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
test-unit:
2+
./vendor/bin/phpunit
3+
4+
php-cs-fixer:
5+
./vendor/bin/php-cs-fixer fix --show-progress dots --dry-run --config .php_cs.php
6+
7+
php-cs-fixer-fix:
8+
./vendor/bin/php-cs-fixer fix --show-progress dots --config .php_cs.php

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# php-jwk
22

3+
[![codecov](https://codecov.io/gh/Strobotti/php-jwk/branch/master/graph/badge.svg)](https://codecov.io/gh/Strobotti/php-jwk)
4+
35
A small PHP library to handle JWKs (Json Web Keys)
46

57
This library helps to create json web key sets from PEM and is also able to pull out PEMs from json web key sets.
@@ -18,7 +20,7 @@ composer require strobotti/php-jwk
1820

1921
## Example usage
2022

21-
See full example [here](blob/master/examples/full-flow.php).
23+
See full example [here](examples/full-flow.php).
2224

2325
### Create a key-object from PEM
2426

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"license": "MIT",
1313
"require": {
1414
"php": ">=7.2.0",
15+
"ext-json": "*",
16+
"ext-openssl": "*",
1517
"phpseclib/phpseclib": "^2.0"
1618
},
1719
"autoload": {
@@ -25,6 +27,7 @@
2527
}
2628
},
2729
"require-dev": {
28-
"phpunit/phpunit": "^8.0"
30+
"phpunit/phpunit": "^8.0",
31+
"friendsofphp/php-cs-fixer": "^2.16"
2932
}
3033
}

src/Key/AbstractKey.php

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
namespace Strobotti\JWK\Key;
66

77
/**
8-
* @package Strobotti\JWK
98
* @author Juha Jantunen <[email protected]>
109
* @license https://opensource.org/licenses/MIT MIT
11-
* @link https://github.com/Strobotti/php-jwk
10+
*
11+
* @see https://github.com/Strobotti/php-jwk
12+
* @since 1.0.0
1213
*/
1314
abstract class AbstractKey implements KeyInterface
1415
{
@@ -40,8 +41,20 @@ abstract class AbstractKey implements KeyInterface
4041
*/
4142
private $alg;
4243

44+
/**
45+
* @since 1.0.0
46+
*
47+
* @return false|string
48+
*/
49+
public function __toString()
50+
{
51+
return \json_encode($this->jsonSerialize(), JSON_PRETTY_PRINT);
52+
}
53+
4354
/**
4455
* {@inheritdoc}
56+
*
57+
* @since 1.0.0
4558
*/
4659
public function getKeyType(): string
4760
{
@@ -50,6 +63,8 @@ public function getKeyType(): string
5063

5164
/**
5265
* {@inheritdoc}
66+
*
67+
* @since 1.0.0
5368
*/
5469
public function getKeyId(): string
5570
{
@@ -58,6 +73,8 @@ public function getKeyId(): string
5873

5974
/**
6075
* {@inheritdoc}
76+
*
77+
* @since 1.0.0
6178
*/
6279
public function getPublicKeyUse(): string
6380
{
@@ -66,26 +83,18 @@ public function getPublicKeyUse(): string
6683

6784
/**
6885
* {@inheritdoc}
86+
*
87+
* @since 1.0.0
6988
*/
7089
public function getAlgorithm(): string
7190
{
7291
return $this->alg;
7392
}
7493

7594
/**
76-
* @param string $kty
95+
* Returns an array presentation of the key.
7796
*
78-
* @return self
79-
*/
80-
protected function setKeyType(string $kty): self
81-
{
82-
$this->kty = $kty;
83-
84-
return $this;
85-
}
86-
87-
/**
88-
* Returns an array presentation of the key
97+
* @since 1.0.0
8998
*
9099
* @return array An assoc to be passed to json_encode
91100
*/
@@ -105,10 +114,7 @@ public function jsonSerialize(): array
105114
}
106115

107116
/**
108-
* @param string $json
109-
* @param KeyInterface|null $prototype
110-
*
111-
* @return KeyInterface
117+
* @since 1.0.0
112118
*/
113119
public static function createFromJSON(string $json, KeyInterface $prototype = null): KeyInterface
114120
{
@@ -136,10 +142,12 @@ public static function createFromJSON(string $json, KeyInterface $prototype = nu
136142
}
137143

138144
/**
139-
* @return false|string
145+
* @since 1.0.0
140146
*/
141-
public function __toString()
147+
protected function setKeyType(string $kty): self
142148
{
143-
return json_encode($this->jsonSerialize(), JSON_PRETTY_PRINT);
149+
$this->kty = $kty;
150+
151+
return $this;
144152
}
145153
}

src/Key/KeyInterface.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
namespace Strobotti\JWK\Key;
66

77
/**
8-
* @package Strobotti\JWK
98
* @author Juha Jantunen <[email protected]>
109
* @license https://opensource.org/licenses/MIT MIT
11-
* @link https://github.com/Strobotti/php-jwk
10+
*
11+
* @see https://github.com/Strobotti/php-jwk
12+
* @since 1.0.0
1213
*
1314
* @method array jsonSerialize()
1415
*/
@@ -24,35 +25,39 @@ interface KeyInterface extends \JsonSerializable
2425
public const ALGORITHM_RS256 = 'RS256';
2526

2627
/**
27-
* Gets the key type, ie. the value of the `kty` field
28+
* Converts this key to a string.
29+
*
30+
* @since 1.0.0
2831
*
29-
* @return string
32+
* @return bool|string
33+
*/
34+
public function __toString();
35+
36+
/**
37+
* Gets the key type, ie. the value of the `kty` field.
38+
*
39+
* @since 1.0.0
3040
*/
3141
public function getKeyType(): string;
3242

3343
/**
34-
* Gets the key id, ie. the value of the `kid` field
44+
* Gets the key id, ie. the value of the `kid` field.
3545
*
36-
* @return null|string
46+
* @since 1.0.0
3747
*/
3848
public function getKeyId(): ?string;
3949

4050
/**
41-
* Gets the public key use, ie. the value of the `use` field
51+
* Gets the public key use, ie. the value of the `use` field.
4252
*
43-
* @return string
53+
* @since 1.0.0
4454
*/
4555
public function getPublicKeyUse(): string;
4656

4757
/**
48-
* Gets the cryptographic algorithm used to sign the key, ie. the value of the `alg` field
58+
* Gets the cryptographic algorithm used to sign the key, ie. the value of the `alg` field.
4959
*
50-
* @return string
60+
* @since 1.0.0
5161
*/
5262
public function getAlgorithm(): string;
53-
54-
/**
55-
* @return string|bool
56-
*/
57-
public function __toString();
5863
}

src/Key/Rsa.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
namespace Strobotti\JWK\Key;
66

77
/**
8-
* @package Strobotti\JWK
98
* @author Juha Jantunen <[email protected]>
109
* @license https://opensource.org/licenses/MIT MIT
11-
* @link https://github.com/Strobotti/php-jwk
10+
*
11+
* @see https://github.com/Strobotti/php-jwk
12+
* @since 1.0.0
1213
*/
1314
class Rsa extends AbstractKey
1415
{
@@ -37,7 +38,7 @@ public function __construct()
3738
/**
3839
* Returns the exponent for the RSA public key.
3940
*
40-
* @return string
41+
* @since 1.0.0
4142
*/
4243
public function getExponent(): string
4344
{
@@ -47,7 +48,7 @@ public function getExponent(): string
4748
/**
4849
* Returns the modulus for the RSA public key.
4950
*
50-
* @return string
51+
* @since 1.0.0
5152
*/
5253
public function getModulus(): string
5354
{
@@ -56,6 +57,8 @@ public function getModulus(): string
5657

5758
/**
5859
* {@inheritdoc}
60+
*
61+
* @since 1.0.0
5962
*/
6063
public function jsonSerialize(): array
6164
{
@@ -69,11 +72,13 @@ public function jsonSerialize(): array
6972
/**
7073
* {@inheritdoc}
7174
*
75+
* @since 1.0.0
76+
*
7277
* @return self
7378
*/
7479
public static function createFromJSON(string $json, KeyInterface $prototype = null): KeyInterface
7580
{
76-
if (!$prototype instanceof Rsa) {
81+
if (!$prototype instanceof self) {
7782
$prototype = new static();
7883
}
7984

src/KeyConverter.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
use phpseclib\Crypt\RSA;
88
use phpseclib\Math\BigInteger;
99
use Strobotti\JWK\Key\KeyInterface;
10+
use Strobotti\JWK\Key\Rsa as RsaKey;
1011
use Strobotti\JWK\Util\Base64UrlConverter;
1112
use Strobotti\JWK\Util\Base64UrlConverterInterface;
1213

1314
/**
14-
* @package Strobotti\JWK
1515
* @author Juha Jantunen <[email protected]>
1616
* @license https://opensource.org/licenses/MIT MIT
17-
* @link https://github.com/Strobotti/php-jwk
17+
*
18+
* @see https://github.com/Strobotti/php-jwk
19+
* @since 1.0.0
1820
*/
1921
class KeyConverter
2022
{
@@ -32,18 +34,15 @@ public function __construct()
3234
}
3335

3436
/**
35-
* @param KeyInterface $key
36-
*
37-
* @return string
37+
* @since 1.0.0
3838
*/
3939
public function keyToPem(KeyInterface $key): string
4040
{
41-
if (!$key instanceof \Strobotti\JWK\Key\Rsa) {
41+
if (!$key instanceof RsaKey) {
4242
throw new \InvalidArgumentException();
4343
}
4444

45-
/** @var \Strobotti\JWK\Key\Rsa $key */
46-
45+
/** @var RsaKey $key */
4746
$rsa = new RSA();
4847

4948
$modulus = $this->base64UrlConverter->decode($key->getModulus(), true);

0 commit comments

Comments
 (0)