Skip to content

Commit b589a25

Browse files
authored
Add setters for Key-classes, fix composer.json, CI-tweaks (#6)
* Add setters for Key-classes, fix composer.json, CI-tweaks - Add setters for KeyInterface and implementing classes - Fix composer.json according to schema for publishing in Packagist - Add check for composer.json validity to Travis config - Add check for php-cs-fixer to Travis config
1 parent 6cb3549 commit b589a25

File tree

9 files changed

+187
-23
lines changed

9 files changed

+187
-23
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ before_script:
99
- composer install
1010

1111
script:
12+
- composer validate
1213
- phpunit
14+
- ./vendor/bin/php-cs-fixer fix -v --diff --dry-run --config .php_cs.php
1315

1416
after_success:
1517
- bash <(curl -s https://codecov.io/bash)

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Basic steps:
2020
1. Clone it locally
2121
1. Install dependencies with Composer
2222
```bash
23-
composer install
23+
$ composer install
2424
```
2525
1. Create a branch on your fork
2626
1. Commit & push
@@ -31,11 +31,11 @@ Some guidelines:
3131

3232
1. Before committing make sure to format the code accordingly:
3333
```bash
34-
make php-cs-fixer-fix
34+
$ make php-cs-fixer-fix
3535
```
3636
1. Also make sure the tests pass successfully and you have sufficient coverage
3737
```bash
38-
make test-unit
38+
$ make test-unit
3939
```
4040

4141
### Git Commit Messages

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ See [JSON Web Key RFC](https://tools.ietf.org/html/rfc7517) for reference.
1616
This library requires PHP version 7.2 or higher and can be installed with composer:
1717

1818
```bash
19-
composer require strobotti/php-jwk
19+
$ composer require strobotti/php-jwk
2020
```
2121

2222
## Example usage

composer.json

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
{
22
"name": "strobotti/php-jwk",
33
"description": "A small PHP library to handle JWKs (Json Web Keys)",
4+
"type": "library",
5+
"keywords": ["jwk", "jwks"],
46
"homepage": "https://github.com/Strobotti/php-jwk",
57
"authors": [
68
{
79
"name": "Juha Jantunen",
810
"email": "juha@strobotti.com",
11+
"homepage": "https://www.strobotti.com",
912
"role": "Developer"
1013
}
1114
],
@@ -18,16 +21,22 @@
1821
},
1922
"autoload": {
2023
"psr-4": {
21-
"Strobotti\\JWK\\": "src"
22-
},
23-
"autoload-dev": {
24-
"psr-4": {
25-
"Strobotti\\JWK\\Tests\\": "tests/"
26-
}
24+
"Strobotti\\JWK\\": "src/"
25+
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"Strobotti\\JWK\\Tests\\": "tests/"
2730
}
2831
},
2932
"require-dev": {
3033
"phpunit/phpunit": "^8.0",
3134
"friendsofphp/php-cs-fixer": "^2.16"
35+
},
36+
"scripts": {
37+
"test": "./vendor/bin/phpunit"
38+
},
39+
"scripts-descriptions": {
40+
"test": "Run all tests"
3241
}
3342
}

src/Key/AbstractKey.php

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ abstract class AbstractKey implements KeyInterface
2323
/**
2424
* The key ID.
2525
*
26-
* @var string
26+
* @var null|string
2727
*/
2828
private $kid;
2929

3030
/**
3131
* The public key use.
3232
*
33-
* @var string
33+
* @var null|string
3434
*/
3535
private $use;
3636

@@ -51,6 +51,19 @@ public function __toString()
5151
return \json_encode($this->jsonSerialize(), JSON_PRETTY_PRINT);
5252
}
5353

54+
/**
55+
* {@inheritdoc}
56+
*
57+
* @since 1.0.0 Protected setter
58+
* @since 1.2.0 Public setter
59+
*/
60+
public function setKeyType(string $kty): KeyInterface
61+
{
62+
$this->kty = $kty;
63+
64+
return $this;
65+
}
66+
5467
/**
5568
* {@inheritdoc}
5669
*
@@ -61,6 +74,18 @@ public function getKeyType(): string
6174
return $this->kty;
6275
}
6376

77+
/**
78+
* {@inheritdoc}
79+
*
80+
* @since 1.2.0
81+
*/
82+
public function setKeyId(?string $kid): KeyInterface
83+
{
84+
$this->kid = $kid;
85+
86+
return $this;
87+
}
88+
6489
/**
6590
* {@inheritdoc}
6691
*
@@ -71,6 +96,18 @@ public function getKeyId(): ?string
7196
return $this->kid;
7297
}
7398

99+
/**
100+
* {@inheritdoc}
101+
*
102+
* @since 1.2.0
103+
*/
104+
public function setPublicKeyUse(?string $use): KeyInterface
105+
{
106+
$this->use = $use;
107+
108+
return $this;
109+
}
110+
74111
/**
75112
* {@inheritdoc}
76113
*
@@ -81,6 +118,18 @@ public function getPublicKeyUse(): ?string
81118
return $this->use;
82119
}
83120

121+
/**
122+
* {@inheritdoc}
123+
*
124+
* @since 1.2.0
125+
*/
126+
public function setAlgorithm(string $alg): KeyInterface
127+
{
128+
$this->alg = $alg;
129+
130+
return $this;
131+
}
132+
84133
/**
85134
* {@inheritdoc}
86135
*
@@ -140,14 +189,4 @@ public static function createFromJSON(string $json, KeyInterface $prototype = nu
140189

141190
return $instance;
142191
}
143-
144-
/**
145-
* @since 1.0.0
146-
*/
147-
protected function setKeyType(string $kty): self
148-
{
149-
$this->kty = $kty;
150-
151-
return $this;
152-
}
153192
}

