Skip to content

Commit 8b2ff14

Browse files
committed
Use more namespaces & shorter lines (PSR-2)
1 parent eafe528 commit 8b2ff14

2 files changed

Lines changed: 57 additions & 28 deletions

File tree

lib/Auth/Process/Fticks.php

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
namespace SimpleSAML\Module\fticks\Auth\Process;
44

5+
use SAML2\Constants;
6+
use SimpleSAML\Configuration;
7+
use SimpleSAML\Error\Exception
8+
use SimpleSAML\Logger;
9+
use SimpleSAML\Session;
10+
use SimpleSAML\Utils;
11+
512
/**
613
* Filter to log F-ticks stats data
714
* See also:
@@ -102,11 +109,12 @@ private function log($msg)
102109
/* SimpleSAMLphp's builtin logging */
103110
case 'simplesamlphp':
104111
default:
105-
\SimpleSAML\Logger::stats($msg);
112+
Logger::stats($msg);
106113
break;
107114
}
108115
}
109116

117+
110118
/**
111119
* Generate a PN hash
112120
*
@@ -129,6 +137,7 @@ private function generatePNhash(&$state)
129137
} elseif (array_key_exists('UserID', $state)) {
130138
$uid = $state['UserID'];
131139
}
140+
132141
/* calculate a hash */
133142
if (isset($uid) and is_string($uid)) {
134143
$userdata = $this->federation;
@@ -146,6 +155,7 @@ private function generatePNhash(&$state)
146155
return false;
147156
}
148157

158+
149159
/**
150160
* Escape F-ticks values
151161
*
@@ -160,6 +170,7 @@ private function escapeFticks($value)
160170
return preg_replace('/[^A-Za-z0-9_\-:.,;\/]+/', '', $value);
161171
}
162172

173+
163174
/**
164175
* Initialize this filter, parse configuration.
165176
*
@@ -176,35 +187,35 @@ public function __construct($config, $reserved)
176187
if (is_string($config['federation'])) {
177188
$this->federation = $config['federation'];
178189
} else {
179-
throw new \SimpleSAML\Error\Exception('Federation identifier must be a string');
190+
throw new Exception('Federation identifier must be a string');
180191
}
181192
} else {
182-
throw new \SimpleSAML\Error\Exception('Federation identifier must be set');
193+
throw new Exception('Federation identifier must be set');
183194
}
184195

185196
if (array_key_exists('salt', $config)) {
186197
if (is_string($config['salt'])) {
187198
$this->salt = $config['salt'];
188199
} else {
189-
throw new \SimpleSAML\Error\Exception('Salt must be a string');
200+
throw new Exception('Salt must be a string');
190201
}
191202
} else {
192-
$this->salt = \SimpleSAML\Utils\Config::getSecretSalt();
203+
$this->salt = Utils\Config::getSecretSalt();
193204
}
194205

195206
if (array_key_exists('userId', $config)) {
196207
if (is_string($config['userId'])) {
197208
$this->userId = $config['userId'];
198209
} else {
199-
throw new \SimpleSAML\Error\Exception('UserId must be a string');
210+
throw new Exception('UserId must be a string');
200211
}
201212
}
202213

203214
if (array_key_exists('realm', $config)) {
204215
if (is_string($config['realm'])) {
205216
$this->realm = $config['realm'];
206217
} else {
207-
throw new \SimpleSAML\Error\Exception('realm must be a string');
218+
throw new Exception('realm must be a string');
208219
}
209220
}
210221

@@ -214,7 +225,7 @@ public function __construct($config, $reserved)
214225
) {
215226
$this->algorithm = $config['algorithm'];
216227
} else {
217-
throw new \SimpleSAML\Error\Exception('algorithm must be a hash algorithm listed in hash_algos()');
228+
throw new Exception('algorithm must be a hash algorithm listed in hash_algos()');
218229
}
219230
}
220231

@@ -224,7 +235,7 @@ public function __construct($config, $reserved)
224235
} elseif (is_string($config['exclude'])) {
225236
$this->exclude = [$config['exclude']];
226237
} else {
227-
throw new \SimpleSAML\Error\Exception('F-ticks exclude must be an array');
238+
throw new Exception('F-ticks exclude must be an array');
228239
}
229240
}
230241

@@ -234,19 +245,19 @@ public function __construct($config, $reserved)
234245
) {
235246
$this->logdest = $config['logdest'];
236247
} else {
237-
throw new \SimpleSAML\Error\Exception('F-ticks log destination must be one of [local, remote, stdout, errorlog, simplesamlphp]');
248+
throw new Exception('F-ticks log destination must be one of [local, remote, stdout, errorlog, simplesamlphp]');
238249
}
239250
}
240251

241252
/* match SSP config or we risk mucking up the openlog call */
242-
$globalConfig = \SimpleSAML\Configuration::getInstance();
253+
$globalConfig = Configuration::getInstance();
243254
$defaultFacility = $globalConfig->getInteger('logging.facility', defined('LOG_LOCAL5') ? constant('LOG_LOCAL5') : LOG_USER);
244255
$defaultProcessName = $globalConfig->getString('logging.processname', 'SimpleSAMLphp');
245256
if (array_key_exists('logconfig', $config)) {
246257
if (is_array($config['logconfig'])) {
247258
$this->logconfig = $config['logconfig'];
248259
} else {
249-
throw new \SimpleSAML\Error\Exception('F-ticks logconfig must be an array');
260+
throw new Exception('F-ticks logconfig must be an array');
250261
}
251262
} else {
252263
$this->logconfig['facility'] = $defaultFacility;
@@ -255,10 +266,10 @@ public function __construct($config, $reserved)
255266
/* warn if we risk mucking up the openlog call (doesn't matter for remote syslog) */
256267
if (in_array($this->logdest, ['local', 'syslog'])) {
257268
if (array_key_exists('facility', $this->logconfig) and $this->logconfig['facility'] !== $defaultFacility) {
258-
\SimpleSAML\Logger::warning('F-ticks syslog facility differs from global config which may cause SimpleSAMLphp\'s logging to behave inconsistently');
269+
Logger::warning('F-ticks syslog facility differs from global config which may cause SimpleSAMLphp\'s logging to behave inconsistently');
259270
}
260271
if (array_key_exists('processname', $this->logconfig) and $this->logconfig['processname'] !== $defaultProcessName) {
261-
\SimpleSAML\Logger::warning('F-ticks syslog processname differs from global config which may cause SimpleSAMLphp\'s logging to behave inconsistently');
272+
Logger::warning('F-ticks syslog processname differs from global config which may cause SimpleSAMLphp\'s logging to behave inconsistently');
262273
}
263274
}
264275
}
@@ -293,15 +304,15 @@ public function process(&$state)
293304
$fticks['RP'] = $state['Destination']['entityid'];
294305

