# 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;