I have static content outside of Drupal that I need to password protect using the users's existing credentials in Drupal. I found an apache module that lets you use MySQL for Apache authentication:

http://sourceforge.net/projects/modauthmysql/

Unfortunately, this doesn't support sha-2 (sha512) that Drupal 7 uses.

Does anyone know of any alternative ways to do this? I'd like to avoid wrapping all this content in PHP because it's generated (and regularly updated) by a tool, has any number of relative links and folders and such, and the only way I can think of handling that with PHP outside of modifying the files is to use mod_rewrite to route all the requests for html files through some sort of front controller that is a Drupal module (or a bootstrapped Drupal).

share|improve this question

2 Answers

up vote 1 down vote accepted

Even if you manage to find an apache module that has sha-2 implemented, you still need to fetch and integrate the salt. The solution will have to include Drupal somehow, since Drupal is the only one who really knows how to authenticate against its database.

I am not aware of any maintained module that does what you want, but it would be really easy to implement in a module.

Pseudocode that illustrates how simple this is in Drupal:

/**
 * Implements hook_menu().
 */
function mystatic_menu() {
  $items['mystatic'] = array(
    'title' => 'Blogs',
    'page callback' => 'mystatic_serve',
    'access arguments' => array('access content'), # Or check against other permissions.
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function mystatic_serve($filename = '') {
  if (empty($filename)) {
   return _mystatic_directory_index(); #String will be printed as $content inside Drupals page.tpl.php template.
  }
  elseif(_mystatic_is_valid_file($filename)) {
    print _mystatic_serve_file($filename);
    drupal_exit(); #String will be sent to user without going trough Drupals theme layer.
  }
  else {
    drupal_not_found();
  }
  return $html;
}

The function _mystatic_directory_index() would generate a list of files, or the index of the static system. To be served inside Drupals theme when people access /mystatic.

The function _mystatic_is_valid_file() would sanitize the filename (to avoid serving e.g. ../../etc/password. There are many tutorials out there for this. It would obviously also check if the file $filename actually exits. Will be served if people open urls such as /mystatic/example.html. Note that /mystatic/somedir/example.html will not work; Drupal splits on / and passes that as separate arguments.

The function _mystatic_serve_file() would simply read $filename from the disk and stream (print) it to the user.

share|improve this answer
This is the approach I started on yesterday before thinking using Apache authentication might be better. The tricky (maybe not really) part is routing all the links within the static content to the module's menu path. In this case the content is generated from Adobe Robohelp. I'm thinking this might all be easy with mod rewrite but haven't tried it. Another issue is handling relative paths to graphics/css/js/etc. I can put a base href in the pages, but not ideal unless Robohelp can generate the content with it already in place. – czervik Aug 31 '11 at 16:05
1  
Just a note that this turned out to all be easy by using a RewriteRule with an internal (non-canonical) redirect. The url doesn't change so relative paths to graphics/css/etc don't break. I just route all requests for .htm/.html files to the module (using index.php?q=my_path/$1). – czervik Aug 31 '11 at 21:26

I did in the past use the export_user_db module, but it is similarly lacking in D7 support.

I am now using modauthmysql quite successfully with D6; I also have an auxiliary module that I can publish to do single-sign-on between Drupal and Apache-authenticated sections of the website.

You are correct that the lack of a Drupal-7-compatible password hash is a limitation, but I don't think this would be too hard to add. Contact me on d.o if you would like to collaborate.

http://drupal.org/user/438598

If you really only need to serve static content, serving it from Drupal per the other answer is easier and better.

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.