serverpreconfigured 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,5 +2,6 @@ import { Model } from "sequelize";
2
2
  export declare class WebSocketAuth extends Model {
3
3
  token: string;
4
4
  expiration: Date;
5
+ auth_connection_token: string;
5
6
  static init(sequelize: any): void;
6
7
  }
@@ -8,6 +8,8 @@ class WebSocketAuth extends sequelize_1.Model {
8
8
  token: sequelize_2.DataTypes.STRING,
9
9
  expiration: sequelize_2.DataTypes.DATE,
10
10
  user_id: sequelize_2.DataTypes.INTEGER,
11
+ is_active: sequelize_2.DataTypes.BOOLEAN,
12
+ auth_connection_token: sequelize_2.DataTypes.STRING,
11
13
  }, {
12
14
  sequelize: sequelize,
13
15
  tableName: 'spc_wsauth'
package/dist/server.d.ts CHANGED
@@ -5,6 +5,7 @@ export { setSessionValue, deleteSessionValue } from "./sessions/secureset";
5
5
  export { getSessionValue } from "./sessions/secureget";
6
6
  export { checkUserPassword } from "./users/users";
7
7
  export { WebSocketAuth } from "./database/models/WSAuth";
8
- export { checkWSAuthToken } from "./wsauth/wsauth";
8
+ export { checkWSAuthToken, authenticateWS, checkConnectionAuth } from "./wsauth/wsauth";
9
+ export { randomString } from "./utils/string/random";
9
10
  import ExpressServer from "./expressServer";
10
11
  export default ExpressServer;
package/dist/server.js CHANGED
@@ -21,6 +21,11 @@ var WSAuth_1 = require("./database/models/WSAuth");
21
21
  exports.WebSocketAuth = WSAuth_1.WebSocketAuth;
22
22
  var wsauth_1 = require("./wsauth/wsauth");
23
23
  exports.checkWSAuthToken = wsauth_1.checkWSAuthToken;
24
+ exports.authenticateWS = wsauth_1.authenticateWS;
25
+ exports.checkConnectionAuth = wsauth_1.checkConnectionAuth;
26
+ var random_1 = require("./utils/string/random");
27
+ exports.randomString = random_1.randomString;
24
28
  const expressServer_1 = __importDefault(require("./expressServer"));
29
+ ;
25
30
  exports.default = expressServer_1.default;
26
31
  const e = new expressServer_1.default();
@@ -1,2 +1,3 @@
1
+ import { PasswordVerifyResult } from './types';
1
2
  export declare function createArgon2Hash(password_string: string): Promise<string | false>;
2
- export declare function checkArgon2Password(password_hash: string, password_string: string): Promise<"Match" | "Dont Match">;
3
+ export declare function checkArgon2Password(password_hash: string, password_string: string): Promise<PasswordVerifyResult>;
@@ -31,7 +31,7 @@ function getUserIdByUserEmail(email) {
31
31
  return __awaiter(this, void 0, void 0, function* () {
32
32
  try {
33
33
  let u = yield getUserByEmail(email);
34
- if (u == null || u == undefined)
34
+ if (!Boolean(u))
35
35
  return NaN;
36
36
  return u.dataValues.id;
37
37
  }
@@ -0,0 +1,4 @@
1
+ export declare enum AuthenticateWSResult {
2
+ OK = 1,
3
+ InvalidToken = 2
4
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var AuthenticateWSResult;
4
+ (function (AuthenticateWSResult) {
5
+ AuthenticateWSResult[AuthenticateWSResult["OK"] = 1] = "OK";
6
+ AuthenticateWSResult[AuthenticateWSResult["InvalidToken"] = 2] = "InvalidToken";
7
+ })(AuthenticateWSResult = exports.AuthenticateWSResult || (exports.AuthenticateWSResult = {}));
@@ -1,3 +1,6 @@
1
+ import { AuthenticateWSResult } from "./types";
1
2
  export declare function getWSAuthDataByUserId(userId: number): Promise<any>;
2
3
  export declare function setWSAuthDataNewToken(userId: number, expiration_hours?: number): Promise<any>;
3
4
  export declare function checkWSAuthToken(userId: number, token: string): Promise<boolean>;
5
+ export declare function authenticateWS(userId: number, token: string, connection_token: string): Promise<AuthenticateWSResult>;
6
+ export declare function checkConnectionAuth(userId: number, connection_token: string): Promise<boolean>;
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const server_1 = require("../server");
13
13
  const server_2 = require("../server");
14
14
  const random_1 = require("./../utils/string/random");
15
+ const types_1 = require("./types");
15
16
  server_1.WebSocketAuth.init(server_2.dataBase);
16
17
  function getWSAuthDataByUserId(userId) {
17
18
  return __awaiter(this, void 0, void 0, function* () {
@@ -32,15 +33,19 @@ function setWSAuthDataNewToken(userId, expiration_hours = 72) {
32
33
  let token = random_1.randomString(50);
33
34
  let expiration = new Date();
34
35
  expiration.setTime(expiration.getTime() + expiration_hours * 60 * 60 * 1000);
35
- if (ws == null || ws == undefined) {
36
+ if (!Boolean(ws)) {
36
37
  return yield server_1.WebSocketAuth.create({ user_id: userId.toString(),
37
38
  token: token,
38
39
  expiration: expiration,
40
+ is_active: true,
41
+ auth_connection_token: "",
39
42
  });
40
43
  }
41
44
  else {
42
45
  ws.token = token;
43
46
  ws.expiration = expiration;
47
+ ws.is_active = true;
48
+ ws.auth_connection_token = "";
44
49
  return yield ws.save();
45
50
  }
46
51
  }
@@ -56,6 +61,8 @@ function checkWSAuthToken(userId, token) {
56
61
  let ws = yield getWSAuthDataByUserId(userId);
57
62
  if (!Boolean(ws))
58
63
  return false;
64
+ if (!ws.dataValues.is_active)
65
+ return false;
59
66
  if (ws.dataValues.token != token)
60
67
  return false;
61
68
  if (Date.now() > ws.dataValues.expiration.getTime())
@@ -63,8 +70,44 @@ function checkWSAuthToken(userId, token) {
63
70
  return true;
64
71
  }
65
72
  catch (e) {
66
- return false;
73
+ throw e;
67
74
  }
68
75
  });
69
76
  }
70
77
  exports.checkWSAuthToken = checkWSAuthToken;
78
+ function authenticateWS(userId, token, connection_token) {
79
+ return __awaiter(this, void 0, void 0, function* () {
80
+ try {
81
+ if (!(yield checkWSAuthToken(userId, token)))
82
+ return types_1.AuthenticateWSResult.InvalidToken;
83
+ let ws = yield getWSAuthDataByUserId(userId);
84
+ ws.auth_connection_token = connection_token;
85
+ yield ws.save();
86
+ return types_1.AuthenticateWSResult.OK;
87
+ }
88
+ catch (e) {
89
+ throw e;
90
+ }
91
+ });
92
+ }
93
+ exports.authenticateWS = authenticateWS;
94
+ function checkConnectionAuth(userId, connection_token) {
95
+ return __awaiter(this, void 0, void 0, function* () {
96
+ try {
97
+ let ws = yield getWSAuthDataByUserId(userId);
98
+ if (!Boolean(ws))
99
+ return false;
100
+ if (!ws.dataValues.is_active)
101
+ return false;
102
+ if (Date.now() > ws.dataValues.expiration.getTime())
103
+ return false;
104
+ if (ws.dataValues.auth_connection_token !== connection_token)
105
+ return false;
106
+ return true;
107
+ }
108
+ catch (e) {
109
+ throw e;
110
+ }
111
+ });
112
+ }
113
+ exports.checkConnectionAuth = checkConnectionAuth;
@@ -21,6 +21,16 @@ module.exports = {
21
21
  type:Sequelize.DATE,
22
22
  allowNull:false,
23
23
  },
24
+ is_active:{
25
+ type:Sequelize.BOOLEAN,
26
+ allowNull:false,
27
+ defaultValue:true,
28
+ },
29
+ auth_connection_token:{
30
+ type:Sequelize.STRING,
31
+ allowNull:true,
32
+ defaultValue:"",
33
+ },
24
34
  created_at:{
25
35
  type:Sequelize.DATE,
26
36
  allowNull:false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serverpreconfigured",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "\"Pre-configured server with authentication system and database integration\"",
5
5
  "main": "dist/server.js",
6
6
  "keywords": ["server","pre configured","database","authentication"],
@@ -3,11 +3,14 @@ import { DataTypes } from "sequelize";
3
3
  export class WebSocketAuth extends Model{
4
4
  declare token:string;
5
5
  declare expiration:Date;
6
+ declare auth_connection_token:string;
6
7
  static init(sequelize:any){
7
8
  super.init({
8
9
  token:DataTypes.STRING,
9
10
  expiration:DataTypes.DATE,
10
11
  user_id:DataTypes.INTEGER,
12
+ is_active:DataTypes.BOOLEAN,
13
+ auth_connection_token:DataTypes.STRING,
11
14
  },{
12
15
  sequelize:sequelize,
13
16
  tableName:'spc_wsauth'
package/src/server.ts CHANGED
@@ -5,7 +5,9 @@ export { setSessionValue,deleteSessionValue } from "./sessions/secureset";
5
5
  export { getSessionValue } from "./sessions/secureget";
6
6
  export { checkUserPassword } from "./users/users";
7
7
  export { WebSocketAuth } from "./database/models/WSAuth";
8
- export { checkWSAuthToken } from "./wsauth/wsauth";
8
+ export { checkWSAuthToken ,authenticateWS,checkConnectionAuth} from "./wsauth/wsauth";
9
+ export { randomString } from "./utils/string/random"
9
10
  import ExpressServer from "./expressServer";
11
+ ;
10
12
  export default ExpressServer;
11
13
  const e=new ExpressServer();
@@ -9,7 +9,7 @@ export async function createArgon2Hash(password_string:string){
9
9
  }
10
10
  }
11
11
 
12
- export async function checkArgon2Password(password_hash:string,password_string:string){
12
+ export async function checkArgon2Password(password_hash:string,password_string:string):Promise<PasswordVerifyResult>{
13
13
  try{
14
14
  if(await argon2.verify(password_hash,password_string))
15
15
  return "Match";
@@ -15,7 +15,7 @@ export async function getUserByEmail(email:string){
15
15
  export async function getUserIdByUserEmail(email:string):Promise<number>{
16
16
  try{
17
17
  let u=await getUserByEmail(email);
18
- if(u==null || u==undefined)
18
+ if(!Boolean(u))
19
19
  return NaN;
20
20
  return u.dataValues.id;
21
21
  }catch(e){
@@ -0,0 +1,4 @@
1
+ export enum AuthenticateWSResult{
2
+ OK=1,
3
+ InvalidToken,
4
+ }
@@ -1,8 +1,9 @@
1
1
  import { WebSocketAuth } from "../server";
2
2
  import { dataBase } from "../server";
3
3
  import {randomString} from "./../utils/string/random";
4
+ import {AuthenticateWSResult} from "./types";
4
5
  WebSocketAuth.init(dataBase);
5
- export async function getWSAuthDataByUserId(userId:number){
6
+ export async function getWSAuthDataByUserId(userId:number):Promise<any>{
6
7
  try{
7
8
  let u=await WebSocketAuth.findOne({where:{user_id:userId.toString()}});
8
9
  return u;
@@ -17,14 +18,18 @@ export async function setWSAuthDataNewToken(userId:number,expiration_hours:numbe
17
18
  let token=randomString(50);
18
19
  let expiration=new Date();
19
20
  expiration.setTime(expiration.getTime()+expiration_hours*60*60*1000);
20
- if(ws==null || ws==undefined){
21
+ if(!Boolean(ws)){
21
22
  return await WebSocketAuth.create({user_id:userId.toString(),
22
23
  token:token,
23
24
  expiration:expiration,
25
+ is_active:true,
26
+ auth_connection_token:"",
24
27
  });
25
28
  }else{
26
29
  ws.token=token;
27
30
  ws.expiration=expiration;
31
+ ws.is_active=true;
32
+ ws.auth_connection_token="";
28
33
  return await ws.save();
29
34
  }
30
35
  }catch(e){
@@ -37,12 +42,44 @@ export async function checkWSAuthToken(userId:number,token:string):Promise<boole
37
42
  let ws=await getWSAuthDataByUserId(userId);
38
43
  if(!Boolean(ws))
39
44
  return false;
45
+ if(!ws.dataValues.is_active)
46
+ return false;
40
47
  if(ws.dataValues.token!=token)
41
48
  return false;
42
49
  if(Date.now()>ws.dataValues.expiration.getTime())
43
50
  return false;
44
51
  return true;
45
52
  }catch(e){
46
- return false;
53
+ throw e;
47
54
  }
55
+ }
56
+
57
+ export async function authenticateWS(userId:number,token:string,connection_token:string):Promise<AuthenticateWSResult>{
58
+ try{
59
+ if(!(await checkWSAuthToken(userId,token)))
60
+ return AuthenticateWSResult.InvalidToken;
61
+ let ws=await getWSAuthDataByUserId(userId);
62
+ ws.auth_connection_token=connection_token;
63
+ await ws.save();
64
+ return AuthenticateWSResult.OK;
65
+ }catch(e){
66
+ throw e;
67
+ }
68
+ }
69
+
70
+ export async function checkConnectionAuth(userId:number,connection_token:string):Promise<boolean>{
71
+ try{
72
+ let ws=await getWSAuthDataByUserId(userId);
73
+ if(!Boolean(ws))
74
+ return false;
75
+ if(!ws.dataValues.is_active)
76
+ return false;
77
+ if(Date.now()>ws.dataValues.expiration.getTime())
78
+ return false;
79
+ if(ws.dataValues.auth_connection_token!==connection_token)
80
+ return false;
81
+ return true;
82
+ }catch(e){
83
+ throw e;
84
+ }
48
85
  }