| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$password_group and $app_name
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 65 | 0 | my $client_secret = $self->keyring->get_password($client_key, $self->site_name); | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | return unless $client_secret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||
| 80 | sub clear_client_cred { | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clear_client_cred: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # FIXME: give an option of clearing only mapping but keeping key in storage? | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | my $client_key = $self->keyring->get_password($CLIENT_MAP_KEY, $self->site_name); | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | return unless $client_key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | $self->keyring->clear_password($client_key, $self->site_name); | |||||||||||||||||||||||||||
| 89 | 0 | $self->keyring->clear_password($CLIENT_MAP_KEY, $self->site_name); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 3 | const my $TOKEN_MAP_KEY => "oauthomatic_token"; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub get_token_cred { | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_token_cred: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | my $token = $self->keyring->get_password($TOKEN_MAP_KEY, $self->site_name); | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | return unless $token; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | my $token_secret = $self->keyring->get_password($token, $self->site_name); | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | return unless $token_secret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | return OAuthomatic::Types::TokenCred->new( | |||||||||||||||||||||||||||
| 102 | token => $token, | ||||||||||||||||||||||||||||
| 103 | secret => $token_secret, | ||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | sub save_token_cred { | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | my ($self, $access) = @_; | ||||||||||||||||||||||||||
|
Subroutine
save_token_cred: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | $self->keyring->set_password($access->token, $access->secret, $self->site_name); | |||||||||||||||||||||||||||
| 111 | 0 | $self->keyring->set_password($TOKEN_MAP_KEY, $access->token, $self->site_name); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | sub clear_token_cred { | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clear_token_cred: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | my $token = $self->keyring->get_password($TOKEN_MAP_KEY, $self->site_name); | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | return unless $token; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | $self->keyring->clear_password($token, $self->site_name); | |||||||||||||||||||||||||||
| 121 | 0 | $self->keyring->clear_password($TOKEN_MAP_KEY, $self->site_name); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 3 | with 'OAuthomatic::SecretStorage'; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 3 | 1; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | __END__ | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =pod | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 NAME | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | OAuthomatic::SecretStorage::Keyring - Save tokens in secure storage, using L<Passwd::Keyring::Auto> | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head1 VERSION | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | version 0.01 | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Implements L<OAuthomatic::SecretStorage> interface using | ||||||||||||||||||||||||||||
| 145 | L<Passwd::Keyring::Auto> (what usually means saving data in Gnome | ||||||||||||||||||||||||||||
| 146 | Keyring, KDE Wallet, Windows Vault or similar secure storage). | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Note that tokens are saved in slightly specific way. Whenever client | ||||||||||||||||||||||||||||
| 149 | key is saved, we create two entries in keyring: | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | username: client_key | ||||||||||||||||||||||||||||
| 152 | password: client_secret | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | and | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | username: "oauthomatic_client_key" | ||||||||||||||||||||||||||||
| 157 | password: client_key | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | The former is natural. The latter helps find what current key is | ||||||||||||||||||||||||||||
| 160 | (this record is not very secret but it is easier to keep it too | ||||||||||||||||||||||||||||
| 161 | than invent second configuration backend). | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | Access tokens are treated similarly: | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | username: token | ||||||||||||||||||||||||||||
| 166 | password: token_secret | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | and | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | username: "oauthomatic_token" | ||||||||||||||||||||||||||||
| 171 | password: token | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head2 config | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | L<OAuthomatic::Config> object used to bundle various configuration params. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head2 server | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | L<OAuthomatic::Server> object used to bundle server-related configuration params. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head2 keyring | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Actual password backend in use. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Usually initialized automatically, guessing best possible password | ||||||||||||||||||||||||||||
| 188 | backend (according to L<Passwd::Keyring::Auto> selection and using | ||||||||||||||||||||||||||||
| 189 | it's C<app_name> and C<password_group> attributes), but can be | ||||||||||||||||||||||||||||
| 190 | specified if application prefers to use sth. specific: | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | keyring => Passwd::Keyring::KDEWallet->new(...)) | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | Marcin Kasperski <Marcin.Kasperski@mekk.waw.pl> | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | This software is copyright (c) 2015 by Marcin Kasperski. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 203 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =cut | ||||||||||||||||||||||||||||