Skip to content

Commit 4337b5e

Browse files
committed
Provide for an expiry date on assertions
1 parent a765ed0 commit 4337b5e

File tree

4 files changed

+64
-8
lines changed

4 files changed

+64
-8
lines changed

.travis.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ services:
1717
# Set up a test database we can use for the the unit tests
1818
before_install:
1919
- mysql -e 'CREATE DATABASE IF NOT EXISTS simplesamlphp;'
20-
- mysql -e 'CREATE TABLE `AttributeFromSQL` (
20+
- mysql -e 'CREATE TABLE IF NOT EXISTS `AttributeFromSQL` (
21+
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
2122
`uid` VARCHAR(100) NOT NULL,
2223
`sp` VARCHAR(250) DEFAULT "%",
2324
`attribute` VARCHAR(30) NOT NULL,
24-
`value` TEXT
25+
`value` TEXT,
26+
`expires` DATE DEFAULT "9999-12-31",
27+
PRIMARY KEY (`id`)
2528
) DEFAULT CHARSET=utf8;
2629
GRANT ALL ON `simplesamlphp`.* TO `phpunit`@`localhost` IDENTIFIED BY "phpunit";
2730
' simplesamlphp
@@ -30,6 +33,7 @@ before_install:
3033
INSERT INTO AttributeFromSQL (uid, sp, attribute, value) VALUES ('user@example.org', 'https://idp.example.org/idp/shibboleth', 'eduPersonEntitlement', 'urn:mace:grnet.gr:eduroam:admin');
3134
INSERT INTO AttributeFromSQL (uid, sp, attribute, value) VALUES ('user@example.org', '%', 'eduPersonAffiliation', 'faculty');
3235
INSERT INTO AttributeFromSQL (uid, attribute, value) VALUES ('user@example.org', 'mail', 'user@example.org');
36+
INSERT INTO AttributeFromSQL (uid, attribute, value, expires) VALUES ('user@example.org', 'mail', 'marty@example.org', '2015-10-21');
3337
" simplesamlphp
3438
3539
before_script:

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,24 @@ available if you want to use a stable version of the module.
2626
You then need to create the following table in your SQL database:
2727

2828
```sql
29-
CREATE TABLE `AttributeFromSQL` (
29+
CREATE TABLE IF NOT EXISTS `AttributeFromSQL` (
30+
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
3031
`uid` VARCHAR(100) NOT NULL,
31-
`sp` VARCHAR(250) DEFAULT '%',
32+
`sp` VARCHAR(250) DEFAULT '%',
3233
`attribute` VARCHAR(30) NOT NULL,
33-
`value` TEXT
34+
`value` TEXT,
35+
`expires` DATE DEFAULT '9999-12-31',
36+
PRIMARY KEY (`id`)
3437
) DEFAULT CHARSET=utf8;
3538
```
3639

40+
Note that if you are upgrading from v1.2 or earlier you need to make the following change to your existing database:
41+
42+
```sql
43+
ALTER TABLE `AttributeFromSQL` ADD `id` INT UNSIGNED NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (id);
44+
ALTER TABLE `AttributeFromSQL` ADD `expires` DATE DEFAULT '9999-12-31';
45+
```
46+
3747
Usage
3848
-----
3949

@@ -65,6 +75,8 @@ Where the parameters are as follows:
6575

6676
* `replace` - behaviour when an existing attribute of the same name is encountered. If `false` (the default) then new values are pushed into an array, creating a multi-valued attribute. If `true`, then existing attributes of the same name are replaced (deleted).
6777

78+
* `ignoreExpiry` - ignore any expiry date (default is to ignore attributes that are beyond the date in the `expires` column).
79+
6880
* `database` - an array containing information about the data store, with the following parameters:
6981

