Created
July 4, 2016 18:55
-
-
Save jorenvh/9e8d6aab71e467a242c8b5c6b905ca4d to your computer and use it in GitHub Desktop.
Slack slash commands middleware for Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // config/slac.php | |
return [ | |
/* | |
* The tokens for the Slack slash commands | |
*/ | |
'slack_tokens' => [ | |
'/yourCommand' => env('TOKEN_YOUR_COMMAND'), | |
], | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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