Skip to content

Commit 014452f

Browse files
committed
extract attributes from XML
1 parent 50f0883 commit 014452f

File tree

2 files changed

+110
-11
lines changed

2 files changed

+110
-11
lines changed

src/Debug/SamlauthDebugReactor.php

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Drupal\ubc_cwl_auth\Debug;
44

55
use Drupal\Core\Config\ConfigFactoryInterface;
6+
use DOMDocument;
7+
use DOMXPath;
68

79
/**
810
* Reactor service — keep this light; push heavy work to queue/worker.
@@ -28,19 +30,60 @@ public function __construct(ConfigFactoryInterface $config_factory) {
2830
* @param array $context
2931
*/
3032
public function handleDebugMessage(string $message, array $context = []) : void {
31-
// Obtain the logger at runtime to avoid container circular references.
32-
$logger = \Drupal::logger('ubc_cwl_auth');
3333

34-
if (strpos($message, 'SAML') !== FALSE || TRUE) {
34+
$config = $this->configFactory->get('ubc_cwl_auth.settings');
35+
if($config->get('ubc_cwl_auth_debug') == 1) {
3536

36-
$config = $this->configFactory->get('ubc_cwl_auth.settings');
37+
if (strpos($message, 'ACS received SAML response') !== FALSE || TRUE) {
3738

38-
if($config->get('ubc_cwl_auth_debug') == 1) {
39+
$attributes = $this->extractAttributes($context['@message'], $config);
40+
$data = json_encode($attributes);
3941

4042
$cid = time();
41-
\Drupal::cache('ubc_cwl_auth')->set($cid, $message, (time() + 24*60*60));
43+
\Drupal::cache('ubc_cwl_auth')->set($cid, $data, (time() + 24*60*60));
4244
}
45+
}
46+
}
47+
48+
private function extractAttributes($xml, $config) {
49+
50+
// Load XML into DOM
51+
$doc = new DOMDocument();
52+
$doc->loadXML($xml);
53+
54+
// Create XPath with namespaces
55+
$xpath = new DOMXPath($doc);
56+
$xpath->registerNamespace('saml2', 'urn:oasis:names:tc:SAML:2.0:assertion');
4357

58+
// The attributes we care about
59+
$targetAttrs = [$config->get('ubc_cwl_auth_attr1'),
60+
$config->get('ubc_cwl_auth_attr2'),
61+
$config->get('ubc_cwl_auth_attr3'),
62+
$config->get('ubc_cwl_auth_attr4'),
63+
$config->get('ubc_cwl_auth_attr5')];
64+
$targetAttrs = array_filter($targetAttrs);
65+
66+
$results = [];
67+
68+
// Loop through each target attribute
69+
foreach ($targetAttrs as $attrName) {
70+
$query = "//saml2:Attribute[@FriendlyName='$attrName']/saml2:AttributeValue";
71+
$values = [];
72+
foreach ($xpath->query($query) as $node) {
73+
$values[] = trim($node->textContent);
74+
}
75+
// If multiple values (like eduPersonAffiliation), join with comma
76+
if ($values) {
77+
$results[$attrName] = implode(', ', $values);
78+
}
4479
}
80+
81+
// Print the results
82+
$return = [];
83+
foreach ($results as $key => $value) {
84+
$return[$key] = $value;
85+
}
86+
return $return;
4587
}
88+
4689
}

src/Form/SettingsForm.php

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,42 @@ public function buildForm(array $form, FormStateInterface $form_state) {
3434
'#type' => 'checkbox',
3535
'#title' => $this->t('UBC CWL Debug Mode'),
3636
'#default_value' => $config->get('ubc_cwl_auth_debug'),
37-
'#description' => $this->t('Turn on debug mode'),
37+
'#description' => $this->t('Turn on debug mode. Log SAML responses.'),
3838
'#return_value' => 1,
3939
];
4040

41+
$form['attr1'] = [
42+
'#type' => 'textfield',
43+
'#title' => $this->t('Attribute 1'),
44+
'#default_value' => $config->get('ubc_cwl_auth_attr1'),
45+
];
46+
47+
$form['attr2'] = [
48+
'#type' => 'textfield',
49+
'#title' => $this->t('Attribute 2'),
50+
'#default_value' => $config->get('ubc_cwl_auth_attr2'),
51+
];
52+
53+
$form['attr3'] = [
54+
'#type' => 'textfield',
55+
'#title' => $this->t('Attribute 3'),
56+
'#default_value' => $config->get('ubc_cwl_auth_attr3'),
57+
];
58+
59+
$form['attr4'] = [
60+
'#type' => 'textfield',
61+
'#title' => $this->t('Attribute 4'),
62+
'#default_value' => $config->get('ubc_cwl_auth_attr4'),
63+
];
64+
65+
$form['attr5'] = [
66+
'#type' => 'textfield',
67+
'#title' => $this->t('Attribute 5'),
68+
'#default_value' => $config->get('ubc_cwl_auth_attr5'),
69+
];
70+
4171
$form['attributes'] = [
42-
'#markup' => $this->getDebugAttributes(),
72+
'#markup' => $this->getDebugAttributes($config),
4373
];
4474

4575
return parent::buildForm($form, $form_state);
@@ -51,18 +81,44 @@ public function buildForm(array $form, FormStateInterface $form_state) {
5181
public function submitForm(array &$form, FormStateInterface $form_state) {
5282
$this->config('ubc_cwl_auth.settings')
5383
->set('ubc_cwl_auth_debug', $form_state->getValue('debug'))
84+
->set('ubc_cwl_auth_attr1', $form_state->getValue('attr1'))
85+
->set('ubc_cwl_auth_attr2', $form_state->getValue('attr2'))
86+
->set('ubc_cwl_auth_attr3', $form_state->getValue('attr3'))
87+
->set('ubc_cwl_auth_attr4', $form_state->getValue('attr4'))
88+
->set('ubc_cwl_auth_attr5', $form_state->getValue('attr5'))
5489
->save();
5590

5691
\Drupal::logger('ubc_cwl_auth')->notice('UBC_CWL_AUTH Debug Setting: '.$form_state->getValue('debug'));
5792

5893
parent::submitForm($form, $form_state);
5994
}
6095

61-
private function getDebugAttributes() {
96+
/**
97+
* Fetch rows from the cache_ubc_cwl_auth table and display
98+
*/
99+
private function getDebugAttributes($config) {
100+
101+
if($config->get('ubc_cwl_auth_debug') != 1) {
102+
return '';
103+
}
104+
105+
$database = \Drupal::database();
106+
$query = $database->select('cache_ubc_cwl_auth', 'c')
107+
->fields('c', ['cid', 'data']);
108+
$result = $query->execute()->fetchAll();
62109

63-
//\Drupal::logger('samlauth')->debug('Test SAML debug message from test');
110+
$table = '<table><thead><tr><th>CID</th><th>DATA</th></tr></thead>';
111+
foreach ($result as $item) {
112+
$cid = $item->cid;
113+
$data = json_decode($item->data);
114+
$data = (array)$data;
64115

65-
return '<p>TODO: fetch data from Cache</p>';
116+
$table .= '<tr><td>'.$cid.'</td><td>'.print_r($data, true).'</td></tr>';
117+
}
118+
$table .= '</table>';
119+
120+
return $table;
66121
}
67122

123+
68124
}

0 commit comments

Comments
 (0)