dotsafe/api-platform-user-security-bundle
Installation Add the bundle via Composer:
composer require dotsafe/api-platform-user-security-bundle
Enable it in config/bundles.php:
return [
// ...
Dotsafe\ApiPlatformUserSecurityBundle\DotsafeApiPlatformUserSecurityBundle::class => ['all' => true],
];
First Use Case: Password Reset
Configure the bundle in config/packages/dotsafe_api_platform_user_security.yaml:
dotsafe_api_platform_user_security:
reset:
enabled: true
token_ttl: 3600 # 1 hour
email_template: 'emails/reset_password.html.twig'
Ensure you have a User entity with resetToken and resetTokenExpiresAt fields (or extend the provided User trait).
Trigger a Reset
Use the ResetPasswordAction in your API Platform controller:
use Dotsafe\ApiPlatformUserSecurityBundle\Action\ResetPasswordAction;
#[ApiResource(
operations: [
new ResetPasswordAction(),
]
)]
class User {}
Password Reset Flow
/api/reset-password-request with email./api/reset-password?token=...)./api/reset-password with token and newPassword.Integration with API Platform
Extend existing User resource operations:
#[ApiResource(
operations: [
new GetCollection(),
new Get(),
new ResetPasswordAction(), // Add to existing operations
]
)]
class User {}
Customizing Tokens
Override token generation in a custom ResetTokenGenerator service:
# config/services.yaml
services:
App\Service\CustomResetTokenGenerator:
decorates: 'dotsafe_api_platform_user_security.reset_token_generator'
Email Templates
Extend the default Twig template (emails/reset_password.html.twig) or override the path in config:
dotsafe_api_platform_user_security:
reset:
email_template: 'custom/path/reset.html.twig'
Validation
Use Symfony’s validator constraints in your User entity:
use Symfony\Component\Validator\Constraints as Assert;
#[Assert\Length(min: 8)]
private ?string $plainPassword = null;
Token Expiry
token_ttl (default: 3600s). Ensure clients handle 404/403 for expired tokens gracefully.resetTokenExpiresAt in the User entity to verify expiry logic.Email Delivery
$this->mailer->send(
new ResetPasswordEmail($user, $token)
);
Missing Documentation
SecurityController.// src/Controller/SecurityController.php
public function changePassword(User $user, string $currentPassword, string $newPassword): Response
{
// Validate current password, update user, etc.
}
Impersonation
HWIOAuthBundle or SensioFrameworkExtraBundle for inspiration.Database Schema
User entity has these fields (or extend Dotsafe\ApiPlatformUserSecurityBundle\Entity\User):
#[ORM\Column(nullable: true)]
private ?string $resetToken;
#[ORM\Column(nullable: true)]
private ?\DateTimeInterface $resetTokenExpiresAt;
Token Generation
$token = $this->resetTokenGenerator->generate($user);
$this->logger->debug('Reset token:', ['token' => $token]);
Configuration Overrides
dump(config('dotsafe_api_platform_user_security')) to verify loaded settings.Event Listeners
ResetPasswordRequestedEvent) for custom logic:
# config/services.yaml
services:
App\EventListener\ResetPasswordListener:
tags:
- { name: kernel.event_listener, event: dotsafe_api_platform_user_security.reset_password_requested, method: onResetRequested }
Custom Actions
Create a new action class (e.g., MagicLinkAction) by copying ResetPasswordAction and overriding methods like process().
Token Storage Replace the default token storage (e.g., switch to Redis):
services:
dotsafe_api_platform_user_security.reset_token_storage:
class: App\Service\RedisResetTokenStorage
arguments: ['@redis']
Security Layers
Add rate-limiting to /reset-password-request:
# config/packages/security.yaml
firewalls:
main:
pattern: ^/api/reset-password-request
rate_limiter: reset_password_limiter
How can I help you explore Laravel packages today?