Commit d550843f authored by Laurent's avatar Laurent

fix Koha/CAS for Lyon 2 university

parent 508e7c1f
import * as http from 'http';
import {Koha} from './koha';
import {WrongLoginPassword} from './wrong-login-password';
import {Account} from '../../models';
import * as cheerio from 'cheerio';
export class KohaCas extends Koha {
......@@ -46,19 +48,13 @@ export class KohaCas extends Koha {
}
return this
.request(account,
{ url: cas_link,
method: "POST",
dontFollowRedirects: true,
content: this._encodeForm(form_data),
headers: {"Content-Type": "application/x-www-form-urlencoded"} });
._requestWorkaroundCookies(account,
{ url: cas_link,
method: "POST",
content: this._encodeForm(form_data),
headers: {"Content-Type": "application/x-www-form-urlencoded"} });
})
})
.then( (response) => {
return (response.statusCode == 302)
? this.request(account, { url: response.headers['Location'] })
: response
})
.then( (response) => {
let $ = cheerio.load(response.content.toString());
let cas_link = $('a[href*="/cas/login?service="]').attr('href');
......@@ -83,4 +79,27 @@ export class KohaCas extends Koha {
return form_data;
}
protected _requestWorkaroundCookies(account: Account, options: any): Promise<http.HttpResponse> {
options.dontFollowRedirects = true;
let cookies = account.getCookies();
if (cookies.length > 0) {
options.headers['Cookie'] = cookies.map( (cookie) => {
return cookie.toString();
});
account.setCookies([]);
}
return this
.request(account, options)
.then( (response) => {
if (response.statusCode == 301 || response.statusCode == 302 || response.statusCode == 303)
return this.request( account,
{ url: response.headers['Location'] });
return response;
});
}
}
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