Skip to content
Snippets Groups Projects
Commit f158e9c5 authored by Thijs Kinkhorst's avatar Thijs Kinkhorst
Browse files

Add metadata converter to admin module

parent 9841f6cb
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,10 @@ use SimpleSAML\Module\saml\IdP\SAML1 as SAML1_IdP; ...@@ -11,6 +11,10 @@ use SimpleSAML\Module\saml\IdP\SAML1 as SAML1_IdP;
use SimpleSAML\Module\saml\IdP\SAML2 as SAML2_IdP; use SimpleSAML\Module\saml\IdP\SAML2 as SAML2_IdP;
use SimpleSAML\Utils\Auth; use SimpleSAML\Utils\Auth;
use SimpleSAML\HTTP\RunnableResponse;
use SimpleSAML\Utils\HTTP;
use Symfony\Component\HttpFoundation\Request;
/** /**
* Controller class for the admin module. * Controller class for the admin module.
* *
...@@ -371,4 +375,70 @@ class FederationController ...@@ -371,4 +375,70 @@ class FederationController
return $entities; return $entities;
} }
/**
* Metadata converter
*
* @param Request $request The current request.
*
* @return \SimpleSAML\XHTML\Template
*/
public function metadataConverter(Request $request)
{
\SimpleSAML\Utils\Auth::requireAdmin();
if ($xmlfile = $request->files->get('xmlfile')) {
$xmldata = trim(file_get_contents($xmlfile));
} elseif ($xmldata = $request->request->get('xmldata')) {
$xmldata = trim($xmldata);
}
if (!empty($xmldata)) {
\SimpleSAML\Utils\XML::checkSAMLMessage($xmldata, 'saml-meta');
$entities = \SimpleSAML\Metadata\SAMLParser::parseDescriptorsString($xmldata);
// get all metadata for the entities
foreach ($entities as &$entity) {
$entity = [
'shib13-sp-remote' => $entity->getMetadata1xSP(),
'shib13-idp-remote' => $entity->getMetadata1xIdP(),
'saml20-sp-remote' => $entity->getMetadata20SP(),
'saml20-idp-remote' => $entity->getMetadata20IdP(),
];
}
// transpose from $entities[entityid][type] to $output[type][entityid]
$output = \SimpleSAML\Utils\Arrays::transpose($entities);
// merge all metadata of each type to a single string which should be added to the corresponding file
foreach ($output as $type => &$entities) {
$text = '';
foreach ($entities as $entityId => $entityMetadata) {
if ($entityMetadata === null) {
continue;
}
// remove the entityDescriptor element because it is unused, and only makes the output harder to read
unset($entityMetadata['entityDescriptor']);
$text .= '$metadata['.var_export($entityId, true).'] = '.
var_export($entityMetadata, true).";\n";
}
$entities = $text;
}
} else {
$xmldata = '';
$output = [];
}
$t = new \SimpleSAML\XHTML\Template($this->config, 'admin:metadata_converter.twig');
$t->data = [
'logouturl' => \SimpleSAML\Utils\Auth::getAdminLogoutURL(),
'xmldata' => $xmldata,
'output' => $output,
];
$this->menu->addOption('logout', \SimpleSAML\Utils\Auth::getAdminLogoutURL(), Translate::noop('Log out'));
return $this->menu->insert($t);
}
} }
...@@ -13,3 +13,6 @@ admin-test: ...@@ -13,3 +13,6 @@ admin-test:
admin-fed: admin-fed:
path: /federation path: /federation
defaults: { _controller: 'SimpleSAML\Module\admin\FederationController::main' } defaults: { _controller: 'SimpleSAML\Module\admin\FederationController::main' }
admin-fed-converter:
path: /metadata-converter
defaults: { _controller: 'SimpleSAML\Module\admin\FederationController::metadataConverter' }
{% set pagetitle = 'Metadata parser'|trans %}
{% set frontpage_section = 'federation' %}
{% extends "base.twig" %}
{% set i=1 %}
{% block content %}
{%- include "@admin/includes/menu.twig" %}
<h2>{{ pagetitle }}</h2>
<form method="post" class="pure-form" enctype="multipart/form-data">
<h3> {% trans 'XML metadata' %}</h3>
<div class="pure-control-group">
<textarea name="xmldata" rows="20" class="text-area edge">{{ xmldata }}</textarea>
</div>
<br>
<div class="center">
<div class="pure-button-group two-elements" role="group">
<label class="pure-button">
<span class="fa fa-folder-open"></span>{{ 'or select a file:'|trans }}
<input type="file" name="xmlfile" class="hidden" multiple>
</label>
{#needs translation#}
<label id="show-file" class="pure-button hollow show-files" disabled>No file selected.</label>
</div>
<br>
<button class="pure-button pure-button-red pure-input-1-3">{{ 'Parse'|trans }}</button>
</div>
</form>
{% if output -%}
<br>
<h2>{{ 'Converted metadata'|trans }}</h2>
{% for type, text in output if text -%}
{# spaceless is to work around a clipboard.js bug that would add extra whitespace #}
{% spaceless %}
<div class="code-box">
<div class="code-box-title">
<h3>{{ type }}</h3>
<button data-clipboard-target="#metadata{{ loop.index }}" id="btn{{ loop.index }}" class="pure-button right clipboard-btn copy">
<i class="fa fa-copy"></i>
</button>
</div>
<div class="code-box-content">
<pre id="metadata{{ loop.index }}">{{ text|escape }}</pre>
</div>
</div>
{% endspaceless %}
<br><br>
{%- set i=i+1 %}
{%- endfor -%}
{% endif -%}
{% endblock content -%}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment