Skip to content

Commit 5efc10a

Browse files
Merge pull request #3 from prolificinteractive/develop
Adds README, tests, and Travis CI configuration
2 parents 2197990 + dfd2d71 commit 5efc10a

File tree

9 files changed

+273
-22
lines changed

9 files changed

+273
-22
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
composer.lock
3+
vendor

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: php
2+
3+
php:
4+
- '5.4'
5+
- '5.5'
6+
- '5.6'
7+
8+
services: mongodb
9+
10+
before_script:
11+
- sleep 15
12+
before_install: echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
13+
install: travis_retry composer update --no-interaction --no-progress --no-suggest --prefer-stable

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# SimpleSAMLphp Mongo Module
2+
3+
[![Travis build status](https://img.shields.io/travis/prolificinteractive/simplesamlphp-module-mongo.svg?style=flat-square)](https://travis-ci.org/prolificinteractive/simplesamlphp-module-mongo)
4+
5+
The mongo module is an implementation of a SimpleSAMLphp (SSP) data store.
6+
7+
## Features
8+
9+
- Can be used for backend storage of sessions in MongoDB
10+
- Includes support for replica sets
11+
12+
## Requirements
13+
14+
PHP 5.4 through 5.6
15+
16+
[SimpleSAMLphp](https://simplesamlphp.org/)
17+
18+
[MongoDB](https://www.mongodb.com/)
19+
20+
[Mongo PHP extension](http://php.net/manual/en/book.mongo.php)
21+
22+
## Installation
23+
24+
If your project manages SSP with [Composer](https://getcomposer.org/) run:
25+
```
26+
php composer.phar require prolificinteractive/simplesamlphp-module-mongo
27+
```
28+
This command will add `prolificinteractive/simplesamlphp-module-mongo` to your projects' composer.json file and install the module
29+
into SSP's `modules` directory, which relative to your project's root directory is conventionally `vendor/simplesamlphp/simplesamlphp/modules`.
30+
31+
## Usage
32+
33+
Set the `store.type` option in your SSP config file to `mongo:Store`.
34+
35+
Provide your MongoDB connection information to the module by copying the file provided in the `config-templates` directory into SSP's config directory, and setting the following environment variables:
36+
```
37+
DB_MONGODB_HOST
38+
DB_MONGODB_PORT
39+
DB_MONGODB_USERNAME
40+
DB_MONGODB_PASSWORD
41+
DB_MONGODB_DATABASE
42+
```
43+
44+
If your connecting to a replica set, you'll need to set the following environment variables below as well:
45+
```
46+
DB_DEFAULT_CONNECTION # Must contain the substring "_replica"
47+
DB_MONGODB_REPLICASET
48+
DB_MONGODB_READ_PREFERENCE
49+
```
50+
See the [Mongo extension PHP Manual](http://php.net/manual/en/mongo.manual.php) for more information about appropriate values for `DB_MONGODB_REPLICASET` and `DB_MONGODB_READ_PREFERENCE`.
51+
52+
Finally, you can enable the module by creating an empty file name `enable` in the `vendor/simplesamlphp/simplesamlphp/modules/mongo` directory.
53+
54+
**Note:** This module stores PHP session data in the `session` collection.
55+
56+
## Contributing to SimpleSAMLphp Mongo Module
57+
58+
To report a bug or enhancement request, feel free to file an issue under the respective heading.
59+
60+
If you wish to contribute to the project, fork this repo and submit a pull request.
61+
62+
## License
63+
64+
![prolific](https://s3.amazonaws.com/prolificsitestaging/logos/Prolific_Logo_Full_Color.png)
65+
66+
Copyright (c) 2017 Prolific Interactive
67+
68+
SimpleSAMLphp Mongo Module is maintained and sponsored by Prolific Interactive. It may be redistributed under the terms specified in the [LICENSE] file.
69+
70+
[LICENSE]: ./LICENSE

composer.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,21 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^5.3.0",
19+
"php": "^5.4",
2020
"ext-mongo": "*",
2121
"simplesamlphp/composer-module-installer": "~1.0"
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": ">=3.70"
25+
},
26+
"autoload": {
27+
"classmap": [
28+
"lib"
29+
]
30+
},
31+
"autoload-dev": {
32+
"classmap": [
33+
"tests"
34+
]
2235
}
2336
}

docs/simplesamlphp-mongo.txt

Whitespace-only changes.

lib/Store/Store.php

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
<?php
2+
/**
3+
* This file is part of the simplesamlphp-module-mongo.
4+
*
5+
* For the full copyright and license information, please view the LICENSE file that was distributed with this source
6+
* code.
7+
*
8+
* @author Chris Beaton <[email protected]>
9+
* @package prolificinteractive/simplesamlphp-module-mongo
10+
*/
11+
12+
use \SimpleSAML\Store;
213

314
/**
415
* Class sspmod_mongo_Store_Store
16+
*
517
*/
6-
class sspmod_mongo_Store_Store extends SimpleSAML_Store
18+
class sspmod_mongo_Store_Store extends Store
719
{
820
protected $connection;
921
protected $db;
@@ -22,8 +34,10 @@ public function __construct($connectionDetails = array())
2234
}
2335

2436
/**
25-
* @param array $connectionDetails
26-
* @return string
37+
* Builds the connection URI from the specified connection details.
38+
*
39+
* @param array $connectionDetails An array of arguments to the database connection URI.
40+
* @return string The connection URI.
2741
*/
2842
static function createConnectionURI($connectionDetails = array()) {
2943
$port = $connectionDetails['port'];
@@ -140,34 +154,22 @@ public function delete($type, $key)
140154
}
141155

142156
/**
143-
* @return \MongoClient
157+
* Returns a new database connection object.
158+
*
159+
* @return \MongoClient The database connection object.
144160
*/
145161
public function getConnection()
146162
{
147163
return $this->connection;
148164
}
149165

150166
/**
151-
* @param \MongoClient $connection
167+
* Sets the database connection for this store.
168+
*
169+
* @param \MongoClient $connection A database connection object.
152170
*/
153171
public function setConnection($connection)
154172
{
155173
$this->connection = $connection;
156174
}
157-
158-
/**
159-
* @return \MongoDB
160-
*/
161-
public function getDb()
162-
{
163-
return $this->db;
164-
}
165-
166-
/**
167-
* @param \MongoDB $db
168-
*/
169-
public function setDb($db)
170-
{
171-
$this->db = $db;
172-
}
173175
}

phpunit.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php">
3+
<php>
4+
<env name="DB_DEFAULT_CONNECTION" value="mongodb"/>
5+
<env name="DB_MONGODB_HOST" value="localhost"/>
6+
<env name="DB_MONGODB_PORT" value="27017"/>
7+
<env name="DB_MONGODB_USERNAME" value=""/>
8+
<env name="DB_MONGODB_PASSWORD" value=""/>
9+
<env name="DB_MONGODB_DATABASE" value="test"/>
10+
</php>
11+
<testsuites>
12+
<testsuite name="Store Tests">
13+
<directory suffix="Test.php">tests/lib/Store</directory>
14+
</testsuite>
15+
</testsuites>
16+
</phpunit>

tests/lib/Store/StoreTest.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
/**
3+
* Test for the store:Mongo data store.
4+
*
5+
* For the full copyright and license information, please view the LICENSE file that was distributed with this source
6+
* code.
7+
*
8+
* @author Chris Beaton <[email protected]>
9+
* @package prolificinteractive/simplesamlphp-module-mongo
10+
*/
11+
12+
namespace SimpleSAML\Test\Module\mongo\Store;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use \SimpleSAML_Configuration as Configuration;
16+
17+
final class StoreTest extends TestCase
18+
{
19+
public function testSingleHostConnection()
20+
{
21+
Configuration::setConfigDir(__DIR__ . '/fixture/single-host');
22+
new \sspmod_mongo_Store_Store();
23+
$this->assertTrue(true);
24+
}
25+
26+
public function testGet()
27+
{
28+
$store = new \sspmod_mongo_Store_Store();
29+
$connection = $store->getConnection();
30+
$database = getenv('DB_MONGODB_DATABASE');
31+
$collection = $connection->{$database}->session;
32+
$collection->remove();
33+
$this->assertEquals(0, $collection->count());
34+
35+
$type = 'session';
36+
$key = 'SESSION_ID';
37+
$value = array('some' => 'thing');
38+
$expire = time() + 1000000;
39+
40+
$result = $store->get($type, $key);
41+
$this->assertNull($result);
42+
43+
$store->set($type, $key, $value, $expire);
44+
$this->assertEquals(1, $collection->count());
45+
46+
$result = $store->get($type, $key);
47+
$this->assertEquals($result, $value);
48+
$this->assertEquals(1, $collection->count());
49+
}
50+
51+
public function testExpiredGet()
52+
{
53+
$store = new \sspmod_mongo_Store_Store();
54+
$connection = $store->getConnection();
55+
$database = getenv('DB_MONGODB_DATABASE');
56+
$collection = $connection->{$database}->session;
57+
$collection->remove();
58+
$this->assertEquals(0, $collection->count());
59+
60+
$type = 'session';
61+
$key = 'SESSION_ID';
62+
$value = array('some' => 'thing');
63+
$expire = 0;
64+
$store->set($type, $key, $value, $expire);
65+
$this->assertEquals(1, $collection->count());
66+
67+
$result = $store->get($type, $key);
68+
$this->assertNull($result);
69+
$this->assertEquals(0, $collection->count());
70+
}
71+
72+
public function testSet()
73+
{
74+
$store = new \sspmod_mongo_Store_Store();
75+
$connection = $store->getConnection();
76+
$database = getenv('DB_MONGODB_DATABASE');
77+
$collection = $connection->{$database}->session;
78+
$collection->remove();
79+
$this->assertEquals(0, $collection->count());
80+
81+
$type = 'session';
82+
$key = 'SESSION_ID';
83+
$value = array('some' => 'thing');
84+
$expire = time() + 1000000;
85+
86+
$result = $store->get($type, $key);
87+
$this->assertNull($result);
88+
89+
$store->set($type, $key, $value, $expire);
90+
$this->assertEquals(1, $collection->count());
91+
92+
$result = $store->get($type, $key);
93+
$this->assertEquals($result, $value);
94+
$this->assertEquals(1, $collection->count());
95+
96+
$value = array('some' => 'otherthing');
97+
$result = $store->set($type, $key, $value, $expire);
98+
$this->assertEquals(1, $collection->count());
99+
$this->assertEquals($expire, $result);
100+
$result = $store->get($type, $key);
101+
$this->assertEquals($result, $value);
102+
$this->assertEquals(1, $collection->count());
103+
}
104+
105+
public function testDelete()
106+
{
107+
$store = new \sspmod_mongo_Store_Store();
108+
$connection = $store->getConnection();
109+
$database = getenv('DB_MONGODB_DATABASE');
110+
$collection = $connection->{$database}->session;
111+
$collection->remove();
112+
$this->assertEquals(0, $collection->count());
113+
114+
$type = 'session';
115+
$key = 'SESSION_ID';
116+
$value = array('some' => 'thing');
117+
$expire = time() + 1000000;
118+
$store->set($type, $key, $value, $expire);
119+
$this->assertEquals(1, $collection->count());
120+
121+
$store->delete($type, $key);
122+
$this->assertEquals(0, $collection->count());
123+
}
124+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
$config = array(
4+
'host' => 'localhost',
5+
'port' => 27017,
6+
'username' => '',
7+
'password' => '',
8+
'database' => 'test'
9+
);
10+

0 commit comments

Comments
 (0)