-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from TREER00T/dev
Version 2.0.2
- Loading branch information
Showing
4 changed files
with
144 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,37 @@ | ||
import { Response } from "express"; | ||
import {Response} from "express"; | ||
import {IResponse} from "./Types"; | ||
|
||
// instance of response object express | ||
let res: Response; | ||
export default { | ||
|
||
builder(obj: IResponse, data?: any, option?: object) { | ||
if (!res) | ||
return; | ||
let jsonContent = JSON.parse(JSON.stringify(this.jsonObject(obj, data, option))); | ||
res.status(obj.code); | ||
res.send(jsonContent); | ||
}, | ||
|
||
initializationRes(response: Response) { | ||
res = response; | ||
}, | ||
|
||
jsonObject(obj: IResponse, data?: any, option?: object) { | ||
|
||
let objectBuilder = { | ||
code: obj.code, | ||
message: obj.message, | ||
data: data | ||
}; | ||
|
||
if (option) | ||
return data ? { | ||
...objectBuilder, | ||
option: option | ||
} : obj; | ||
|
||
return data ? objectBuilder : obj; | ||
} | ||
builder(obj: IResponse, data?: any, option?: object) { | ||
if (!res) | ||
return; | ||
let jsonContent = JSON.parse(JSON.stringify(this.jsonObject(obj, data, option))); | ||
res.status(obj.code); | ||
res.send(jsonContent); | ||
}, | ||
|
||
initializationRes(response: Response) { | ||
res = response; | ||
}, | ||
|
||
jsonObject(obj: IResponse, data?: any, option?: object) { | ||
|
||
let objectBuilder = { | ||
code: obj.code, | ||
message: obj.message, | ||
data: data | ||
}; | ||
|
||
if (option) | ||
return data ? { | ||
...objectBuilder, | ||
option: option | ||
} : obj; | ||
|
||
return data ? objectBuilder : obj; | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,89 @@ | ||
import { TokenExpiredError, JsonWebTokenError } from "jsonwebtoken"; | ||
import {TokenExpiredError, JsonWebTokenError} from "jsonwebtoken"; | ||
import jwt from "jsonwebtoken"; | ||
import { JWK, parse } from "node-jose"; | ||
import {JWK, parse} from "node-jose"; | ||
import Json from "../util/ReturnJson"; | ||
import Response from "../util/Response"; | ||
import * as dotenv from "dotenv"; | ||
import { ValidationException } from "../exception/ValidationException"; | ||
import {ValidationException} from "../exception/ValidationException"; | ||
|
||
dotenv.config(); | ||
|
||
export default { | ||
|
||
// Checks phone number E.g : 09030207892 return true | ||
isPhoneNumber(data: string): boolean { | ||
return /^\d+$/.test(data); | ||
}, | ||
// Checks phone number E.g : 09030207892 return true | ||
isPhoneNumber(data: string): boolean { | ||
return /\+?^\d+$/.test(data); | ||
}, | ||
|
||
requestEndpointHandler(requestMethod: string): string { | ||
const arrayOfHttpMethods = [ | ||
"/api/auth/generate/user", | ||
"/api/auth/verify/authCode" | ||
]; | ||
requestEndpointHandler(requestMethod: string): string { | ||
const arrayOfHttpMethods = [ | ||
"/api/auth/generate/user", | ||
"/api/auth/verify/authCode" | ||
]; | ||
|
||
// Searching in string to ensure exactly string | ||
// In some case it was break something like this: /api/channel/1452/us | ||
return arrayOfHttpMethods.includes(requestMethod) ? "" : requestMethod.match(/\d+/g)?.length > 0 ? "AuthRoute" : ""; | ||
}, | ||
// Searching in string to ensure exactly string | ||
// In some case it was break something like this: /api/channel/1452/us | ||
return arrayOfHttpMethods.includes(requestMethod) ? "" : requestMethod.match(/\d+/g)?.length > 0 ? "AuthRoute" : ""; | ||
}, | ||
|
||
// Verify jwt and check jwt expired time | ||
async getJwtVerify(token): Promise<any> { | ||
// Verify jwt and check jwt expired time | ||
async getJwtVerify(token): Promise<any> { | ||
|
||
return new Promise(async res => { | ||
return new Promise(async res => { | ||
|
||
try { | ||
jwt.verify(token, process.env.PUBLIC_KEY, {}, (err, decoded) => { | ||
try { | ||
jwt.verify(token, process.env.PUBLIC_KEY, {}, (err, decoded) => { | ||
|
||
if (err instanceof TokenExpiredError) { | ||
res("TOKEN_EXP"); | ||
return Json.builder(Response.HTTP_UNAUTHORIZED_TOKEN_EXP); | ||
} | ||
if (err instanceof TokenExpiredError) { | ||
res("TOKEN_EXP"); | ||
return Json.builder(Response.HTTP_UNAUTHORIZED_TOKEN_EXP); | ||
} | ||
|
||
if (err instanceof JsonWebTokenError) { | ||
res("IN_VALID_TOKEN"); | ||
return Json.builder(Response.HTTP_UNAUTHORIZED_INVALID_TOKEN); | ||
} | ||
if (err instanceof JsonWebTokenError) { | ||
res("IN_VALID_TOKEN"); | ||
return Json.builder(Response.HTTP_UNAUTHORIZED_INVALID_TOKEN); | ||
} | ||
|
||
res(decoded); | ||
}); | ||
} catch (e) { | ||
ValidationException(e); | ||
} | ||
|
||
res(decoded); | ||
}); | ||
} catch (e) { | ||
ValidationException(e); | ||
} | ||
|
||
}); | ||
}, | ||
|
||
}, | ||
|
||
// The jwt decrypt with JWK library and return jwt | ||
async getJwtDecrypt(encryptedBody) { | ||
try { | ||
let keystore = JWK.createKeyStore(); | ||
await keystore.add(await JWK.asKey(process.env.JWE_PRAIVATE_KEY, "pem")); | ||
let outPut = parse.compact(encryptedBody); | ||
let decryptedVal = await outPut.perform(keystore); | ||
let token = Buffer.from(decryptedVal.plaintext).toString(); | ||
|
||
// The jwt decrypt with JWK library and return jwt | ||
async getJwtDecrypt(encryptedBody) { | ||
try { | ||
let keystore = JWK.createKeyStore(); | ||
await keystore.add(await JWK.asKey(process.env.JWE_PRAIVATE_KEY, "pem")); | ||
let outPut = parse.compact(encryptedBody); | ||
let decryptedVal = await outPut.perform(keystore); | ||
let token = Buffer.from(decryptedVal.plaintext).toString(); | ||
if (!decryptedVal?.plaintext) | ||
return Json.builder(Response.HTTP_UNAUTHORIZED_INVALID_TOKEN); | ||
|
||
if (!decryptedVal?.plaintext) | ||
return Json.builder(Response.HTTP_UNAUTHORIZED_INVALID_TOKEN); | ||
return token.replace(/["]+/g, ""); | ||
|
||
return token.replace(/["]+/g, ""); | ||
} catch (e) { | ||
ValidationException(e); | ||
} | ||
}, | ||
|
||
} catch (e) { | ||
ValidationException(e); | ||
} | ||
}, | ||
|
||
|
||
// Returns split jwt without bearer | ||
getSplitBearerJwt(bearerHeader) { | ||
try { | ||
let token = bearerHeader.split(" ")[1]; | ||
if (token && bearerHeader) | ||
return token; | ||
return false; | ||
} catch (e) { | ||
ValidationException(e); | ||
|
||
// Returns split jwt without bearer | ||
getSplitBearerJwt(bearerHeader) { | ||
try { | ||
let token = bearerHeader.split(" ")[1]; | ||
if (token && bearerHeader) | ||
return token; | ||
return false; | ||
} catch (e) { | ||
ValidationException(e); | ||
} | ||
} | ||
} | ||
}; |