From 73b09b3ad1dca72187bee4f83dd8185c39064b46 Mon Sep 17 00:00:00 2001 From: Julian Blake Kongslie Date: Sat, 29 Jan 2011 19:51:18 -0800 Subject: Added Piny::Auth. --- libpiny/debian/changelog | 6 +++ libpiny/debian/control | 2 +- libpiny/debian/libpiny-perl.postinst | 16 ++++++++ libpiny/debian/libpiny-perl.postrm | 13 +++++++ libpiny/lib/Piny/Auth.pm | 74 ++++++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 1 deletion(-) create mode 100755 libpiny/debian/libpiny-perl.postinst create mode 100755 libpiny/debian/libpiny-perl.postrm create mode 100644 libpiny/lib/Piny/Auth.pm diff --git a/libpiny/debian/changelog b/libpiny/debian/changelog index 8bd42ee..d9d0db8 100644 --- a/libpiny/debian/changelog +++ b/libpiny/debian/changelog @@ -1,3 +1,9 @@ +libpiny-perl (0.17) unstable; urgency=low + + * Added Piny::Auth and key file. + + -- Julian Blake Kongslie Sat, 29 Jan 2011 19:50:58 -0800 + libpiny-perl (0.16) unstable; urgency=low * Fix some of the mutators in Piny::Repo. diff --git a/libpiny/debian/control b/libpiny/debian/control index d066800..2bb329a 100644 --- a/libpiny/debian/control +++ b/libpiny/debian/control @@ -8,7 +8,7 @@ Standards-version: 3.9.1 Package: libpiny-perl Architecture: all -Depends: ${perl:Depends}, ${misc:Depends}, libconfig-simple-perl, libemail-valid-loose-perl, libmoose-perl, libmoosex-singleton-perl, libmoosex-strictconstructor-perl +Depends: ${perl:Depends}, ${misc:Depends}, libconfig-simple-perl, libdigest-hmac-perl, libdigest-sha1-perl, libemail-valid-loose-perl, libmoose-perl, libmoosex-singleton-perl, libmoosex-strictconstructor-perl Description: Perl interface for the piny infrastructure This is a set of modules for accomplishing administrative tasks in the piny.be infrastructure. diff --git a/libpiny/debian/libpiny-perl.postinst b/libpiny/debian/libpiny-perl.postinst new file mode 100755 index 0000000..ba29e39 --- /dev/null +++ b/libpiny/debian/libpiny-perl.postinst @@ -0,0 +1,16 @@ +#!/bin/sh + +set -e + +case "$1" in + + configure) + + if [ ! -f /etc/libpiny.key ]; then + umask 0177 + dd if=/dev/random of=/etc/libpiny.key bs=512 count=1 + fi + + ;; + +esac diff --git a/libpiny/debian/libpiny-perl.postrm b/libpiny/debian/libpiny-perl.postrm new file mode 100755 index 0000000..575ea84 --- /dev/null +++ b/libpiny/debian/libpiny-perl.postrm @@ -0,0 +1,13 @@ +#!/bin/sh + +set -e + +case "$1" in + + purge) + + rm -f /etc/libpiny.key + + ;; + +esac diff --git a/libpiny/lib/Piny/Auth.pm b/libpiny/lib/Piny/Auth.pm new file mode 100644 index 0000000..2bbf60b --- /dev/null +++ b/libpiny/lib/Piny/Auth.pm @@ -0,0 +1,74 @@ +# Copyright © 2010 Julian Blake Kongslie +# Licensed under the BSD 3-clause license. + +use strict; +use warnings; + +package Piny::Auth; + +use Moose; +use MooseX::StrictConstructor; + +use Digest::HMAC_SHA1; + +# Attributes + +has 'key' => + ( is => 'ro' + , isa => 'String' + , lazy_build => 1 + ); + +# Public methods + +sub hash { + my ( $s, $params ) = @_; + + my $hmac = Digest::HMAC_SHA1->new( $s->key ); + + if ( ref $params ) { + foreach my $key ( sort keys %$params ) { + $hmac->add( length( $key ) . "\0" . $key . "\0" . length( $params->{$key} ) . "\0" . $params->{$key} . "\0" ); + }; + } else { + $hmac->add( $params ); + }; + + return $hmac->b64digest; +}; + +# Builder methods + +# If constructed with just one argument, then treat it as the key. +around BUILDARGS => sub { + my ( $orig, $class ) = ( shift, shift ); + + if ( @_ == 1 && ! ref $_[0] ) { + return $class->$orig( key => $_[0] ); + } else { + return $class->$orig( @_ ); + }; +}; + +sub _build_key { + my ( $s ) = @_; + + open( my $fh, "<", "/etc/libpiny.key" ) or die "Can't open libpiny.key: $!"; + + my $key; + + { + local $/; + $key = <$fh>; + }; + + close( $fh ); + + return $key; +}; + +# Moose boilerplate + +__PACKAGE__->meta->make_immutable; + +1; -- cgit v1.2.3