src/Key/KeyInterface.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,23 @@
1515
*/
1616
interface KeyInterface extends \JsonSerializable
1717
{
18+
/**
19+
* @since 1.0.0
20+
*/
1821
public const KEY_TYPE_RSA = 'RSA';
22+
23+
/**
24+
* @since 1.0.0
25+
*
26+
* @todo Model currently not implemented
27+
*/
1928
public const KEY_TYPE_OKP = 'OKP';
29+
30+
/**
31+
* @since 1.0.0
32+
*
33+
* @todo Model currently not implemented
34+
*/
2035
public const KEY_TYPE_EC = 'EC';
2136

2237
public const PUBLIC_KEY_USE_SIGNATURE = 'sig';
@@ -33,27 +48,65 @@ interface KeyInterface extends \JsonSerializable
3348
*/
3449
public function __toString();
3550

51+
/**
52+
* Sets the key type, ie. the value for the `kty` field.
53+
*
54+
* See the KEY_TYPE_* constants for reference.
55+
*
56+
* @return KeyInterface
57+
*
58+
* @since 1.2.0
59+
*/
60+
public function setKeyType(string $kty): self;
61+
3662
/**
3763
* Gets the key type, ie. the value of the `kty` field.
3864
*
3965
* @since 1.0.0
4066
*/
4167
public function getKeyType(): string;
4268

69+
/**
70+
* Sets the key id, ie. the value of the `kid` field.
71+
*
72+
* @return KeyInterface
73+
*
74+
* @since 1.2.0
75+
*/
76+
public function setKeyId(?string $kid): self;
77+
4378
/**
4479
* Gets the key id, ie. the value of the `kid` field.
4580
*
4681
* @since 1.0.0
4782
*/
4883
public function getKeyId(): ?string;
4984

85+
/**
86+
* Sets the public key use, ie. the value of the `use` field.
87+
*
88+
* @return KeyInterface
89+
*
90+
* @since 1.2.0
91+
*/
92+
public function setPublicKeyUse(?string $use): self;
93+
5094
/**
5195
* Gets the public key use, ie. the value of the `use` field.
5296
*
5397
* @since 1.0.0
5498
*/
5599
public function getPublicKeyUse(): ?string;
56100

101+
/**
102+
* Sets the cryptographic algorithm used to sign the key, ie. the value of the `alg` field.
103+
*
104+
* @return KeyInterface
105+
*
106+
* @since 1.2.0
107+
*/
108+
public function setAlgorithm(string $alg): self;
109+
57110
/**
58111
* Gets the cryptographic algorithm used to sign the key, ie. the value of the `alg` field.
59112
*

src/Key/Rsa.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ public function __construct()
3535
$this->setKeyType(KeyInterface::KEY_TYPE_RSA);
3636
}
3737

38+
/**
39+
* Sets the exponent for the RSA public key, ie. the `e` field.
40+
*
41+
* @since 1.2.0
42+
*/
43+
public function setExponent(string $e): self
44+
{
45+
$this->e = $e;
46+
47+
return $this;
48+
}
49+
3850
/**
3951
* Returns the exponent for the RSA public key.
4052
*
@@ -46,7 +58,19 @@ public function getExponent(): string
4658
}
4759

4860
/**
49-
* Returns the modulus for the RSA public key.
61+
* Sets the modulus for the RSA public key, ie. the `n` field.
62+
*
63+
* @since 1.2.0
64+
*/
65+
public function setModulus(string $n): KeyInterface
66+
{
67+
$this->n = $n;
68+
69+
return $this;
70+
}
71+
72+
/**
73+
* Returns the modulus for the RSA public key, ie. the `n`field.
5074
*
5175
* @since 1.0.0
5276
*/

tests/Key/AbstractKeyTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Strobotti\JWK\Key\AbstractKey;
9+
use Strobotti\JWK\Key\KeyInterface;
910

1011
/**
1112
* @internal
@@ -27,6 +28,28 @@ public function testCreateFromJSON(): void
2728

2829
static::assertSame($json, "{$key}");
2930
}
31+
32+
public function testSettersAndGetters(): void
33+
{
34+
$key = new AbstractKeyTest__AbstractKey__Mock();
35+
$key->setAlgorithm(KeyInterface::ALGORITHM_RS256)
36+
->setPublicKeyUse(KeyInterface::PUBLIC_KEY_USE_SIGNATURE)
37+
->setKeyType(KeyInterface::KEY_TYPE_RSA)
38+
->setKeyId('asdf')
39+
;
40+
41+
static::assertSame(KeyInterface::ALGORITHM_RS256, $key->getAlgorithm());
42+
static::assertSame(KeyInterface::PUBLIC_KEY_USE_SIGNATURE, $key->getPublicKeyUse());
43+
static::assertSame(KeyInterface::KEY_TYPE_RSA, $key->getKeyType());
44+
static::assertSame('asdf', $key->getKeyId());
45+
46+
// Test nullable fields
47+
$key->setKeyId(null);
48+
$key->setPublicKeyUse(null);
49+
50+
static::assertNull($key->getKeyId());
51+
static::assertNull($key->getPublicKeyUse());
52+
}
3053
}
3154

3255
final class AbstractKeyTest__AbstractKey__Mock extends AbstractKey

tests/Key/RsaTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,18 @@ public function testToString(): void
6969

7070
static::assertSame($json, "{$key}");
7171
}
72+
73+
public function testSettersAndGetters(): void
74+
{
75+
$e = 'AQAB';
76+
$n = 'iGaLqP6y-SJCCBq5Hv6pGDbG_SQ11MNjH7rWHcCFYz4hGwHC4lcSurTlV8u3avoVNM8jXevG1Iu1SY11qInqUvjJur--hghr1b56OPJu6H1iKulSxGjEIyDP6c5BdE1uwprYyr4IO9th8fOwCPygjLFrh44XEGbDIFeImwvBAGOhmMB2AD1n1KviyNsH0bEB7phQtiLk-ILjv1bORSRl8AK677-1T8isGfHKXGZ_ZGtStDe7Lu0Ihp8zoUt59kx2o9uWpROkzF56ypresiIl4WprClRCjz8x6cPZXU2qNWhu71TQvUFwvIvbkE1oYaJMb0jcOTmBRZA2QuYw-zHLwQ';
77+
78+
$key = new Rsa();
79+
$key->setExponent($e)
80+
->setModulus($n)
81+
;
82+
83+
static::assertSame($e, $key->getExponent());
84+
static::assertSame($n, $key->getModulus());
85+
}
7286
}

0 commit comments

Comments
 (0)