Commit d4e3dfd7 by Percy Quispe Huarcaya

feat: Adding token creator and a parser

parent 5177ce93
## Proyecto
Security
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -4,7 +4,7 @@
"description": "Library to manage tokens using js",
"type": "module",
"main": "src/util/token.ts",
"types": "types/security-ndjs-lib.d.ts",
"types": "types/index.d.ts",
"scripts": {
"build": "tsc",
"test": "jest --config jest.config.ts"
......
......@@ -36,6 +36,7 @@ export const createToken = (id: string, issuer: string, subject: object, ...time
// Create and sign the JWT
const token = jwt.sign(payload, signingKey, options);
return token;
};
......@@ -50,6 +51,8 @@ export const parseJWT = (token: string, ...id: string[]) => {
const decoded = jwt.verify(token, Buffer.from(apiKey, 'base64'), {
algorithms: [tokenAlgorithm]
});
console.log("parseJWT--------------------")
console.log(decoded);
return decoded;
} catch (err) {
throw err;
......
import { createJWT, parseJWT } from 'security-ndjs-lib';
import { JwtPayload } from 'jsonwebtoken';
import { createJWT, parseJWT } from '../src/util/token';
describe('Token', () => {
test('should generate and validate a token sending duration time', () => {
......@@ -13,8 +14,8 @@ describe('Token', () => {
console.log("------------------------>");
console.log(test);
const payload = test.subject;
expect(payload.usuarioId).toBe(395);
const payload = test.sub;
expect(payload).not.toBeNull();
});
test('should generate and validate a token without sending duration time', () => {
......@@ -25,8 +26,8 @@ describe('Token', () => {
const token = createJWT(id, subject);
const test = parseJWT(token);
const payload = test.subject;
expect(payload.usuarioId).toBe(395);
const payload = test.sub;
expect(payload).not.toBeNull();
});
test('should throw an error for an invalid token', () => {
......
{
"compilerOptions": {
"target": "ES2017",
"module": "ESNext",
"target": "ES2020", // o una versión compatible
"module": "ES2020", // Para usar módulos ES
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"typeRoots": ["./node_modules/@types", "./types"] // Include your custom types folder
"outDir": "./dist", // directorio de salida para los archivos compilados
"rootDir": "./src" // directorio raíz de los archivos fuente
},
"include": ["src/**/*", "types/**/*"], // Include both src and types
"include": ["src/**/*", "types/tokenmodel.ts"],
"exclude": ["node_modules"]
}
\ No newline at end of file
import { JwtPayload } from "jsonwebtoken";
declare module 'security-ndjs-lib' {
export function createJWT(id: string, subject: object, time?: number): string;
export function parseJWT(token?: string, id?: string): TokenModel; // Adjust the signature if needed
export function parseJWT(token?: string, id?: string): JwtPayload; // Adjust the signature if needed
}
\ No newline at end of file
interface TokenModel {
id: string;
issuedAt: number;
subject: any;
issuer: string;
iat: number;
exp: number;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment