Skip to content

Commit 055eb58

Browse files
flaviohelenompociot
authored andcommitted
Added Authentication to RedisCache (botman#446)
1 parent ac5ce4c commit 055eb58

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

‎.travis.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ php:
1010
before_script:
1111
- travis_retry composer self-update --preview
1212
- travis_retry composer install --prefer-dist --no-interaction
13+
- sudo redis-server /etc/redis/redis.conf --port 6380 --requirepass 'secret'
1314

1415
script:
1516
- vendor/bin/phpunit --coverage-clover=coverage.xml
@@ -19,4 +20,4 @@ before_install:
1920
- echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
2021

2122
after_success:
22-
- codecov
23+
- codecov

‎src/Cache/RedisCache.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,22 @@ class RedisCache implements CacheInterface
1717
private $redis;
1818
private $host;
1919
private $port;
20+
private $auth;
2021

2122
/**
2223
* RedisCache constructor.
2324
* @param $host
2425
* @param $port
26+
* @param $auth
2527
*/
26-
public function __construct($host = '127.0.0.1', $port = 6379)
28+
public function __construct($host = '127.0.0.1', $port = 6379, $auth = null)
2729
{
2830
if (! class_exists('Redis')) {
2931
throw new RuntimeException('phpredis extension is required for RedisCache');
3032
}
3133
$this->host = $host;
3234
$this->port = $port;
35+
$this->auth = $auth;
3336
$this->connect();
3437
}
3538

@@ -107,6 +110,10 @@ private function connect()
107110
{
108111
$this->redis = new Redis();
109112
$this->redis->connect($this->host, $this->port);
113+
if ($this->auth !== null) {
114+
$this->redis->auth($this->auth);
115+
}
116+
110117
if (function_exists('igbinary_serialize') && defined('Redis::SERIALIZER_IGBINARY')) {
111118
$this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY);
112119
} else {

‎tests/Cache/RedisCacheTest.php‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BotMan\BotMan\Tests;
44

55
use Redis;
6+
use RedisException;
67
use PHPUnit_Framework_TestCase;
78
use BotMan\BotMan\Cache\ArrayCache;
89
use BotMan\BotMan\Cache\RedisCache;
@@ -21,10 +22,34 @@ protected function setUp()
2122

2223
public function tearDown()
2324
{
25+
$script = sprintf("for i, name in ipairs(redis.call('KEYS', '%s*')) do redis.call('DEL', name); end", RedisCache::KEY_PREFIX);
26+
2427
$redis = new Redis();
2528
$redis->connect('127.0.0.1');
26-
$script = sprintf("for i, name in ipairs(redis.call('KEYS', '%s*')) do redis.call('DEL', name); end", RedisCache::KEY_PREFIX);
2729
$redis->eval($script);
30+
$redis->close();
31+
32+
$redis = new Redis();
33+
$redis->connect('127.0.0.1', 6380);
34+
$redis->auth('secret');
35+
$redis->eval($script);
36+
$redis->close();
37+
}
38+
39+
/** @test */
40+
public function valid_auth()
41+
{
42+
$cache = new RedisCache('127.0.0.1', 6380, 'secret');
43+
$cache->put('foo', 'bar', 1);
44+
static::assertTrue($cache->has('foo'));
45+
}
46+
47+
/** @test */
48+
public function invalid_auth()
49+
{
50+
static::setExpectedException(RedisException::class);
51+
$cache = new RedisCache('127.0.0.1', 6380, 'invalid');
52+
$cache->put('foo', 'bar', 1);
2853
}
2954

3055
/** @test */

0 commit comments

Comments
 (0)