django-rest-swagger
django-rest-swagger copied to clipboard
How to include Oauth2 auth urls in Swagger?
Hey guys,
I use Django Oauth Toolkit for a Oauth2 password flow. Unfortunately, Swagger doesn't recognize the auth urls.
This is my urls.py:
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
urlpatterns = [
url(r'^oauth2/', include('oauth2_provider.urls', namespace='oauth2_provider')),
url(r'^v1/docs/$', get_swagger_view(title='My API'), name='api_schema'),
url(r'^v1/', include(router.urls)),
url(r'^v1/auth/', include('rest_framework.urls', namespace='rest_framework'))
How can I fix that?
@lggwettmann do you know how to do it?
I ended up writing the open api specification manually with a tool called stoplight. I added the oauth2 endpoints myself. I recommend just writing it yourself as it will be much faster than fixing the file created with django-rest-swagger. There is a new django package drf-yasg https://github.com/axnsan12/drf-yasg which seems to do a better job. Just make sure you hit their requirements. I haven't tried it out yet.
This is how my auth path looks like now:
"/api/oauth2/token/": {
"post": {
"summary": "get token",
"description": "Triggers the log-in flow to receive access tokens.",
"consumes": [
"application/x-www-form-urlencoded"
],
"parameters": [
{
"type": "string",
"default": "",
"name": "client_id",
"required": true,
"in": "formData"
},
{
"type": "string",
"default": "",
"name": "username",
"in": "formData"
},
{
"type": "string",
"default": "",
"name": "password",
"in": "formData"
},
{
"type": "string",
"default": "",
"name": "client_secret",
"required": true,
"in": "formData"
},
{
"type": "string",
"default": "password",
"name": "grant_type",
"required": true,
"in": "formData"
},
{
"in": "formData",
"name": "refresh_token",
"type": "string"
}
],
"responses": {
"200": {
"description": "Login response with access token.",
"schema": {
"type": "object",
"properties": {
"access_token": {
"type": "string"
},
"expires_in": {
"type": "number",
"format": "float"
},
"token_type": {
"type": "string"
},
"scope": {
"type": "string"
},
"refresh_token": {
"type": "string"
}
}
}
},
"400": {
"description": "Workflow failure",
"schema": {
"type": "object",
"properties": {
"status": {
"type": "string",
"description": "Outcome of the workflow"
},
"message": {
"type": "string",
"description": "Error message for the workflow"
}
}
}
},
"401": {
"description": "Unauthorized",
"schema": {
"type": "object",
"properties": {
"error": {
"type": "string",
"description": "Short message for the log in action."
},
"error_description": {
"type": "string",
"description": "Error message for the log in action."
}
}
}
},
"500": {
"description": "Internal bug. Please file a bug report at bubble.is/bug_report with the request that triggers this bug",
"schema": {
"type": "object",
"properties": {
"code": {
"type": "string",
"description": "Error code"
},
"message": {
"type": "string",
"description": "Error message for the operation"
}
}
}
}
},
"security": [
{}
],
"operationId": "get_token"
}
},
I added the oauth2 endpoints myself.
@lggwettmann thanks so much! I just wonder how to render your custom json schema as additional endpoints in my swagger doc.
I just downloaded the generated swagger file and added the oauth path manually. It's super crappy. So that would be definitely a nice addition to django-swagger: adding custom json schema info.
and added the oauth path manually
Did you create an special view for this? Where should I add this? Sorry for being intrusive.
Sorry for me not being clear. I did not make it work. I set up all my views in django-rest-swagger, downloaded the file from the UI and manually entered the json schema provided above. No views, no templates, nothing automatic. Just you writing a json schema in the code editor of choice, I'm afraid.
Thanks @lggwettmann I was using swagger.json to deploy API gateway but was struggling to add oauth2 urls. Manually adding JSON object helped me but I am still looking for a better solution as I cannot add these objects every time when my swagger document will be updated.