Skip to content

Commit

Permalink
Add setters for Key-classes, fix composer.json, CI-tweaks (#6)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Strobotti authored Mar 30, 2020
1 parent 6cb3549 commit b589a25
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ before_script:
- composer install

script:
- composer validate
- phpunit
- ./vendor/bin/php-cs-fixer fix -v --diff --dry-run --config .php_cs.php

after_success:
- bash <(curl -s https://codecov.io/bash)
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Basic steps:
1. Clone it locally
1. Install dependencies with Composer
```bash
composer install
$ composer install
```
1. Create a branch on your fork
1. Commit & push
Expand All @@ -31,11 +31,11 @@ Some guidelines:

1. Before committing make sure to format the code accordingly:
```bash
make php-cs-fixer-fix
$ make php-cs-fixer-fix
```
1. Also make sure the tests pass successfully and you have sufficient coverage
```bash
make test-unit
$ make test-unit
```

### Git Commit Messages
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ See [JSON Web Key RFC](https://tools.ietf.org/html/rfc7517) for reference.
This library requires PHP version 7.2 or higher and can be installed with composer:

```bash
composer require strobotti/php-jwk
$ composer require strobotti/php-jwk
```

## Example usage
Expand Down
21 changes: 15 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name": "strobotti/php-jwk",
"description": "A small PHP library to handle JWKs (Json Web Keys)",
"type": "library",
"keywords": ["jwk", "jwks"],
"homepage": "https://github.com/Strobotti/php-jwk",
"authors": [
{
"name": "Juha Jantunen",
"email": "[email protected]",
"homepage": "https://www.strobotti.com",
"role": "Developer"
}
],
Expand All @@ -18,16 +21,22 @@
},
"autoload": {
"psr-4": {
"Strobotti\\JWK\\": "src"
},
"autoload-dev": {
"psr-4": {
"Strobotti\\JWK\\Tests\\": "tests/"
}
"Strobotti\\JWK\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Strobotti\\JWK\\Tests\\": "tests/"
}
},
"require-dev": {
"phpunit/phpunit": "^8.0",
"friendsofphp/php-cs-fixer": "^2.16"
},
"scripts": {
"test": "./vendor/bin/phpunit"
},
"scripts-descriptions": {
"test": "Run all tests"
}
}
63 changes: 51 additions & 12 deletions src/Key/AbstractKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ abstract class AbstractKey implements KeyInterface
/**
* The key ID.
*
* @var string
* @var null|string
*/
private $kid;

/**
* The public key use.
*
* @var string
* @var null|string
*/
private $use;

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

/**
* {@inheritdoc}
*
* @since 1.0.0 Protected setter
* @since 1.2.0 Public setter
*/
public function setKeyType(string $kty): KeyInterface
{
$this->kty = $kty;

return $this;
}

/**
* {@inheritdoc}
*
Expand All @@ -61,6 +74,18 @@ public function getKeyType(): string
return $this->kty;
}

/**
* {@inheritdoc}
*
* @since 1.2.0
*/
public function setKeyId(?string $kid): KeyInterface
{
$this->kid = $kid;

return $this;
}

/**
* {@inheritdoc}
*
Expand All @@ -71,6 +96,18 @@ public function getKeyId(): ?string
return $this->kid;
}

/**
* {@inheritdoc}
*
* @since 1.2.0
*/
public function setPublicKeyUse(?string $use): KeyInterface
{
$this->use = $use;

return $this;
}

/**
* {@inheritdoc}
*
Expand All @@ -81,6 +118,18 @@ public function getPublicKeyUse(): ?string
return $this->use;
}

/**
* {@inheritdoc}
*
* @since 1.2.0
*/
public function setAlgorithm(string $alg): KeyInterface
{
$this->alg = $alg;

return $this;
}

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -140,14 +189,4 @@ public static function createFromJSON(string $json, KeyInterface $prototype = nu

return $instance;
}

/**
* @since 1.0.0
*/
protected function setKeyType(string $kty): self
{
$this->kty = $kty;

return $this;
}
}
53 changes: 53 additions & 0 deletions src/Key/KeyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,23 @@
*/
interface KeyInterface extends \JsonSerializable
{
/**
* @since 1.0.0
*/
public const KEY_TYPE_RSA = 'RSA';

/**
* @since 1.0.0
*
* @todo Model currently not implemented
*/
public const KEY_TYPE_OKP = 'OKP';

/**
* @since 1.0.0
*
* @todo Model currently not implemented
*/
public const KEY_TYPE_EC = 'EC';

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

/**
* Sets the key type, ie. the value for the `kty` field.
*
* See the KEY_TYPE_* constants for reference.
*
* @return KeyInterface
*
* @since 1.2.0
*/
public function setKeyType(string $kty): self;

/**
* Gets the key type, ie. the value of the `kty` field.
*
* @since 1.0.0
*/
public function getKeyType(): string;

/**
* Sets the key id, ie. the value of the `kid` field.
*
* @return KeyInterface
*
* @since 1.2.0
*/
public function setKeyId(?string $kid): self;

/**
* Gets the key id, ie. the value of the `kid` field.
*
* @since 1.0.0
*/
public function getKeyId(): ?string;

/**
* Sets the public key use, ie. the value of the `use` field.
*
* @return KeyInterface
*
* @since 1.2.0
*/
public function setPublicKeyUse(?string $use): self;

/**
* Gets the public key use, ie. the value of the `use` field.
*
* @since 1.0.0
*/
public function getPublicKeyUse(): ?string;

/**
* Sets the cryptographic algorithm used to sign the key, ie. the value of the `alg` field.
*
* @return KeyInterface
*
* @since 1.2.0
*/
public function setAlgorithm(string $alg): self;

/**
* Gets the cryptographic algorithm used to sign the key, ie. the value of the `alg` field.
*
Expand Down
26 changes: 25 additions & 1 deletion src/Key/Rsa.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public function __construct()
$this->setKeyType(KeyInterface::KEY_TYPE_RSA);
}

/**
* Sets the exponent for the RSA public key, ie. the `e` field.
*
* @since 1.2.0
*/
public function setExponent(string $e): self
{
$this->e = $e;

return $this;
}

/**
* Returns the exponent for the RSA public key.
*
Expand All @@ -46,7 +58,19 @@ public function getExponent(): string
}

/**
* Returns the modulus for the RSA public key.
* Sets the modulus for the RSA public key, ie. the `n` field.
*
* @since 1.2.0
*/
public function setModulus(string $n): KeyInterface
{
$this->n = $n;

return $this;
}

/**
* Returns the modulus for the RSA public key, ie. the `n`field.
*
* @since 1.0.0
*/
Expand Down
23 changes: 23 additions & 0 deletions tests/Key/AbstractKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\TestCase;
use Strobotti\JWK\Key\AbstractKey;
use Strobotti\JWK\Key\KeyInterface;

/**
* @internal
Expand All @@ -27,6 +28,28 @@ public function testCreateFromJSON(): void

static::assertSame($json, "{$key}");
}

public function testSettersAndGetters(): void
{
$key = new AbstractKeyTest__AbstractKey__Mock();
$key->setAlgorithm(KeyInterface::ALGORITHM_RS256)
->setPublicKeyUse(KeyInterface::PUBLIC_KEY_USE_SIGNATURE)
->setKeyType(KeyInterface::KEY_TYPE_RSA)
->setKeyId('asdf')
;

static::assertSame(KeyInterface::ALGORITHM_RS256, $key->getAlgorithm());
static::assertSame(KeyInterface::PUBLIC_KEY_USE_SIGNATURE, $key->getPublicKeyUse());
static::assertSame(KeyInterface::KEY_TYPE_RSA, $key->getKeyType());
static::assertSame('asdf', $key->getKeyId());

// Test nullable fields
$key->setKeyId(null);
$key->setPublicKeyUse(null);

static::assertNull($key->getKeyId());
static::assertNull($key->getPublicKeyUse());
}
}

final class AbstractKeyTest__AbstractKey__Mock extends AbstractKey
Expand Down
14 changes: 14 additions & 0 deletions tests/Key/RsaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,18 @@ public function testToString(): void

static::assertSame($json, "{$key}");
}

public function testSettersAndGetters(): void
{
$e = 'AQAB';
$n = 'iGaLqP6y-SJCCBq5Hv6pGDbG_SQ11MNjH7rWHcCFYz4hGwHC4lcSurTlV8u3avoVNM8jXevG1Iu1SY11qInqUvjJur--hghr1b56OPJu6H1iKulSxGjEIyDP6c5BdE1uwprYyr4IO9th8fOwCPygjLFrh44XEGbDIFeImwvBAGOhmMB2AD1n1KviyNsH0bEB7phQtiLk-ILjv1bORSRl8AK677-1T8isGfHKXGZ_ZGtStDe7Lu0Ihp8zoUt59kx2o9uWpROkzF56ypresiIl4WprClRCjz8x6cPZXU2qNWhu71TQvUFwvIvbkE1oYaJMb0jcOTmBRZA2QuYw-zHLwQ';

$key = new Rsa();
$key->setExponent($e)
->setModulus($n)
;

static::assertSame($e, $key->getExponent());
static::assertSame($n, $key->getModulus());
}
}

0 comments on commit b589a25

Please sign in to comment.