Skip to content

Commit

Permalink
Merge pull request #2 from therealgambo/jwt-plugin
Browse files Browse the repository at this point in the history
Add wrapper class for JWT plugin
  • Loading branch information
therealgambo authored May 23, 2017
2 parents 136daac + 2a5fe2d commit 4d0b040
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/Kong/Apis/Plugins/Jwt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace TheRealGambo\Kong\Apis\Plugins;

use TheRealGambo\Kong\Apis\AbstractApi;
use TheRealGambo\Kong\Apis\Consumer;

final class Jwt extends AbstractApi implements JwtInterface
{
/**
* Create a new consumer in Kong
*
* @see https://getkong.org/plugins/jwt/#create-a-consumer
*
* @param array $body
* @param array $headers
*
* @return array|\stdClass
*/
public function createConsumer(array $body = [], array $headers = [])
{
$consumer = new Consumer($this->url, $this->port);

return $consumer->add($body, $headers);
}

/**
* Create a new JWT credential for a consumer
*
* @see https://getkong.org/plugins/jwt/#create-a-jwt-credential
*
* @param string $identifier
* @param array $body
* @param array $headers
*
* @return array|\stdClass
*/
public function create($identifier, array $body = [], array $headers = [])
{
$this->setAllowedOptions(['key', 'algorithm', 'rsa_public_key', 'secret']);
$body = $this->createRequestBody($body);

return $this->postRequest('consumers/' . $identifier . '/jwt', $body, $headers);
}

/**
* Delete a JWT credential for a consumer
*
* @see https://getkong.org/plugins/jwt/#delete-a-jwt-credential
*
* @param string $identifier
* @param string $jwt_identifier
* @param array $headers
*
* @return array|\stdClass
*/
public function delete($identifier, $jwt_identifier, array $headers = [])
{
return $this->deleteRequest('consumers/' . $identifier . '/jwt/' . $jwt_identifier, $headers);
}

/**
* List all JWT credentials for a consumer
*
* @see https://github.com/Mashape/getkong.org/issues/423
*
* @param string $identifier
* @param array $params
* @param array $headers
*
* @return array|\stdClass
*/
public function list($identifier, array $params = [], array $headers = [])
{
return $this->getRequest('consumers/' . $identifier . '/jwt', $params, $headers);
}
}
11 changes: 11 additions & 0 deletions src/Kong/Apis/Plugins/JwtInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace TheRealGambo\Kong\Apis\Plugins;

interface JwtInterface
{
public function createConsumer(array $body = [], array $headers = []);
public function create($identifier, array $body = [], array $headers = []);
public function delete($identifier, $jwt_identifier, array $headers = []);
public function list($identifier, array $params = [], array $headers = []);
}
11 changes: 11 additions & 0 deletions src/Kong/Kong.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use TheRealGambo\Kong\Apis\Plugin;
use TheRealGambo\Kong\Apis\Plugins\BasicAuth;
use TheRealGambo\Kong\Apis\Plugins\KeyAuth;
use TheRealGambo\Kong\Apis\Plugins\Jwt;
use TheRealGambo\Kong\Apis\Sni;
use TheRealGambo\Kong\Apis\Upstream;
use TheRealGambo\Kong\Exceptions\InvalidUrlException;
Expand Down Expand Up @@ -203,4 +204,14 @@ public function getPluginBasicAuth()
{
return new BasicAuth($this->url, $this->port);
}

/**
* Returns a new instance of the JWT plugin
*
* @return \TheRealGambo\Kong\Apis\Plugins\Jwt
*/
public function getPluginJwt()
{
return new Jwt($this->url, $this->port);
}
}

0 comments on commit 4d0b040

Please sign in to comment.