Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-24960] Android: TiHTTPClient credentials encoding issue.

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2017-07-18T22:30:17.000+0000
Affected Version/sRelease 6.1.1
Fix Version/sRelease 6.1.2
ComponentsAndroid
Labelsn/a
ReporterJorge Macias Garcia
AssigneeYordan Banev
Created2017-07-14T14:44:21.000+0000
Updated2017-07-21T17:13:39.000+0000

Description

Encoding authentication credentials creates characters invalid for HTTP headers. *Test case:* _index.js_
(function controller(args) {
  'use strict';

  //Set events
  ['simplePost', 'basicAuthPost'].forEach(function (proxy) {
    $.addListener($[proxy], 'click', prepareRequest);
  });

  /**
   * @method  prepareRequest
   * @param   {object}       e Callback event
   */
  function prepareRequest(e) {
    var withCredentials = true;

    if (e.source.id === 'simplePost') {
      withCredentials = false;
    }

    request(withCredentials);
  }

  /**
   * Make a request
   * @method  request
   * @param   {boolean} withCredentials Flag for make a auth request or not
   */
  function request(withCredentials) {
    var client,
      config,
      url,
      data;

    data = {
      message: 'This is my message'
    };

    config = {
      onload: success,
      onerror: error,
      timeout: 5000
    };

    if (withCredentials) {
      //Returns HTTP Headers in a success auth
      url = 'https://srvasic.upv.es/recursos/error401.asp';
      config.withCredentials = true;

      //Works
      //config.username = 'user';
      //config.password = 'p$wd';
      
      //Fails
      config.username = 'CR8FB51C9B3C5EDEEB959BCBCF55339FB1';
      config.password = '3F853CE4EB1CE3337D51B0CEE707FDCC8E0D';
      
      //Works
      //config.username = 'CR8FB51C9B3C5EDE';
      //config.password = '3F853CE4EB1CE333';
    } elseĀ  {
      //Echo return
      url = 'https://httpbin.org/post';
    }

    console.log('HTTP CLIENT CONFIG ' + JSON.stringify(config));

    client = Ti.Network.createHTTPClient(config);

    try {
      console.log('URL: ' + url);
      client.open('POST', url);
    } catch (e) {
      console.log('error opening client');
      console.log(JSON.stringify(e));
    }

    try {
      client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    } catch (e) {
      console.log('error setting request headers');
      console.log(JSON.stringify(e));
    }

    try {
      client.send(data);
    } catch (e) {
      console.log('error sending data');
      console.log(JSON.stringify(e));
    }
  }

  /**
   * HttpClient Success Callback
   * @method  success
   * @param   {object} e Callback Event
   */
  function success(e) {
    console.log('Success callback: ' + this.responseText);
  }

  /**
   * HttpClient Error Callback
   * @method  error
   * @param   {object} e Callback Event
   */
  function error(e) {
    console.log('Error callback : ' + e.error);
    console.log(this.responseText);
  }

  $.index.open();

})(arguments[0] || {});
_index.xml_
<Alloy>
	<Window class="container" layout="vertical">
		<Label id="simplePost">Simple POST</Label>
		<Label id="basicAuthPost">Basic Auth POST</Label>
	</Window>
</Alloy>

Comments

  1. Yordan Banev 2017-07-14

    PR: https://github.com/appcelerator/titanium_mobile/pull/9220
  2. Samir Mohammed 2017-07-14

    [~ybanev] Is their a backport for this ticket?
  3. Yordan Banev 2017-07-17

    [~smohammed], sorry for the delay, here is the backport: 6_1_X PR: https://github.com/appcelerator/titanium_mobile/pull/9222
  4. Samir Mohammed 2017-07-21

    Verified fix in SDK Version 6.2.0.v20170719160617 and SDK Version: 6.1.2.v20170720023139. Test and other information can be found at: Master: https://github.com/appcelerator/titanium_mobile/pull/9220 6_1_X: https://github.com/appcelerator/titanium_mobile/pull/9222

JSON Source