blob: ce4989beb0fba85c46452c6ad34e52daa5a1b605 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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 => 'Value'
, 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;
|