Download the PHP package zepekegno/obfuscate-id-bundle without Composer
On this page you can find all versions of the php package zepekegno/obfuscate-id-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zepekegno/obfuscate-id-bundle
More information about zepekegno/obfuscate-id-bundle
Files in zepekegno/obfuscate-id-bundle
Package obfuscate-id-bundle
Short Description Symfony bundle to securely obfuscate and deobfuscate entity IDs in URLs. Provides attributes and Twig filters to handle obfuscation seamlessly within Symfony applications.
License MIT
Homepage https://github.com/zepekegno224/ObfuscateIdBundle
Informations about the package obfuscate-id-bundle
Zepekegno ObfuscateBundle Documentation
1. Introduction
The Zepekegno ObfuscateBundle provides the ability to obfuscate and deobfuscate IDs in your Symfony application. It supports obfuscating IDs in both URLs and Twig templates, and it allows you to use Symfony Attributes to automatically resolve obfuscated IDs to actual entities in your controllers.
2. Installation
Step 1 : Install the bundle via Composer
You can install the bundle using Composer by running:
Step 2 : Register the bundle
Symfony 7 should automatically register the bundle, but if it doesn’t, manually add it to your :
Step 3 : Configure the secret key
In your file, add a secret key that will be used to obfuscate and deobfuscate IDs:
This key will be used in the obfuscation algorithm. The secret key must be 32 bytes long for AES-256 encryption
3. Usage with Symfony Attributes
Using in a controller The bundle provides a custom attribute that you can use to automatically resolve obfuscated IDs in your controller methods.
Below are examples of how to use this attribute in different scenarios.
Example 1: Basic ID Obfuscation
In this example, the ObfuscateId
attribute is used with the id route parameter, which is obfuscated in the URL and resolved to the Post entity in the controller.
``
Explanation:
- without any parameters tells Symfony to use the default route parameter (id in this case).
The ValueResolver
automatically deobfuscates the id parameter and resolves it to a Post entity.- The resulting URL might look like
/post/abc123
instead of/post/123
.
Example 2: Custom Route Parameter
In this example, the ObfuscateId attribute specifies a custom route parameter, post
, instead of the default id.
``
Explanation:
#[ObfuscateId(routeParam: 'post')]
specifies that the route parameter post should be used.- The
ValueResolver
will use thepost
parameter in the URL, obfuscate it, and resolve it as a Post entity. - The URL will be
/post/abc123
instead of/post/123
, whereabc123
is the obfuscated ID.Example 3: Custom Identifier Field
In this example, the
ObfuscateId
attribute uses a custom identifier field calledpost
.
Explanation
#[ObfuscateId(identifierField: 'post')]
specifies that the ID field in the Post entity should be called post.- This is useful if your database uses a different primary key field name than
id
. - The URL will look like
/post/abc123
instead of/post/123
, whereabc123
is the obfuscated ID.Example 4:
In this example, we use the
ObfuscateId
attribute the index method specifying both a custom route parameter(post)
and thePost
entity type.
Explanation
#[ObfuscateId(routeParam: 'post', entity: Post::class)]
:routeParam: 'post'
specifies the route parameter used in the URL.entity: Post::class
tells Symfony to resolve the obfuscated ID as a Post entity.
- The URL will be
/post/abc123
instead of/post/123
, whereabc123
is the obfuscated ID.Example 5: Custom Route Parameter
In this example, we specify the Post entity in the ObfuscateId attribute so Symfony knows to resolve the ID as a Post entity. ``
Explanation
- specifies that the id route parameter should be deobfuscated and resolved as a Post entity.
- The URL will be mapped to the actual Post entity with ID
123
after deobfuscation. - This configuration is useful when you want to define the entity type explicitly.
4. Usage in Twig Templates
In addition to using obfuscated IDs in routes, you can also obfuscate IDs in Twig templates using the provided filters.
Available Twig Filters
- : To obfuscate an ID.
- : To deobfuscate an obfuscated ID.
Example Twig Template
Generating URLs with Obfuscated IDs
To generate a URL that contains an obfuscated ID:
In this example, post.id|offusquer will obfuscate the post’s ID and pass it into the id parameter of the path() function.
6. Customization
You can customize the obfuscation algorithm by using your owner obfuscate service. Here how you can customize the obfuscation algorithm
7. Conclusion
The Zepekegno ObfuscateBundle simplifies the process of hiding sensitive IDs in URLs using obfuscation techniques. It integrates seamlessly with Symfony’s routing system and Twig templating engine, allowing you to easily obfuscate and deobfuscate IDs both in the backend and frontend. With support for Symfony Attributes, you can resolve obfuscated IDs into entities directly in your controller methods, making your code cleaner and more secure.