Add security requirements handling (#54)

* Add security requirements handling
* Update swagger-ui to 3.9.2, ReDoc to 1.20.0

Closes #39.
This commit is contained in:
Cristi Vîjdea
2018-01-23 12:43:25 +02:00
committed by GitHub
parent f18ff60ae7
commit fc35d9043e
21 changed files with 340 additions and 293 deletions
+1
View File
@@ -31,6 +31,7 @@ SWAGGER_DEFAULTS = {
'type': 'basic'
}
},
'SECURITY_REQUIREMENTS': None,
'LOGIN_URL': getattr(settings, 'LOGIN_URL', None),
'LOGOUT_URL': getattr(settings, 'LOGOUT_URL', None),
'VALIDATOR_URL': '',
-1
View File
@@ -94,7 +94,6 @@ class _OpenAPICodec(object):
:return: swagger spec as dict
:rtype: OrderedDict
"""
swagger.security_definitions = swagger_settings.SECURITY_DEFINITIONS
return swagger.as_odict()
+7 -1
View File
@@ -197,10 +197,16 @@ class OpenAPISchemaGenerator(object):
if url is None and request is not None:
url = request.build_absolute_uri()
return openapi.Swagger(
swagger = openapi.Swagger(
info=self.info, paths=paths,
_url=url, _prefix=prefix, _version=self.version, **dict(components)
)
swagger.security_definitions = swagger_settings.SECURITY_DEFINITIONS
security_requirements = swagger_settings.SECURITY_REQUIREMENTS
if security_requirements is None:
security_requirements = [{security_scheme: [] for security_scheme in swagger_settings.SECURITY_DEFINITIONS}]
swagger.security = security_requirements
return swagger
def create_view(self, callback, method, request=None):
"""Create a view instance from a view callback as registered in urlpatterns.
+13
View File
@@ -27,6 +27,8 @@ class SwaggerAutoSchema(ViewInspector):
operation_id = self.get_operation_id(operation_keys)
description = self.get_description()
security = self.get_security()
assert security is None or isinstance(security, list), "security must be a list of securiy requirement objects"
tags = self.get_tags(operation_keys)
responses = self.get_responses()
@@ -38,6 +40,7 @@ class SwaggerAutoSchema(ViewInspector):
parameters=parameters,
consumes=consumes,
tags=tags,
security=security
)
def get_request_body_parameters(self, consumes):
@@ -286,6 +289,16 @@ class SwaggerAutoSchema(ViewInspector):
description = self._sch.get_description(self.path, self.method)
return description
def get_security(self):
"""Return a list of security requirements for this operation.
Returning an empty list marks the endpoint as unauthenticated (i.e. removes all accepted
authentication schemes). Returning ``None`` will inherit the top-level secuirty requirements.
:return: security requirements
:rtype: list"""
return self.overrides.get('security', None)
def get_tags(self, operation_keys):
"""Get a list of tags for this operation. Tags determine how operations relate with each other, and in the UI
each tag will show as a group containing the operations that use it.
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+6 -2
View File
@@ -13,8 +13,8 @@ no_body = object()
def swagger_auto_schema(method=None, methods=None, auto_schema=None, request_body=None, query_serializer=None,
manual_parameters=None, operation_id=None, operation_description=None, responses=None,
field_inspectors=None, filter_inspectors=None, paginator_inspectors=None,
manual_parameters=None, operation_id=None, operation_description=None, security=None,
responses=None, field_inspectors=None, filter_inspectors=None, paginator_inspectors=None,
**extra_overrides):
"""Decorate a view method to customize the :class:`.Operation` object generated from it.
@@ -62,6 +62,9 @@ def swagger_auto_schema(method=None, methods=None, auto_schema=None, request_bod
:param str operation_id: operation ID override; the operation ID must be unique accross the whole API
:param str operation_description: operation description override
:param str security: security requirements override; used to specify which authetication mechanism
is requried to call this API; an empty list marks the endpoint as unauthenticated (i.e. removes all accepted
authentication schemes), and ``None`` will inherit the top-level secuirty requirements
:param dict[str,(.Schema,.SchemaRef,.Response,str,Serializer)] responses: a dict of documented manual responses
keyed on response status code. If no success (``2xx``) response is given, one will automatically be
generated from the request body and http method. If any ``2xx`` response is given the automatic response is
@@ -94,6 +97,7 @@ def swagger_auto_schema(method=None, methods=None, auto_schema=None, request_bod
'manual_parameters': manual_parameters,
'operation_id': operation_id,
'operation_description': operation_description,
'security': security,
'responses': responses,
'filter_inspectors': list(filter_inspectors) if filter_inspectors else None,
'paginator_inspectors': list(paginator_inspectors) if paginator_inspectors else None,