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

lockit-utils

v0.5.3

Published

utilities for lockit

Downloads

22

Readme

Lockit utilities

Build Status NPM version

Small utilities module for lockit.

Installation

npm install lockit-utils

var utls = require('lockit-utils');

Configuration

// redirect target when requesting restricted page
exports.login = {
  route: '/login'
};

// database connection string
// CouchDB
exports.db = 'http://127.0.0.1:5984/';

// MongoDB
// exports.db = {
//   url: 'mongodb://127.0.0.1/',
//   name: 'test',
//   collection: 'users'
// };

// PostgreSQL
// exports.db = {
//   url: 'postgres://127.0.0.1:5432/',
//   name: 'users',
//   collection: 'my_user_table'
// };

// MySQL
// exports.db = {
//   url: 'mysql://127.0.0.1:3306/',
//   name: 'users',
//   collection: 'my_user_table'
// };

// SQLite
// exports.db = {
//   url: 'sqlite://',
//   name: ':memory:',
//   collection: 'my_user_table'
// };

Features

  • protect routes from unauthorized access and redirect
  • get database and lockit adapter from connection string
  • generate link to QR code image for two-factor auth
  • verify provided two-factor token
  • destroy a session (works with cookie sessions and session stores)

Methods

restrict([config])

Prevent users who aren't logged-in from accessing routes. Use login.route for redirection. Function also remembers the requested url and user is redirected after successful login. If rest is enabled you'll get a 401 response.

  • config Object optional - Configuration object

    • login String - Route that handles the login process - default '/login'

Example

config.js

exports.login = {
  route: '/login'
};

app.js

var config = require('./config.js');
app.get('/private', utils.restrict(config), function(req, res) {
  res.send('only a logged in user can see this');
})

getDatabase(config)

Get type of database and database adapter name from connection information.

  • config Object - Configuration object

    • db String, Object - Database connection string / object

Returns

  • Object - Object containing database type and adapter

Example

config.js (CouchDB)

exports.db = 'http://127.0.0.1:5984/';

config.js (all other DBs)

exports.db = {
  url: 'postgres://127.0.0.1:5432/',
  name: 'users',
  collection: 'my_user_table'
}

app.js

var config = require('./config.js');
var db = util.getDatabase(config);
// {
//   type: 'couchdb',
//   adapter: 'lockit-couchdb-adapter'
// }

qr(config)

Generate link to QR code, uses Google Charts.

  • config Object - Configuration object

    • key String - Individual random key for user

    • email String - User email for Google Authenticator app

    • issuer String - Issuer for Google Authenticator - default 'Lockit'

Returns

  • String - URL for QR code

Example

var config = {
  key: 'abcd1234',
  email: '[email protected]'
};
var link = util.qr(config);
// https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=otpauth%3A%2F%2Ftotp%2FLockit%3Amirco.zeiss%40gmail.com%3Fsecret%3DMFRGGZBRGI2DI%3D%3D%3D%26issuer%3DLockit

verify(token, key, [options])

Verify a two-factor authentication token, uses time-based one-time password algorithm (totp). To be used with Google Authenticator.

  • token String - The two-factor token to verify

  • key String - The individual key for the user

  • options Object optional - Options object for notp#totp.verify

    • window String - Allowable margin for counter - default 6

    • time Number - Time step of counter in seconds - default 30

Returns

  • Boolean - true if token is valid

Example

var key = 'abcd1234';
var token = '236709';
var valid = util.verify(token, key);
if (valid) {
  // continue here
}

destroy(req, done)

Destroy the current session. Works with cookie sessions and session stores.

  • req Object - The default Express request object

  • done function - Function executed when session is destroyed

Example

util.destroy(req, function() {
  // user is now logged out
});

pipe(source, target)

Pipe events from source to target. source can be a single event emitter or an Array of event emitters.

  • source Object, Array - Single event emitter or Array of event emitters

  • target Object - Single event emitter

Example

var util = require('util');
var events = require('events');
var utils = require('lockit-utils');

var Child = function() {};
util.inherits(Child, events.EventEmitter);

var Mother = function() {};
util.inherits(Mother, events.EventEmitter);

var child = new Child();
var mother = new Mother();

utils.pipe(child, mother);

mother.on('action', function(action) {
  console.log('look the child is ' + action);
});

child.emit('action', 'smiling');

Test

make test

License

MIT