Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to regenerate api token. #202

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Attribute\IsGranted;

/**
* @author Jordi Boggiano <[email protected]>
Expand Down Expand Up @@ -78,6 +77,25 @@ public function showAction(Request $request): Response
]);
}

#[Route('/profile/regenerate-token', name: 'profile_regenerate_token')]

public function regenerateToken(Request $request): Response
{
$user = $this->getUser();
if (!$user instanceof User) {
throw $this->createNotFoundException();
}

if (!$this->isCsrfTokenValid('token', $request->query->get('_token'))) {
return new Response('Invalid Csrf Params', 400);
}

$user->generateApiToken();
$this->getEM()->flush();

return $this->redirectToRoute('profile_show');
}

#[Route('/profile/edit', name: 'profile_edit')]
public function editAction(Request $request): Response
{
Expand Down
3 changes: 3 additions & 0 deletions templates/profile/show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<section class="col-md-9">
{% set isMaintainer = is_granted('ROLE_MAINTAINER') %}
{% set apiToken = get_api_token(app.user, false, true) %}
{% set token = csrf_token('token') %}

{%- if apiToken is not null %}
<h3 class="font-normal profile-title">{{ 'profile.your_api_token'|trans }}</h3>

Expand All @@ -15,6 +17,7 @@
<button class="btn btn-success btn-show-api-token" type="button">{{ 'profile.show_api_token'|trans }}</button>
</span>
</div>
<a href="{{ path('profile_regenerate_token', {'_token': token}) }}" class="onclick-confirm" data-msg="Are you sure you want to refresh the token?"><i class="fa fa-refresh"></i> Regenerate token</a>

<p>You need to authenticate to access their Composer repository, for example to enter credentials run command:</p>
<pre>composer config --global --auth http-basic.{{ app.request.getHttpHost() }} {{ user.userIdentifier }} {{ show_api_token(apiToken) }}</pre>
Expand Down