Refactored authentication config

This commit is contained in:
Emmanuel Vella 2014-07-25 12:08:00 +02:00
parent c03d35bee4
commit 6bc971c50a
4 changed files with 93 additions and 16 deletions

View File

@ -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()

View File

@ -62,6 +62,7 @@
<xsd:complexType name="authentication">
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="delivery" type="authentication_delivery_enum"/>
<xsd:attribute name="type" type="xsd:string"/>
<xsd:attribute name="custom_endpoint" type="xsd:boolean" default="false"/>
</xsd:complexType>

View File

@ -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
```

View File

@ -30,14 +30,17 @@
<option value="{{ header }}"{{ defaultRequestFormat == format ? ' selected' : '' }}>{{ format }}</option>
{% endfor %}
</select>
{% if authentication and authentication.delivery in ['query', 'http_basic', 'header'] %}
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=""/>
{% if authentication %}
{% if authentication.delivery == 'http' and authentication.type == 'basic' %}
api login: <input type="text" id="api_login" value=""/>
api password: <input type="text" id="api_pass" value=""/>
{% elseif authentication.delivery in ['query', 'http', 'header'] %}
api key: <input type="text" id="api_key" value=""/>
{% endif %}
{% if authentication.custom_endpoint %}
api endpoint: <input type="text" id="api_endpoint" value=""/>
{% endif %}
{% endif %}
</div>
{% 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 }}';