diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 7a90cb6..13bdcab 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -66,10 +66,11 @@ class Configuration implements ConfigurationInterface ->isRequired() ->validate() // header|query|request, but only query is implemented for now - ->ifNotInArray(array('query')) + ->ifNotInArray(array('query', 'http_basic')) ->thenInvalid("Unknown authentication delivery type '%s'.") ->end() ->end() + ->scalarNode('custom_endpoint')->defaultFalse()->end() ->end() ->end() ->end() diff --git a/README.md b/README.md index a93a2cb..77699b0 100644 --- a/README.md +++ b/README.md @@ -212,7 +212,8 @@ configure this sandbox using the following parameters: 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 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 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 diff --git a/Resources/views/layout.html.twig b/Resources/views/layout.html.twig index 89638d8..778ad81 100644 --- a/Resources/views/layout.html.twig +++ b/Resources/views/layout.html.twig @@ -21,8 +21,14 @@ - {% if authentication %} - api key: + {% if authentication and authentication.delivery in ['query', 'http_basic'] %} + api key: + {% endif %} + {% if authentication and authentication.delivery in ['http_basic'] %} + api pass: + {% endif %} + {% if authentication and authentication.custom_endpoint %} + api endpoint: {% endif %}
@@ -190,22 +196,34 @@ // disable all the fiels and buttons $('input, button', $(this)).attr('disabled', 'disabled'); - // append the api key - if (api_key_parameter) { + // append the query authentication + if (authentication_delivery == 'query') { url += url.indexOf('?') > 0 ? '&' : '?'; url += api_key_parameter + '=' + $('#api_key').val(); } + // prepare the api enpoint {% if endpoint == '' and app.request is defined and app.request.host -%} - {% set endpoint = app.request.getBaseUrl() -%} - {% endif -%} + var endpoint = '{{ app.request.getBaseUrl() }}'; + {% else -%} + var endpoint = '{{ endpoint }}'; + {% endif -%} + if ($('#api_endpoint') && $('#api_endpoint').val() != null) { + endpoint = $('#api_endpoint').val(); + } // and trigger the API call $.ajax({ - url: '{{ endpoint }}' + url, + url: endpoint + url, type: method, data: content.length ? content : params, 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) { displayResponse(xhr, method, url, result_container); @@ -288,12 +306,14 @@ }); - {% 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 search = window.location.search; - var api_key_start = search.indexOf(api_key_parameter) + api_key_parameter.length + 1; - + if (api_key_start > 0 ) { var api_key_end = search.indexOf('&', api_key_start); @@ -304,7 +324,7 @@ $('#api_key').val(api_key); } {% else %} - var api_key_parameter = false; + var authentication_delivery = false; {% endif %} {% endif %}