Skip to content

Commit

Permalink
Merge pull request #16 from fschrempf/feature/login-cookie-check
Browse files Browse the repository at this point in the history
Check if cookie expired when doing CT API login
  • Loading branch information
fschrempf authored Apr 5, 2024
2 parents 9938ead + d845744 commit cb3c8b3
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 cb3c8b3

Please sign in to comment.