any workaround to add token authorization decorator to endpoint at swagger python server stub any workaround to add token authorization decorator to endpoint at swagger python server stub flask flask

any workaround to add token authorization decorator to endpoint at swagger python server stub


Update

Here is a example yaml to use JWT as bearer format:https://github.com/zalando/connexion/blob/master/examples/openapi3/jwt/openapi.yaml

After you generate the flask server, on the swagger-ui you can find the 'Authorize' button. And if you execute /secret before 'Authorize' you will get a 401 error.

So for your situation, you have to change it into:

openapi: 3.0.2info:    title: test api    version: 1.0.0servers:- url: /api/v1/  description: Example API Servicepaths:    /about:        get:            summary: general summary            description: get current version            security:            - jwt: ['secret']            responses:                '200':                    description: About information                    content:                        application/json:                            schema:                                type: stringcomponents:  securitySchemes:    jwt:      type: http      scheme: bearer      bearerFormat: JWT      x-bearerInfoFunc: app.decode_token

Hence, after you have installed connexion[swagger-ui] and start the server by python -m swagger_server. Then, navigate to http://0.0.0.0:8080/api/v1/ui/, you can test the auth works properly. If you call the /about before authorize, it will hit a 401 error.


To add auth from code:

from flask_restx import Apiauthorizations = {    'Bearer Auth': {        'type': 'apiKey',        'in': 'header',        'name': 'Authorization'    },}api = Api(app, security='Bearer Auth', authorizations=authorizations)

Btw, better migrate the flask_restplus into flask_restx, as flask_restplus is no longer be maintained.

Source

https://github.com/noirbizarre/flask-restplus/issues/398#issuecomment-444336893