lib/OAuthomatic/SecretStorage/Keyring.pm

filter: Statementstmt 32.8% 22 / 67 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 38.4% 5 / 13 Podpod 0.0% 0 / 6 totaltotal 24.3% 27 / 111 CC 12 SCAR 24.1
CC
12
Cov
24%
_build_keyring
30.0
get_client_cred
12.0
get_token_cred
12.0
CRAP 11.1
« blib/lib/OAuthomatic/UserInteraction/ConsolePrompts.pm ↑ Summary lib/OAuthomatic/Config.pm »
1 package OAuthomatic::SecretStorage::Keyring;
2 # ABSTRACT: Save tokens in secure storage, using L<Passwd::Keyring::Auto>
3  
4 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use namespace::sweep;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use MooseX::AttributeShortcuts;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Passwd::Keyring::Auto qw(get_keyring);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 3 has 'config' => (
12 is => 'ro', isa => 'OAuthomatic::Config', required => 1,
13 handles => [
14 'app_name',
15 'password_group',
16 'debug',
17 ]);
18  
19  
20 3 has 'server' => (
21 is => 'ro', isa => 'OAuthomatic::Server', required => 1,
22 handles => [
23 'site_name',
24 ]);
25  
26  
27 has 'keyring' => (
28 is => 'lazy', trigger => sub {
29 0 ▶ my ($self, $keyring, $old_val) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
30 0 ▶ unless($keyring->is_persistent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keyring->is_persistent) 0 0 0
31 0 OAuthomatic::Error::Generic->throw(
32 ident => "Attempt to use non-persistent keyring",
33 extra => "Suggested backend is non-persistent: ") . ref($keyring) . ".\n";
34 }
35 3 });
36  
37 sub _build_keyring {
38 0 ▶ my $self = shift;
Subroutine _build_keyring: not called CC=5 SCAR=34.0
39 0 my $password_group = $self->password_group;
40 0 my $app_name = $self->app_name;
41 0 ▶ OAuthomatic::Error::Generic->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $password_group and $app_name 0 0 0
Condition0%0/3
$password_group and $app_name
!ll&&!rl&&r
000
MC/DC0%0/2
$password_group and $app_name
$password_group $app_name
Truth table0%0/3
A and B
A= $password_groupB= $app_name
ABresult
0X0
100
111
42 ident => "Bad parameter",
43 extra => "You must specify password_group and app_name (or specify keyring)")
44 unless ($password_group && $app_name);
45 0 my $keyring = get_keyring(group => $password_group,
46 app => $app_name);
47 0 ▶ unless($keyring->is_persistent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keyring->is_persistent) 0 0 0
48 0 OAuthomatic::Error::Generic->throw(
49 ident => "Bad keyring configuration",
50 extra => "Got non-persistent keyring backend (") . ref($keyring) . "). Reconfigure Passwd::Keyring::Auto (and maybe install some Passwd::Keyring::<backend>) to use something non-volatile.\n";
51 }
52 0 ▶ if($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
53 0 print "[OAuthomatic] Constructed keyring. Type: ", ref($keyring), ", app: $app_name, group: $password_group\n";
54 }
55 0 return $keyring;
56 }
57  
58 3 const my $CLIENT_MAP_KEY => "oauthomatic_client_key";
59  
60 sub get_client_cred {
61 0 ▶ my ($self) = @_;
Subroutine get_client_cred: not called CC=3 SCAR=24.8
Pod undocumented
62  
63 0 my $client_key = $self->keyring->get_password($CLIENT_MAP_KEY, $self->site_name);
64 0 ▶ return unless $client_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $client_key 0 0 0
65 0 my $client_secret = $self->keyring->get_password($client_key, $self->site_name);
66 0 ▶ return unless $client_secret;
Branch: 1 branch
BranchTrueFalseTotal
unless $client_secret 0 0 0
67 0 return OAuthomatic::Types::ClientCred->new(
68 key => $client_key,
69 secret => $client_secret,
70 );
71 }
72  
73 sub save_client_cred {
74 0 ▶ my ($self, $client) = @_;
Subroutine save_client_cred: not called CC=1 SCAR=6.9
Pod undocumented
75  
76 0 $self->keyring->set_password($client->key, $client->secret, $self->site_name);
77 0 $self->keyring->set_password($CLIENT_MAP_KEY, $client->key, $self->site_name);
78  
79 0 return;
80 }
81  
82 sub clear_client_cred {
83 0 ▶ my ($self) = @_;
Subroutine clear_client_cred: not called CC=2 SCAR=17.9
Pod undocumented
84  
85 # FIXME: give an option of clearing only mapping but keeping key in storage?
86  
87 0 my $client_key = $self->keyring->get_password($CLIENT_MAP_KEY, $self->site_name);
88 0 ▶ return unless $client_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $client_key 0 0 0
89  
90 0 $self->keyring->clear_password($client_key, $self->site_name);
91 0 $self->keyring->clear_password($CLIENT_MAP_KEY, $self->site_name);
92  
93 0 return;
94 }
95  
96 3 const my $TOKEN_MAP_KEY => "oauthomatic_token";
97  
98 sub get_token_cred {
99 0 ▶ my ($self) = @_;
Subroutine get_token_cred: not called CC=3 SCAR=24.8
Pod undocumented
100  
101 0 my $token = $self->keyring->get_password($TOKEN_MAP_KEY, $self->site_name);
102 0 ▶ return unless $token;
Branch: 1 branch
BranchTrueFalseTotal
unless $token 0 0 0
103 0 my $token_secret = $self->keyring->get_password($token, $self->site_name);
104 0 ▶ return unless $token_secret;
Branch: 1 branch
BranchTrueFalseTotal
unless $token_secret 0 0 0
105 0 return OAuthomatic::Types::TokenCred->new(
106 token => $token,
107 secret => $token_secret,
108 );
109 }
110  
111 sub save_token_cred {
112 0 ▶ my ($self, $access) = @_;
Subroutine save_token_cred: not called CC=1 SCAR=6.9
Pod undocumented
113  
114 0 $self->keyring->set_password($access->token, $access->secret, $self->site_name);
115 0 $self->keyring->set_password($TOKEN_MAP_KEY, $access->token, $self->site_name);
116  
117 0 return;
118 }
119  
120 sub clear_token_cred {
121 0 ▶ my ($self) = @_;
Subroutine clear_token_cred: not called CC=2 SCAR=17.9
Pod undocumented
122  
123 0 my $token = $self->keyring->get_password($TOKEN_MAP_KEY, $self->site_name);
124 0 ▶ return unless $token;
Branch: 1 branch
BranchTrueFalseTotal
unless $token 0 0 0
125  
126 0 $self->keyring->clear_password($token, $self->site_name);
127 0 $self->keyring->clear_password($TOKEN_MAP_KEY, $self->site_name);
128  
129 0 return;
130 }
131  
132 3 with 'OAuthomatic::SecretStorage';
133  
134 3 1;
135  
136 __END__
137  
138 =pod
139  
140 =encoding UTF-8
141  
142 =head1 NAME
143  
144 OAuthomatic::SecretStorage::Keyring - Save tokens in secure storage, using L<Passwd::Keyring::Auto>
145  
146 =head1 VERSION
147  
148 version 0.0201
149  
150 =head1 DESCRIPTION
151  
152 Implements L<OAuthomatic::SecretStorage> interface using
153 L<Passwd::Keyring::Auto> (what usually means saving data in Gnome
154 Keyring, KDE Wallet, Windows Vault or similar secure storage).
155  
156 Note that tokens are saved in slightly specific way. Whenever client
157 key is saved, we create two entries in keyring:
158  
159 username: client_key
160 password: client_secret
161  
162 and
163  
164 username: "oauthomatic_client_key"
165 password: client_key
166  
167 The former is natural. The latter helps find what current key is
168 (this record is not very secret but it is easier to keep it too
169 than invent second configuration backend).
170  
171 Access tokens are treated similarly:
172  
173 username: token
174 password: token_secret
175  
176 and
177  
178 username: "oauthomatic_token"
179 password: token
180  
181 =head1 ATTRIBUTES
182  
183 =head2 config
184  
185 L<OAuthomatic::Config> object used to bundle various configuration params.
186  
187 =head2 server
188  
189 L<OAuthomatic::Server> object used to bundle server-related configuration params.
190  
191 =head2 keyring
192  
193 Actual password backend in use.
194  
195 Usually initialized automatically, guessing best possible password
196 backend (according to L<Passwd::Keyring::Auto> selection and using
197 it's C<app_name> and C<password_group> attributes), but can be
198 specified if application prefers to use sth. specific:
199  
200 keyring => Passwd::Keyring::KDEWallet->new(...))
201  
202 =head1 AUTHOR
203  
204 Marcin Kasperski <Marcin.Kasperski@mekk.waw.pl>
205  
206 =head1 COPYRIGHT AND LICENSE
207  
208 This software is copyright (c) 2015 by Marcin Kasperski.
209  
210 This is free software; you can redistribute it and/or modify it under
211 the same terms as the Perl 5 programming language system itself.
212  
213 =cut
« blib/lib/OAuthomatic/UserInteraction/ConsolePrompts.pm ↑ Summary lib/OAuthomatic/Config.pm »