7082
* `dsn` - the data source name, defaults to _mysql:host=localhost;dbname=simplesamlphp_
@@ -84,7 +96,7 @@ database. This can be done manually with SQL similar to the following:
8496
```sql
8597
INSERT INTO AttributeFromSQL (uid, sp, attribute, value) VALUES ('user@example.org', '%', 'eduPersonEntitlement', 'urn:mace:exampleIdP.org:demoservice:demo-admin');
8698
INSERT INTO AttributeFromSQL (uid, sp, attribute, value) VALUES ('user@example.org', 'https://idp.example.org/idp/shibboleth', 'eduPersonEntitlement', 'urn:mace:grnet.gr:eduroam:admin');
87-
INSERT INTO AttributeFromSQL (uid, sp, attribute, value) VALUES ('user@example.org', '%', 'eduPersonAffiliation', 'faculty');
99+
INSERT INTO AttributeFromSQL (uid, sp, attribute, value, expires) VALUES ('user@example.org', '%', 'eduPersonAffiliation', 'faculty', '2020-12-31');
88100
INSERT INTO AttributeFromSQL (uid, attribute, value) VALUES ('user@example.org', 'mail', 'user@example.org');
89101
```
90102

lib/Auth/Process/AttributeFromSQL.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class sspmod_sqlattribs_Auth_Process_AttributeFromSQL extends SimpleSAML_Auth_Pr
3333
/** @var array|null Limit returned attribute set */
3434
private $limit = null;
3535

36+
/** @var bool|false Should we ignore expiry */
37+
private $ignoreExpiry = false;
38+
3639
/**
3740
* Initialize this filter, parse configuration.
3841
*
@@ -83,6 +86,10 @@ public function __construct($config, $reserved)
8386
}
8487
$this->limit = $config['limit'];
8588
}
89+
90+
if (array_key_exists('ignoreExpiry', $config)) {
91+
$this->ignoreExpiry = (bool)$config['ignoreExpiry'];
92+
}
8693
}
8794

8895
/**
@@ -140,7 +147,7 @@ public function process(&$request)
140147
$db = $this->connect();
141148

142149
try {
143-
$sth = $db->prepare('SELECT attribute,value FROM ' . $this->table . ' WHERE uid=? AND (sp=\'%\' OR sp=?);');
150+
$sth = $db->prepare('SELECT `attribute`,`value` FROM ' . $this->table . ' WHERE `uid`=? AND (`sp`=\'%\' OR `sp`=?)' . ($this->ignoreExpiry ? '' : ' AND `expires`>CURRENT_DATE') . ';');
144151
} catch (PDOException $e) {
145152
throw new SimpleSAML_Error_Exception('AttributeFromSQL: prepare() failed: ' . $e->getMessage());
146153
}

tests/lib/Auth/Process/AttributeFromSQLTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private static function processFilter(array $config, array $request)
1919
$filter->process($request);
2020
return $request;
2121
}
22-
22+
2323
protected function setUp()
2424
{
2525
\SimpleSAML_Configuration::loadFromArray(array(), '[ARRAY]', 'simplesaml');
@@ -102,4 +102,37 @@ public function testReplace()
102102
);
103103
$this->assertEquals($expectedData, $attributes, "Expected data was not correct");
104104
}
105+
/**
106+
* Test attribute replacement
107+
*/
108+
109+
public function testIgnoreExpires()
110+
{
111+
$config = array(
112+
'attribute' => 'eduPersonPrincipalName',
113+
'limit' => array('mail',),
114+
'ignoreExpiry' => true,
115+
'database' => array(
116+
'username' => 'phpunit',
117+
'password' => 'phpunit',
118+
),
119+
);
120+
$request = array(
121+
'Attributes' => array(
122+
'eduPersonPrincipalName' => array('user@example.org'),
123+
'displayName' => array('Example User'),
124+
),
125+
'Destination' => array(
126+
'entityid' => 'https://idp.example.org/idp/shibboleth',
127+
),
128+
);
129+
$result = self::processFilter($config, $request);
130+
$attributes = $result['Attributes'];
131+
$expectedData = array(
132+
'eduPersonPrincipalName' => array('user@example.org'),
133+
'displayName' => array('Example User'),
134+
'mail' => array('user@example.org', 'marty@example.org'),
135+
);
136+
$this->assertEquals($expectedData, $attributes, "Expected data was not correct");
137+
}
105138
}

0 commit comments

Comments
 (0)