Skip to content
/ RAuth Public

Make your service of the authentication with RAuth.

License

Notifications You must be signed in to change notification settings

reigncl/RAuth

Repository files navigation

RAuth 🔏 - Reign Authorization and Authentication library

Package Quality

RAuth library provides a simple way for using Authorization and Authentication via JWT encapsulating their main methods. Allows to handle multiple sessions ensuring trust between an application and its users.

How to use

$ npm install rauth

Create a session control

import 'rauth/engines/SQLiteEngine';
import { SessionControl } from 'rauth/session/SessionControl';

const sessionControl = new SessionControl({ engineConnectionStore: 'SQLite' });

Create an authorization handler

This handler allows create the session

// Handler to GET /authorize?grant_type=basic

// Here is your method to validate the credentials

const session = await sessionControl.createSession(username);

res.setHeader('Content-Type', 'application/json');
return res.end(JSON.stringify(session), 'utf8');

Validate token

const token = query.token;
const session = await sessionControl.verify(token);

Create a refresh token handler

This handler allows refresh the session.

// Handler to GET /authorize?grant_type=refresh_token&refresh_token=...
const refreshToken = query.refresh_token;

const session = await sessionControl.refreshSession(refreshToken);

res.setHeader('Content-Type', 'application/json');
return res.end(JSON.stringify(session), 'utf8');

Create a revoke token handler

This handler allows revoke a refresh token.

// Handler to GET /logout
await sessionControl.revokeSession(session);

res.setHeader('Content-Type', 'application/json');
return res.end(JSON.stringify(session), 'utf8');

Revoke all tokens

await sessionControl.revokeAllSessions(session)

List all session

const sessions = await sessionControl.getAllSessions(session);

Events

sessionControl.on('create-session', callback(){});

Events list and arguments:

  • create-session: Event emitted after of created the object o row in your Storage.
    • Args: { register: Register }
      • regiter (Register): Register inserted in your Storage.
  • refresh-session: Event emitted after of to refresh the object o row in your Storage.
    • Args: { register: Register }
      • regiter (Register): Register inserted in your Storage.

Engines

The engines help control the session storage. Currently, RAuth provides following engines:

  • Mongoose rauth/engines/MongooseEngine (Sample)
  • SQLite rauth/engines/SQLiteEngine (Requires sqlite installed)
  • Memory rauth/engines/MemoryEngine
  • TypeORM rauth/engines/TypeormEngine (Sample Requires typeorm installed)

Samples

Mongoose:

import 'rauth/engines/MongooseEngine';
export const sessionControl = new SessionControl({
  connectionStore: new ConnectionStore('Mongoose', { model: SessionModel }),
});

Memory:

import 'rauth/engines/MemoryEngine';
export const sessionControl = new SessionControl({
  engineConnectionStore: 'Memory',
});

SQLite:

import 'rauth/engines/SQLiteEngine';
export const sessionControl = new SessionControl({
  engineConnectionStore: 'SQLite',
});
// Or
import 'rauth/engines/SQLiteEngine';
export const sessionControl = new SessionControl({
  connectionStore: new ConnectionStore('SQLite', {
    filename: `${__dirname}/db.sqlite`,
    table: 'sessions',
  }),
});

Typeorm:

import '../engines/TypeormEngine';
export const sessionControl = new SessionControl({
  connectionStore: new ConnectionStore('Typeorm', { entity: Session }),
});