diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index bd5c8fc..6997992 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -86,16 +86,50 @@ class Configuration implements ConfigurationInterface ->end() ->arrayNode('authentication') ->children() - ->scalarNode('name')->isRequired()->end() ->scalarNode('delivery') ->isRequired() ->validate() - ->ifNotInArray(array('query', 'http_basic', 'header')) + ->ifNotInArray(array('query', 'http', 'header')) ->thenInvalid("Unknown authentication delivery type '%s'.") ->end() ->end() + ->scalarNode('name')->isRequired()->end() + ->enumNode('type') + ->info('Required if http delivery is selected.') + ->values(array('basic', 'bearer')) + ->end() ->booleanNode('custom_endpoint')->defaultFalse()->end() ->end() + ->validate() + ->ifTrue(function($v) { + return 'http' === $v['delivery'] && !$v['type'] ; + }) + ->thenInvalid('"type" is required when using http delivery.') + ->end() + # http_basic BC + ->beforeNormalization() + ->ifTrue(function ($v) { + return 'http_basic' === $v['delivery']; + }) + ->then(function ($v) { + $v['delivery'] = 'http'; + $v['type'] = 'basic'; + + return $v; + }) + ->end() + ->beforeNormalization() + ->ifTrue(function ($v) { + return 'http' === $v['delivery']; + }) + ->then(function ($v) { + if ('http' === $v['delivery'] && !isset($v['name'])) { + $v['name'] = 'Authorization'; + } + + return $v; + }) + ->end() ->end() ->end() ->end() diff --git a/Resources/config/schema/api_doc.xsd b/Resources/config/schema/api_doc.xsd index b8c97ab..21b7567 100644 --- a/Resources/config/schema/api_doc.xsd +++ b/Resources/config/schema/api_doc.xsd @@ -62,6 +62,7 @@ + diff --git a/Resources/doc/index.md b/Resources/doc/index.md index 7744b0e..f632a27 100644 --- a/Resources/doc/index.md +++ b/Resources/doc/index.md @@ -307,7 +307,10 @@ configure this sandbox using the following parameters: name: access_token # access token name or query parameter name or header name - delivery: query # `query`, `http_basic`, and `header` are supported + delivery: http # `query`, `http`, and `header` are supported + + # Required if http delivery is selected. + type: basic # `basic`, `bearer` are supported custom_endpoint: true # default is `false`, if `true`, your user will be able to # specify its own endpoint @@ -365,6 +368,29 @@ You can specify your own API name: nelmio_api_doc: name: My API +You can choose between different authentication methods: + + # app/config/config.yml + nelmio_api_doc: + authentication: + delivery: header + name: X-Custom + + # app/config/config.yml + nelmio_api_doc: + authentication: + delivery: query + name: param + + # app/config/config.yml + nelmio_api_doc: + authentication: + delivery: http + type: basic # or bearer + +When choosing an `http` delivery, `name` defaults to `Authorization`, +and the header value will automatically be prefixed by the corresponding type (ie. `Basic` or `Bearer`). + You can specify which sections to exclude from the documentation generation: # app/config/config.yml @@ -440,5 +466,6 @@ nelmio_api_doc: authentication: name: ~ # Required delivery: ~ # Required + type: ~ custom_endpoint: false ``` diff --git a/Resources/views/layout.html.twig b/Resources/views/layout.html.twig index a1dd415..f1e44cf 100644 --- a/Resources/views/layout.html.twig +++ b/Resources/views/layout.html.twig @@ -30,14 +30,17 @@ {% endfor %} - {% if authentication and authentication.delivery in ['query', 'http_basic', 'header'] %} - api key: - {% endif %} - {% if authentication and authentication.delivery in ['http_basic'] %} - api pass: - {% endif %} - {% if authentication and authentication.custom_endpoint %} - api endpoint: + {% if authentication %} + {% if authentication.delivery == 'http' and authentication.type == 'basic' %} + api login: + api password: + {% elseif authentication.delivery in ['query', 'http', 'header'] %} + api key: + {% endif %} + + {% if authentication.custom_endpoint %} + api endpoint: + {% endif %} {% endif %} {% endif %} @@ -419,10 +422,20 @@ headers: headers, crossDomain: true, beforeSend: function (xhr) { - if (authentication_delivery == 'http_basic') { - xhr.setRequestHeader('Authorization', 'Basic ' + btoa($('#api_key').val() + ':' + $('#api_pass').val())); - }else if(authentication_delivery == 'header') { - xhr.setRequestHeader(api_key_parameter, $('#api_key').val()); + if (authentication_delivery) { + var value; + + if ('http' == authentication_delivery) { + if ('basic' == authentication_type) { + value = 'Basic ' + btoa($('#api_login').val() + ':' + $('#api_pass').val()); + } else if ('bearer' == authentication_type) { + value = 'Bearer ' + $('#api_key').val(); + } + } else if ('header' == authentication_delivery) { + value = $('#api_key').val(); + } + + xhr.setRequestHeader(api_key_parameter, value); } }, complete: function(xhr) { @@ -547,8 +560,10 @@ }); - {% if authentication and authentication.delivery == 'http_basic' %} + {% if authentication and authentication.delivery == 'http' %} var authentication_delivery = '{{ authentication.delivery }}'; + var api_key_parameter = '{{ authentication.name }}'; + var authentication_type = '{{ authentication.type }}'; {% elseif authentication and authentication.delivery == 'query' %} var authentication_delivery = '{{ authentication.delivery }}'; var api_key_parameter = '{{ authentication.name }}';