Passwd/Keyring/KDEWallet.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« auto/share/dist/Passwd-Keyring-KDEWallet/imap_query_example.pl ↑ Summary
1 package Passwd::Keyring::KDEWallet;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use parent 'Keyring';
6 0 ▶use Net::DBus;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use Try::Tiny;
8 use Proc::SyncExec qw/sync_exec/;
9 use Carp;
10 use File::Which;
11  
12 =head1 NAME
13  
14 Passwd::Keyring::KDEWallet - Password storage implementation based on KDE Wallet.
15  
16 =head1 VERSION
17  
18 Version 1.0001
19  
20 =cut
21  
22 our $VERSION = '1.0001';
23  
24 our $APP_NAME = "Passwd::Keyring";
25 our $FOLDER_NAME = "Perl-Passwd-Keyring";
26  
27 # Max time we wait for kwalletd
28 our $KWALLETD_START_TIMEOUT = 2.0;
29 # Frequency of checks
30 our $KWALLETD_CHECK_FREQUENCY = 0.1;
31  
32 =head1 SYNOPSIS
33  
34 KDE Wallet based implementation of L<Passwd::Keyring>.
35  
36 use Passwd::Keyring::KDEWallet;
37  
38 my $keyring = Passwd::Keyring::KDEWallet->new(
39 app=>"blahblah scraper",
40 group=>"Johnny web scrapers",
41 );
42  
43 my $username = "John"; # or get from .ini, or from .argv...
44  
45 my $password = $keyring->get_password($username, "blahblah.com");
46 unless( $password ) {
47 $password = <somehow interactively prompt for password>;
48  
49 # securely save password for future use
50 $keyring->set_password($username, $password, "blahblah.com");
51 }
52  
53 login_somewhere_using($username, $password);
54 if( password_was_wrong ) {
55 $keyring->clear_password($username, "blahblah.com");
56 }
57  
58 Note: see L<Passwd::Keyring::Auto::KeyringAPI> for detailed comments
59 on keyring method semantics (this document is installed with
60 C<Passwd::Keyring::Auto> package).
61  
62 =head1 SUBROUTINES/METHODS
63  
64 =head2 new
65  
66 Passwd::Keyring::KDEWallet->new(
67 app=>'app name', group=>'passwords folder');
68  
69 Passwd::Keyring::KDEWallet->new(
70 app=>'app name', group=>'passwords folder',
71 start_kwalletd_if_missing=>1);
72  
73 Initializes the processing. Croaks if kwallet (or d-bus, or anything
74 needed) does not seem to be available.
75  
76 Handled named parameters:
77  
78 =over 4
79  
80 =item app
81  
82 symbolic application name (used in "Application .... is asking
83 to open the wallet" KDE Wallet prompt)
84  
85 =item group
86  
87 name for the password group (used as KDE Wallet folder name)
88  
89 =item dont_start_daemon
90  
91 by default, in case kwalletd service is missing, we try to start
92 it, this option disables this behaviour
93  
94 =item kwalletd_path
95  
96 path to kwalletd binary, used in case we try starting it. Default:
97 C<kwalletd> (relative path means searching in C<PATH>).
98  
99 =back
100  
101 =cut
102  
103 sub new {
104 my ($cls, %args) = @_;
105  
106 my $self = {};
107 $self->{app} = $args{app} || 'Passwd::Keyring::KDEWallet';
108 $self->{group} = $args{group} || 'Passwd::Keyring::default';
109 $self->{dont_start_daemon} = $args{dont_start_daemon} || '';
110 $self->{service_name} = 'org.kde.kwalletd';
111 $self->{module_name} = '/modules/kwalletd';
112 bless $self, $cls;
113 $self->{kwalletd_path} = $args{kwalletd_path} || $self->_which_kwalletd;
114  
115 #$self->{bus} = Net::DBus->find()
116 $self->{bus} = Net::DBus->session()
117 or croak("KWallet not available (can't access DBus)");
118  
119 $self->_init_kwallet();
120  
121 $self->_open_if_not_open();
122  
123 unless($self->{kwallet}->hasFolder($self->{handle}, $self->{group}, $self->{app})) {
124 $self->{kwallet}->createFolder($self->{handle}, $self->{group}, $self->{app})
125 or croak("Failed to create or access $self->{group} folder (app $self->{app}).\nDid you reject prompt to open the wallet?\n");
126 }
127  
128 return $self;
129 }
130  
131 sub _which_kwalletd {
132 my ($self) = @_;
133 for my $kwalletd ('kwalletd','kwalletd5') {
134 my $which = which($kwalletd);
135 if ($which) {
136 $self->{service_name} = "org.kde.$kwalletd";
137 $self->{module_name} = "/modules/$kwalletd";
138 return $which;
139 }
140 }
141 croak("KWallet not available (no binary found)");
142 }
143  
144 # Called from the constructor, setups self->{kwallet} attribute (top level
145 # service object)
146 sub _init_kwallet {
147 my $self = shift;
148  
149 # get_service may fail by itself, if kwalletd is down, in some
150 # cases it fails with
151 #
152 # org.freedesktop.DBus.Error.ServiceUnknown: The name org.kde.kwalletd was not provided by any .service files
153  
154 my $kwallet_svc;
155 my $error;
156 try {
157 $kwallet_svc = $self->{bus}->get_service($self->{service_name});
158 } catch {
159 $error = $_;
160 chomp($error);
161 };
162  
163 unless($kwallet_svc) {
164 # Mayhaps we are allowed to start kwalletd?
165 if(! $self->{dont_start_daemon}
166 && $error =~ /^org\.freedesktop\.DBus\.Error\.ServiceUnknown:/) {
167 print STDERR "KWallet service not available, attempting to start $self->{kwalletd_path}\n";
168 # spawn kwalletd
169 my $pid = sync_exec
170 sub {
171 # Without this prove (tests) hang
172 close (STDOUT);
173 return 1;
174 },
175 $self->{kwalletd_path};
176 unless($pid) {
177 croak "KWallet not available (not installed or not started)\nand attempt to start it failed\nOriginal error:\n$error" . "Attempted command:\n" . $self->{kwalletd_path} . "\nCommand failure: $!\n";
178 };
179 my $error2;
180 my $DEADLINE = time() + $KWALLETD_START_TIMEOUT;
181 # while($proc->alive && time() <= $DEADLINE) {
182 while(time() <= $DEADLINE) {
183 try {
184 $kwallet_svc = $self->{bus}->get_service('org.kde.kwalletd');
185 } catch {
186 $error2 = $_;
187 chomp($error2);
188 # warn "Still not available: $@\n";
189 };
190 last if $kwallet_svc;
191 sleep($KWALLETD_CHECK_FREQUENCY);
192 }
193 unless($kwallet_svc) {
194 croak "KWallet not available (not installed or not started),\nand attempt to start it did not help\nFirst error:\n$error\nLast error:\n$error2\nAttempted command: $self->{kwalletd_path}\n";
195 }
196 } else {
197 croak "KWallet not available (not installed or not started),\nand we are forbidden to start it ourselves.\nOriginal error:\n$error\n";
198 }
199 }
200  
201 $self->{kwallet} = $kwallet_svc->get_object(
202 $self->{module_name}, 'org.kde.KWallet')
203 or croak("Kwallet not available (can't find wallet)");
204 }
205  
206 sub _open_if_not_open {
207 my $self = shift;
208  
209 if($self->{handle}) {
210 if($self->{kwallet}->isOpen($self->{handle})) {
211 return;
212 }
213 }
214 my $net_wallet = $self->{kwallet}->networkWallet()
215 or croak("Kwallet not available (can't access network wallet");
216 $self->{handle} = $self->{kwallet}->open($net_wallet, 0, $self->{app})
217 or croak("Failed to open the KDE wallet");
218 }
219  
220 =head2 set_password(username, password, realm)
221  
222 Sets (stores) password identified by given realm for given user
223  
224 =cut
225  
226 sub set_password {
227 my ($self, $user_name, $user_password, $realm) = @_;
228 $self->_open_if_not_open();
229 my $status = $self->{kwallet}->writePassword(
230 $self->{handle}, $self->{group}, "$realm || $user_name", $user_password, $self->{app});
231 if($status) { # non-zero means failure
232 croak("Failed to save the password (status $status, user name $user_name, realm $realm, handle $self->{handle}, group $self->{group})");
233 }
234 }
235  
236 =head2 get_password($user_name, $realm)
237  
238 Reads previously stored password for given user in given app.
239 If such password can not be found, returns undef.
240  
241 =cut
242  
243 sub get_password {
244 my ($self, $user_name, $realm) = @_;
245 $self->_open_if_not_open();
246 my $reply = $self->{kwallet}->readPassword(
247 $self->{handle}, $self->{group}, "$realm || $user_name", $self->{app});
248 # In case of missing passsword we get empty string. I do not know
249 # whether it is possible to distinguish missing password from empty password,
250 # but empty passwords are exotic enough to ignore.
251 return undef if ! defined($reply) or $reply eq '';
252 return $reply;
253 }
254  
255 =head2 clear_password($user_name, $realm)
256  
257 Removes given password (if present)
258  
259 =cut
260  
261 sub clear_password {
262 my ($self, $user_name, $realm) = @_;
263 $self->_open_if_not_open();
264 my $status = $self->{kwallet}->removeEntry(
265 $self->{handle}, $self->{group}, "$realm || $user_name", $self->{app});
266 if($status == 0) {
267 return 1;
268 } else {
269 # TODO: classify failures
270 return 0;
271 }
272 }
273  
274 =head2 is_persistent
275  
276 Returns info, whether this keyring actually saves passwords persistently.
277  
278 (true in this case)
279  
280 =cut
281  
282 sub is_persistent {
283 my ($self) = @_;
284 return 1;
285 }
286  
287 =head1 AUTHOR
288  
289 Marcin Kasperski
290  
291 Approach inspired by L<http://www.perlmonks.org/?node_id=869620>.
292  
293 =head1 BUGS
294  
295 Please report any bugs or feature requests to
296 issue tracker at L<https://helixteamhub.cloud/mekk/projects/perl/issues>.
297  
298 =head1 SUPPORT
299  
300 You can find documentation for this module with the perldoc command.
301  
302 perldoc Passwd::Keyring::KDEWallet
303  
304 You can also look for information at:
305  
306 L<http://search.cpan.org/~mekk/Passwd-Keyring-KDEWallet/>
307  
308 Source code is tracked at:
309  
310 L<https://helixteamhub.cloud/mekk/projects/perl/repositories/keyring-kdewallet>
311  
312 =head1 LICENSE AND COPYRIGHT
313  
314 Copyright 2012-2020 Marcin Kasperski.
315  
316 This program is free software; you can redistribute it and/or modify it
317 under the terms of either: the GNU General Public License as published
318 by the Free Software Foundation; or the Artistic License.
319  
320 See http://dev.perl.org/licenses/ for more information.
321  
322 =cut
323  
324  
325 1; # End of Passwd::Keyring::KDEWallet
« auto/share/dist/Passwd-Keyring-KDEWallet/imap_query_example.pl ↑ Summary