DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 6.12 Storing User Credentials in a MySQL Database

Problem

You wish to use user and password information in your MySQL database for authenticating users.

Solution

For Apache 1.3, use mod_auth_mysql:

Auth_MySQL_Info db_host.example.com db_user my_password
Auth_MySQL_General_DB auth_database_name

<Directory /www/htdocs/private>
    AuthName "Protected directory"
    AuthType Basic 
    require valid-user
</Directory>

For Apache 2.1 and later, use mod_authn_dbi:

  AuthnDbiDriver Config1 mysql
  AuthnDbiHost Config1 db.example.com
  AuthnDbiUsername Config1 db_username
  AuthnDbiPassword Config1 db_password
  AuthnDbiName Config1 auth_database_name
  AuthnDbiTable Config1 auth_database_table
  AuthnDbiUsernameField Config1 user_field
  AuthnDbiPasswordField Config1 password_field
  AuthnDbiIsActiveField Config1 is_active_field

  AuthnDbiConnMin Config1 3
  AuthnDbiConnSoftMax Config1 12
  AuthnDbiConnHardMax Config1 20
  AuthnDbiConnTTL Config1 600

<Directory  "/www/htdocs/private">
  AuthType Digest
  AuthName  "Protected directory>
  AuthBasicProvider dbi
  AuthnDbiServerConfig Config1
  Require valid-user
</Directory>

Discussion

There are a number of modules called mod_auth_mysql. The module used in the previous example is the mod_auth_mysql from http://www.diegonet.com/support/mod_auth_mysql.shtml. For the full explanation of the database fields that you will need to create, and the additional options that the module affords, you should consult the documentation on the web site.

If you are running Apache 2.1 or later, you will want to take advantage of the new authentication framework, and use the module mod_authn_dbi, available from http://open.cyanworlds.com/mod_authn_dbi/. Due to the new authentication API in Apache 2.1, a number of things are possible that were not possible in earlier versions. For example, a single module, such as mod_authn_dbi, can be used for either Basic or Digest authentication, by simply changing the AuthType directive from Basic to Digest. (AuthBasicProvider would also become AuthDigestProvider in the previous example.)

mod_authn_dbi uses libdbi, which is a generic database access library, allowing you to use your favorite database server to provide authentication services. libdbi drivers are available for most popular database servers. For a more complete description of mod_authn_dbi, you should consult the documentation on the web site.

See Also

    [ Team LiB ] Previous Section Next Section