Node.js and Zend Auth with Sessions stored in the database.

Recently on a project I had to make changes to a underlying portion of the sites architecture to move sessions in Zend Framework from file storage to database storage. However this affected a piece of the architecture. Node.js, which manages all our real time interaction, looked at sessions at the file level. This was quite a easy transition for the function as it was abstracted away in a function call so the theory was to just replace the function “guts” with a new component. In regards to setting up Node.js authentication with Zend Framework please check out this (http://anthonyw.net/?p=269) article.

The original function:

function authorizePHPSession(phpSessionId, cb) {
    try {
        fs.readFile('/path/to/sessions/sess_'+phpSessionId, 'utf8', function(err, data) {
            if(err) {
                throw err;
            }

            if(data.search(phpSessionId.toString()) > -1) {
                cb(null, true);
            } else {
                cb(null, false);
            }
        });
    } catch(e) {
        cb(null, false);
    }
}

Here is the new code:

function authorizePHPSession(data, cb) {
    var phpSessionId = getPHPSessionId(data.headers.cookie);
    dbClient.connect();
    dbClient.query(
        "SELECT * FROM `Sessions` WHERE data LIKE '%Zend_Auth|a:1:{s:7:\"storage\";s:%:\"%\";}%' AND `id` = ?",
        [phpSessionId],
        function(error, rows, fields) {
            if(error) {
                console.log('Error: ' +error);
                cb(null, false);
            } else {
                if(rows.length == 1) {
                    data.phpSessionId = phpSessionId.toString();
                    cb(null, true);
                } else {
                    cb(null, false);
                }
            }
        });
    dbClient.end();
}

This authorization check now access the Sessions table set up via Zend Framework, for more information in regards to moving session from the file level to the database level please check out this article: http://dionysus.uraganov.net/frameworks/zend-framework-storing-session-in-database/. The check makes two assumptions. One there is an “id” field in your table schema that matches the string in your headers/cookies. Second is the string it matches againnst the database entries. The web application uses the default Zend_Auth namespace so if there are customizations on this namespace the string will vary based on that customization.

How I stumbled upon a Dodge Charger with the Blacktop package.

So to give a little history on this subject. I once owned a Dodge. Best car I ever had, fun, easy to customize, just a PITA when things went wrong. Modified it so much that it was not street legal and or driveable except on a very long stretch of road far off in Germany… Enter the Acura RSX I currently own. Hell of a reliable and powerful car. I am grateful that it gets me to the super market reliable and fast when I get on the gas, but I have had enough. I want something bigger, able to fit my friends in it and it has to above else look mean as hell.

One day while driving along a high way in Jersey City I passed the Hudson Dodge dealership. Something out of the ordinary caught my eye. Silver Charger with really big black rims… I pulled over into the visitor parking area and instantly fell in love. This car was immaculate. I mean it has everything found in the higher end V8 models and then some. A ridiculously loud sound system, huge wheels, the new 3.6L V6, and the new ZF 8-speed transmission. This new transmissions allows this car to have more bark than its bite. I spoke with the salesman briefly and asked about the sticker price, how much I can expect for a trade in with my Acura, and when I can test drive it.

I still have yet to test drive it due to time constraints but I did get a chance to sit in it and play with the interior. It is a step above anything I own and I look forward to getting one by the end of the year. Here is a quick picture that I snapped as I left the dealership.

This car is beautiful, I am in love again with a Dodge.