Merge pull request #172 from blaugueux/http-basic

Added support for HTTP Basic authentication and custom api endpoint.
This commit is contained in:
Jordi Boggiano 2013-04-11 06:20:33 -07:00
commit a30e5744e7
3 changed files with 35 additions and 13 deletions

View File

@ -66,10 +66,11 @@ class Configuration implements ConfigurationInterface
->isRequired() ->isRequired()
->validate() ->validate()
// header|query|request, but only query is implemented for now // header|query|request, but only query is implemented for now
->ifNotInArray(array('query')) ->ifNotInArray(array('query', 'http_basic'))
->thenInvalid("Unknown authentication delivery type '%s'.") ->thenInvalid("Unknown authentication delivery type '%s'.")
->end() ->end()
->end() ->end()
->scalarNode('custom_endpoint')->defaultFalse()->end()
->end() ->end()
->end() ->end()
->end() ->end()

View File

@ -214,7 +214,8 @@ configure this sandbox using the following parameters:
sandbox: sandbox:
authentication: # default null, if set, the value of the api key is read from the query string and appended to every sandbox api call authentication: # default null, if set, the value of the api key is read from the query string and appended to every sandbox api call
name: access_token name: access_token
delivery: query # only query delivery is supported for now delivery: query # query or http_basic are supported
custom_endpoint: true # default false, if true, your user will be able to specify its own endpoint
enabled: true # default: true, you can set this parameter to `false` to disable the sandbox enabled: true # default: true, you can set this parameter to `false` to disable the sandbox
endpoint: http://sandbox.example.com/ # default: /app_dev.php, use this parameter to define which URL to call through the sandbox endpoint: http://sandbox.example.com/ # default: /app_dev.php, use this parameter to define which URL to call through the sandbox
accept_type: application/json # default null, if set, the value is automatically populated as the Accept header accept_type: application/json # default null, if set, the value is automatically populated as the Accept header

View File

@ -21,8 +21,14 @@
<option value="json"{{ defaultRequestFormat == 'json' ? ' selected' : '' }}>JSON</option> <option value="json"{{ defaultRequestFormat == 'json' ? ' selected' : '' }}>JSON</option>
<option value="xml"{{ defaultRequestFormat == 'xml' ? ' selected' : '' }}>XML</option> <option value="xml"{{ defaultRequestFormat == 'xml' ? ' selected' : '' }}>XML</option>
</select> </select>
{% if authentication %} {% if authentication and authentication.delivery in ['query', 'http_basic'] %}
api key: <input type="text" id="api_key"/> api key: <input type="text" id="api_key" value=""/>
{% endif %}
{% if authentication and authentication.delivery in ['http_basic'] %}
api pass: <input type="text" id="api_pass" value=""/>
{% endif %}
{% if authentication and authentication.custom_endpoint %}
api endpoint: <input type="text" id="api_endpoint" value=""/>
{% endif %} {% endif %}
</div> </div>
<br style="clear: both;" /> <br style="clear: both;" />
@ -190,22 +196,34 @@
// disable all the fiels and buttons // disable all the fiels and buttons
$('input, button', $(this)).attr('disabled', 'disabled'); $('input, button', $(this)).attr('disabled', 'disabled');
// append the api key // append the query authentication
if (api_key_parameter) { if (authentication_delivery == 'query') {
url += url.indexOf('?') > 0 ? '&' : '?'; url += url.indexOf('?') > 0 ? '&' : '?';
url += api_key_parameter + '=' + $('#api_key').val(); url += api_key_parameter + '=' + $('#api_key').val();
} }
// prepare the api enpoint
{% if endpoint == '' and app.request is defined and app.request.host -%} {% if endpoint == '' and app.request is defined and app.request.host -%}
{% set endpoint = app.request.getBaseUrl() -%} var endpoint = '{{ app.request.getBaseUrl() }}';
{% else -%}
var endpoint = '{{ endpoint }}';
{% endif -%} {% endif -%}
if ($('#api_endpoint') && $('#api_endpoint').val() != null) {
endpoint = $('#api_endpoint').val();
}
// and trigger the API call // and trigger the API call
$.ajax({ $.ajax({
url: '{{ endpoint }}' + url, url: endpoint + url,
type: method, type: method,
data: content.length ? content : params, data: content.length ? content : params,
headers: headers, headers: headers,
crossDomain: true,
beforeSend: function (xhr) {
if (authentication_delivery == 'http_basic') {
xhr.setRequestHeader('Authorization', 'Basic ' + btoa($('#api_key').val() + ':' + $('#api_pass').val()));
}
},
complete: function(xhr) { complete: function(xhr) {
displayResponse(xhr, method, url, result_container); displayResponse(xhr, method, url, result_container);
@ -288,10 +306,12 @@
}); });
{% if authentication %} {% if authentication and authentication.delivery == 'http_basic' %}
var authentication_delivery = '{{ authentication.delivery }}';
{% elseif authentication and authentication.delivery == 'query' %}
var authentication_delivery = '{{ authentication.delivery }}';
var api_key_parameter = '{{ authentication.name }}'; var api_key_parameter = '{{ authentication.name }}';
var search = window.location.search; var search = window.location.search;
var api_key_start = search.indexOf(api_key_parameter) + api_key_parameter.length + 1; var api_key_start = search.indexOf(api_key_parameter) + api_key_parameter.length + 1;
if (api_key_start > 0 ) { if (api_key_start > 0 ) {
@ -304,7 +324,7 @@
$('#api_key').val(api_key); $('#api_key').val(api_key);
} }
{% else %} {% else %}
var api_key_parameter = false; var authentication_delivery = false;
{% endif %} {% endif %}
{% endif %} {% endif %}
</script> </script>