Skip to content

Commit e163da2

Browse files
committed
Ability to set custom schema path
1 parent c5b05cd commit e163da2

File tree

6 files changed

+46
-12
lines changed

6 files changed

+46
-12
lines changed

lib/Saml2/LogoutRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public function isValid($retrieveParametersFromServer = false)
340340
$security = $this->_settings->getSecurityData();
341341

342342
if ($security['wantXMLValidation']) {
343-
$res = OneLogin_Saml2_Utils::validateXML($dom, 'saml-schema-protocol-2.0.xsd', $this->_settings->isDebugActive());
343+
$res = OneLogin_Saml2_Utils::validateXML($dom, 'saml-schema-protocol-2.0.xsd', $this->_settings->isDebugActive(), $this->_settings->getSchemasPath());
344344
if (!$res instanceof DOMDocument) {
345345
throw new OneLogin_Saml2_ValidationError(
346346
"Invalid SAML Logout Request. Not match the saml-schema-protocol-2.0.xsd",

lib/Saml2/LogoutResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function isValid($requestId = null, $retrieveParametersFromServer = false
127127
$security = $this->_settings->getSecurityData();
128128

129129
if ($security['wantXMLValidation']) {
130-
$res = OneLogin_Saml2_Utils::validateXML($this->document, 'saml-schema-protocol-2.0.xsd', $this->_settings->isDebugActive());
130+
$res = OneLogin_Saml2_Utils::validateXML($this->document, 'saml-schema-protocol-2.0.xsd', $this->_settings->isDebugActive(), $this->_settings->getSchemasPath());
131131
if (!$res instanceof DOMDocument) {
132132
throw new OneLogin_Saml2_ValidationError(
133133
"Invalid SAML Logout Response. Not match the saml-schema-protocol-2.0.xsd",

lib/Saml2/Response.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function isValid($requestId = null)
143143

144144
if ($security['wantXMLValidation']) {
145145
$errorXmlMsg = "Invalid SAML Response. Not match the saml-schema-protocol-2.0.xsd";
146-
$res = OneLogin_Saml2_Utils::validateXML($this->document, 'saml-schema-protocol-2.0.xsd', $this->_settings->isDebugActive());
146+
$res = OneLogin_Saml2_Utils::validateXML($this->document, 'saml-schema-protocol-2.0.xsd', $this->_settings->isDebugActive(), $this->_settings->getSchemasPath());
147147
if (!$res instanceof DOMDocument) {
148148
throw new OneLogin_Saml2_ValidationError(
149149
$errorXmlMsg,
@@ -153,7 +153,7 @@ public function isValid($requestId = null)
153153

154154
# If encrypted, check also the decrypted document
155155
if ($this->encrypted) {
156-
$res = OneLogin_Saml2_Utils::validateXML($this->decryptedDocument, 'saml-schema-protocol-2.0.xsd', $this->_settings->isDebugActive());
156+
$res = OneLogin_Saml2_Utils::validateXML($this->decryptedDocument, 'saml-schema-protocol-2.0.xsd', $this->_settings->isDebugActive(), $this->_settings->getSchemasPath());
157157
if (!$res instanceof DOMDocument) {
158158
throw new OneLogin_Saml2_ValidationError(
159159
$errorXmlMsg,

lib/Saml2/Settings.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private function _loadPaths()
159159
'base' => $basePath,
160160
'config' => $basePath,
161161
'cert' => $basePath.'certs/',
162-
'lib' => $basePath.'lib/',
162+
'lib' => $basePath.'lib/Saml2/',
163163
'extlib' => $basePath.'extlib/'
164164
);
165165

@@ -226,9 +226,23 @@ public function getExtLibPath()
226226
*/
227227
public function getSchemasPath()
228228
{
229+
if (isset($this->_paths['schemas'])) {
230+
return $this->_paths['schemas'];
231+
}
229232
return $this->_paths['lib'].'schemas/';
230233
}
231234

235+
/**
236+
* Set schemas path
237+
*
238+
* @param string $path
239+
* @return $this
240+
*/
241+
public function setSchemasPath($path)
242+
{
243+
$this->_paths['schemas'] = $path;
244+
}
245+
232246
/**
233247
* Loads settings info from a settings Array
234248
*
@@ -934,7 +948,7 @@ public function validateMetadata($xml)
934948
assert('is_string($xml)');
935949

936950
$errors = array();
937-
$res = OneLogin_Saml2_Utils::validateXML($xml, 'saml-schema-metadata-2.0.xsd', $this->_debug);
951+
$res = OneLogin_Saml2_Utils::validateXML($xml, 'saml-schema-metadata-2.0.xsd', $this->_debug, $this->getSchemasPath());
938952
if (!$res instanceof DOMDocument) {
939953
$errors[] = $res;
940954
} else {

lib/Saml2/Utils.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,13 @@ public static function loadXML($dom, $xml)
113113
* @param string|DOMDocument $xml The XML string or document which should be validated.
114114
* @param string $schema The schema filename which should be used.
115115
* @param bool $debug To disable/enable the debug mode
116+
* @param string $schemaPath Change schema path
116117
*
117118
* @return string|DOMDocument $dom string that explains the problem or the DOMDocument
118119
*
119120
* @throws Exception
120121
*/
121-
public static function validateXML($xml, $schema, $debug = false)
122+
public static function validateXML($xml, $schema, $debug = false, $schemaPath = null)
122123
{
123124
assert('is_string($xml) || $xml instanceof DOMDocument');
124125
assert('is_string($schema)');
@@ -136,7 +137,12 @@ public static function validateXML($xml, $schema, $debug = false)
136137
}
137138
}
138139

139-
$schemaFile = __DIR__.'/schemas/' . $schema;
140+
if (isset($schemaPath)) {
141+
$schemaFile = $schemaPath . $schema;
142+
} else {
143+
$schemaFile = __DIR__ . '/schemas/' . $schema;
144+
}
145+
140146
$oldEntityLoader = libxml_disable_entity_loader(false);
141147
$res = $dom->schemaValidate($schemaFile);
142148
libxml_disable_entity_loader($oldEntityLoader);
@@ -626,7 +632,7 @@ public static function getSelfRoutedURLNoQuery()
626632
if (!empty($_SERVER['REQUEST_URI'])) {
627633
$route = $_SERVER['REQUEST_URI'];
628634
if (!empty($_SERVER['QUERY_STRING'])) {
629-
$route = self::str_lreplace($_SERVER['QUERY_STRING'], '', $route);
635+
$route = self::strLreplace($_SERVER['QUERY_STRING'], '', $route);
630636
if (substr($route, -1) == '?') {
631637
$route = substr($route, 0, -1);
632638
}
@@ -648,7 +654,7 @@ public static function getSelfRoutedURLNoQuery()
648654
return $selfRoutedURLNoQuery;
649655
}
650656

651-
public static function str_lreplace($search, $replace, $subject)
657+
public static function strLreplace($search, $replace, $subject)
652658
{
653659
$pos = strrpos($subject, $search);
654660

tests/src/OneLogin/Saml2/SettingsTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function testGetLibPath()
9595
$settings = new OneLogin_Saml2_Settings();
9696
$base = $settings->getBasePath();
9797

98-
$this->assertEquals($base.'lib/', $settings->getLibPath());
98+
$this->assertEquals($base.'lib/Saml2/', $settings->getLibPath());
9999
}
100100

101101
/**
@@ -121,10 +121,24 @@ public function testGetSchemasPath()
121121
$settings = new OneLogin_Saml2_Settings();
122122
$base = $settings->getBasePath();
123123

124-
$this->assertEquals($base.'lib/schemas/', $settings->getSchemasPath());
124+
$this->assertEquals($base.'lib/Saml2/schemas/', $settings->getSchemasPath());
125125

126126
}
127127

128+
/**
129+
* Tests getSchemasPath method of the Settings
130+
*
131+
* @covers OneLogin_Saml2_Settings::setSchemasPath
132+
*/
133+
public function testSetSchemasPath()
134+
{
135+
$settings = new OneLogin_Saml2_Settings();
136+
$base = $settings->getBasePath();
137+
$this->assertEquals($base.'lib/Saml2/schemas/', $settings->getSchemasPath());
138+
$settings->setSchemasPath('custompath/');
139+
$this->assertEquals('custompath/', $settings->getSchemasPath());
140+
}
141+
128142
/**
129143
* Tests shouldCompressRequests method of OneLogin_Saml2_Settings.
130144
*

0 commit comments

Comments
 (0)