blib/lib/Passwd/Keyring/Memory.pm

filter: Statementstmt 100% 37 / 37 Branchbran 100% 6 / 6 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 100% 63 / 63 CC 6 SCAR 7.6
CC
6
Cov
100%
new
4.0
get_password
2.0
clear_password
2.0
CRAP 2.1
↑ Summary
1 package Passwd::Keyring::Memory;
2  
3 9 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Passwd::Keyring::Memory - fallback keyring for environments
10 where no better keyring is available.
11  
12 =head1 VERSION
13  
14 Version 1.0000
15  
16 =cut
17  
18 9 our $VERSION = '1.0000';
19  
20 =head1 SYNOPSIS
21  
22 use Passwd::Keyring::Memory;
23  
24 my $keyring = Passwd::Keyring::Memory->new();
25  
26 $keyring->set_password("John", "verysecret", "my-realm");
27  
28 my $password = $keyring->get_password("John", "my-realm");
29  
30 $keyring->clear_password("John", "my-realm");
31  
32 Note: see L<Passwd::Keyring::Auto::KeyringAPI> for detailed comments on
33 keyring method semantics (this document is installed with
34 Passwd::Keyring::Auto package).
35  
36  
37 =head1 SUBROUTINES/METHODS
38  
39 =head2 new
40  
41 Initializes the processing.
42  
43 =cut
44  
45 # Global map of folders to simulate case of a few ring objects working on the same data.
46 # (could be state variable in new, but let's not limit perl versions)
47 9 my $_passwords = {};
48  
49 12 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
50 12 my ($cls, %args) = @_;
51 my $self = {
52 app => $args{app} || "Passwd::Keyring::Memory",
53 12 ▶ group => $args{group} || "Passwd::Keyring::Memory default passwords",
Condition100%4/4
$args{'app'} || 'Passwd::Keyring::Memory'
l!l
102
$args{'group'} || 'Passwd::Keyring::Memory default passwords'
l!l
102
MC/DC100%1/1
$args{'app'} || 'Passwd::Keyring::Memory'
$args{'app'}
MC/DC100%1/1
$args{'group'} || 'Passwd::Keyring::Memory default passwords'
$args{'group'}
Truth table100%2/2
A
A= $args{'app'}
Aresult
11
00
Truth table100%2/2
A
A= $args{'group'}
Aresult
11
00
54 };
55 12 my $group = $self->{group};
56 12 ▶ unless(exists $_passwords->{$group}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $_passwords->{$group}) 9 3 12
57 9 $_passwords->{$group} = {};
58 }
59  
60 12 $self->{_passwords} = $_passwords->{$group}; # key → password
61  
62 12 bless $self, $cls;
63 12 return $self;
64 }
65  
66 59 ▶sub _password_key {
Subroutine _password_key: called CC=1 SCAR=0.0
67 59 my ($self, $realm, $user_name) = @_;
68 59 return join("||", $realm, $user_name);
69 }
70  
71 =head2 set_password(username, password, realm)
72  
73 Sets (stores) password identified by given realm for given user
74  
75 =cut
76  
77 13 ▶sub set_password {
Subroutine set_password: called CC=1 SCAR=0.0
Pod documented
78 13 my ($self, $user_name, $user_password, $realm) = @_;
79 13 my $key = $self->_password_key($realm, $user_name);
80 13 $self->{_passwords}->{ $key } = $user_password;
81  
82 #use Data::Dumper; print STDERR Dumper($_passwords);
83 }
84  
85 =head2 get_password($user_name, $realm)
86  
87 Reads previously stored password for given user in given app.
88 If such password can not be found, returns undef.
89  
90 =cut
91  
92 33 ▶sub get_password {
Subroutine get_password: called CC=2 SCAR=6.9
Pod documented
93 33 my ($self, $user_name, $realm) = @_;
94 33 my $key = $self->_password_key($realm, $user_name);
95  
96 33 ▶ if( exists $self->{_passwords}->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_passwords'}{$key}) { } 23 10 33
97 23 return $self->{_passwords}->{$key};
98 } else {
99 10 return undef;
100 }
101 }
102  
103 =head2 clear_password($user_name, $realm)
104  
105 Removes given password (if present)
106  
107 =cut
108  
109 13 ▶sub clear_password {
Subroutine clear_password: called CC=2 SCAR=6.9
Pod documented
110 13 my ($self, $user_name, $realm) = @_;
111  
112 13 my $key = $self->_password_key($realm, $user_name);
113  
114 #use Data::Dumper; print STDERR Dumper($_passwords);
115  
116 13 ▶ if( exists $self->{_passwords}->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_passwords'}{$key}) { } 10 3 13
117 10 delete $self->{_passwords}->{$key};
118 10 return 1;
119 } else {
120 3 return 0;
121 }
122 }
123  
124 =head2 is_persistent
125  
126 Returns info, whether this keyring actually saves passwords persistently.
127  
128 (false in this case)
129  
130 =cut
131  
132 1 ▶sub is_persistent {
Subroutine is_persistent: called CC=1 SCAR=0.0
Pod documented
133 1 my ($self) = @_;
134 1 return 0;
135 }
136  
137  
138 =head1 AUTHOR
139  
140 Marcin Kasperski
141  
142 =head1 BUGS
143  
144 Please report any bugs or feature requests to
145 issue tracker at L<https://helixteamhub.cloud/mekk/projects/perl/issues>.
146  
147 =head1 SUPPORT
148  
149 You can find documentation for this module with the perldoc command.
150  
151 perldoc Passwd::Keyring::Memory
152  
153 You can also look for information at:
154  
155 L<http://search.cpan.org/~mekk/Passwd-Keyring-Memory/>
156  
157 Source code is tracked at:
158  
159 L<https://helixteamhub.cloud/mekk/projects/perl/repositories/keyring-memory>
160  
161 =head1 LICENSE AND COPYRIGHT
162  
163 Copyright 2012-2020 Marcin Kasperski.
164  
165 This program is free software; you can redistribute it and/or modify it
166 under the terms of either: the GNU General Public License as published
167 by the Free Software Foundation; or the Artistic License.
168  
169 See http://dev.perl.org/licenses/ for more information.
170  
171 =cut
172  
173  
174 9 1; # End of Passwd::Keyring::Memory
↑ Summary