connexion icon indicating copy to clipboard operation
connexion copied to clipboard

request.context['user'] = token_info.get('sub', token_info.get('uid')) - AttributeError: 'str' object has no attribute 'get'

Open gitttt opened this issue 4 years ago • 4 comments

Description

We have a vuejs frontend talking to a connexion-based REST API. We use jwt tokens. Sometimes, without any obvious trigger, we get the following exception:

File "venv/lib/python3.8/site-packages/flask/app.py", line 2464, in call return self.wsgi_app(environ, start_response)
File "venv/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app response = self.handle_exception(e)
File "venv/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception reraise(exc_type, exc_value, tb)
File "venv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise raise value
File "venv/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app response = self.full_dispatch_request()
File "venv/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e)
File "venv/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb)
File "venv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise raise value
File "venv/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() 
File "venv/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request return self.view_functionsrule.endpoint
File "venv/lib/python3.8/site-packages/connexion/decorators/decorator.py", line 48, in wrapper response = function(request)
File "venv/lib/python3.8/site-packages/connexion/decorators/security.py", line 325, in wrapper request.context['user'] = token_info.get('sub', token_info.get('uid'))
AttributeError: 'str' object has no attribute 'get'
172.19.0.9 - - [21/Jul/2020 07:13:34] "GET /v0.0.1/gateway/11 HTTP/1.0" 500 -

Expected behaviour

It works without exception.

Actual behaviour

A 500 error with the above stack trace.

Steps to reproduce

Using the frontend for some time (sorry, can't be more specific)

Additional info:

A possibly related issue may here.

Output of the commands:

  • python --version 3.8.0

  • pip show connexion | grep "^Version\:" Version: 2.6.0

gitttt avatar Jul 21 '20 08:07 gitttt

This may or my not apply to your situation, but I encountered this error when the function I specified for x-bearerInfoFunc returned something other than a dict.

Here is a sample adapted from the JWT example that throws AttributeError: 'bool' object has no attribute 'get' when you try to access /secret with a token (valid or invalid).

openapi.yaml

openapi: 3.0.0
info:
  title: JWT Example
  version: '1.0'
paths:
  /secret:
    get:
      summary: Return secret string
      operationId: app.get_secret
      responses:
        '200':
          description: secret response
          content:
            'text/plain':
              schema:
                type: string
      security:
      - jwt: ['secret']

components:
  securitySchemes:
    jwt:
      type: http
      scheme: bearer
      bearerFormat: JWT
      x-bearerInfoFunc: app.decode_token

app.py

import connexion

def decode_token(token):
    return True


def get_secret(user, token_info) -> str:
    return '''
    You are user_id {user} and the secret is 'wbevuec'.
    Decoded token claims: {token_info}.
    '''.format(user=user, token_info=token_info)


if __name__ == '__main__':
    app = connexion.FlaskApp(__name__)
    app.add_api('openapi.yaml')
    app.run(port=8080)

Tsubashi avatar Jul 24 '20 15:07 Tsubashi

Thank you @Tsubashi , this was the hint that helped me.

My decode_token() function returned a string as part of the Exception handling (e.g. token expired). Would be helpful for others to cover this in the docs imho.

gitttt avatar Jul 30 '20 12:07 gitttt

Thanks for the report @gitttt @Tsubashi. We should clarify in the docs (currently the return type is only documented for Oauth2) and see if we can catch the exception to provide a better error message.

RobbeSneyders avatar Jul 20 '21 08:07 RobbeSneyders

Hi I am facing the same issue. Would there be a fix ?

kishore0549 avatar Jun 17 '22 04:06 kishore0549

Added to documentation in #1764

RobbeSneyders avatar Nov 01 '23 17:11 RobbeSneyders