295306
/* SAML session id */
296-
$session = \SimpleSAML\Session::getSessionFromRequest();
307+
$session = Session::getSessionFromRequest();
297308
$fticks['CSI'] = $session->getTrackID();
298309

299310
/* Authentication method identifier */
300311
if (array_key_exists('saml:sp:State', $state) and array_key_exists('saml:sp:AuthnContext', $state['saml:sp:State'])) {
301312
$fticks['AM'] = $state['saml:sp:State']['saml:sp:AuthnContext'];
302313
} elseif (array_key_exists('SimpleSAML_Auth_State.stage', $state) and preg_match('/UserPass/', $state['SimpleSAML_Auth_State.stage'])) {
303314
/* hack to try identify LDAP et al as Password */
304-
$fticks['AM'] = 'urn:oasis:names:tc:SAML:2.0:ac:classes:Password';
315+
$fticks['AM'] = Constants::AC_PASSWORD;
305316
}
306317

307318
/* ePTID */

tests/lib/Auth/Process/FticksTest.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
namespace SimpleSAML\Test\Module\fticks\Auth\Process;
44

5-
class FticksTest extends \PHPUnit_Framework_TestCase
5+
use PHPUnit\Framework\TestCase;
6+
use SimpleSAML\Module\fticks\Auth\Process\Fticks;
7+
use SimpleSAML\Configuration;
8+
use SimpleSAML\Logger;
9+
use SimpleSAML\Logger\StandardErrorLoggingHandler;
10+
11+
class FticksTest extends TestCase
612
{
713
/** @var array minimal request */
814
private static $minRequest = [
@@ -40,7 +46,7 @@ class FticksTest extends \PHPUnit_Framework_TestCase
4046
*/
4147
private static function processFilter(array $config, array $request)
4248
{
43-
$filter = new \SimpleSAML\Module\fticks\Auth\Process\Fticks($config, null);
49+
$filter = new Fticks($config, null);
4450
$filter->process($request);
4551
return $request;
4652
}
@@ -50,13 +56,13 @@ private static function processFilter(array $config, array $request)
5056
*/
5157
protected function setUp()
5258
{
53-
\SimpleSAML\Configuration::loadFromArray([
59+
Configuration::loadFromArray([
5460
'secretsalt' => 'secretsalt',
5561
], '[ARRAY]', 'simplesaml');
5662
/*
57-
$rm = new ReflectionMethod('\SimpleSAML\Logger', 'createLoggingHandler');
63+
$rm = new ReflectionMethod(Logger::class, 'createLoggingHandler');
5864
$rm->setAccessible(true);
59-
$rm->invoke('\SimpleSAML\Logger\StandardErrorLoggingHandler');
65+
$rm->invoke(StandardErrorLoggingHandler::class);
6066
*/
6167
}
6268

@@ -67,7 +73,8 @@ public function testMinimal()
6773
{
6874
$config = ['federation' => 'ACME', 'logdest' => 'stdout'];
6975
$request = self::$minRequest;
70-
$this->expectOutputRegex('/^'.preg_quote('F-TICKS/ACME/1.0#RESULT=OK#AP=https://localhost/sp#RP=https://localhost/idp#CSI=CL', '/').'[^#]+#TS=\d+#$/');
76+
$pattern = preg_quote('F-TICKS/ACME/1.0#RESULT=OK#AP=https://localhost/sp#RP=https://localhost/idp#CSI=CL', '/');
77+
$this->expectOutputRegex('/^'.$pattern.'[^#]+#TS=\d+#$/');
7178
$result = self::processFilter($config, $request);
7279
}
7380

@@ -78,7 +85,9 @@ public function testAsServiceProvider()
7885
{
7986
$config = ['federation' => 'ACME', 'logdest' => 'stdout',];
8087
$request = array_merge(self::$minRequest, self::$spRequest);
81-
$this->expectOutputRegex('/^'.preg_quote('F-TICKS/ACME/1.0#RESULT=OK#AP=https://localhost/saml:sp:IdP#RP=https://localhost/idp#CSI=CL', '/').'[^#]+'.preg_quote('#AM=urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified#TS=1000#', '/').'$/');
88+
$pattern1 = preg_quote('F-TICKS/ACME/1.0#RESULT=OK#AP=https://localhost/saml:sp:IdP#RP=https://localhost/idp#CSI=CL', '/');
89+
$pattern2 = preg_quote('#AM=urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified#TS=1000#', '/');
90+
$this->expectOutputRegex('/^'.$pattern1.'[^#]+'.$pattern2.'$/');
8291
$result = self::processFilter($config, $request);
8392
}
8493

@@ -93,7 +102,9 @@ public function testSPwithUserId()
93102
'eduPersonPrincipalName' => 'user2@example.net',
94103
],
95104
]);
96-
$this->expectOutputRegex('/^'.preg_quote('F-TICKS/ACME/1.0#RESULT=OK#AP=https://localhost/saml:sp:IdP#RP=https://localhost/idp#CSI=CL', '/').'[^#]+'.preg_quote('#AM=urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified#PN=e5d066a96d5809a21264e153013c3c793e6574cb77afdfa248ad2cefab9b0451#TS=1000#', '/').'$/');
105+
$pattern1 = preg_quote('F-TICKS/ACME/1.0#RESULT=OK#AP=https://localhost/saml:sp:IdP#RP=https://localhost/idp#CSI=CL', '/');
106+
$pattern2 = preg_quote('#AM=urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified#PN=e5d066a96d5809a21264e153013c3c793e6574cb77afdfa248ad2cefab9b0451#TS=1000#', '/');
107+
$this->expectOutputRegex('/^'.$pattern1.'[^#]+'.$pattern2.'$/');
97108
$result = self::processFilter($config, $request);
98109
}
99110

