Skip to content

Commit

Permalink
WIP move to SSP UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko Ivančić committed Nov 14, 2024
1 parent 5e2abff commit e881224
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 22 deletions.
2 changes: 1 addition & 1 deletion bin/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
$database = Database::getInstance();
$databaseMigration = new DatabaseMigration($database);

if ($databaseMigration->isUpdated()) {
if ($databaseMigration->isMigrated()) {
echo 'Database is up to date, skipping.' . PHP_EOL;
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion hooks/hook_federationpage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function oidc_hook_federationpage(Template $template): void
$href = Module::getModuleURL('oidc/admin-clients/index.php');
$text = Translate::noop('OpenID Connect Registry');

if (! (new DatabaseMigration())->isUpdated()) {
if (! (new DatabaseMigration())->isMigrated()) {
$href = Module::getModuleURL('oidc/install.php');
$text = Translate::noop('OpenID Connect Installation');
}
Expand Down
18 changes: 17 additions & 1 deletion public/assets/css/src/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,23 @@ h4 {
/* Style for the content area */
.content {
flex-grow: 1;
padding: 20px;
padding-left: 20px;
max-width: inherit;
background-color: #fff;
}

ul.config {
list-style: disc outside none;
}

/* Text colors */
.black-text { color: black; }
.red-text { color: red; }
.lightcoral-text { color: lightcoral; }
.green-text { color: green; }
.yellow-text { color: yellow; }
.blue-text { color: blue; }
.magenta-text { color: magenta; }
.cyan-text { color: cyan; }
.lightcyan-text { color: lightcyan; }
.white-text { color: white; }
3 changes: 3 additions & 0 deletions routing/routes/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
*/
$routes->add(RoutesEnum::AdminConfigOverview->name, RoutesEnum::AdminConfigOverview->value)
->controller([AdminController::class, 'configOverview']);
$routes->add(RoutesEnum::AdminRunMigrations->name, RoutesEnum::AdminRunMigrations->value)
->controller([AdminController::class, 'runMigrations'])
->methods([HttpMethodsEnum::POST->value]);

/**
* OpenID Connect Discovery routes.
Expand Down
2 changes: 2 additions & 0 deletions src/Codebooks/RoutesEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ enum RoutesEnum: string
{
// Admin area
case AdminConfigOverview = 'admin/config-overview';
case AdminRunMigrations = 'admin/run-migrations';
case AdminClients = 'admin/clients';

// Protocols
case Authorization = 'authorization';
Expand Down
26 changes: 25 additions & 1 deletion src/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

namespace SimpleSAML\Module\oidc\Controller;

use SimpleSAML\Locale\Translate;
use SimpleSAML\Module\oidc\Admin\Authorization;
use SimpleSAML\Module\oidc\Codebooks\RoutesEnum;
use SimpleSAML\Module\oidc\Factories\TemplateFactory;
use SimpleSAML\Module\oidc\ModuleConfig;
use SimpleSAML\Module\oidc\Services\DatabaseMigration;
use SimpleSAML\Module\oidc\Services\SessionMessagesService;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

class AdminController
Expand All @@ -16,6 +20,8 @@ public function __construct(
protected readonly ModuleConfig $moduleConfig,
protected readonly TemplateFactory $templateFactory,
protected readonly Authorization $authorization,
protected readonly DatabaseMigration $databaseMigration,
protected readonly SessionMessagesService $sessionMessagesService,
) {
$this->authorization->requireSspAdmin(true);

Check warning on line 26 in src/Controller/AdminController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/AdminController.php#L26

Added line #L26 was not covered by tests
}
Expand All @@ -24,8 +30,26 @@ public function configOverview(): Response
{
return $this->templateFactory->build(
'oidc:config/overview.twig',
['moduleConfig' => $this->moduleConfig],
[
'moduleConfig' => $this->moduleConfig,
'databaseMigration' => $this->databaseMigration,
],
RoutesEnum::AdminConfigOverview->value,
);

Check warning on line 38 in src/Controller/AdminController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/AdminController.php#L31-L38

Added lines #L31 - L38 were not covered by tests
}

public function runMigrations(): Response

Check warning on line 41 in src/Controller/AdminController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/AdminController.php#L41

Added line #L41 was not covered by tests
{
if ($this->databaseMigration->isMigrated()) {
$message = Translate::noop('Database is already migrated.');
$this->sessionMessagesService->addMessage($message);
return new RedirectResponse($this->moduleConfig->getModuleUrl(RoutesEnum::AdminConfigOverview->value));

Check warning on line 46 in src/Controller/AdminController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/AdminController.php#L43-L46

Added lines #L43 - L46 were not covered by tests
}

$this->databaseMigration->migrate();
$message = Translate::noop('Database migrated successfully.');
$this->sessionMessagesService->addMessage($message);

Check warning on line 51 in src/Controller/AdminController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/AdminController.php#L49-L51

Added lines #L49 - L51 were not covered by tests

return new RedirectResponse($this->moduleConfig->getModuleUrl(RoutesEnum::AdminConfigOverview->value));

Check warning on line 53 in src/Controller/AdminController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/AdminController.php#L53

Added line #L53 was not covered by tests
}
}
2 changes: 1 addition & 1 deletion src/Controller/InstallerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(
*/
public function __invoke(ServerRequest $request): Template|RedirectResponse
{
if ($this->databaseMigration->isUpdated()) {
if ($this->databaseMigration->isMigrated()) {
return new RedirectResponse((new HTTP())->addURLParameters('admin-clients/index.php', []));
}

Expand Down
12 changes: 2 additions & 10 deletions src/Factories/AuthSimpleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function build(ClientEntityInterface $clientEntity): Simple
*/
public function getDefaultAuthSource(): Simple
{
return new Simple($this->getDefaultAuthSourceId());
return new Simple($this->moduleConfig->getDefaultAuthSourceId());

Check warning on line 47 in src/Factories/AuthSimpleFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Factories/AuthSimpleFactory.php#L47

Added line #L47 was not covered by tests
}

/**
Expand All @@ -54,14 +54,6 @@ public function getDefaultAuthSource(): Simple
*/
public function resolveAuthSourceId(ClientEntityInterface $client): string
{
return $client->getAuthSourceId() ?? $this->getDefaultAuthSourceId();
}

/**
* @throws \Exception
*/
public function getDefaultAuthSourceId(): string
{
return $this->moduleConfig->config()->getString(ModuleConfig::OPTION_AUTH_SOURCE);
return $client->getAuthSourceId() ?? $this->moduleConfig->getDefaultAuthSourceId();

Check warning on line 57 in src/Factories/AuthSimpleFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Factories/AuthSimpleFactory.php#L57

Added line #L57 was not covered by tests
}
}
10 changes: 10 additions & 0 deletions src/Factories/TemplateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use SimpleSAML\Module\oidc\Bridges\SspBridge;
use SimpleSAML\Module\oidc\Codebooks\RoutesEnum;
use SimpleSAML\Module\oidc\ModuleConfig;
use SimpleSAML\Module\oidc\Services\SessionMessagesService;
use SimpleSAML\XHTML\Template;

class TemplateFactory
Expand All @@ -34,6 +35,7 @@ public function __construct(
protected readonly ModuleConfig $moduleConfig,
protected readonly Menu $oidcMenu,
protected readonly SspBridge $sspBridge,
protected readonly SessionMessagesService $sessionMessagesService,
) {
}

Expand All @@ -60,6 +62,7 @@ public function build(
'moduleConfiguration' => $this->moduleConfig,
'oidcMenu' => $this->oidcMenu,
'showMenu' => $this->showMenu,
'sessionMessages' => $this->sessionMessagesService->getMessages(),
];

Check warning on line 66 in src/Factories/TemplateFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Factories/TemplateFactory.php#L60-L66

Added lines #L60 - L66 were not covered by tests

if ($this->sspBridge->module()->isModuleEnabled('admin')) {
Expand Down Expand Up @@ -87,6 +90,13 @@ protected function includeDefaultMenuItems(): void
\SimpleSAML\Locale\Translate::noop('Config Overview '),
),
);

Check warning on line 92 in src/Factories/TemplateFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Factories/TemplateFactory.php#L87-L92

Added lines #L87 - L92 were not covered by tests

$this->oidcMenu->addItem(
$this->oidcMenu->buildItem(
$this->moduleConfig->getModuleUrl(RoutesEnum::AdminClients->value),
\SimpleSAML\Locale\Translate::noop('Clients '),
),
);

Check warning on line 99 in src/Factories/TemplateFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Factories/TemplateFactory.php#L94-L99

Added lines #L94 - L99 were not covered by tests
}

public function setShowMenu(bool $showMenu): TemplateFactory

Check warning on line 102 in src/Factories/TemplateFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Factories/TemplateFactory.php#L102

Added line #L102 was not covered by tests
Expand Down
8 changes: 8 additions & 0 deletions src/ModuleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ public function getIssuer(): string
return $issuer;
}

/**
* @throws \Exception
*/
public function getDefaultAuthSourceId(): string

Check warning on line 167 in src/ModuleConfig.php

View check run for this annotation

Codecov / codecov/patch

src/ModuleConfig.php#L167

Added line #L167 was not covered by tests
{
return $this->config()->getString(self::OPTION_AUTH_SOURCE);

Check warning on line 169 in src/ModuleConfig.php

View check run for this annotation

Codecov / codecov/patch

src/ModuleConfig.php#L169

Added line #L169 was not covered by tests
}

public function getModuleUrl(string $path = null): string
{
$base = $this->sspBridge->module()->getModuleURL(self::MODULE_NAME);
Expand Down
1 change: 1 addition & 0 deletions src/Services/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public function __construct()
$moduleConfig,
$oidcMenu,
$sspBridge,
$sessionMessagesService,
);

Check warning on line 165 in src/Services/Container.php

View check run for this annotation

Codecov / codecov/patch

src/Services/Container.php#L159-L165

Added lines #L159 - L165 were not covered by tests
$this->services[TemplateFactory::class] = $templateFactory;

Expand Down
11 changes: 7 additions & 4 deletions src/Services/DatabaseMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,21 @@ public function __construct(Database $database = null)
$this->database = $database ?? Database::getInstance();
}

public function isUpdated(): bool
public function isMigrated(): bool

Check warning on line 38 in src/Services/DatabaseMigration.php

View check run for this annotation

Codecov / codecov/patch

src/Services/DatabaseMigration.php#L38

Added line #L38 was not covered by tests
{
return empty($this->getNotImplementedVersions());

Check warning on line 40 in src/Services/DatabaseMigration.php

View check run for this annotation

Codecov / codecov/patch

src/Services/DatabaseMigration.php#L40

Added line #L40 was not covered by tests
}

public function getNotImplementedVersions(): array

Check warning on line 43 in src/Services/DatabaseMigration.php

View check run for this annotation

Codecov / codecov/patch

src/Services/DatabaseMigration.php#L43

Added line #L43 was not covered by tests
{
$implementedVersions = $this->versions();
$notImplementedVersions = array_filter(get_class_methods($this), function ($method) use ($implementedVersions) {
return array_filter(get_class_methods($this), function ($method) use ($implementedVersions) {

Check warning on line 46 in src/Services/DatabaseMigration.php

View check run for this annotation

Codecov / codecov/patch

src/Services/DatabaseMigration.php#L46

Added line #L46 was not covered by tests
if (preg_match('/^version(\d+)/', $method, $matches)) {
return !in_array($matches[1], $implementedVersions, true);
}

return false;
});

return empty($notImplementedVersions);
}

public function versions(): array
Expand Down
8 changes: 7 additions & 1 deletion templates/base.twig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
<div class="content">
<h3>{{ subPageTitle }}</h3>

{# TODO mivanci status messages#}
{% if sessionMessages is defined and sessionMessages is not empty %}
<div class="message-box">
{% for message in sessionMessages %}
{{ message|trans }}<br>
{% endfor %}
</div>
{% endif %}

{% block oidcContent %}{% endblock %}
</div>
Expand Down
118 changes: 117 additions & 1 deletion templates/config/overview.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,122 @@

{% block oidcContent %}

<p>{{ 'TODO config overview'|trans }}</p>
<h4>{{ 'Database Migrations'|trans }}</h4>

{% if databaseMigration.isMigrated %}
<p><i class="fa fa-check" title="OK"></i>{{ 'All database migrations are implemented.'|trans }}</p>
{% else %}
<p class="red-text">
<i class="fa fa-ban" title="Not OK"></i>
{% trans %}There are database migrations that have not been implemented.
Use the button below to run them now.{% endtrans %}
</p>

<form method="post" class="pure-form" action="{{ moduleURL('oidc/admin/run-migrations') }}">
<input type="hidden" name="fromUi" value="1">
<input type="hidden" name="migrate" value="1">
<button class="pure-button pure-button-red ">{{ 'Run migrations'|trans }}</button>
</form>
<br>
{% endif %}

<h4>{{ 'Protocol Settings'|trans }}</h4>

<table class="table pure-table pure-table-bordered ">
<thead>
<tr>
<th>{{ 'Setting'|trans }}</th>
<th>{{ 'Value'|trans }}</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ 'Issuer'|trans }}</td>
<td> {{ moduleConfig.getIssuer }}</td>
</tr>
<tr>
<td>{{ 'Tokens Time-To-Live'|trans }}</td>
<td>
<ul class="config">
<li>
{{ 'Authorization Code:'|trans }}
{{ moduleConfig.getAuthCodeDuration|date("%mm %dd %hh %i' %s''") }}
</li>
<li>
{{ 'Access Token:'|trans }}
{{ moduleConfig.getAccessTokenDuration|date("%mm %dd %hh %i' %s''") }}
</li>
<li>
{{ 'Refresh Token:'|trans }}
{{ moduleConfig.getRefreshTokenDuration|date("%mm %dd %hh %i' %s''") }}
</li>
</ul>

</td>
</tr>
<tr>
<td>{{ 'Default Authentication Source'|trans }}</td>
<td> {{ moduleConfig.getDefaultAuthSourceId }}</td>
</tr>
<tr>
<td>{{ 'User Identifier Attribute'|trans }}</td>
<td> {{ moduleConfig.getUserIdentifierAttribute }}</td>
</tr>
<tr>
<td>{{ 'PKI'|trans }}</td>
<td>
<ul class="config">
<li>Private Key: {{ moduleConfig.getProtocolPrivateKeyPath }}</li>
<li>
Private Key Password Set:
{{ moduleConfig.getProtocolPrivateKeyPassPhrase ? 'Yes'|trans : 'No'|trans }}
</li>
<li>
{{ 'Public Key:'|trans }}
{{ moduleConfig.getProtocolCertPath }}
</li>
</ul>

</td>
</tr>
<tr>
<td>{{ 'Signing Algorithm'|trans }}</td>
<td> {{ moduleConfig.getProtocolSigner.algorithmId }}</td>
</tr>
<tr>
<td>{{ 'Supported ACRs'|trans }}</td>
<td>
{% if moduleConfig.getAcrValuesSupported is not empty %}
<ul class="config">
{% for acr in moduleConfig.getAcrValuesSupported %}
<li>{{ acr }}</li>
{% endfor %}
</ul>
{% else %}
{{ 'None defined'|trans }}
{% endif %}
</td>
</tr>
</tbody>
</table>

<br>
<h4>{{ 'Federation Settings'|trans }}</h4>

<table class="table pure-table pure-table-bordered ">
<thead>
<tr>
<th>{{ 'Setting'|trans }}</th>
<th>{{ 'Value'|trans }}</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ 'Federation Enabled:'|trans }}</td>
<td> {{ moduleConfig.getFederationEnabled ? 'Yes'|trans : 'No'|trans }}</td>
</tr>

</tbody>
</table>

{% endblock oidcContent -%}
2 changes: 1 addition & 1 deletion tests/unit/src/Controller/InstallerControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testItReturnsToMainPageIfAlreadyUpdated(): void
{
$this->databaseMigrationMock
->expects($this->once())
->method('isUpdated')
->method('isMigrated')
->willReturn(true);

$this->assertInstanceOf(
Expand Down

0 comments on commit e881224

Please sign in to comment.