Dancer::Plugin::EncodeID - Encode/Decode (or obfuscate) IDs in URLs
version 0.02
use Dancer; use Dancer::Plugin::EncodeID; set show_errors => true; # Set the secret key (better yet: put this in your config.yml) setting plugins => { EncodeID => { secret => 'my_secret_key' } }; # Generate an encoded/obfuscaed ID in URL # # When the user visits this page, she will see URLs such as: # http://myserver.com/item/c98ea08a8e8ad715 # instead of # http://myserver.com/item/42 # get '/' => sub { # Any ID (numeric or alpha-numeric) you want to obfuscate my $clear_text_id = int(rand(42)+1); # Encode the ID, generate the URL my $encoded_id = encode_id($clear_text_id); my $url = request->uri_for("/item/$encoded_id"); return "Link for Item $clear_text_id: <a href=\"$url\">$url</a>"; }; # # Decode a given ID, show the requested item # get '/item/:encoded_id' => sub { # Decode the ID back to clear-text my $clear_text_id = decode_id( params->{encoded_id} ) ; return "Showing item '$clear_text_id'"; }; dance;
encode_id(ID [,PREFIX]) - Encodes the given ID, returns the encoded hash value. If "PREFIX" is given, it will be added to the ID before encoding. It can be used when decoding to verify the decoded value is valid.
encode_id(ID [,PREFIX])
decode_id(ID [,PREFIX]) - Decodes the given ID, returns the original (cleartext) ID value. If "PREFIX" is given, it will be used to verify the validity of the ID.
decode_id(ID [,PREFIX])
This module aims to make it as easy as possible to obfuscate internal IDs when using them in a URL given to users. Instead of seeing http://myserver.com/item/42 users will see http://myserver.com/item/c98ea08a8e8ad715 . This will prevent nosy users from trying to iterate all items based on a simple ID in the URL.
Configuration requires a secret key at a minimum.
Either put this in your config.yml file:
plugins: EncodeID: secret: 'my_secret_password'
Or set the secret key at run time, with:
setting plugins => { EncodeID => { secret => 'my_secret_code' } };
Assaf Gordon, <gordon at cshl.edu>
<gordon at cshl.edu>
Please report any bugs or feature requests to https://github.com/agordon/Dancer-Plugin-EncodeID/issues
A fully functional command-line tool to encode/decode IDs is available in the ./eg/ folder.
./eg/
Dancer, Dancer::Plugin
You can find documentation for this module with the perldoc command.
perldoc Dancer::Plugin::EncodeID
Idea and implementation for this module were greatly influenced by similar mechanism used in the Galaxy project (http://usegalaxy.org).
Copyright 2011 Assaf Gordon.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install Dancer::Plugin::EncodeID, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dancer::Plugin::EncodeID
CPAN shell
perl -MCPAN -e shell install Dancer::Plugin::EncodeID
For more information on module installation, please visit the detailed CPAN module installation guide.