@@ -104,7 +115,9 @@ public function testAsIdentityProvider()
104115
{
105116
$config = ['federation' => 'ACME', 'logdest' => 'stdout',];
106117
$request = array_merge(self::$minRequest, self::$idpRequest);
107-
$this->expectOutputRegex('/^'.preg_quote('F-TICKS/ACME/1.0#RESULT=OK#AP=https://localhost/sp#RP=https://localhost/idp#CSI=CL', '/').'[^#]+'.preg_quote('#AM=urn:oasis:names:tc:SAML:2.0:ac:classes:Password#PN=d844a9a0666bb3990e88f72b8f5c20accbcfa46f7b8a7ab38593bfbbab6e9cbc#TS=', '/').'\d+#$/');
118+
$pattern1 = preg_quote('F-TICKS/ACME/1.0#RESULT=OK#AP=https://localhost/sp#RP=https://localhost/idp#CSI=CL', '/');
119+
$pattern2 = preg_quote('#AM=urn:oasis:names:tc:SAML:2.0:ac:classes:Password#PN=d844a9a0666bb3990e88f72b8f5c20accbcfa46f7b8a7ab38593bfbbab6e9cbc#TS=', '/');
120+
$this->expectOutputRegex('/^'.$pattern1.'[^#]+'.$pattern2.'\d+#$/');
108121
$result = self::processFilter($config, $request);
109122
}
110123

