| 1 | package Dancer::Plugin::Auth::Extensible::Provider::Database; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'Dancer::Plugin::Auth::Extensible::Provider::Base'; | ||
| 7 | use Dancer::Plugin::Database; | ||
| 8 | use Dancer qw(:syntax); | ||
| 9 | |||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Dancer::Plugin::Auth::Extensible::Database - authenticate via a database | ||
| 14 | |||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | This class is an authentication provider designed to authenticate users against | ||
| 19 | a database, using L<Dancer::Plugin::Database> to access a database. | ||
| 20 | |||
| 21 | L<Crypt::SaltedHash> is used to handle hashed passwords securely; you wouldn't | ||
| 22 | want to store plain text passwords now, would you? (If your answer to that is | ||
| 23 | yes, please reconsider; you really don't want to do that, when it's so easy to | ||
| 24 | do things right!) | ||
| 25 | |||
| 26 | See L<Dancer::Plugin::Database> for how to configure a database connection | ||
| 27 | appropriately; see the L</CONFIGURATION> section below for how to configure this | ||
| 28 | authentication provider with database details. | ||
| 29 | |||
| 30 | See L<Dancer::Plugin::Auth::Extensible> for details on how to use the | ||
| 31 | authentication framework, including how to pick a more useful authentication | ||
| 32 | provider. | ||
| 33 | |||
| 34 | |||
| 35 | =head1 CONFIGURATION | ||
| 36 | |||
| 37 | This provider tries to use sensible defaults, so you may not need to provide | ||
| 38 | much configuration if your database tables look similar to those in the | ||
| 39 | L</SUGGESTED SCHEMA> section below. | ||
| 40 | |||
| 41 | The most basic configuration, assuming defaults for all options, and defining a | ||
| 42 | single authentication realm named 'users': | ||
| 43 | |||
| 44 | plugins: | ||
| 45 | Auth::Extensible: | ||
| 46 | realms: | ||
| 47 | users: | ||
| 48 | provider: 'Database' | ||
| 49 | |||
| 50 | You would still need to have provided suitable database connection details to | ||
| 51 | L<Dancer::Plugin::Database>, of course; see the docs for that plugin for full | ||
| 52 | details, but it could be as simple as, e.g.: | ||
| 53 | |||
| 54 | plugins: | ||
| 55 | Auth::Extensible: | ||
| 56 | realms: | ||
| 57 | users: | ||
| 58 | provider: 'Database' | ||
| 59 | Database: | ||
| 60 | driver: 'SQLite' | ||
| 61 | database: 'test.sqlite' | ||
| 62 | |||
| 63 | |||
| 64 | A full example showing all options: | ||
| 65 | |||
| 66 | plugins: | ||
| 67 | Auth::Extensible: | ||
| 68 | realms: | ||
| 69 | users: | ||
| 70 | provider: 'Database' | ||
| 71 | # optionally set DB connection name to use (see named | ||
| 72 | # connections in Dancer::Plugin::Database docs) | ||
| 73 | db_connection_name: 'foo' | ||
| 74 | |||
| 75 | # Optionally disable roles support, if you only want to check | ||
| 76 | # for successful logins but don't need to use role-based access: | ||
| 77 | disable_roles: 1 | ||
| 78 | |||
| 79 | # optionally specify names of tables if they're not the defaults | ||
| 80 | # (defaults are 'users', 'roles' and 'user_roles') | ||
| 81 | users_table: 'users' | ||
| 82 | roles_table: 'roles' | ||
| 83 | user_roles_table: 'user_roles' | ||
| 84 | |||
| 85 | # optionally set the column names (see the SUGGESTED SCHEMA | ||
| 86 | # section below for the default names; if you use them, they'll | ||
| 87 | # Just Work) | ||
| 88 | users_id_column: 'id' | ||
| 89 | users_username_column: 'username' | ||
| 90 | users_password_column: 'password' | ||
| 91 | roles_id_column: 'id' | ||
| 92 | roles_role_column: 'role' | ||
| 93 | user_roles_user_id_column: 'user_id' | ||
| 94 | user_roles_role_id_column: 'roles_id' | ||
| 95 | |||
| 96 | See the main L<Dancer::Plugin::Auth::Extensible> documentation for how to | ||
| 97 | configure multiple authentication realms. | ||
| 98 | |||
| 99 | =head1 SUGGESTED SCHEMA | ||
| 100 | |||
| 101 | If you use a schema similar to the examples provided here, you should need | ||
| 102 | minimal configuration to get this authentication provider to work for you. | ||
| 103 | |||
| 104 | The examples given here should be MySQL-compatible; minimal changes should be | ||
| 105 | required to use them with other database engines. | ||
| 106 | |||
| 107 | =head2 users table | ||
| 108 | |||
| 109 | You'll need a table to store user accounts in, of course. A suggestion is | ||
| 110 | something like: | ||
| 111 | |||
| 112 | CREATE TABLE users ( | ||
| 113 | id INTEGER AUTO_INCREMENT PRIMARY KEY, | ||
| 114 | username VARCHAR(32) NOT NULL UNIQUE KEY, | ||
| 115 | password VARCHAR(40) NOT NULL | ||
| 116 | ); | ||
| 117 | |||
| 118 | You will quite likely want other fields to store e.g. the user's name, email | ||
| 119 | address, etc; all columns from the users table will be returned by the | ||
| 120 | C<logged_in_user> keyword for your convenience. | ||
| 121 | |||
| 122 | =head2 roles table | ||
| 123 | |||
| 124 | You'll need a table to store a list of available roles in (unless you're not | ||
| 125 | using roles - in which case, disable role support (see the L</CONFIGURATION> | ||
| 126 | section). | ||
| 127 | |||
| 128 | CREATE TABLE roles ( | ||
| 129 | id INTEGER AUTO_INCREMENT PRIMARY KEY, | ||
| 130 | role VARCHAR(32) NOT NULL | ||
| 131 | ); | ||
| 132 | |||
| 133 | =head2 user_roles table | ||
| 134 | |||
| 135 | Finally, (unless you've disabled role support) you'll need a table to store | ||
| 136 | user <-> role mappings (i.e. one row for every role a user has; so adding | ||
| 137 | extra roles to a user consists of adding a new role to this table). It's | ||
| 138 | entirely up to you whether you use an "id" column in this table; you probably | ||
| 139 | shouldn't need it. | ||
| 140 | |||
| 141 | CREATE TABLE user_roles ( | ||
| 142 | user_id INTEGER NOT NULL, | ||
| 143 | role_id INTEGER NOT NULL, | ||
| 144 | UNIQUE KEY user_role (user_id, role_id) | ||
| 145 | ); | ||
| 146 | |||
| 147 | If you're using InnoDB tables rather than the default MyISAM, you could add a | ||
| 148 | foreign key constraint for better data integrity; see the MySQL documentation | ||
| 149 | for details, but a table definition using foreign keys could look like: | ||
| 150 | |||
| 151 | CREATE TABLE user_roles ( | ||
| 152 | user_id INTEGER, FOREIGN KEY (user_id) REFERENCES users (id), | ||
| 153 | role_id INTEGER, FOREIGN_KEY (role_id) REFERENCES roles (id), | ||
| 154 | UNIQUE KEY user_role (user_id, role_id) | ||
| 155 | ) ENGINE=InnoDB; | ||
| 156 | |||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | |||
| 161 | sub authenticate_user { | ||
| 162 | my ($self, $username, $password) = @_; | ||
| 163 | |||
| 164 | # Look up the user: | ||
| 165 | my $user = $self->get_user_details($username); | ||
| 166 | return unless $user; | ||
| 167 | |||
| 168 | # OK, we found a user, let match_password (from our base class) take care of | ||
| 169 | # working out if the password is correct | ||
| 170 | |||
| 171 | my $settings = $self->realm_settings; | ||
| 172 | my $password_column = $settings->{users_password_column} || 'password'; | ||
| 173 | return $self->match_password($password, $user->{$password_column}); | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | # Return details about the user. The user's row in the users table will be | ||
| 178 | # fetched and all columns returned as a hashref. | ||
| 179 | sub get_user_details { | ||
| 180 | my ($self, $username) = @_; | ||
| 181 | return unless defined $username; | ||
| 182 | |||
| 183 | my $settings = $self->realm_settings; | ||
| 184 | |||
| 185 | # Get our database handle and find out the table and column names: | ||
| 186 | my $database = database($settings->{db_connection_name}) | ||
| 187 | or die "No database connection"; | ||
| 188 | |||
| 189 | my $users_table = $settings->{users_table} || 'users'; | ||
| 190 | my $username_column = $settings->{users_username_column} || 'username'; | ||
| 191 | my $password_column = $settings->{users_password_column} || 'password'; | ||
| 192 | |||
| 193 | # Look up the user, | ||
| 194 | my $user = $database->quick_select( | ||
| 195 | $users_table, { $username_column => $username } | ||
| 196 | ); | ||
| 197 | if (!$user) { | ||
| 198 | debug("No such user $username"); | ||
| 199 | return; | ||
| 200 | } else { | ||
| 201 | return $user; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | sub get_user_roles { | ||
| 206 | my ($self, $username) = @_; | ||
| 207 | |||
| 208 | my $settings = $self->realm_settings; | ||
| 209 | # Get our database handle and find out the table and column names: | ||
| 210 | my $database = database($settings->{db_connection_name}); | ||
| 211 | |||
| 212 | # Get details of the user first; both to check they exist, and so we have | ||
| 213 | # their ID to use. | ||
| 214 | my $user = $self->get_user_details($username) | ||
| 215 | or return; | ||
| 216 | |||
| 217 | # Right, fetch the roles they have. There's currently no support for | ||
| 218 | # JOINs in Dancer::Plugin::Database, so we'll need to do this query | ||
| 219 | # ourselves - so we'd better take care to quote the table & column names, as | ||
| 220 | # we're going to have to interpolate them. (They're coming from our config, | ||
| 221 | # so should be pretty trustable, but they might conflict with reserved | ||
| 222 | # identifiers or have unacceptable characters to not be quoted.) | ||
| 223 | # Because I've tried to be so flexible in allowing the user to configure | ||
| 224 | # table names, column names, etc, this is going to be fucking ugly. | ||
| 225 | # Seriously ugly. Clear bag of smashed arseholes territory. | ||
| 226 | |||
| 227 | |||
| 228 | my $roles_table = $database->quote_identifier( | ||
| 229 | $settings->{roles_table} || 'roles' | ||
| 230 | ); | ||
| 231 | my $roles_role_id_column = $database->quote_identifier( | ||
| 232 | $settings->{roles_id_column} || 'id' | ||
| 233 | ); | ||
| 234 | my $roles_role_column = $database->quote_identifier( | ||
| 235 | $settings->{roles_role_column} || 'role' | ||
| 236 | ); | ||
| 237 | |||
| 238 | |||
| 239 | my $user_roles_table = $database->quote_identifier( | ||
| 240 | $settings->{user_roles_table} || 'user_roles' | ||
| 241 | ); | ||
| 242 | my $user_roles_user_id_column = $database->quote_identifier( | ||
| 243 | $settings->{user_roles_user_id_column} || 'user_id' | ||
| 244 | ); | ||
| 245 | my $user_roles_role_id_column = $database->quote_identifier( | ||
| 246 | $settings->{user_roles_role_id_column} || 'role_id' | ||
| 247 | ); | ||
| 248 | |||
| 249 | # Yes, there's SQL interpolation here; yes, it makes me throw up a little. | ||
| 250 | # However, all the variables used have been quoted appropriately above, so | ||
| 251 | # although it might look like a camel's arsehole, at least it's safe. | ||
| 252 | my $sql = <<QUERY; | ||
| 253 | SELECT $roles_table.$roles_role_column | ||
| 254 | FROM $user_roles_table | ||
| 255 | JOIN $roles_table | ||
| 256 | ON $roles_table.$roles_role_id_column | ||
| 257 | = $user_roles_table.$user_roles_role_id_column | ||
| 258 | WHERE $user_roles_table.$user_roles_user_id_column = ? | ||
| 259 | QUERY | ||
| 260 | |||
| 261 | my $sth = $database->prepare($sql) | ||
| 262 | or die "Failed to prepare query - error: " . $database->err_str; | ||
| 263 | |||
| 264 | $sth->execute($user->{$settings->{users_id_column} || 'id'}); | ||
| 265 | |||
| 266 | my @roles; | ||
| 267 | while (my($role) = $sth->fetchrow_array) { | ||
| 268 | push @roles, $role; | ||
| 269 | } | ||
| 270 | |||
| 271 | return \@roles; | ||
| 272 | |||
| 273 | # If you read through this, I'm truly, truly sorry. This mess was the price | ||
| 274 | # of making things so configurable. Send me your address, and I'll send you | ||
| 275 | # a complementary fork to remove your eyeballs with as way of apology. | ||
| 276 | # If I can bear to look at this code again, I think I might seriously | ||
| 277 | # refactor it and use Template::Tiny or something on it. Or Acme::Bleach. | ||
| 278 | } | ||
| 279 | |||
| 280 | |||
| 281 | |||
| 282 | 1; |