Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,13 @@ import { getConfig } from "./src/config/env.js";
import { TransportError } from "./src/errors/TransportError.js";
import { RateLimitError } from "./src/errors/RateLimitError.js";
import { TimeoutError } from "./src/errors/TimeoutError.js";
import { ExpressAdapter } from "@bull-board/express";
import { BullMQAdapter } from "@bull-board/api/bullMQAdapter";
import { createBullBoard } from "@bull-board/api";
import express from "express";
import { oauth2TokenQueue } from "./src/queues/OAuth2Token.js";
import { ioRedisClient } from "./src/queues/OAuth2Token.js";
import "./src/job/oauth2-token-generation-worker.js";
import { scheduleAccessTokenRefresh } from "./src/queues/OAuth2Token.js";
import { OAuth2Handler } from "./src/auth/handlers/OAuth2AuthHandler.js";


const app = express();

const serverAdapter = new ExpressAdapter();
createBullBoard({
queues: [new BullMQAdapter(oauth2TokenQueue)],
serverAdapter,
});
serverAdapter.setBasePath("/admin/queues");
app.use("/admin/queues", serverAdapter.getRouter());


// Load configuration from environment variables
Expand All @@ -31,7 +20,28 @@ const { baseURL, authType, credentials, oath2Config, redisConfig, accessTokenURL
const authHandler = CreateAuthHandler(authType, credentials, oath2Config, accessTokenURL, redisConfig);

if (authType === 'oauth2') {
if (oath2Config && accessTokenURL) {
const oauth2Client = new OAuth2Handler(oath2Config, accessTokenURL, redisConfig);
try {
const data = await oauth2Client.getAccessToken();
if (data?.access_token && data?.expires_in) {
const expirationTime = data.expires_in - 10;
await ioRedisClient.del('next-job-time-for-oauth2-access-token');
await ioRedisClient.set(
'next-job-time-for-oauth2-access-token',
JSON.stringify(expirationTime),
'EX',
expirationTime
);
} else {
console.log("Invalid token");
}
} catch (error) {
console.error(error);
}
}
(async () => {

await scheduleAccessTokenRefresh();
})();
}
Expand Down
12 changes: 2 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
"release:minor": "zx release.js --minor",
"release:major": "zx release.js --major"
},
"keywords": [
"Windows Authentication",
"Nodejs",
"Network Transport Wrapper"
],
"keywords": ["Windows Authentication", "Nodejs", "Network Transport Wrapper"],
"author": "Paullaster Okoth",
"license": "MIT",
"description": "A lightWeight, robust and easily extensible HTTP(/2,/3)/HTTPS network transport utility",
Expand All @@ -42,19 +38,15 @@
"zx": "^8.3.2"
},
"dependencies": {
"@bull-board/express": "^6.13.1",
"axios": "^1.7.9",
"axios-ntlm": "^1.4.3",
"bottleneck": "^2.19.5",
"bullmq": "^5.61.0",
"dotenv": "^16.4.7",
"express": "^5.1.0",
"ioredis": "^5.8.1",
"node-cache": "^5.1.2",
"pino": "^9.6.0",
"pino-pretty": "^13.0.0"
},
"files": [
"dist"
]
"files": ["dist"]
}
1 change: 1 addition & 0 deletions src/auth/handlers/OAuth2AuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class OAuth2Handler implements AuthHandler {
}
});
data = tokenResponse;
await this.redisConnection.del('microsoft-bc-oauth2-access-token');
await this.redisConnection.set('microsoft-bc-oauth2-access-token', data.access_token, "EX", data.expires_in);
this.accessToken = data.access_token;
}
Expand Down
1 change: 0 additions & 1 deletion src/job/oauth2-token-generation-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const oauth2AccessTokenWorker = new Worker('oauth2-access-token-queue',
const oauth2Client = new OAuth2Handler(config, accessTokenURL, redis);
try {
const data = await oauth2Client.getAccessToken();
console.log(data);
if (data?.access_token && data?.expires_in) {
const expirationTime = data.expires_in - 10;
await ioRedisClient.set(
Expand Down
2 changes: 1 addition & 1 deletion src/queues/OAuth2Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const oauth2TokenQueue = new Queue('oauth2-access-token-queue', { connect

export const scheduleAccessTokenRefresh = async () => {
const storedTime = await ioRedisClient.get('next-job-time-for-oauth2-access-token');
const refreshInterval = parseInt(String(JSON.parse(storedTime || '1')), 10) * 1000;
const refreshInterval = parseInt(String(JSON.parse(storedTime || '3500')), 10) * 1000;
await oauth2TokenQueue.upsertJobScheduler(
'schedule-oauth2-access-token-generation',
{ every: refreshInterval },
Expand Down
30 changes: 15 additions & 15 deletions tests/unit/oauth2.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { transport } from "../../index.ts";

async function runTest() {
try {
// const response = await transport.get("/api/KineticTechnology/CashMgt/v2.0/imprestAPI", {}, { headers: { 'Prefer': "maxpagesize=2"}}) as Response;
const response = await transport.get("/api/KineticTechnology/CashMgt/v2.0/imprestAPI", {}, { headers: { 'Prefer': "maxpagesize=2" } }) as Response;
// const filter = await transport.filter({
// date_from: "2022-01-01",
// date_to: '2022-01-01',
Expand All @@ -12,20 +12,20 @@ async function runTest() {
// status: "Completed",
// })

const updateSalaryAdance = await transport.put('/api/KineticTechnology/PayRoll/v2.0/payrollAdvance', {
"applicationReason": "kvckbvc",
"applicationDate": "2025-03-14",
"applicationAmount": 39500,
"no": 'SADV000',
},
{
params: {
compan: process.env.BC_COMPANY_NAME
},
primaryKey: ['no']
} as Record<string, any>
)
console.log("Updated Salary advance: :", await (updateSalaryAdance as Response));
// const updateSalaryAdance = await transport.put('/api/KineticTechnology/PayRoll/v2.0/payrollAdvance', {
// "applicationReason": "kvckbvc",
// "applicationDate": "2025-03-14",
// "applicationAmount": 39500,
// "no": 'SADV000',
// },
// {
// params: {
// compan: process.env.BC_COMPANY_NAME
// },
// primaryKey: ['no']
// } as Record<string, any>
// )
console.log("Updated Salary advance: :", await (response as Response));
} catch (error) {
console.error("Error:", error);
}
Expand Down