@@ -128,7 +141,9 @@ public function testExample()
128141
'schacHomeOrganization' => 'example.com',
129142
],
130143
]);
131-
$this->expectOutputRegex('/^'.preg_quote('F-TICKS/ACME/1.0#RESULT=OK#AP=https://localhost/sp#RP=https://localhost/idp#CSI=CL', '/').'[^#]+'.preg_quote('#AM=urn:oasis:names:tc:SAML:2.0:ac:classes:Password#TS=', '/').'\d+#REALM=example.com#$/');
144+
$pattern1 = preg_quote('F-TICKS/ACME/1.0#RESULT=OK#AP=https://localhost/sp#RP=https://localhost/idp#CSI=CL', '/');
145+
$pattern2 = preg_quote('#AM=urn:oasis:names:tc:SAML:2.0:ac:classes:Password#TS=', '/');
146+
$this->expectOutputRegex('/^'.$pattern1.'[^#]+'.$pattern2.'\d+#REALM=example.com#$/');
132147
$result = self::processFilter($config, $request);
133148
}
134149

@@ -139,7 +154,8 @@ public function testFilteringArray()
139154
{
140155
$config = ['federation' => 'ACME', 'logdest' => 'stdout', 'exclude' => ['PN', 'AM']];
141156
$request = array_merge(self::$minRequest, self::$idpRequest);
142-
$this->expectOutputRegex('/^'.preg_quote('F-TICKS/ACME/1.0#RESULT=OK#AP=https://localhost/sp#RP=https://localhost/idp#CSI=CL', '/').'[^#]+#TS=\d+#$/');
157+
$pattern1 = preg_quote('F-TICKS/ACME/1.0#RESULT=OK#AP=https://localhost/sp#RP=https://localhost/idp#CSI=CL', '/');
158+
$this->expectOutputRegex('/^'.$pattern1.'[^#]+#TS=\d+#$/');
143159
$result = self::processFilter($config, $request);
144160
}
145161

@@ -150,7 +166,9 @@ public function testFilteringString()
150166
{
151167
$config = ['federation' => 'ACME', 'logdest' => 'stdout', 'exclude' => 'AM'];
152168
$request = array_merge(self::$minRequest, self::$idpRequest);
153-
$this->expectOutputRegex('/^'.preg_quote('F-TICKS/ACME/1.0#RESULT=OK#AP=https://localhost/sp#RP=https://localhost/idp#CSI=CL', '/').'[^#]+'.preg_quote('#PN=d844a9a0666bb3990e88f72b8f5c20accbcfa46f7b8a7ab38593bfbbab6e9cbc#TS=', '/').'\d+#$/');
169+
$pattern1 = preg_quote('F-TICKS/ACME/1.0#RESULT=OK#AP=https://localhost/sp#RP=https://localhost/idp#CSI=CL', '/');
170+
$pattern2 = preg_quote('#PN=d844a9a0666bb3990e88f72b8f5c20accbcfa46f7b8a7ab38593bfbbab6e9cbc#TS=', '/');
171+
$this->expectOutputRegex('/^'.$pattern1.'[^#]+'.$pattern2.'\d+#$/');
154172
$result = self::processFilter($config, $request);
155173
}
156174
}

0 commit comments

Comments
 (0)