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/lib/Piny/Auth.pm | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 libpiny/lib/Piny/Auth.pm (limited to 'libpiny/lib/Piny') 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