Permalink
Browse files

Add ValidateSlackToken middleware

- Add config dist
- Add tests
- Config slack slash commands here: https://$slackInstance.slack.com/apps/manage/custom-integrations
- Screenshots: http://imgur.com/a/xtCDS
  • Loading branch information...
stringerbell committed May 29, 2016
1 parent f66c9e3 commit 68f455995850b7afd6456b0245a1aed46aadf9aa
@@ -1,5 +1,7 @@
<?php
use App\Pipeline\SlackJiraPipeline;
use App\Validator\Slack\ValidateSlackToken;
use App\Validator\Slack\ValidateSlackTokenFactory;
use App\Validator\ValidateBody;
use Zend\Expressive\Application;
use Zend\Expressive\Container\ApplicationFactory;
@@ -20,9 +22,10 @@
],
// Use 'factories' for services provided by callbacks/factory classes.
'factories' => [
Application::class => ApplicationFactory::class,
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
SlackJiraPipeline::class => SlackJiraPipeline::class,
Application::class => ApplicationFactory::class,
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
SlackJiraPipeline::class => SlackJiraPipeline::class,
ValidateSlackToken::class => ValidateSlackTokenFactory::class,
],
],
];
@@ -0,0 +1,9 @@
<?php
return [
'slack_config' => [
'tokens' => [
'/slack_jira' => 'token',
],
],
];
@@ -2,6 +2,7 @@
namespace App\Pipeline;
use App\Validator\Slack\ValidateSlackToken;
use App\Validator\ValidateBody;
use Interop\Container\ContainerInterface;
use Zend\Stratigility\MiddlewarePipe;
@@ -12,6 +13,7 @@ public function __invoke(ContainerInterface $container)
{
$pipeline = new MiddlewarePipe();
$pipeline->pipe($container->get(ValidateBody::class));
$pipeline->pipe($container->get(ValidateSlackToken::class));
return $pipeline;
}
@@ -0,0 +1,31 @@
<?php
namespace App\Validator\Slack;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Expressive\Container\Exception\InvalidArgumentException;
class ValidateSlackToken
{
/**
* @var array
*/
private $validTokens;
public function __construct(array $validTokens)
{
$this->validTokens = $validTokens;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
$body = $request->getParsedBody();
$token = $body['token'] ?? "";
if ($this->validTokens[$request->getUri()->getPath()] != $token) {
throw new InvalidArgumentException("Invalid Slack Token");
}
return $next($request, $response, $error ?? null);
}
}
@@ -0,0 +1,15 @@
<?php
namespace App\Validator\Slack;
use Interop\Container\ContainerInterface;
class ValidateSlackTokenFactory
{
public function __invoke(ContainerInterface $container)
{
$validTokens = $container->get('config')['slack_config']['tokens'] ?? [];
return new ValidateSlackToken($validTokens);
}
}
@@ -0,0 +1,22 @@
<?php
namespace AppTest\Validator\Slack;
use App\Validator\Slack\ValidateSlackToken;
use App\Validator\Slack\ValidateSlackTokenFactory;
use Interop\Container\ContainerInterface;
class ValidateSlackTokenFactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function itWillDoTheNeedful()
{
$container = $this->prophesize(ContainerInterface::class);
$factory = new ValidateSlackTokenFactory();
$container->get('config')->shouldBeCalled()->willReturn([]);
$result = $factory($container->reveal());
$this->assertInstanceOf(ValidateSlackToken::class, $result);
}
}
@@ -0,0 +1,70 @@
<?php
namespace AppTest\Validator\Slack;
use App\Validator\Slack\ValidateSlackToken;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Zend\Expressive\Container\Exception\InvalidArgumentException;
class ValidateSlackTokenTest extends \PHPUnit_Framework_TestCase
{
private $validTokens;
private $validateSlackToken;
public function setUp()
{
$this->validTokens = [
'/route1' => 'validToken',
'/route2' => 'anotherToken'
];
$this->validateSlackToken = new ValidateSlackToken($this->validTokens);
}
/**
* @test
* @expectedException InvalidArgumentException
*/
public function itWillThrowForInvalidSlackToken()
{
/** @var ServerRequestInterface $request */
$request = $this->prophesize(ServerRequestInterface::class);
/** @var ResponseInterface $response */
$response = $this->prophesize(ResponseInterface::class);
$next = function ($request, $response, $next) {
return $next;
};
$body =[
'token' => 'invalidToken'
];
$uriInterface = $this->prophesize(UriInterface::class);
$request->getUri()->shouldBeCalled()->willReturn($uriInterface);
$uriInterface->getPath()->shouldBeCalled()->willReturn('/route1');
$request->getParsedBody()->shouldBeCalled()->willReturn($body);
$this->validateSlackToken->__invoke($request->reveal(), $response->reveal(), $next);
}
/**
* @test
*/
public function itWillNotThrowForValidSlackToken()
{
/** @var ServerRequestInterface $request */
$request = $this->prophesize(ServerRequestInterface::class);
/** @var ResponseInterface $response */
$response = $this->prophesize(ResponseInterface::class);
$next = function ($request, $response) {
return $response;
};
$body =[
'token' => 'validToken'
];
$uriInterface = $this->prophesize(UriInterface::class);
$request->getUri()->shouldBeCalled()->willReturn($uriInterface);
$uriInterface->getPath()->shouldBeCalled()->willReturn('/route1');
$request->getParsedBody()->shouldBeCalled()->willReturn($body);
$result = $this->validateSlackToken->__invoke($request->reveal(), $response->reveal(), $next);
$this->assertInstanceOf(ResponseInterface::class, $result);
}
}

0 comments on commit 68f4559

Please sign in to comment.