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 5572422
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
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
11 changes: 10 additions & 1 deletion 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 @@ -60,6 +61,14 @@ let getCsrfToken = this.getCsrfTokenReal;

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

const cookieIsValid = (c) => {

Check failure on line 64 in src/ctconnection.js

View workflow job for this annotation

GitHub Actions / eslint

'c' is already declared in the upper scope on line 4 column 7

Check failure on line 64 in src/ctconnection.js

View workflow job for this annotation

GitHub Actions / eslint

'c' is already declared in the upper scope on line 4 column 7
const parsed = cookie.parse(c);
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 5572422

Please sign in to comment.