| 1 | # Connector::Proxy::Authentication::Password | ||
| 2 | # | ||
| 3 | # Check given authentication passwords against KeyNanny | ||
| 4 | # | ||
| 5 | package Connector::Proxy::Authentication::KeyNanny; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use English; | ||
| 10 | use Data::Dumper; | ||
| 11 | use KeyNanny::Connector; | ||
| 12 | |||
| 13 | use Moose; | ||
| 14 | extends 'Connector::Proxy'; | ||
| 15 | |||
| 16 | has keynanny => ( | ||
| 17 | is => 'ro', | ||
| 18 | isa => 'KeyNanny::Connector', | ||
| 19 | lazy => 1, | ||
| 20 | builder => '_init_keynanny', | ||
| 21 | ); | ||
| 22 | |||
| 23 | sub _init_keynanny { | ||
| 24 | |||
| 25 | my $self = shift; | ||
| 26 | return KeyNanny::Connector->new({ | ||
| 27 | LOCATION => $self->LOCATION(), | ||
| 28 | }); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub get { | ||
| 32 | my $self = shift; | ||
| 33 | my $arg = shift; | ||
| 34 | my $params = shift; | ||
| 35 | |||
| 36 | my @path = $self->_build_path_with_prefix( $arg ); | ||
| 37 | my $user = $path[ (scalar @path) - 1 ]; | ||
| 38 | |||
| 39 | my $password = $params->{password}; | ||
| 40 | |||
| 41 | if (!$user) { | ||
| 42 | $self->log()->error('No username'); | ||
| 43 | die "no username given"; | ||
| 44 | } | ||
| 45 | |||
| 46 | if (!$password) { | ||
| 47 | $self->log()->error('No password'); | ||
| 48 | die "no password given"; | ||
| 49 | } | ||
| 50 | |||
| 51 | if ($user =~ /[^a-zA-Z0-9_\-\.]/) { | ||
| 52 | $self->log()->error('Invalid chars in username ('.$user.')'); | ||
| 53 | return $self->_node_not_exists( $user ); | ||
| 54 | } | ||
| 55 | |||
| 56 | my $knpath = join("/", @path ); | ||
| 57 | $self->log()->debug('verify password for ' . $user . ', path ' . $knpath ); | ||
| 58 | |||
| 59 | # Keynanny uses the slash as seperator | ||
| 60 | my $secret; | ||
| 61 | eval { | ||
| 62 | $secret = $self->keynanny()->get( $knpath ); | ||
| 63 | }; | ||
| 64 | if ($EVAL_ERROR) { | ||
| 65 | $self->log()->error('Error talking to keynanny ' . $EVAL_ERROR); | ||
| 66 | return $self->_node_not_exists( $user ); | ||
| 67 | } | ||
| 68 | |||
| 69 | if (!$secret) { | ||
| 70 | return $self->_node_not_exists( $user ); | ||
| 71 | } elsif ($secret eq $password) { | ||
| 72 | $self->log()->info('Password accepted for ' . $user); | ||
| 73 | return 1; | ||
| 74 | } else { | ||
| 75 | $self->log()->info('Password mismatch for ' . $user); | ||
| 76 | return 0; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | sub get_meta { | ||
| 82 | my $self = shift; | ||
| 83 | |||
| 84 | # If we have no path, we tell the caller that we are a connector | ||
| 85 | my @path = $self->_build_path( shift ); | ||
| 86 | if (scalar @path == 0) { | ||
| 87 | return { TYPE => "connector" }; | ||
| 88 | } | ||
| 89 | return {TYPE => "scalar" }; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub exists { | ||
| 93 | |||
| 94 | my $self = shift; | ||
| 95 | my $arg = shift; | ||
| 96 | |||
| 97 | if ((scalar @{$arg}) == 0) { | ||
| 98 | return 1; | ||
| 99 | } | ||
| 100 | |||
| 101 | my @path = $self->_build_path_with_prefix( $arg ); | ||
| 102 | |||
| 103 | my $secret = $self->keynanny()->get( join("/", @path ) ); | ||
| 104 | |||
| 105 | if ($secret) { | ||
| 106 | return 1; | ||
| 107 | } else { | ||
| 108 | return 0; | ||
| 109 | } | ||
| 110 | |||
| 111 | } | ||
| 112 | |||
| 113 | no Moose; | ||
| 114 | __PACKAGE__->meta->make_immutable; | ||
| 115 | |||
| 116 | 1; | ||
| 117 | __END__ |