Skip to content

Commit

Permalink
Automatically encrypt the password column.
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Jan 30, 2014
1 parent f47461a commit 1aa3b91
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 11 deletions.
5 changes: 3 additions & 2 deletions Build.PL
Expand Up @@ -7,8 +7,9 @@ my $build = Module::Build->new(
DBIx::Class => 0,
DBD::mysql => 0,
Moose => 0,
MooseX::MarkAsMethods => 0,
MooseX::NonMoose => 0,
MooseX::MarkAsMethods => 0,
MooseX::NonMoose => 0,
DBIx::Class::EncodedColumn => 0,
},
build_requires => {
Test::Class => 0,
Expand Down
36 changes: 36 additions & 0 deletions bin/dump_dbic
@@ -0,0 +1,36 @@
#!/usr/bin/env perl

use strict;
use warnings;
use 5.010;

use DBIx::Class::Schema::Loader qw[make_schema_at];

make_schema_at(
'Lystyng::Schema', {
dump_directory => 'lib',
use_moose => 1,
components => [ 'EncodedColumn' ],
custom_column_info => \&encode_password,
}, [
"dbi:mysql:dbname=$ENV{LYSTYNG_DB_NAME};host=$ENV{LYSTYNG_DB_SERVER}",
$ENV{LYSTYNG_DB_USER}, $ENV{LYSTYNG_DB_PASS},
],
);

sub encode_password {
my ($table, $column_name) = @_;

if ($column_name eq 'password') {
return {
encode_column => 1,
encode_class => 'Digest',
encode_args => {
algorithm => 'SHA-1',
format => 'hex',
salt_length => 10,
},
encode_check_method => 'check_password',
};
}
}
16 changes: 14 additions & 2 deletions lib/Lystyng/Schema/Result/List.pm
Expand Up @@ -18,6 +18,18 @@ use MooseX::NonMoose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'DBIx::Class::Core';

=head1 COMPONENTS LOADED
=over 4
=item * L<DBIx::Class::EncodedColumn>
=back
=cut

__PACKAGE__->load_components("EncodedColumn");

=head1 TABLE: C<list>
=cut
Expand Down Expand Up @@ -115,8 +127,8 @@ __PACKAGE__->belongs_to(
);


# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-01-07 22:12:36
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:BxQAjsxwfUnZNtVCJPzXjg
# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-01-30 20:13:57
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:3eYA3YhJdL90HQRfNN07EA


# You can replace this text with custom code or comments, and it will be preserved on regeneration
Expand Down
16 changes: 14 additions & 2 deletions lib/Lystyng/Schema/Result/ListItem.pm
Expand Up @@ -18,6 +18,18 @@ use MooseX::NonMoose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'DBIx::Class::Core';

=head1 COMPONENTS LOADED
=over 4
=item * L<DBIx::Class::EncodedColumn>
=back
=cut

__PACKAGE__->load_components("EncodedColumn");

=head1 TABLE: C<list_item>
=cut
Expand Down Expand Up @@ -92,8 +104,8 @@ __PACKAGE__->belongs_to(
);


# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-01-07 22:12:36
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:RoyFBd+PBGmWt1HIJH1FZw
# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-01-30 20:13:57
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:JdNndq1NJ/hzKunvCfj99g


# You can replace this text with custom code or comments, and it will be preserved on regeneration
Expand Down
30 changes: 27 additions & 3 deletions lib/Lystyng/Schema/Result/User.pm
Expand Up @@ -18,6 +18,18 @@ use MooseX::NonMoose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'DBIx::Class::Core';

=head1 COMPONENTS LOADED
=over 4
=item * L<DBIx::Class::EncodedColumn>
=back
=cut

__PACKAGE__->load_components("EncodedColumn");

=head1 TABLE: C<user>
=cut
Expand Down Expand Up @@ -47,6 +59,10 @@ __PACKAGE__->table("user");
=head2 password
data_type: 'char'
encode_args: {algorithm => "SHA-1",format => "hex",salt_length => 10}
encode_check_method: 'check_password'
encode_class: 'Digest'
encode_column: 1
is_nullable: 0
size: 64
Expand All @@ -60,7 +76,15 @@ __PACKAGE__->add_columns(
"email",
{ data_type => "varchar", is_nullable => 0, size => 200 },
"password",
{ data_type => "char", is_nullable => 0, size => 64 },
{
data_type => "char",
encode_args => { algorithm => "SHA-1", format => "hex", salt_length => 10 },
encode_check_method => "check_password",
encode_class => "Digest",
encode_column => 1,
is_nullable => 0,
size => 64,
},
);

=head1 PRIMARY KEY
Expand Down Expand Up @@ -93,8 +117,8 @@ __PACKAGE__->has_many(
);


# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-01-07 22:12:36
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:K6jCcJvzi8cOrpjW0pgYpw
# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-01-30 20:20:50
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:dOskylIQblron+9s87lFpw


# You can replace this text with custom code or comments, and it will be preserved on regeneration
Expand Down
5 changes: 3 additions & 2 deletions t/Test/Lystyng/Schema/Result/User.pm
Expand Up @@ -26,15 +26,16 @@ sub basic : Tests {
ok($user, 'Got another user');
isa_ok($user, 'Lystyng::Schema::Result::User');

foreach (qw[username password email]) {
foreach (qw[username email]) {
is($user->$_, $user2->$_, "$_ is correct");
}
ok($user->check_password('pass'), 'password is correct');

$user2->password('another password');
$user2->update;
# re-read from db
$user2->discard_changes;
is($user2->password, 'another password', 'Updated password is correct');
ok($user2->check_password('another password'), 'Updated password is correct');

$user_rs->delete;
}
Expand Down

0 comments on commit 1aa3b91

Please sign in to comment.