npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

passport-authtkt

v0.0.2

Published

mod_auth_tkt based authentication.

Downloads

140

Readme

Passport-AuthTkt

Passport strategy for authenticating with a mod_auth_tkt ticket cookie.

Install

$ npm install passport-authtkt

Usage

Configure Strategy

The AuthTkt authentication strategy authenticates requests based on the presence and validity of an auth_tkt cookie. To use it, you should configure the cookieParser middleware as well as Passport:

app.configure(function() {
    ...
    app.use(express.cookieParser());
    app.use(passport.initialize());
    ...
});

To use the strategy:

authtkt = require('passport-authtkt');

...

passport.use(new authtkt.Strategy('mysecret', {
    timeout: 60*60, // 1 hour timeout; omit to not have a timeout
    encodeUserData: true,
    jsonUserData: true
}));

The first argument is the authentication secret that is used to sign cookies. This should be a secret string, and should ideally be different from any session secret passed to the Express session middleware.

Valid options include:

  • key - name of the cookie.
  • `encodeUserData - encode and decode the userData string using base64. Defaults to true.
  • jsonUserData - encode and decode the userData string as JSON. Defaults to false.
  • ip - use the given IP address (a dotted quad string) to create/validate tickets.
  • timeout - time, in seconds, for ticket validation.

Authenticate Requests

Use passport.authenticate(), specifying the 'authtkt' strategy, to authenticate requests.

For example, as route middleware in an Express application:

app.post('/foo', 
    passport.authenticate('authtkt', { failureRedirect: '/login' }),
    function(req, res) {
        ...
    }
);

Note that the authenticator does not need to store anything in the session. If you do not configure any session middleware, you should pass session: false in the options to the authentication hook:

app.post('/foo', 
    passport.authenticate('authtkt', { session: false, failureRedirect: '/login' }),
    function(req, res) {
        ...
    }
);

When the authenticator is used, req.authInfo will be the parsed ticket, assuming authentication was successful. This is an object with the keys:

  • userid, the user id encoded in the authentication cookie
  • userData, the user data encoded in the cookie. If the strategy is set up with jsonUserData, this may be an object containing user information; otherwise it will be a string.
  • tokens, a list of authentication tokens, if set in the authentication cookie
  • timestamp, the timestamp (as seconds from the epoch) of the cookie

req.user will be the same as req.authInfo.userData.

The AuthTkt instance configured with the secret and options is available as strategy.authtkt. This can be used e.g. to call createTicket() during login - see below.

When req.authInfo is set on requests where the authenticator is used, the authentication cookie will be set if either there is a timeout configured, or the user id, user data or tokens for the ticket in req.authInfo has changed. This also applies if authentication was unsuccessful, i.e. if req.authInfo is set later by other middleware or routes.

Saving the ticket cookie

To create a cookie, use the helper functions createTicket() and encodeCookieValue() on an AuthTkt instance. For example, a login route may do the following:

var strategy = req._passport.instance._strategy('authtkt');
var ticket = strategy.authtkt.createTicket(user.id, {userData: user});
return res.cookie(strategy.key, strategy.authtkt.base64Encode(ticket));

In this example, user is an object or string representing a user that has been validated (e.g. looked up in a database and authenticated). If user an object, jsonUserData: true should be set in the strategy options (see above). user.id is the user's id.

The example may be shortned further using the getCookieValue() helper:

var strategy = req._passport.instance._strategy('authtkt');
res.cookie(strategy.key, strategy.authtkt.getCookieValue(user.id, {userData: user}));

To log the user out, simply clear the cookie:

res.clearCookie(strategy.key);

See authtkt.js for more details about the methods available on the authtkt object, which is an instance of the AuthTkt prototype found in that file. This is in fact a generic utiltiy for creating and parsing auth_tkt tickets, and so may be useful in other contexts.

Parsing cookies client or server side

Sometimes, it may be useful to parse an auth_tkt cookie on the client. The cookie format allows user id, tokens and the user data string to be extracted without knowing the authentication secret (which should be known to the server only).

To aid this, passport-authtkt ships with a module called authtktutils.js that defines an object with functions splitTicket(), base64Encode() and base64Decode(). This may be loaded as either a Node module, a RequireJS module, or a JavaScript source file (which will define a global authtktUtils with the above functions).

A typical usage pattern may be:

var cookieValue = $.cookie('auth_tkt'); // Using the jQuery.cookie plugin
var ticket = authtktUtils.base64Decode(cookieValue);
var ticketData = authtktUtils.splitCookie(cookieValue, {jsonUserData: true});
var userData = ticketData.userData;

See authtktutils.js for more details.

Tests

$ npm install --dev
$ make test

Build Status

Credits

  • Martin Aspeli
  • Based heavily on passport-local by Jared Hanson
  • Laurence Rowe provided the Python implementation of the mod_auth_tkt algorithm used as the basis for the JavaScript port in authtkt.js.

License

The MIT License

Copyright (c) 2013 Martin Aspeli