Skip to content

Commit

Permalink
Add token based authentication via grpc-gateway with tests. Fixes GH #18
Browse files Browse the repository at this point in the history
  • Loading branch information
hexfusion committed Jun 17, 2017
1 parent 82685c0 commit 0ff5a53
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
29 changes: 15 additions & 14 deletions lib/Net/Etcd/Auth.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use warnings;
=cut

use Moo;
use JSON;
use Carp;
use Types::Standard qw(Str Int Bool HashRef ArrayRef);
use Net::Etcd::Auth::Role;
Expand Down Expand Up @@ -70,9 +71,9 @@ has name => (

sub _build_name {
my ($self) = @_;
my $user = $self->etcd->name;
return $user if $user;
return;
my $user = $self->etcd->name;
return $user if $user;
return;
}

=head2 password
Expand All @@ -87,18 +88,18 @@ has password => (

sub _build_password {
my ($self) = @_;
my $pwd = $self->etcd->password;
return $pwd if $pwd;
return;
my $pwd = $self->etcd->password;
return $pwd if $pwd;
return;
}

=head1 PUBLIC METHODS
=head2 authenticate
Enable authentication, this requires name and password.
Returns token with valid authentication.
$etcd->auth({ name => $user, password => $pass })->authenticate;
my $token = $etcd->auth({ name => $user, password => $pass })->authenticate;
=cut

Expand All @@ -107,12 +108,12 @@ sub authenticate {
$self->{endpoint} = '/auth/authenticate';
$self->{headers}{'Content-Type'} = 'application/json';
return unless ($self->password && $self->name);
# $self->password;
# $self->name;
#print STDERR Dumper($self);
$self->request;

return $self;
$self->request;
my $auth = from_json($self->{response}{content});
if ($auth && defined $auth->{token}) {
return $auth->{token};
}
return;
}

=head2 enable
Expand Down
2 changes: 1 addition & 1 deletion lib/Net/Etcd/Auth/Role.pm
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Delete role

sub delete {
my ($self) = @_;
confess 'name required for ' . __PACKAGE__ . '->delete'
confess 'role required for ' . __PACKAGE__ . '->delete'
unless $self->{role};
$self->{endpoint} = '/auth/role/delete';
$self->request;
Expand Down
11 changes: 2 additions & 9 deletions lib/Net/Etcd/Role/Actions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,9 @@ has headers => ( is => 'lazy' );
sub _build_headers {
my ($self) = @_;
my $headers;
my $auth = $self->etcd->auth->authenticate;
# print STDERR Dumper($auth->authenticate);
my $auth_token;
$auth_token = $auth->token if $auth;

print STDERR "Auth token " . $auth_token if $auth_token;

my $token = $self->etcd->auth->authenticate;
$headers->{'Content-Type'} = 'application/json';
$headers->{'Authorization'} = $auth_token if $auth_token;
$headers->{'Authorization'} = $token if $token;
return $headers;
}
=head2 hold
Expand Down Expand Up @@ -128,7 +122,6 @@ sub _build_request {
my $cv = $self->cv ? $self->cv : AE::cv;
$cv->begin;

print STDERR Dumper($self->headers);
http_request(
'POST',
$self->etcd->api_path . $self->{endpoint},
Expand Down
26 changes: 26 additions & 0 deletions t/auth.t
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,30 @@ lives_ok(
"disable auth"
);


cmp_ok( $role->{response}{success}, '==', 1, "revoke role success" );

# cleanup role
lives_ok( sub { $role = $etcd->role( { role => 'root' } )->delete;
},
"delete role" );

#print STDERR Dumper($role);

cmp_ok( $role->{response}{success}, '==', 1, "delete role success" );


# remove user
lives_ok(
sub {
$user =
$etcd->user( { name => 'root' })->delete;
},
"delete user"
);

#print STDERR Dumper($user);

cmp_ok( $user->{response}{success}, '==', 1, "delete user success" );

1;

0 comments on commit 0ff5a53

Please sign in to comment.