From 0a0fd9a27f4278929fb25a57a7cbfabcab154229 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 25 Mar 2018 13:40:35 +0200 Subject: [PATCH] [lib.client.xhr][lib.client.client-driver] manages authentication and HTTP token for XHR --- webpack/lib/client/client-driver.js | 5 ++- webpack/lib/client/xhr.js | 52 ++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/webpack/lib/client/client-driver.js b/webpack/lib/client/client-driver.js index 65f9d9a..46d4f8e 100644 --- a/webpack/lib/client/client-driver.js +++ b/webpack/lib/client/client-driver.js @@ -18,9 +18,10 @@ export default class ClientDriver{ /* (1) Creates a client driver * * @_resource Target resource (typically an URL) + * @_auth Authentication data (depends on Driver implementation) * ---------------------------------------------------------*/ - constructor(_resource){ + constructor(_resource, _auth={}){ /* (1) Initialize driver ---------------------------------------------------------*/ @@ -28,6 +29,7 @@ export default class ClientDriver{ this.force_close = false; this.state = ClientDriver.STATE.READY; this.stack = []; + this.error = false; /* (2) Default User callbacks */ this.callback = { @@ -90,6 +92,7 @@ export default class ClientDriver{ /* (2) Set explicit attributes */ this.resource = _resource; + this.auth = _auth; return; diff --git a/webpack/lib/client/xhr.js b/webpack/lib/client/xhr.js index e4e5361..cda11b4 100644 --- a/webpack/lib/client/xhr.js +++ b/webpack/lib/client/xhr.js @@ -5,20 +5,35 @@ export default class XHRClientDriver extends ClientDriver{ /* (1) Creates a client driver * - * @_resource Target resource (typically an URL) + * @_resource Target resource + * (typically a hostname without trailing HTTP:// nor HTTPS://) + * @_auth Authentication object + * + * + * [fornat::AuthObject] + * { + * ssl: + * } * ---------------------------------------------------------*/ - constructor(_resource){ + constructor(_resource, _auth={ssl:true}){ /* (0) Parent check */ - if( !super().error ) + if( super(_resource, _auth).error ){ + this.force_close = true; return; + } - /* (1) Set inherited attributes */ - this.resource = _resource; - - /* (2) Create useful attributes */ + /* (1) Create useful attributes */ this.xhr = null; + this.http_token = null; + + /* (2) Manage _resource format (remove http://, https://) */ + this.resource = _resource.replace(/^https?:\/\//, ''); + + /* (3) Manage @proto default values */ + this.proto = typeof _auth !== 'object' && _auth['ssl'] != null && _auth.ssl === true; + this.proto = (this.proto) ? 'https://' : 'http://'; } @@ -132,8 +147,11 @@ export default class XHRClientDriver extends ClientDriver{ /* (1) Build full URL */ let request_url = this.resource.split(/\/$/).concat(http_uri.split(/^\//)).filter((v) => v.trim().length).join('/'); - /* (2) Open connection */ - this.xhr.open(http_method, request_url, true); + /* (2) Build protocol (http, https) */ + let protocol = this.http_token != null ? `${this.proto}${this.http_token}@` : this.proto; + + /* (3) Open connection */ + this.xhr.open(http_method, `${protocol}${request_url}`, true); /* (3) Send request */ this.xhr.send(form_data); @@ -142,4 +160,20 @@ export default class XHRClientDriver extends ClientDriver{ } + + /* (4) Set HTTP token + * + * @_token HTTP token + * + ---------------------------------------------------------*/ + setHttpToken(_token=null){ + + /* Manage _token default values */ + if( typeof _token === 'string' ) + this.http_token = _token; + else + this.http_token = null; + } + + } \ No newline at end of file