Authen-ModAuthPubTkt

 view release on metacpan or  search on metacpan

lib/Authen/ModAuthPubTkt.pm  view on Meta::CPAN

package Authen::ModAuthPubTkt;
require Exporter;
our @ISA=qw(Exporter);
our @EXPORT = qw/pubtkt_generate
		 pubtkt_verify
		 pubtkt_parse/;

use strict;
use warnings;
use Carp;
use MIME::Base64;
use File::Temp qw/tempfile/;
use IPC::Run3;


# ABSTRACT: A Module to generate Mod-Auth-PubTkt compatible Cookies

=pod

=head1 NAME

Authen::ModAuthPubTkt - Generate Tickets (Signed HTTP Cookies) for mod_auth_pubtkt protected websites.

=head1 VERSION

version 0.1.1

=cut
our $VERSION = '0.1.1';

=pod

=head1 SYNOPSIS

On the command-line, generate the public + private keys:
(More details available at L<https://neon1.net/mod_auth_pubtkt/install.html>)

	$ openssl genrsa -out key.priv.pem 1024
	$ openssl rsa -in key.priv.pem -out key.pub.pem -pubout


Then in your perl script (which is probably the your custom login website), use the following code to issue tickets:

	use Authen::ModAuthPubTkt;

	my $ticket = pubtkt_generate(
		privatekey => "key.priv.pem",
		keytype    => "rsa",
		clientip   => undef,  # or a valid IP address
		userid     => "102",  # or any ID that makes sense to your application, e.g. email
		validuntil => time() + 86400, # valid for one day
		graceperiod=> 3600,   # grace period of an hour
		tokens     => undef,  # comma separated string of tokens.
		userdata   => undef   # any application specific data to pass.
	);

	## $ticket string will look something like:
	## "uid=102;validuntil=1337899939;graceperiod=1337896339;tokens=;udata=;sig=h5qR" \
	## "yZZDl8PfW8wNxPYkcOMlAxtWuEyU5bNAwEFT9lztN3I7V13SaGOHl+U6wB+aMkvvLQiaAfD2xF/Hl" \
	## "+QmLDEvpywp98+5nRS+GeihXTvEMRaA4YVyxb4NnZujCZgX8IBhP6XBlw3s7180jxE9I8DoDV8bDV" \
	## "k/2em7yMEzLns="


To verify a ticket, use the following code:

	my $ok = pubtkt_verify (
		publickey => "key.pub.pem",
		keytype   => "rsa",
		ticket    => $ticket
	);
	die "Ticket verification failed.\n" if not $ok;

To extract items from a ticket, use the following code:

	my %items = pubtkt_parse($ticket);

	## %items will be something like:
	## {
	##    'uid' => 102,
	##    'validuntil' => 1337899939,
	##    'graceperiod => 1337896339,
	##    'tokens' => "",



( run in 0.588 second using v1.01-cache-2.11-cpan-39bf76dae61 )