|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\RealMe\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\NullHTTPRequest; |
|
6
|
|
|
use SilverStripe\Core\Config\Config; |
|
7
|
|
|
use SilverStripe\Core\Environment; |
|
8
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
9
|
|
|
use SilverStripe\Core\TempFolder; |
|
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
11
|
|
|
use SilverStripe\RealMe\RealMeService; |
|
12
|
|
|
|
|
13
|
|
|
class RealMeServiceTest extends SapphireTest |
|
14
|
|
|
{ |
|
15
|
|
|
private static $pathForTempCertificate; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var RealMeService |
|
19
|
|
|
*/ |
|
20
|
|
|
private $service; |
|
21
|
|
|
|
|
22
|
|
|
public function testGetCertificateContents() |
|
23
|
|
|
{ |
|
24
|
|
|
self::$pathForTempCertificate = TempFolder::getTempFolder(BASE_PATH) . '/tmpcert.pem'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Test standard certificate |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
$contents = file_get_contents(__DIR__ . '/certs/standard_cert.pem'); |
|
31
|
|
|
|
|
32
|
|
|
// Strip carriage returns |
|
33
|
|
|
$contents = str_replace("\r", '', $contents); |
|
34
|
|
|
|
|
35
|
|
|
$path = self::$pathForTempCertificate; |
|
36
|
|
|
file_put_contents($path, $contents); |
|
37
|
|
|
|
|
38
|
|
|
/** @var RealMeService $service */ |
|
39
|
|
|
$service = Injector::inst()->get(RealMeService::class); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertEquals('Redacted private key goes here', $service->getCertificateContents($path, 'key')); |
|
42
|
|
|
$this->assertEquals('Redacted certificate goes here', $service->getCertificateContents($path, 'certificate')); |
|
43
|
|
|
|
|
44
|
|
|
unlink($path); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Test certificate with RSA private key |
|
48
|
|
|
*/ |
|
49
|
|
|
|
|
50
|
|
|
$contents = file_get_contents(__DIR__ . '/certs/rsa_cert.pem'); |
|
51
|
|
|
|
|
52
|
|
|
// Strip carriage returns |
|
53
|
|
|
$contents = str_replace("\r", '', $contents); |
|
54
|
|
|
|
|
55
|
|
|
$path = self::$pathForTempCertificate; |
|
56
|
|
|
file_put_contents($path, $contents); |
|
57
|
|
|
|
|
58
|
|
|
/** @var RealMeService $service */ |
|
59
|
|
|
$service = Injector::inst()->get(RealMeService::class); |
|
60
|
|
|
$this->assertEquals('Redacted private key goes here', $service->getCertificateContents($path, 'key')); |
|
61
|
|
|
$this->assertEquals('Redacted certificate goes here', $service->getCertificateContents($path, 'certificate')); |
|
62
|
|
|
|
|
63
|
|
|
unlink($path); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testGetAuth() |
|
67
|
|
|
{ |
|
68
|
|
|
$auth = $this->service->getAuth(new NullHTTPRequest()); |
|
69
|
|
|
$this->assertTrue(get_class($auth) === 'OneLogin_Saml2_Auth'); |
|
70
|
|
|
|
|
71
|
|
|
// Service Provider settings |
|
72
|
|
|
$spData = $auth->getSettings()->getSPData(); |
|
73
|
|
|
$this->assertSame('https://example.com/realm/service', $spData['entityId']); |
|
74
|
|
|
$this->assertSame('https://example.com/Security/login/RealMe/acs', $spData['assertionConsumerService']['url']); |
|
75
|
|
|
$this->assertSame('urn:oasis:names:tc:SAML:2.0:nameid-format:persistent', $spData['NameIDFormat']); |
|
76
|
|
|
|
|
77
|
|
|
// Identity Provider settings |
|
78
|
|
|
$idpData = $auth->getSettings()->getIdPData(); |
|
79
|
|
|
$this->assertSame('https://mts.realme.govt.nz/saml2', $idpData['entityId']); |
|
80
|
|
|
$this->assertSame('https://mts.realme.govt.nz/logon-mts/mtsEntryPoint', $idpData['singleSignOnService']['url']); |
|
81
|
|
|
|
|
82
|
|
|
// Security settings |
|
83
|
|
|
$securityData = $auth->getSettings()->getSecurityData(); |
|
84
|
|
|
$this->assertSame( |
|
85
|
|
|
'urn:nzl:govt:ict:stds:authn:deployment:GLS:SAML:2.0:ac:classes:LowStrength', |
|
86
|
|
|
$securityData['requestedAuthnContext'][0] |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testGetAuthCustomSPEntityId() |
|
91
|
|
|
{ |
|
92
|
|
|
Config::modify()->set( |
|
93
|
|
|
RealMeService::class, |
|
94
|
|
|
'sp_entity_ids', |
|
95
|
|
|
['mts' => 'https://example.com/custom-realm/custom-service'] |
|
96
|
|
|
); |
|
97
|
|
|
$spData = $this->service->getAuth(new NullHTTPRequest())->getSettings()->getSPData(); |
|
98
|
|
|
$this->assertSame('https://example.com/custom-realm/custom-service', $spData['entityId']); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testGetAuthCustomIdPEntityId() |
|
102
|
|
|
{ |
|
103
|
|
|
Config::modify()->set( |
|
104
|
|
|
RealMeService::class, |
|
105
|
|
|
'idp_entity_ids', |
|
106
|
|
|
['mts' => ['login' => 'https://example.com/idp-entry']] |
|
107
|
|
|
); |
|
108
|
|
|
$idpData = $this->service->getAuth(new NullHTTPRequest())->getSettings()->getIdPData(); |
|
109
|
|
|
$this->assertSame('https://example.com/idp-entry', $idpData['entityId']); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function testGetAuthCustomAuthnContext() |
|
113
|
|
|
{ |
|
114
|
|
|
Config::modify()->set( |
|
115
|
|
|
RealMeService::class, |
|
116
|
|
|
'authn_contexts', |
|
117
|
|
|
['mts' => 'urn:nzl:govt:ict:stds:authn:deployment:GLS:SAML:2.0:ac:classes:ModStrength::OTP:Mobile:SMS'] |
|
118
|
|
|
); |
|
119
|
|
|
$securityData = $this->service->getAuth(new NullHTTPRequest())->getSettings()->getSecurityData(); |
|
120
|
|
|
$this->assertSame( |
|
121
|
|
|
'urn:nzl:govt:ict:stds:authn:deployment:GLS:SAML:2.0:ac:classes:ModStrength::OTP:Mobile:SMS', |
|
122
|
|
|
$securityData['requestedAuthnContext'][0] |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public static function setUpBeforeClass() |
|
127
|
|
|
{ |
|
128
|
|
|
Environment::putEnv('REALME_CERT_DIR=' . __DIR__ . '/certs'); |
|
129
|
|
|
Environment::putEnv('REALME_SIGNING_CERT_FILENAME=' . 'standard_cert.pem'); |
|
130
|
|
|
|
|
131
|
|
|
parent::setUpBeforeClass(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
protected function setUp() |
|
135
|
|
|
{ |
|
136
|
|
|
parent::setUp(); |
|
137
|
|
|
$this->service = Injector::inst()->create(RealMeService::class); |
|
138
|
|
|
|
|
139
|
|
|
// Configure for login integration and mts by default |
|
140
|
|
|
Config::modify()->set(RealMeService::class, 'sp_entity_ids', ['mts' => 'https://example.com/realm/service']); |
|
141
|
|
|
Config::modify()->set( |
|
142
|
|
|
RealMeService::class, |
|
143
|
|
|
'metadata_assertion_service_domains', |
|
144
|
|
|
['mts' => 'https://example.com'] |
|
145
|
|
|
); |
|
146
|
|
|
Config::modify()->set( |
|
147
|
|
|
RealMeService::class, |
|
148
|
|
|
'authn_contexts', |
|
149
|
|
|
['mts' => 'urn:nzl:govt:ict:stds:authn:deployment:GLS:SAML:2.0:ac:classes:LowStrength'] |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public static function tearDownAfterClass() |
|
154
|
|
|
{ |
|
155
|
|
|
parent::tearDownAfterClass(); |
|
156
|
|
|
|
|
157
|
|
|
// Ensure self::$pathForTempCertificate is unlink'd (otherwise it won't get unlinked if the test fails) |
|
158
|
|
|
if (file_exists(self::$pathForTempCertificate)) { |
|
159
|
|
|
unlink(self::$pathForTempCertificate); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|