Skip to content

Commit

Permalink
Check if cookie expired when doing CT API login
Browse files Browse the repository at this point in the history
Otherwise we might use an expired cookie which causes unauthenticated
API requests and therefore incomplete data to be returned.
  • Loading branch information
fschrempf committed Apr 5, 2024
1 parent 9938ead commit d845744
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"dependencies": {
"axios": "^0.21.4",
"cookie": "^0.6.0",
"ldap-escape": "^2.0.5",
"ldap-filter": "^0.3.3",
"ldapjs": "^2.3.1",
Expand Down
15 changes: 12 additions & 3 deletions src/ctconnection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const axiosReal = require('axios').default;
const cookie = require('cookie');
const log = require('./logging');
const c = require('./constants');
const ctapi = require('./ctapi');
Expand Down Expand Up @@ -44,12 +45,12 @@ exports.infoReal = async (baseurl) => {
return ctapi.request(request);
};

exports.getCsrfTokenReal = async (baseurl, cookie) => {
exports.getCsrfTokenReal = async (baseurl, ck) => {
const request = {
method: 'get',
url: baseurl + c.API_SLUG + c.CSRF_AP,
headers: {
Cookie: cookie,
Cookie: ck,
},
json: true,
};
Expand All @@ -60,6 +61,14 @@ let getCsrfToken = this.getCsrfTokenReal;

const getCookie = (result) => result.headers['set-cookie'][0];

const cookieIsValid = (ck) => {
const parsed = cookie.parse(ck);
const expires = Date.parse(parsed.expires);
if (expires > Date.now()) return true;
log.info('Cookie expired');
return false;
};

const getLoginRequest = (baseurl, user, password) => ({
method: 'post',
url: baseurl + c.API_SLUG + c.LOGIN_AP,
Expand Down Expand Up @@ -129,7 +138,7 @@ exports.getPromiseReal = async (url, site) => {
while (retryWithAuth) {
retryWithAuth = false;
try {
if (!this.isConnected(site.name)) {
if (!this.isConnected(site.name) || !cookieIsValid(conn.cookie)) {
log.debug('Try again to log in');
await this.login(site);
}
Expand Down

0 comments on commit d845744

Please sign in to comment.