I’ve got a simple Drupal website set up and is nearing completion.
This website needs to be entirely locked down except for an pop up box, allowing you to login, and this is fine, but the login shouldn’t go through Drupal’s standard Authentication, instead the username and password need to be passed to a Web Service which returns true or false, for login success or failure.
I can build a pop up, but how do I route the authentication through the web service and also, open a session to stop direct linking without logging in. All I need to check is that the person is registered with the parent site (who provided the Web Service).
Step 1 is to create a new module.
I found this reference useful: http://yetanotherprogrammingblog.com/node/14
This solution is based on drupal-6. There are differences in 7.
In the module you need to override hook_login_validate. e.g.
In the above note that
I was connecting to a grails (spring/java) application to call the service. This mainly determines the call to make and variables. I use the built-in grails authentication service to make the authentication and override that as well to make a custom authentication. The call just against a rest service should be similar. You need a http request and parse the response.
A snippet from my PHP class doing _mymodule_validate_password is:
At the end of the day my request gives me a JSON {“success”: true, “username”:”888″}. The username is superfluous.
Grails does a redirect if authenticated to the service providing data. You could just check that you get ‘200’ back from your web service and be done, or alternatively just check that your JSON data has the equivalent of ‘success’ in it. In the case above, we check the authentication, record the redirect and then call the redirect to get authentication data.
You can return basic drupal profile information from the service and set the details. Override mymodule_user to get access to post update call-backs. I update the details on every login, as the back-end is the primary source of data. The reference at the top of this article uses ‘update’.
}
Depending on what you are trying to do the fun may not end here. In my case I had to update custom profile information and save a client cookie to record login details for subsequent web service calls. Some argue that this is not ‘restful’ as there is a drupal session and a session in the web services provider.