Skip to content

Instantly share code, notes, and snippets.

@jorenvh
Created July 4, 2016 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorenvh/9e8d6aab71e467a242c8b5c6b905ca4d to your computer and use it in GitHub Desktop.
Save jorenvh/9e8d6aab71e467a242c8b5c6b905ca4d to your computer and use it in GitHub Desktop.
Slack slash commands middleware for Laravel
<?php // config/slac.php
return [
/*
* The tokens for the Slack slash commands
*/
'slack_tokens' => [
'/yourCommand' => env('TOKEN_YOUR_COMMAND'),
],
];
<?php // app/Http/Middleware/VerifySlackToken
namespace App\Http\Middleware;
use Closure;
use Exception;
use Illuminate\Contracts\Config\Repository;
class VerifySlackToken
{
/**
* @var \Illuminate\Contracts\Config\Repository
*/
private $config;
public function __construct(Repository $config)
{
$this->config = $config->get('slack.slack_tokens');
}
/**
* Handle an incoming request.
*
* @param $request
* @param Closure $next
* @return mixed
* @throws Exception
*/
public function handle($request, Closure $next)
{
if($this->config[$request->command] != $request->token) {
throw new Exception('Invalid Slack command token given');
}
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment