Rose::DB::Object::MakeMethods::Pg(3pm) | User Contributed Perl Documentation | Rose::DB::Object::MakeMethods::Pg(3pm) |
Rose::DB::Object::MakeMethods::Pg - Create PostgreSQL-specific object methods for Rose::DB::Object-derived objects.
package MyDBObject; our @ISA = qw(Rose::DB::Object); use Rose::DB::Object::MakeMethods::Pg ( chkpass => [ 'password', 'secret' => { encrypted_suffix => '_mangled', cmp_suffix => '_equals', }, ], ); ... $o = MyDBObject->new(...); $o->password('foobar'); # Something like: ":vOR7BujbRZSLM" (varies based on salt used) print $o->password_encrypted; print $o->password; # "foobar" print "ok" if($o->password_is('foobar'); # "ok" $o->secret('baz'); # Something like: ":jqROBZMqtWGJE" (varies based on salt used) print $o->secret_mangled; print $o->secret; # "baz" print "ok" if($o->secret_equals('baz'); # "ok"
"Rose::DB::Object::MakeMethods::Pg" creates methods that deal with data types that are specific to the PostgreSQL database server. It inherits from Rose::Object::MakeMethods. See the Rose::Object::MakeMethods documentation to learn about the interface. The method types provided by this module are described below.
All method types defined by this module are designed to work with objects that are subclasses of (or otherwise conform to the interface of) Rose::DB::Object. In particular, the object is expected to have a "db" method that returns a Rose::DB-derived object. See the Rose::DB::Object documentation for more details.
"Chkpass is a password type that is automatically checked and converted upon entry. It is stored encrypted. To compare, simply compare against a clear text password and the comparison function will encrypt it before comparing.
If you precede the string with a colon, the encryption and checking are skipped so that you can enter existing passwords into the field.
On output, a colon is prepended. This makes it possible to dump and reload passwords without re-encrypting them. If you want the password (encrypted) without the colon then use the raw() function. This allows you to use the type with things like Apache's Auth_PostgreSQL module."
This data type is very handy for storing encrypted values such as passwords while still retaining the ability to perform SELECTs and such using unencrypted values in comparisons. For example, the query
SELECT * FROM users WHERE password = 'foobar'
will actually find all the users whose passwords are "foobar", even though all the passwords are encrypted in the database.
The encrypted value is stored in a hash key with the same name, but with "encrypted_suffix" appended.
If passed an argument that begins with ":", it is assumed to be an encrypted value and is stored as such. Undef is returned, since it is not feasible to determine the unencrypted value based on the encrypted value.
If passed an argument that does not begin with ":", it is taken as the unencrypted value. The value is encrypted using Perl's "crypt()" function paired with a randomly selected salt, and the unencrypted value is returned.
If called with no arguments, the encrypted value is returned, if it is known. If not, undef is returned.
If passed an argument that begins with ":", it is assumed to be an encrypted value and is stored as such. The unencrypted value is set to undef, since it is not feasible to determine the unencrypted value based on the encrypted value. The encrypted value is returned.
If passed an argument that does not begin with ":", it is taken as the unencrypted value. The value is encrypted using Perl's "crypt()" function paired with a randomly selected salt, and the encrypted value is returned.
Example:
package MyDBObject; our @ISA = qw(Rose::DB::Object); use Rose::DB::Object::MakeMethods::Pg ( chkpass => [ 'password', 'get_password' => { interface => 'get', hash_key => 'password' }, 'set_password' => { interface => 'set', hash_key => 'password' }, 'secret' => { encrypted_suffix => '_mangled', cmp_suffix => '_equals', }, ], ); ... $o = MyDBObject->new(...); $o->set_password('blah'); $o->password('foobar'); # Something like: ":vOR7BujbRZSLM" (varies based on salt used) print $o->password_encrypted; print $o->get_password; # "foobar" print $o->password; # "foobar" print "ok" if($o->password_is('foobar'); # "ok" $o->secret('baz'); # Something like: ":jqROBZMqtWGJE" (varies based on salt used) print $o->secret_mangled; print $o->secret; # "baz" print "ok" if($o->secret_equals('baz'); # "ok"
John C. Siracusa (siracusa@gmail.com)
Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2022-08-26 | perl v5.28.1 |