Ticket #91: crypt.patch

Line 
1 === lib/Plagger/Cache.pm
2 ==================================================================
3 --- lib/Plagger/Cache.pm        (revision 25125)
4 +++ lib/Plagger/Cache.pm        (local)
5 @@ -30,6 +30,13 @@
6      }, $class;
7  }
8  
9 +sub base { $_[0]->{base} }
10 +
11 +sub path_to {
12 +    my($self, @path) = @_;
13 +    File::Spec->catfile($self->{base}, @path);
14 +}
15 +
16  sub get {
17      my $self = shift;
18  
19 === lib/Plagger/Plugin/Subscription/Bloglines.pm
20 ==================================================================
21 --- lib/Plagger/Plugin/Subscription/Bloglines.pm        (revision 25125)
22 +++ lib/Plagger/Plugin/Subscription/Bloglines.pm        (local)
23 @@ -5,6 +5,8 @@
24  our $VERSION = '0.10';
25  use WebService::Bloglines;
26  
27 +sub encrypt_config { 'password' }
28 +
29  sub register {
30      my($self, $context) = @_;
31  
32 === lib/Plagger/Plugin.pm
33 ==================================================================
34 --- lib/Plagger/Plugin.pm       (revision 25125)
35 +++ lib/Plagger/Plugin.pm       (local)
36 @@ -4,6 +4,7 @@
37  
38  __PACKAGE__->mk_accessors( qw(conf rule rule_hook cache) );
39  
40 +use Plagger::Crypt;
41  use Plagger::Rule;
42  use Plagger::Rules;
43  
44 @@ -22,6 +23,7 @@
45  
46  sub init {
47      my $self = shift;
48 +
49      if (my $rule = $self->{rule}) {
50          $rule = [ $rule ] if ref($rule) eq 'HASH';
51          my $op = $self->{rule_op};
52 @@ -29,8 +31,23 @@
53      } else {
54          $self->{rule} = Plagger::Rule->new({ module => 'Always' });
55      }
56 +
57 +    if (my $params = $self->encrypt_config) {
58 +        $params = [ $params ] unless ref $params;
59 +
60 +        for my $key (@$params) {
61 +            my $config = $self->conf;
62 +            # support foo/bar/baz
63 +            while ($key =~ s!^(\w+)/!!) {
64 +                $config = $config->{$1};
65 +            }
66 +            warn $config->{$key} = Plagger::Crypt->decrypt($config->{$key});
67 +        }
68 +    }
69  }
70  
71 +sub encrypt_config { }
72 +
73  sub conf { $_[0]->{conf} }
74  sub rule { $_[0]->{rule} }
75  
76 === lib/Plagger/Crypt/Base64.pm
77 ==================================================================
78 --- lib/Plagger/Crypt/Base64.pm (revision 25125)
79 +++ lib/Plagger/Crypt/Base64.pm (local)
80 @@ -0,0 +1,18 @@
81 +package Plagger::Crypt::Base64;
82 +use strict;
83 +use MIME::Base64 ();
84 +
85 +sub id { 'base64' }
86 +
87 +sub decrypt {
88 +    my($self, $text) = @_;
89 +    MIME::Base64::decode($text);
90 +}
91 +
92 +sub encrypt {
93 +    my($self, $text) = @_;
94 +    MIME::Base64::encode($text, '');
95 +}
96 +
97 +1;
98 +
99
100 Property changes on: lib/Plagger/Crypt/Base64.pm
101 ___________________________________________________________________
102 Name: svn:keywords
103  +Id Revision
104
105 === lib/Plagger/Crypt/Blowfish.pm
106 ==================================================================
107 --- lib/Plagger/Crypt/Blowfish.pm       (revision 25125)
108 +++ lib/Plagger/Crypt/Blowfish.pm       (local)
109 @@ -0,0 +1,53 @@
110 +package Plagger::Crypt::Blowfish;
111 +use strict;
112 +use Crypt::CBC;
113 +
114 +sub id { 'Blowfish' }
115 +
116 +sub cipher {
117 +    my $self = shift;
118 +
119 +    my $key = $self->read_key;
120 +    return Crypt::CBC->new({
121 +        key     => $key,
122 +        cipher  => 'Blowfish',
123 +        padding => 'null',
124 +    });
125 +}
126 +
127 +sub read_key {
128 +    my $self = shift;
129 +
130 +    my $context = Plagger->context;
131 +    my $path    = $context->cache->path_to("Blowfish.key");
132 +
133 +    open my $fh, $path or $context->error("$path: $!");
134 +    chomp(my $key = <$fh>);
135 +    close $fh;
136 +
137 +    $key;
138 +}
139 +
140 +sub generate_key {
141 +    my($self, $key) = @_;
142 +
143 +    my $context = Plagger->context;
144 +    my $path    = $context->cache->path_to("Blowfish.key");
145 +
146 +    open my $fh, ">", $path or $context->error("$path: $!");
147 +    print $fh $key;
148 +    close $fh;
149 +}
150 +
151 +sub decrypt {
152 +    my($self, $text) = @_;
153 +    $self->cipher->decrypt_hex($text);
154 +}
155 +
156 +sub encrypt {
157 +    my($self, $text) = @_;
158 +    $self->cipher->encrypt_hex($text);
159 +}
160 +
161 +1;
162 +
163
164 Property changes on: lib/Plagger/Crypt/Blowfish.pm
165 ___________________________________________________________________
166 Name: svn:keywords
167  +Id Revision
168
169 === lib/Plagger/Crypt.pm
170 ==================================================================
171 --- lib/Plagger/Crypt.pm        (revision 25125)
172 +++ lib/Plagger/Crypt.pm        (local)
173 @@ -0,0 +1,27 @@
174 +package Plagger::Crypt;
175 +use strict;
176 +use Module::Pluggable::Fast
177 +    search => [ qw/Plagger::Crypt/ ],
178 +    require => 1;
179 +
180 +my %handlers = map { $_->id => $_ } __PACKAGE__->plugins;
181 +my $re = "^(" . join("|", map $_->id, __PACKAGE__->plugins) . ")::";
182 +
183 +sub decrypt {
184 +    my($class, $ciphertext, @args) = @_;
185 +
186 +    if ($ciphertext =~ s!$re!!) {
187 +        my $handler = $handlers{$1};
188 +        my @param = split /::/, $ciphertext;
189 +        return $handler->decrypt(@param, @args);
190 +    }
191 +
192 +    return $ciphertext; # just plain text
193 +}
194 +
195 +sub encrypt {
196 +    my($class, $plaintext, $driver, @param) = @_;
197 +
198 +}
199 +
200 +1;
201
202 Property changes on: lib/Plagger/Crypt.pm
203 ___________________________________________________________________
204 Name: svn:keywords
205  +Id Revision
206
207