blib/lib/Passwd/Keyring/Secret.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Passwd::Keyring::Secret;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Glib::Object::Introspection;
Subroutine BEGIN: called CC=1 SCAR=2.6
8  
9 eval
10 {
11 Glib::Object::Introspection->setup(
12 basename => 'Secret',
13 version => '1',
14 package => 'Secret',
15 class_static_methods => [
16 'Secret::Collection::for_alias_sync',
17 'Secret::Service::get_sync',
18 ]
19 );
20 };
21  
22 if ($@)
23 {
24 if (ref($@) eq 'Glib::Error' && $@->domain() eq 'g-irepository-error-quark')
25 {
26 my $msg = $@;
27 chomp($msg);
28 $@ = (<< "EOM") . "Import failed";
29 Error: $msg
30 You probably need to install the file 'Secret-1.typelib' on your system.
31 EOM
32 }
33  
34 croak $@;
35 }
36  
37 =head1 NAME
38  
39 Passwd::Keyring::Secret - Password storage implementation using the GObject-based Secret library.
40  
41 =head1 VERSION
42  
43 Version 1.01
44  
45 =cut
46  
47 our $VERSION = '1.01';
48  
49 =head1 SYNOPSIS
50  
51 C<Passwd::Keyring> compliant implementation that is using the
52 GObject-based Secret library to provide secure storage for passwords
53 and similar sensitive data.
54  
55 use Passwd::Keyring::Secret;
56  
57 my $keyring = Passwd::Keyring::Secret->new(
58 app => "blahblah scraper",
59 group => "Johnny web scrapers"
60 );
61  
62 my $username = "John"; # or get from .ini, or from .argv ...
63  
64 my $password = $keyring->get_password($username, "blahblah.com");
65 unless ($password)
66 {
67 $password = <somehow interactively prompt for password>;
68  
69 # securely save password for future use
70 $keyring->set_password($username, $password, "blahblah.com");
71 }
72  
73 login_somewhere_using($username, $password);
74 if (password_was_wrong)
75 {
76 $keyring->clear_password($username, "blahblah.com");
77 }
78  
79 B<Note:> see L<Passwd::Keyring::Auto::KeyringAPI> for detailed comments
80 on keyring method semantics (this document is installed with the
81 L<Passwd::Keyring::Auto> package).
82  
83 =head1 METHODS
84  
85 =head2 new(app => 'app name', group => 'passwords folder', ...)
86  
87 Initializes the processing. Croaks if keyring for a given alias name
88 or the Secret service itself does not seem to be available.
89  
90 Handled named parameters:
91  
92 - app - symbolic application name (not used at the moment, but may be
93 used as comment and in prompts in the future, so set sensibly)
94  
95 - group - name for the password group (will be visible in Seahorse, so
96 can be used by the user to manage passwords; different group means
97 different password set; a few apps may share the same group if they
98 need to use the same password set)
99  
100 - alias (I<optional>) - alias name of the keyring (the default keyring
101 will be used if undefined; use C<"session"> to store passwords in the
102 session keyring which doesn't get stored across login sessions)
103  
104 =cut
105  
106 sub new
107 {
108 my ($ref, %params) = @_;
109 my $class = ref($ref) || $ref;
110 my $self = (ref($ref) eq '') ? {
111 app => $params{app} || 'Passwd::Keyring',
112 group => $params{group} || 'Passwd::Keyring passwords',
113 alias => $params{alias} || 'default'
114 } : {
115 app => $params{app} || $ref->{app},
116 group => $params{group} || $ref->{group},
117 alias => $params{alias} || $ref->{alias}
118 };
119 bless($self, $class);
120  
121 $self->{schema} = undef;
122  
123 my $service_proxy = Secret::Service->get_sync('open-session');
124 my $collection = Secret::Collection->for_alias_sync($service_proxy, $self->{alias}, 'none');
125 croak "Secret store seems to be unavailable (failed to get $self->{alias} keyring)" unless $collection;
126 $self->{_service_proxy} = $service_proxy;
127  
128 return $self;
129 }
130  
131 =head2 set_password($username, $password, $realm)
132  
133 Stores password identified by given realm for given user.
134  
135 =cut
136  
137 sub set_password
138 {
139 my ($self, $user_name, $user_password, $realm) = @_;
140 Secret::password_store_sync(
141 $self->{schema},
142 { group => $self->{group}, realm => $realm, user => $user_name },
143 $self->{alias},
144 "$self->{group}/$realm/$user_name (by $self->{app})",
145 $user_password
146 ) or croak "Failed to set password";
147 }
148  
149 =head2 get_password($username, $realm)
150  
151 Looks up previously stored password for given user and given realm.
152 Returns C<undef> if such a password could not be found.
153  
154 =cut
155  
156 sub get_password
157 {
158 my ($self, $user_name, $realm) = @_;
159 my $pwd = Secret::password_lookup_sync(
160 $self->{schema},
161 { group => $self->{group}, realm => $realm, user => $user_name }
162 );
163 #carp "Cannot find a password" unless defined $pwd;
164 return $pwd;
165 }
166  
167 =head2 clear_password($username, $realm)
168  
169 Removes password matching given user and given realm (if present).
170 Returns whether a password was removed.
171  
172 =cut
173  
174 sub clear_password
175 {
176 my ($self, $user_name, $realm) = @_;
177 return Secret::password_clear_sync(
178 $self->{schema},
179 { group => $self->{group}, realm => $realm, user => $user_name }
180 );
181 }
182  
183 =head2 is_persistent()
184  
185 Returns whether this keyring actually saves passwords persistently
186 (true unless initial parameter C<alias> was set to C<"session">).
187  
188 =cut
189  
190 sub is_persistent
191 {
192 my ($self) = @_;
193 return $self->{alias} ne 'session';
194 }
195  
196 =head1 INSTALLATION
197  
198 Run the following commands to install this module:
199  
200 ./Build.PL
201 ./Build
202 ./Build test
203 ./Build install
204  
205 =head1 SUPPORT
206  
207 After installation, you can find the documentation for this module
208 using the perldoc command:
209  
210 perldoc Passwd::Keyring::Secret
211  
212 You can also look for information at
213  
214 L<https://search.cpan.org/~uhle/Passwd-Keyring-Secret/>.
215  
216 The source code is tracked at
217  
218 L<https://gitlab.com/uhle/Passwd-Keyring-Secret>.
219  
220 =head1 BUGS
221  
222 Please report any bugs or feature requests to the issue tracker at
223 L<https://gitlab.com/uhle/Passwd-Keyring-Secret/-/issues>.
224  
225 =head1 AUTHOR
226  
227 Thomas Uhle <uhle@cpan.org>
228  
229 =head1 COPYRIGHT AND LICENSE
230  
231 Copyright (c) 2020-2021 Thomas Uhle. All rights reserved.
232  
233 This module is free software; you can redistribute it and/or modify it
234 under the terms of the Artistic License 2.0 as published by the Perl
235 Foundation.
236  
237 See L<https://www.perlfoundation.org/artistic-license-20.html> for more
238 information.
239  
240 =cut
241  
242 1;
243  
244 __END__
↑ Summary