Lockit utilities
Small utilities module for lockit.
Installation
npm install lockit-utils
var utls = ;
Configuration
// redirect target when requesting restricted pageexportslogin = route: '/login'; // database connection string// CouchDBexportsdb = '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 objectlogin
String - Route that handles the login process - default'/login'
Example
config.js
exportslogin = route: '/login';
app.js
var config = ;app
getDatabase(config)
Get type of database and database adapter name from connection information.
-
config
Object - Configuration objectdb
String, Object - Database connection string / object
Returns
- Object - Object containing database
type
andadapter
Example
config.js (CouchDB)
exportsdb = 'http://127.0.0.1:5984/';
config.js (all other DBs)
exportsdb = url: 'postgres://127.0.0.1:5432/' name: 'users' collection: 'my_user_table'
app.js
var config = ;var db = util;// {// 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: 'mirco.zeiss@gmail.com';var link = util;// 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 - default6
-
time
Number - Time step of counter in seconds - default30
-
Returns
- Boolean -
true
if token is valid
Example
var key = 'abcd1234';var token = '236709';var valid = util;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;
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 = ;var events = ;var utils = ; var {};util; var {};util; var child = ;var mother = ; utils; mother; child;
Test
make test
License
MIT