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 25, 2024
1 parent 76073b5 commit 260dec9
Show file tree
Hide file tree
Showing 15 changed files with 584 additions and 328 deletions.
2 changes: 2 additions & 0 deletions public/assets/css/src/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,5 @@ table.client-table {
width: 25%;
font-weight: bolder;
}

.confirm-action {}
22 changes: 22 additions & 0 deletions public/assets/js/src/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

(function() {

// Attach `confirm-action` click event to all elements with the `confirm-action` class.
document.querySelectorAll('.confirm-action').forEach(button => {
button.addEventListener('click', function (event) {
// Get custom confirmation text
const confirmText = this.getAttribute('data-confirm-text') ?? 'Are you sure?';
// Optional: Retrieve additional data
const itemId = this.getAttribute('data-confirm-id') ?? 'N/A';

if (!confirm(confirmText)) {
// Prevent the default action if the user cancels
event.preventDefault();
} else {
// Optional: Handle confirmed action
console.log(
`Confirmed action "${confirmText}" for item with ID "${itemId}"`);
}
});
});
})();
3 changes: 3 additions & 0 deletions routing/routes/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
->controller([ClientController::class, 'index']);
$routes->add(RoutesEnum::AdminClientsShow->name, RoutesEnum::AdminClientsShow->value)
->controller([ClientController::class, 'show']);
$routes->add(RoutesEnum::AdminClientsResetSecret->name, RoutesEnum::AdminClientsResetSecret->value)
->controller([ClientController::class, 'resetSecret'])
->methods([HttpMethodsEnum::POST->value]);

/*****************************************************************************************************************
* OpenID Connect
Expand Down
10 changes: 10 additions & 0 deletions src/Codebooks/ParametersEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Module\oidc\Codebooks;

enum ParametersEnum: string
{
case ClientId = 'client_id';
}
1 change: 1 addition & 0 deletions src/Codebooks/RoutesEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ enum RoutesEnum: string

case AdminClients = 'admin/clients';
case AdminClientsShow = 'admin/clients/show';
case AdminClientsResetSecret = 'admin/clients/reset-secret';

/*****************************************************************************************************************
* OpenID Connect
Expand Down
40 changes: 36 additions & 4 deletions src/Controllers/Admin/ClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

namespace SimpleSAML\Module\oidc\Controllers\Admin;

use SimpleSAML\Locale\Translate;
use SimpleSAML\Module\oidc\Admin\Authorization;
use SimpleSAML\Module\oidc\Bridges\SspBridge;
use SimpleSAML\Module\oidc\Codebooks\ParametersEnum;
use SimpleSAML\Module\oidc\Codebooks\RoutesEnum;
use SimpleSAML\Module\oidc\Entities\Interfaces\ClientEntityInterface;
use SimpleSAML\Module\oidc\Exceptions\OidcException;
use SimpleSAML\Module\oidc\Factories\TemplateFactory;
use SimpleSAML\Module\oidc\Repositories\AllowedOriginRepository;
use SimpleSAML\Module\oidc\Repositories\ClientRepository;
use SimpleSAML\Module\oidc\Services\AuthContextService;
use SimpleSAML\Module\oidc\Services\SessionMessagesService;
use SimpleSAML\Module\oidc\Utils\Routes;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -22,6 +27,9 @@ public function __construct(
protected readonly Authorization $authorization,
protected readonly ClientRepository $clientRepository,
protected readonly AllowedOriginRepository $allowedOriginRepository,
protected readonly SspBridge $sspBridge,
protected readonly SessionMessagesService $sessionMessagesService,
protected readonly Routes $routes,
) {
$this->authorization->requireAdminOrUserWithPermission(AuthContextService::PERM_CLIENT);

Check warning on line 34 in src/Controllers/Admin/ClientController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ClientController.php#L34

Added line #L34 was not covered by tests
}
Expand All @@ -33,7 +41,7 @@ public function __construct(
*/
protected function getClientFromRequest(Request $request): ClientEntityInterface

Check warning on line 42 in src/Controllers/Admin/ClientController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ClientController.php#L42

