summaryrefslogtreecommitdiff
path: root/libpiny/lib/Piny/Auth.pm
diff options
context:
space:
mode:
authorJulian Blake Kongslie <jblake@omgwallhack.org>2011-01-29 19:51:18 -0800
committerJulian Blake Kongslie <jblake@omgwallhack.org>2011-01-29 19:51:18 -0800
commit73b09b3ad1dca72187bee4f83dd8185c39064b46 (patch)
treecbfd5346659f2a39e75a891978b8d0773dd980a9 /libpiny/lib/Piny/Auth.pm
parent7262b1cbb3fdb41ea6c6f8ea6220abb98f08aa5f (diff)
downloadpiny-code-73b09b3ad1dca72187bee4f83dd8185c39064b46.tar.gz
piny-code-73b09b3ad1dca72187bee4f83dd8185c39064b46.zip
Added Piny::Auth.
Diffstat (limited to 'libpiny/lib/Piny/Auth.pm')
-rw-r--r--libpiny/lib/Piny/Auth.pm74
1 files changed, 74 insertions, 0 deletions
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 <jblake@omgwallhack.org>
+# 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;