Skip to content
This repository
Browse code

Automatically encrypt the password column.

  • Loading branch information...
commit 1aa3b91f13f1a41acb808c39cf9b7c4ae5ee2b16 1 parent f47461a
Dave Cross authored January 30, 2014
5  Build.PL
@@ -7,8 +7,9 @@ my $build = Module::Build->new(
7 7
         DBIx::Class => 0,
8 8
         DBD::mysql  => 0,
9 9
         Moose       => 0,
10  
-        MooseX::MarkAsMethods => 0,
11  
-        MooseX::NonMoose      => 0,
  10
+        MooseX::MarkAsMethods      => 0,
  11
+        MooseX::NonMoose           => 0,
  12
+        DBIx::Class::EncodedColumn => 0,
12 13
     },
13 14
     build_requires => {
14 15
         Test::Class => 0,
36  bin/dump_dbic
... ...
@@ -0,0 +1,36 @@
  1
+#!/usr/bin/env perl
  2
+
  3
+use strict;
  4
+use warnings;
  5
+use 5.010;
  6
+
  7
+use DBIx::Class::Schema::Loader qw[make_schema_at];
  8
+
  9
+make_schema_at(
  10
+  'Lystyng::Schema', {
  11
+    dump_directory => 'lib',
  12
+    use_moose => 1,
  13
+    components => [ 'EncodedColumn' ],
  14
+    custom_column_info => \&encode_password,
  15
+  }, [
  16
+    "dbi:mysql:dbname=$ENV{LYSTYNG_DB_NAME};host=$ENV{LYSTYNG_DB_SERVER}",
  17
+    $ENV{LYSTYNG_DB_USER}, $ENV{LYSTYNG_DB_PASS},
  18
+  ],
  19
+);
  20
+
  21
+sub encode_password {
  22
+  my ($table, $column_name) = @_;
  23
+
  24
+  if ($column_name eq 'password') {
  25
+    return {
  26
+      encode_column => 1,
  27
+      encode_class  => 'Digest',
  28
+      encode_args   => {
  29
+        algorithm => 'SHA-1',
  30
+        format => 'hex',
  31
+        salt_length => 10,
  32
+      },
  33
+      encode_check_method => 'check_password',
  34
+    };
  35
+  }
  36
+}
16  lib/Lystyng/Schema/Result/List.pm
@@ -18,6 +18,18 @@ use MooseX::NonMoose;
18 18
 use MooseX::MarkAsMethods autoclean => 1;
19 19
 extends 'DBIx::Class::Core';
20 20
 
  21
+=head1 COMPONENTS LOADED
  22
+
  23
+=over 4
  24
+
  25
+=item * L<DBIx::Class::EncodedColumn>
  26
+
  27
+=back
  28
+
  29
+=cut
  30
+
  31
+__PACKAGE__->load_components("EncodedColumn");
  32
+
21 33
 =head1 TABLE: C<list>
22 34
 
23 35
 =cut
@@ -115,8 +127,8 @@ __PACKAGE__->belongs_to(
115 127
 );
116 128
 
117 129
 
118  
-# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-01-07 22:12:36
119  
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:BxQAjsxwfUnZNtVCJPzXjg
  130
+# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-01-30 20:13:57
  131
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:3eYA3YhJdL90HQRfNN07EA
120 132
 
121 133
 
122 134
 # You can replace this text with custom code or comments, and it will be preserved on regeneration
16  lib/Lystyng/Schema/Result/ListItem.pm
@@ -18,6 +18,18 @@ use MooseX::NonMoose;
18 18
 use MooseX::MarkAsMethods autoclean => 1;
19 19
 extends 'DBIx::Class::Core';
20 20
 
  21
+=head1 COMPONENTS LOADED
  22
+
  23
+=over 4
  24
+
  25
+=item * L<DBIx::Class::EncodedColumn>
  26
+
  27
+=back
  28
+
  29
+=cut
  30
+
  31
+__PACKAGE__->load_components("EncodedColumn");
  32
+
21 33
 =head1 TABLE: C<list_item>
22 34
 
23 35
 =cut
@@ -92,8 +104,8 @@ __PACKAGE__->belongs_to(
92 104
 );
93 105
 
94 106
 
95  
-# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-01-07 22:12:36
96  
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:RoyFBd+PBGmWt1HIJH1FZw
  107
+# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-01-30 20:13:57
  108
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:JdNndq1NJ/hzKunvCfj99g
97 109
 
98 110
 
99 111
 # You can replace this text with custom code or comments, and it will be preserved on regeneration
30  lib/Lystyng/Schema/Result/User.pm
@@ -18,6 +18,18 @@ use MooseX::NonMoose;
18 18
 use MooseX::MarkAsMethods autoclean => 1;
19 19
 extends 'DBIx::Class::Core';
20 20
 
  21
+=head1 COMPONENTS LOADED
  22
+
  23
+=over 4
  24
+
  25
+=item * L<DBIx::Class::EncodedColumn>
  26
+
  27
+=back
  28
+
  29
+=cut
  30
+
  31
+__PACKAGE__->load_components("EncodedColumn");
  32
+
21 33
 =head1 TABLE: C<user>
22 34
 
23 35
 =cut
@@ -47,6 +59,10 @@ __PACKAGE__->table("user");
47 59
 =head2 password
48 60
 
49 61
   data_type: 'char'
  62
+  encode_args: {algorithm => "SHA-1",format => "hex",salt_length => 10}
  63
+  encode_check_method: 'check_password'
  64
+  encode_class: 'Digest'
  65
+  encode_column: 1
50 66
   is_nullable: 0
51 67
   size: 64
52 68
 
@@ -60,7 +76,15 @@ __PACKAGE__->add_columns(
60 76
   "email",
61 77
   { data_type => "varchar", is_nullable => 0, size => 200 },
62 78
   "password",
63  
-  { data_type => "char", is_nullable => 0, size => 64 },
  79
+  {
  80
+    data_type           => "char",
  81
+    encode_args         => { algorithm => "SHA-1", format => "hex", salt_length => 10 },
  82
+    encode_check_method => "check_password",
  83
+    encode_class        => "Digest",
  84
+    encode_column       => 1,
  85
+    is_nullable         => 0,
  86
+    size                => 64,
  87
+  },
64 88
 );
65 89
 
66 90
 =head1 PRIMARY KEY
@@ -93,8 +117,8 @@ __PACKAGE__->has_many(
93 117
 );
94 118
 
95 119
 
96  
-# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-01-07 22:12:36
97  
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:K6jCcJvzi8cOrpjW0pgYpw
  120
+# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-01-30 20:20:50
  121
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:dOskylIQblron+9s87lFpw
98 122
 
99 123
 
100 124
 # You can replace this text with custom code or comments, and it will be preserved on regeneration
5  t/Test/Lystyng/Schema/Result/User.pm
@@ -26,15 +26,16 @@ sub basic : Tests {
26 26
   ok($user, 'Got another user');
27 27
   isa_ok($user, 'Lystyng::Schema::Result::User');
28 28
 
29  
-  foreach (qw[username password email]) {
  29
+  foreach (qw[username email]) {
30 30
     is($user->$_, $user2->$_, "$_ is correct");
31 31
   }
  32
+  ok($user->check_password('pass'), 'password is correct');
32 33
 
33 34
   $user2->password('another password');
34 35
   $user2->update;
35 36
   # re-read from db
36 37
   $user2->discard_changes;
37  
-  is($user2->password, 'another password', 'Updated password is correct');
  38
+  ok($user2->check_password('another password'), 'Updated password is correct');
38 39
 
39 40
   $user_rs->delete;
40 41
 }

0 notes on commit 1aa3b91

Please sign in to comment.
Something went wrong with that request. Please try again.