Added line #L42 was not covered by tests
{
($clientId = $request->query->getString('client_id'))
($clientId = $request->query->getString(ParametersEnum::ClientId->value))
|| throw new OidcException('Client ID not provided.');

Check warning on line 45 in src/Controllers/Admin/ClientController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ClientController.php#L44-L45

Added lines #L44 - L45 were not covered by tests

$authedUserId = $this->authorization->isAdmin() ? null : $this->authorization->getUserId();

Check warning on line 47 in src/Controllers/Admin/ClientController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ClientController.php#L47

Added line #L47 was not covered by tests
Expand All @@ -50,7 +58,6 @@ public function index(Request $request): Response

$pagination = $this->clientRepository->findPaginated($page, $query, $authedUserId);

Check warning on line 59 in src/Controllers/Admin/ClientController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ClientController.php#L59

Added line #L59 was not covered by tests


return $this->templateFactory->build(
'oidc:clients.twig',
[
Expand All @@ -71,14 +78,39 @@ public function show(Request $request): Response
$client = $this->getClientFromRequest($request);
$allowedOrigins = $this->allowedOriginRepository->get($client->getIdentifier());

Check warning on line 79 in src/Controllers/Admin/ClientController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ClientController.php#L78-L79

Added lines #L78 - L79 were not covered by tests

// TODO mivanci rename *-ssp.twig templates after removing old ones.
return $this->templateFactory->build(
'oidc:clients/show-ssp.twig',
'oidc:clients/show.twig',
[
'client' => $client,
'allowedOrigins' => $allowedOrigins,
],
RoutesEnum::AdminClients->value,
);

Check warning on line 88 in src/Controllers/Admin/ClientController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ClientController.php#L81-L88

Added lines #L81 - L88 were not covered by tests
}

/**
* @throws \SimpleSAML\Module\oidc\Exceptions\OidcException
*/
public function resetSecret(Request $request): Response

Check warning on line 94 in src/Controllers/Admin/ClientController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ClientController.php#L94

Added line #L94 was not covered by tests
{
$client = $this->getClientFromRequest($request);

Check warning on line 96 in src/Controllers/Admin/ClientController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ClientController.php#L96

Added line #L96 was not covered by tests

$oldSecret = $request->request->get('secret');

Check warning on line 98 in src/Controllers/Admin/ClientController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ClientController.php#L98

Added line #L98 was not covered by tests

if ($oldSecret !== $client->getSecret()) {
throw new OidcException('Client secret does not match.');

Check warning on line 101 in src/Controllers/Admin/ClientController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ClientController.php#L100-L101

Added lines #L100 - L101 were not covered by tests
}

$client->restoreSecret($this->sspBridge->utils()->random()->generateID());
$authedUserId = $this->authorization->isAdmin() ? null : $this->authorization->getUserId();
$this->clientRepository->update($client, $authedUserId);

Check warning on line 106 in src/Controllers/Admin/ClientController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ClientController.php#L104-L106

Added lines #L104 - L106 were not covered by tests

$message = Translate::noop('Client secret has been reset.');
$this->sessionMessagesService->addMessage($message);

Check warning on line 109 in src/Controllers/Admin/ClientController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ClientController.php#L108-L109

Added lines #L108 - L109 were not covered by tests

return $this->routes->getRedirectResponseToModuleUrl(
RoutesEnum::AdminClientsShow->value,
[ParametersEnum::ClientId->value => $client->getIdentifier()],
);

Check warning on line 114 in src/Controllers/Admin/ClientController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ClientController.php#L111-L114

Added lines #L111 - L114 were not covered by tests
}
}
7 changes: 4 additions & 3 deletions src/Controllers/Admin/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use SimpleSAML\Module\oidc\ModuleConfig;
use SimpleSAML\Module\oidc\Services\DatabaseMigration;
use SimpleSAML\Module\oidc\Services\SessionMessagesService;
use SimpleSAML\Module\oidc\Utils\Routes;
use SimpleSAML\OpenID\Federation;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

class ConfigController
Expand All @@ -24,6 +24,7 @@ public function __construct(
protected readonly DatabaseMigration $databaseMigration,
protected readonly SessionMessagesService $sessionMessagesService,
protected readonly Federation $federation,
protected readonly Routes $routes,
) {
$this->authorization->requireAdmin(true);

Check warning on line 29 in src/Controllers/Admin/ConfigController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ConfigController.php#L29

Added line #L29 was not covered by tests
}
Expand All @@ -44,14 +45,14 @@ public function runMigrations(): Response
if ($this->databaseMigration->isMigrated()) {
$message = Translate::noop('Database is already migrated.');
$this->sessionMessagesService->addMessage($message);
return new RedirectResponse($this->moduleConfig->getModuleUrl(RoutesEnum::AdminMigrations->value));
return $this->routes->getRedirectResponseToModuleUrl(RoutesEnum::AdminMigrations->value);

Check warning on line 48 in src/Controllers/Admin/ConfigController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ConfigController.php#L45-L48

Added lines #L45 - L48 were not covered by tests
}

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

Check warning on line 53 in src/Controllers/Admin/ConfigController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ConfigController.php#L51-L53

Added lines #L51 - L53 were not covered by tests

return new RedirectResponse($this->moduleConfig->getModuleUrl(RoutesEnum::AdminMigrations->value));
return $this->routes->getRedirectResponseToModuleUrl(RoutesEnum::AdminMigrations->value);

Check warning on line 55 in src/Controllers/Admin/ConfigController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ConfigController.php#L55

Added line #L55 was not covered by tests
}

