Comments (4)
I implement by this code
from sanic_jwt.endpoints import AuthenticateEndpoint
from sanic_openapi import doc
class LoginSwDoc:
username = doc.String("Username", required=True)
password = doc.String("Password", required=True)
# add swagger doc to sanic JWT method
AuthenticateEndpoint.decorators.extend([
doc.summary("Authenticate user and get token"),
doc.consumes(LoginSwDoc, location='body'),
doc.produces(LoginSwDoc),
doc.consumes({'AUTHORIZATION': str}, location='header'),
])
from sanic-openapi.
Hi,
tried on sanic 20.9.0 with no success.
Is it supposed to work also on more recent releases ?
from sanic-openapi.
this was my attempt:
def document_auth_bp():
from sanic_jwt.endpoints import RetrieveUserEndpoint, AuthenticateEndpoint, VerifyEndpoint, RefreshEndpoint
inner_me = RetrieveUserEndpoint.get
@doc.summary("returns the user for this JWT Token")
@doc.consumes({'AUTHORIZATION': str}, location='header')
@doc.produces(User)
async def doc_me(self, request, *args, **kwargs):
return await inner_me(self, request, *args, **kwargs)
RetrieveUserEndpoint.get = doc_me
inner_auth = AuthenticateEndpoint.post
@doc.summary("login a user")
@doc.consumes(doc.JsonBody({
"username": doc.String("the username"),
"password": doc.String("the password"),
}), location="body")
@doc.response(401, None, description="invalid credentials")
@doc.response(200, {"access_token": str, "refresh_token": str}, description="the token to use for api interaction")
async def doc_auth(self, request, *args, **kwargs):
return await inner_auth(self, request, *args, **kwargs)
AuthenticateEndpoint.post = doc_auth
inner_verify = VerifyEndpoint.get
@doc.summary("verifies a JWT")
@doc.consumes({'AUTHORIZATION': str}, location='header')
@doc.response(401, None, description="Token is invalid or expired")
@doc.response(200, {"valid": True}, description="Token is valid")
async def doc_verify(self, request, *args, **kwargs):
return await inner_verify(self, request, *args, **kwargs)
VerifyEndpoint.get = doc_verify
inner_refresh = RefreshEndpoint.post
@doc.summary("generates a new access token with the refresh token")
@doc.consumes({'AUTHORIZATION': str}, location='header')
@doc.consumes(doc.JsonBody({
"refresh_token": doc.String("the refresh token"),
}), location="body")
@doc.response(401, None, description="Token is invalid or expired")
@doc.response(200, {"access_token": str}, description="the new access token")
async def doc_refresh(self, request, *args, **kwargs):
return await inner_refresh(self, request, *args, **kwargs)
RefreshEndpoint.post = doc_refresh
document_auth_bp()
jwt = Initialize(
app,
...
)
i feel pretty dirty right now, but it works, would love to see a cleaner solution!
from sanic-openapi.
My solution:
sanicjwt = Initialize(
app,
...
)
for route in sanicjwt.bp.routes:
view = route.handler.view_class
for http_method in route.methods:
h = getattr(view, http_method.lower(), None)
if h:
name = route.handler.__name__
d = doc.route_specs[h]
if name == "AuthenticateEndpoint":
d.summary = "Authenticate user and get token"
if name == "RetrieveUserEndpoint":
d.summary = "Retrieve current user"
if name == "VerifyEndpoint":
d.summary = "Verify token"
if name == "RefreshEndpoint":
d.summary = "Generate new token using refresh token"
Seems a little bit more elegant...
from sanic-openapi.
Related Issues (20)
- Oh, that's bullshit! It's too bad that my project is criticized by my leaders every day because of its running problems HOT 3
- Route already registered: /swagger/<file_uri:/?.+> [GET,HEAD] HOT 3
- pyinstaller may cause problems with sanic and/or sanic-openapi HOT 4
- can not import openapi2_blueprint from sanic_openapi HOT 2
- [UserWarning] Use instance.ctx._spec instead HOT 1
- When I run the following example, the following error occurs HOT 5
- [Feature Request][OpenApi3] Add support for enums HOT 6
- [Bug]can not render all methods with the same url when use routes of Blueprint way HOT 2
- [Bug] Decorator doc/openapi not work when using `routes of HTTPMethodView under Bluebprint instanc` way HOT 1
- [Bug] Swagger cant show path variable HOT 7
- Method POST not allowed for URL HOT 2
- Can't handle duplicate path HOT 1
- [Bug] Even after excluding an endpoint from doc, it still shows the route on swagger, although not the contents of it.
- [Feature Request] Custom Ordering of Endpoints and Authorization HOT 5
- Cannot add `allowed options`/ choices to a query parameter HOT 1
- [Feature Request] disable secure for individual route HOT 4
- distutils package is depricated in python 3.10 HOT 4
- [Feature Request] Create new Release for Sanic 21.12.0 HOT 2
- [Feature Request] Allow default endpoint in OpenAPI V3 Blueprints to be configurable HOT 1
- Duplicate route names error HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sanic-openapi.