Skip to content

Commit 446fcb4

Browse files
committed
NFS-616: adds helper to create compatible session ids
- adds method to replace not allowed characters with - - adds tests for the session creation
1 parent 4ae7cf8 commit 446fcb4

3 files changed

Lines changed: 66 additions & 5 deletions

File tree

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit colors="true" bootstrap="vendor/autoload.php">
2+
<phpunit colors="true" bootstrap="vendor/autoload.php" stderr="true" >
33
<testsuites>
44
<testsuite name="Plugins SDK unit test suite">
55
<file>test/SSODataTest.php</file>

src/PluginSession.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ private function deleteInstance($remoteCallHandler){
149149
$this->exitRemoteCall();
150150
}
151151

152+
private function createCompatibleSessionId(String $string): String
153+
{
154+
$allowedChars = '/[^a-zA-Z0-9,-]/';
155+
return preg_replace($allowedChars, '-', $string);
156+
}
157+
152158
/**
153159
* Exit the script
154160
*
@@ -166,9 +172,11 @@ protected function exitRemoteCall() {
166172
*/
167173
protected function openSession($name) {
168174

175+
$sessionId = $this->createCompatibleSessionId($this->sso->getSessionId());
176+
177+
session_id($sessionId);
169178
session_name($name);
170179
session_start();
171-
172180
}
173181

174182
/**
@@ -183,7 +191,7 @@ protected function closeSession() {
183191
* (DEPRECATED) Translate a base64 string to PEM encoded public key.
184192
*
185193
* @param string $data base64 encoded key
186-
*
194+
* @deprecated
187195
* @return string PEM encoded key
188196
*/
189197
public static function base64ToPEMPublicKey($data) {

test/PluginSessionTest.php

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ private function setupEnvironment($queryParamPid = null, $queryParamJwt = null,
6363
$_GET[PluginSession::QUERY_PARAM_PID] = $queryParamPid;
6464
$_GET[PluginSession::QUERY_PARAM_JWT] = $queryParamJwt;
6565

66-
if($clearSession)
67-
$_SESSION = [];
66+
if($clearSession) {
67+
session_write_close();
68+
session_abort();
69+
$_SESSION = [];
70+
}
6871
}
6972

7073
/**
@@ -416,4 +419,54 @@ public function testDeleteFailedCallInterface() {
416419
new $Session($this->pluginId, $this->publicKey, null, 0, $handler);
417420
}
418421

422+
/**
423+
* @test
424+
*
425+
* Test that a session is created.
426+
*
427+
* @covers \Staffbase\plugins\sdk\PluginSession::__construct
428+
*/
429+
public function testSessionIsCreated() {
430+
$tokenData = $this->tokenData;
431+
$this->setupEnvironment(null, $this->token, true);
432+
433+
$mock = $this->getMockBuilder($this->classname)
434+
->disableOriginalConstructor()
435+
->getMock();
436+
437+
$reflectedClass = new ReflectionClass($this->classname);
438+
$constructor = $reflectedClass->getConstructor();
439+
440+
$this->assertEquals(PHP_SESSION_NONE, session_status());
441+
$constructor->invoke($mock, $this->pluginId, $this->publicKey);
442+
$this->assertEquals(PHP_SESSION_ACTIVE, session_status());
443+
444+
$this->assertEquals($tokenData[PluginSession::CLAIM_SESSION_ID], session_id());
445+
}
446+
447+
public function testSessionIdCheck() {
448+
449+
$sessionHash = 'HOjLTR6+D5YIY0/waqJQp3Bg=';
450+
$sessionId = 'HOjLTR6-D5YIY0-waqJQp3Bg-';
451+
452+
$tokenData = $this->tokenData;
453+
$tokenData[PluginSession::CLAIM_SESSION_ID] = $sessionHash;
454+
$token = SSOTokenTest::createSignedTokenFromData($this->privateKey, $tokenData);
455+
456+
$this->setupEnvironment(null, $token, true);
457+
458+
$mock = $this->getMockBuilder($this->classname)
459+
->disableOriginalConstructor()
460+
->getMock();
461+
462+
$reflectedClass = new ReflectionClass($this->classname);
463+
$constructor = $reflectedClass->getConstructor();
464+
465+
$this->assertEquals(PHP_SESSION_NONE, session_status());
466+
$constructor->invoke($mock, $this->pluginId, $this->publicKey);
467+
$this->assertEquals(PHP_SESSION_ACTIVE, session_status());
468+
469+
$this->assertEquals($sessionId, session_id());
470+
}
471+
419472
}

0 commit comments

Comments
 (0)