Commit e33b68df authored by Laurent's avatar Laurent

Improve sessions management

parent eaca8c31
import * as Application from 'application';
declare var android: any;
declare var java: any;
import * as Application from 'application'
declare var android: any
declare var java: any
export class Cookies {
public syncHttpToWebViews(): Cookies {
new WebViewCookies().setCookies(this.getCookies());
return this;
new WebViewCookies().setCookies(this.getCookies())
return this
}
public setCookies(cookies:Array<java.net.HttpCookie>): Cookies {
new JavaHTTPCookies().setCookies(cookies);
return this;
public setCookies(cookies: Map<java.net.URI, Array<java.net.HttpCookie>>): Cookies {
new JavaHTTPCookies().setCookies(cookies)
return this
}
public getCookies(): Array<java.net.HttpCookie> {
return new JavaHTTPCookies().getCookies();
public getCookies(): Map<java.net.URI, Array<java.net.HttpCookie>> {
return new JavaHTTPCookies().getCookies()
}
public clearCookies(): Cookies {
new JavaHTTPCookies().clearCookies();
new WebViewCookies().clearCookies();
return this;
new JavaHTTPCookies().clearCookies()
new WebViewCookies().clearCookies()
return this
}
}
......@@ -32,49 +32,51 @@ export class Cookies {
class JavaHTTPCookies {
public getCookieStore(): java.net.CookieStore {
let cookie_manager = java.net.CookieHandler.getDefault();
let cookie_manager = java.net.CookieHandler.getDefault()
return (null == cookie_manager)
? null
: cookie_manager.getCookieStore();
: cookie_manager.getCookieStore()
}
public getCookies(): Array<java.net.HttpCookie> {
let cookies:Array<java.net.HttpCookie> = new Array<java.net.HttpCookie>();
let cookie_store = this.getCookieStore();
public getCookies(): Map<java.net.URI, Array<java.net.HttpCookie>> {
let cookies:Map<java.net.URI, Array<java.net.HttpCookie>> = new Map<java.net.URI, Array<java.net.HttpCookie>>()
let cookie_store = this.getCookieStore()
if (null == cookie_store)
return cookies;
let cookie_list = cookie_store.getCookies();
for (let i = 0; i < cookie_list.size(); i++) {
cookies.push(cookie_list.get(i));
};
return cookies;
return cookies
let uris = cookie_store.getURIs()
for (let i = 0; i < uris.size(); i++) {
let uri = uris.get(i)
cookies.set(uri, Array.from(cookie_store.get(uri).toArray()))
}
return cookies
}
public setCookies(cookies: Array<java.net.HttpCookie>): JavaHTTPCookies {
let cookie_store = this.getCookieStore();
public setCookies(cookies: Map<java.net.URI, Array<java.net.HttpCookie>>): JavaHTTPCookies {
let cookie_store = this.getCookieStore()
if (null == cookie_store)
return this;
return this
cookie_store.removeAll();
cookies.forEach((cookie) => {
cookie_store.add(new java.net.URI(cookie.getDomain()),
cookie);
});
java.net.CookieHandler.getDefault().setCookiePolicy(java.net.CookiePolicy.ACCEPT_ALL)
cookie_store.removeAll()
cookies.forEach( (uri_cookies:Array<java.net.HttpCookie>, uri: java.net.URI) => {
uri_cookies.forEach( (cookie: java.net.HttpCookie) => { cookie_store.add(uri, cookie) })
})
return this;
return this
}
public clearCookies(): JavaHTTPCookies {
let cookie_store = this.getCookieStore();
let cookie_store = this.getCookieStore()
if (null != cookie_store)
cookie_store.removeAll();
return this;
cookie_store.removeAll()
return this
}
}
......@@ -82,23 +84,25 @@ class JavaHTTPCookies {
class WebViewCookies {
public setCookies(cookies: Array<java.net.HttpCookie>): WebViewCookies {
let cookie_manager = android.webkit.CookieManager.getInstance();
cookie_manager.setAcceptCookie(true);
cookie_manager.removeSessionCookie();
cookies.forEach((cookie) => {
cookie_manager.setCookie(cookie.getDomain(), cookie.toString());
});
android.webkit.CookieSyncManager.createInstance(Application.android.context).sync();
cookie_manager.flush();
return this;
public setCookies(cookies: Map<java.net.URI, Array<java.net.HttpCookie>>): WebViewCookies {
let cookie_manager = android.webkit.CookieManager.getInstance()
cookie_manager.setAcceptCookie(true)
cookie_manager.removeSessionCookie()
cookies.forEach( (uri_cookies:Array<java.net.HttpCookie>, uri: java.net.URI) => {
uri_cookies.forEach( (cookie: java.net.HttpCookie) => {
cookie_manager.setCookie(uri.toString(), cookie.toString())
})
})
android.webkit.CookieSyncManager.createInstance(Application.android.context).sync()
cookie_manager.flush()
return this
}
public clearCookies(): WebViewCookies {
android.webkit.CookieManager.getInstance().removeSessionCookie();
return this;
android.webkit.CookieManager.getInstance().removeSessionCookie()
return this
}
}
......@@ -3,6 +3,7 @@ import {Koha} from './koha';
import {WrongLoginPassword} from './wrong-login-password';
import {Account} from '../../models';
import * as cheerio from 'cheerio';
import * as UrlParse from 'url-parse';
export class KohaCas extends Koha {
public getIdentifier(): string {
......@@ -23,7 +24,8 @@ export class KohaCas extends Koha {
protected _signIn(account): Promise<any> {
if (!(account.credentials['login'] && account.credentials['password']))
return Promise.reject(new WrongLoginPassword());
account.setCookies([])
return this
.request(account,
{ url: account.getUrl() })
......@@ -42,17 +44,18 @@ export class KohaCas extends Koha {
username: account.credentials['login'],
password: account.credentials['password'],
execution: form_inputs['execution'],
geolocation: '',
'_eventId': form_inputs['_eventId'],
'lt': form_inputs['lt'] || '',
'submit': form_inputs['submit']
}
return this
._requestWorkaroundCookies(account,
{ url: cas_link,
method: "POST",
content: this._encodeForm(form_data),
headers: {"Content-Type": "application/x-www-form-urlencoded"} });
.request(account,
{ url: cas_link,
method: "POST",
content: this._encodeForm(form_data),
headers: {"Content-Type": "application/x-www-form-urlencoded"} });
})
})
.then( (response) => {
......@@ -73,33 +76,9 @@ export class KohaCas extends Koha {
$('form input')
.filter( (i, elem) => { return $(elem).attr('name') != undefined; } )
.each((i, elem) => {
let input = $(elem);
form_data[input.attr('name')] = input.attr('value') || '';
let input = $(elem)
form_data[input.attr('name')] = input.attr('value') || ''
});
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