Skip to content

Commit

Permalink
Merge pull request #202 from vtsykun/feat/api-token2
Browse files Browse the repository at this point in the history
Allow to regenerate api token.
  • Loading branch information
vtsykun authored Dec 2, 2023
2 parents 2b38b0a + 73a554c commit 7e47e05
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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

0 comments on commit 7e47e05

Please sign in to comment.