3.0.1 • Published 3 years ago

bdt105connexion v3.0.1

Weekly downloads
180
License
-
Repository
github
Last release
3 years ago

Connexion

This package gives functions to authentification capabilities based on JWT and MySql

Dependencies

  • bcryptjs
  • bdt105toolbox
  • jsonwebtoken
  • mysql

How does it work?

The principle is to check once against a database table with login and password. If success then a JWT is created and can be check any time without having to request the database anymore.

How to install?

npm install --save bdt105connexion

Create a table containing at least two fields: login and password (the name of the fields can be customized)

Example

CREATE TABLE `database`.`user` (
  `iduser` INT NOT NULL,
  `login` VARCHAR(200) NOT NULL,
  `password` VARCHAR(200) NOT NULL,
  PRIMARY KEY (`iduser`));

How to use?

Create an object Connexion

import { Connexion, MySqlConfiguration, JwtConfiguration } from './connexion';

let mySqlConfiguration = new MySqlConfiguration(
    "localhost", 3306, 
    "root", "root", "connexion", 
    "user", "login", "password"); // Optional - defines host, port, user, password and database to connect to MySql
let jwtConfiguration = new JwtConfiguration("secretbdt105", "$2a$10$abcdefabcdefabcdefabcd", 
    "userRequestEmail@mail.com", "adminToken"); // Optional - defines JWT parameters

var c = new Connexion(mySqlConfiguration, jwtConfiguration);

// Add a new user
var callbackCreateUser = (err: any, data: any) =>{
    console.log("User creation result", data);
    console.log("User creation error", err);
}

var encrypt = c.encrypt("adm");

c.querySql((err: any, data: any) => callbackCreateUser(err, data), "insert into user (login, password, application) values ('admin', '" + encrypt +"', 'test')");

// get token from a user
var callbackGetUserToken = (err: any, data: any) =>{
    console.log("User check result", data);
    console.log("User check error", err);
    var newUserId = data.insertId;

    // Checks if user is valid
    var token = data;
    var decoded = c.checkJwt(token);
    console.log(decoded);
}

c.getJwt((err: any, data: any) => callbackGetUserToken(err, data), "admin", "adm");

MySqlConfiguration set all parameters to access the database and perfom the login/password verification.

class MySqlConfiguration {
    public host: string;
    public user: string;
    public password: string;
    public port: number;
    public database: string;

    public userTableName: string; // Table where users are stored
    public loginFieldName: string; // Name of the login field
    public passwordFieldName: string; // Name of the password field
}

JwtConfiguration set all parameters for JWT manipulations.

class JwtConfiguration {
    public secret: string;
    public salt: string; // Your hash "salt" parameter is used to encrypt passwords, it must start with '$2a$10$' (salt version compatible), total salt length must be >= 29.
    public userRequestEmail: string;
    public adminToken: string; // Define "adminToken" to have a default token for singup actions (adding a user without authentification).
}

If "jwtConfiguration" parameter is not set then no JWT functions are usable. If "mySqlConfiguration" parameter is not set then no MySql functions are usable. If user table name, login field name or password field name are ommited, then no authentification is possible (MySql queries remain possible). To generate an encrypted password you may use function encrypt().

Functions

querySql(callback: Function, sql: string)

Executes a sql query and calls callback function. Callback function will be like callback(err: any, rows: any).

import { Connexion } from "bdt105connexion/dist";

let configuration = new Configuration("user", "login", "password"); // defines table name, login field name, password field name
let mySqlConfiguration = new MySqlConfiguration("localhost", 3306, "admin", "admin_password", "password"); // defines host, port, user, password and database to connect to MySql
let jwtConfiguration = new JwtConfiguration("secret", "salt", "userRequestEmail@mail.com", "adminToken"); // defines JWT parameters

let conn = new Connexion(mySqlConfiguration, jwtConfiguration, configuration);

let callback = function (err: any, rows: any){
    if (err){
        console.log("Oups error");
    }else{
        console.log("Everything is fine");
    }
}

conn.querySql(callback, "select * from order, customers where order.customerId = customer.customerId");

getJwt(callback: Function, login: string, plainPassword: string, where: string = null)

Gets Json Web Token according to login and password. Add a specific where to the query if needed. Callback function will be like callback(err: any, jwt: any).

checkJwt(token: string): Token

Returns a Token object {"token":token, "status": "OK|ERR", "decoded": decoded}. The token is valid if status="OK" and/or decode is not null.

isTokenValid(token: string) : boolean

Is the token valid?

encrypt(plain: string)

Encrypts a string according to "salt" parameter.

compareEncrypt(encrypted: string, plain: string): boolean

Compares two encrypted strings. If true then strings are equal

3.0.1

3 years ago

3.0.0

3 years ago

2.0.35

3 years ago

2.0.33

3 years ago

2.0.34

3 years ago

2.0.31

4 years ago

2.0.32

4 years ago

2.0.30

4 years ago

2.0.29

4 years ago

2.0.28

4 years ago

2.0.26

4 years ago

2.0.27

4 years ago

2.0.25

4 years ago

2.0.24

4 years ago

2.0.22

4 years ago

2.0.23

4 years ago

2.0.21

4 years ago

2.0.20

4 years ago

2.0.19

4 years ago

2.0.17

4 years ago

2.0.18

4 years ago

2.0.15

4 years ago

2.0.16

4 years ago

2.0.13

4 years ago

2.0.14

4 years ago

2.0.12

4 years ago

2.0.11

4 years ago

2.0.10

4 years ago

2.0.9

4 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

6 years ago

1.0.26

6 years ago

1.0.25

6 years ago

1.0.24

6 years ago

1.0.23

6 years ago

1.0.22

6 years ago

1.0.21

6 years ago

1.0.20

6 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.7

6 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago