From 0b7e73459454152f6d9cba55f7a240184f3d8216 Mon Sep 17 00:00:00 2001
From: Benjamin Laugueux <benjamin@laugueux.org>
Date: Wed, 10 Apr 2013 20:23:10 +0200
Subject: [PATCH] Added http basic authentication and custom api endpoint.

Fixed test
---
 DependencyInjection/Configuration.php |  3 +-
 README.md                             |  3 +-
 Resources/views/layout.html.twig      | 42 ++++++++++++++++++++-------
 3 files changed, 35 insertions(+), 13 deletions(-)

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 @@
                     <option value="json"{{ defaultRequestFormat == 'json' ? ' selected' : '' }}>JSON</option>
                     <option value="xml"{{ defaultRequestFormat == 'xml' ? ' selected' : '' }}>XML</option>
                 </select>
-                {% if authentication %}
-                    api key: <input type="text" id="api_key"/>
+                {% if authentication and authentication.delivery in ['query', 'http_basic'] %}
+                    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 %}
             </div>
             <br style="clear: both;" />
@@ -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 %}
         </script>