| 1 | 4 | ▶ | use strictures 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 2 | package Mojito::Auth::Elasticsearch; | ||||||||||||||||||||
| 3 | 4 | $Mojito::Auth::Elasticsearch::VERSION = '0.25'; | |||||||||||||||||||
| 4 | 4 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 4 | ▶ | use Mojito::Page::CRUD::Elasticsearch; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 4 | ▶ | use List::Util qw/first/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 4 | with('Mojito::Role::DB::Elasticsearch'); | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =head1 Name | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | Mojito::Auth::Elasticsearch - authentication delegatee class for Elasticsearch | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | has editer => ( | ||||||||||||||||||||
| 17 | is => 'ro', | ||||||||||||||||||||
| 18 | lazy => 1, | ||||||||||||||||||||
| 19 | 0 | ▶ | default => sub { Mojito::Page::CRUD::Elasticsearch->new(collection_name => 'users') }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 20 | 4 | ); | |||||||||||||||||||
| 21 | =head1 Methods | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head2 add_user | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | Provide the username, realm (default Mojito) and password. | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 0 | ▶ | sub add_user { | ||||||||||||||||||
|
Subroutine
add_user: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 30 | 0 | my ($self, $args) = @_; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 0 | ▶ | my $username = $args->{username} || $self->username; | ||||||||||||||||||
|
Condition0%0/3
$args->{'username'} || $self->username
MC/DC0%0/2
$args->{'username'} || $self->username
$args->{'username'}
$self->username
Truth table0%0/3
A || B
A= $args->{'username'}B= $self->username
| |||||||||||||||||||||
| 33 | 0 | ▶ | if ($self->get_user($username)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | 0 | warn "Username '$username' already taken!"; | |||||||||||||||||||
| 35 | 0 | return; | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | 0 | my @digest_input_parts = qw/ username realm password /; | |||||||||||||||||||
| 38 | 0 | my $digest_input = join ':', map { $self->$_ } @digest_input_parts; | |||||||||||||||||||
| 39 | 0 | my $HA1 = Digest::MD5::md5_hex($digest_input); | |||||||||||||||||||
| 40 | 0 | my $md5_password = Digest::MD5::md5_hex( $self->password ); | |||||||||||||||||||
| 41 | # TODO - Make sure backend support unique user names | ||||||||||||||||||||
| 42 | # For Mongo we can ensure an index, but a general technique is | ||||||||||||||||||||
| 43 | # to check for existence of a username before attempting to add it. | ||||||||||||||||||||
| 44 | 0 | my $id = $self->editer->create( | |||||||||||||||||||
| 45 | { | ||||||||||||||||||||
| 46 | first_name => $self->first_name, | ||||||||||||||||||||
| 47 | last_name => $self->last_name, | ||||||||||||||||||||
| 48 | email => $self->email, | ||||||||||||||||||||
| 49 | username => $self->username, | ||||||||||||||||||||
| 50 | realm => $self->realm, | ||||||||||||||||||||
| 51 | HA1 => $HA1, | ||||||||||||||||||||
| 52 | password => $md5_password | ||||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 0 | return $id; | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =head2 get_user | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | Get a user from the database. | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | ▶ | sub get_user { | ||||||||||||||||||
|
Subroutine
get_user: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 66 | 0 | my ( $self, $username ) = @_; | |||||||||||||||||||
| 67 | 0 | ▶ | $username //= $self->username; | ||||||||||||||||||
|
Condition0%0/3
$username //= $self->username
MC/DC0%0/2
$username //= $self->username
$username
$self->username
Truth table0%0/3
A //= B
A= $usernameB= $self->username
| |||||||||||||||||||||
| 68 | 0 | ▶ | return if !$username; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | # If we don't have any users yet, the be somewhat graceful about it | ||||||||||||||||||||
| 70 | 0 | ▶ | return if not scalar @{$self->collection->{hits}{hits}}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | # Get collection | ||||||||||||||||||||
| 73 | 0 | my $collection = $self->collection->{hits}{hits}; | |||||||||||||||||||
| 74 | #my @users = values %{$collection}; | ||||||||||||||||||||
| 75 | 0 | my @users = @{$collection}; | |||||||||||||||||||
| 76 | 0 | ▶ | my $user = first {$_->{_source}{username} eq $username} @users; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 77 | 0 | return $user->{_source}; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =head2 remove_user | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | Remove a user from the database. | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =cut | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | ▶ | sub remove_user { | ||||||||||||||||||
|
Subroutine
remove_user: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 87 | 0 | my ( $self, $username ) = @_; | |||||||||||||||||||
| 88 | 0 | ▶ | $username //= $self->username; | ||||||||||||||||||
|
Condition0%0/3
$username //= $self->username
MC/DC0%0/2
$username //= $self->username
$username
$self->username
Truth table0%0/3
A //= B
A= $usernameB= $self->username
| |||||||||||||||||||||
| 89 | 0 | ▶ | return if !$username; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | # Just in case we have multiple occurrences of the same user | ||||||||||||||||||||
| 91 | 0 | my @users = map { $_->{_source} } @{$self->collection->{hits}{hits}}; | |||||||||||||||||||
| 92 | 0 | my @wanted_users = grep {$_->{username} eq $username} @users; | |||||||||||||||||||
| 93 | 0 | my @wanted_ids = map {$_->{id} } @wanted_users; | |||||||||||||||||||
| 94 | 0 | my $users_deleted = 0; | |||||||||||||||||||
| 95 | 0 | foreach my $id (@wanted_ids) { | |||||||||||||||||||
| 96 | 0 | $self->editer->delete($id); | |||||||||||||||||||
| 97 | 0 | $users_deleted++; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | 0 | return $users_deleted; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | # Apply the role after the (role) required interface is defined (get_user, add_user) | ||||||||||||||||||||
| 103 | 4 | with('Mojito::Auth::Role'); | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | =head2 BUILD | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | Set some things post object construction, pre object use. | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 0 | ▶ | sub BUILD { | ||||||||||||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 112 | 0 | my $self = shift; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | # We use the users collection for Auth stuff | ||||||||||||||||||||
| 115 | 0 | $self->collection_name('users'); | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 4 | 1; | |||||||||||||||||||