public function protocolSettings(): Response

Check warning on line 58 in src/Controllers/Admin/ConfigController.php

View check run for this annotation

Codecov / codecov/patch

src/Controllers/Admin/ConfigController.php#L58

Added line #L58 was not covered by tests
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/Client/ShowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __invoke(ServerRequest $request): Template
$client = $this->getClientFromRequest($request);
$allowedOrigins = $this->allowedOriginRepository->get($client->getIdentifier());

return $this->templateFactory->build('oidc:clients/show.twig', [
return $this->templateFactory->build('oidc:clients/show-old.twig', [
'client' => $client,
'allowedOrigins' => $allowedOrigins,
]);
Expand Down
20 changes: 20 additions & 0 deletions src/Utils/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SimpleSAML\Module\oidc\Bridges\SspBridge;
use SimpleSAML\Module\oidc\Codebooks\RoutesEnum;
use SimpleSAML\Module\oidc\ModuleConfig;
use Symfony\Component\HttpFoundation\RedirectResponse;

class Routes
{
Expand All @@ -23,6 +24,19 @@ public function getModuleUrl(string $resource = '', array $parameters = []): str
return $this->sspBridge->module()->getModuleUrl($resource, $parameters);

Check warning on line 24 in src/Utils/Routes.php

View check run for this annotation

Codecov / codecov/patch

src/Utils/Routes.php#L24

Added line #L24 was not covered by tests
}

public function getRedirectResponseToModuleUrl(

Check warning on line 27 in src/Utils/Routes.php

View check run for this annotation

Codecov / codecov/patch

src/Utils/Routes.php#L27

Added line #L27 was not covered by tests
string $resource = '',
array $parameters = [],
int $status = 302,
array $headers = [],
): RedirectResponse {
return new RedirectResponse(
$this->getModuleUrl($resource, $parameters),
$status,
$headers,
);

Check warning on line 37 in src/Utils/Routes.php

View check run for this annotation

Codecov / codecov/patch

src/Utils/Routes.php#L33-L37

Added lines #L33 - L37 were not covered by tests
}

/*****************************************************************************************************************
* Admin area
****************************************************************************************************************/
Expand Down Expand Up @@ -60,6 +74,12 @@ public function urlAdminClientsShow(string $clientId, array $parameters = []): s
return $this->getModuleUrl(RoutesEnum::AdminClientsShow->value, $parameters);

Check warning on line 74 in src/Utils/Routes.php

View check run for this annotation

Codecov / codecov/patch

src/Utils/Routes.php#L73-L74

Added lines #L73 - L74 were not covered by tests
}

public function urlAdminClientsResetSecret(string $clientId, array $parameters = []): string

Check warning on line 77 in src/Utils/Routes.php

View check run for this annotation

Codecov / codecov/patch

src/Utils/Routes.php#L77

Added line #L77 was not covered by tests
{
$parameters['client_id'] = $clientId;
return $this->getModuleUrl(RoutesEnum::AdminClientsResetSecret->value, $parameters);

Check warning on line 80 in src/Utils/Routes.php

View check run for this annotation

Codecov / codecov/patch

src/Utils/Routes.php#L79-L80

Added lines #L79 - L80 were not covered by tests
}

/*****************************************************************************************************************
* OpenID Connect
****************************************************************************************************************/
Expand Down
7 changes: 6 additions & 1 deletion templates/base.twig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@

{% endblock content -%}

{% block postload %}{% endblock postload %}
{% block postload %}

{{ parent() }}

<script src="{{ asset('js/src/default.js', 'oidc') }}"></script>
{% endblock postload %}

{% block oidcPostload %}{% endblock %}
5 changes: 4 additions & 1 deletion templates/clients.twig
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@
<a class="pure-button" href="#">
<i class="fa fa-pen-to-square"></i>
</a>
<a class="pure-button" href="#">
<a class="pure-button confirm-action"
data-confirm="{{ 'Are you sure you want to delete this client?'|trans }}"
data-id="{{ client.getIdentifier }}"
href="#">
<i class="fa fa-trash-can"></i>
</a>
</div>
Expand Down
Loading

0 comments on commit 260dec9

Please sign in to comment.