Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logout failed : provide a valid authorization token #14

Open
prashantjeena opened this issue Oct 2, 2019 · 7 comments
Open

logout failed : provide a valid authorization token #14

prashantjeena opened this issue Oct 2, 2019 · 7 comments

Comments

@prashantjeena
Copy link

prashantjeena commented Oct 2, 2019

Screenshot (3)
Screenshot (1)

I reached at this point in my app, but when i tried executing login and logout in localhost login worked fine but logout gave this error : provide a valid authorization token .

Screenshot (2)

Please take a look. Thanks in advance

@recep-yildiz
Copy link

recep-yildiz commented Oct 27, 2019

Hey, I've just solve the logout problem. You should specify an authorization dictionary and give it to Api as a parameter. For that:

Firstly add this ->

authorizations = { 'Basic Auth': { 'type': 'apiKey', 'in': 'header', 'name': 'Authorization' }, }

to the root directory init.py and then add authorizations dictionary as an Api param ->

api = Api(blueprint, doc='/documentation', title='title here', version='1.0', description='a description', security='Basic Auth', authorizations=authorizations )

This makes all api methods required auth. Last thing you should do is replaceing auth_token = data.split(" ")[1] with this: auth_token = data.split(" ")[0] in logout_user function in auth_helper.py, in first if block.

This worked for me, hope this helps you too.

@prashantjeena
Copy link
Author

Hey, I've just solve the logout problem. You should specify an authorization dictionary and give it to Api as a parameter. For that:

Firstly add this ->

authorizations = { 'Basic Auth': { 'type': 'apiKey', 'in': 'header', 'name': 'Authorization' }, }

to the root directory init.py and then add authorizations dictionary as an Api param ->

api = Api(blueprint, doc='/documentation', title='title here', version='1.0', description='a description', security='Basic Auth', authorizations=authorizations )

This makes all api methods required auth. Last thing you should do is replaceing auth_token = data.split(" ")[1] with this: auth_token = data.split(" ")[0] in logout_user function in auth_helper.py, in first if block.

This worked for me, hope this helps you too.

where to add the api line ?
when i added it to the same init file ....it said name Api is not defined.

@prashantjeena
Copy link
Author

prashantjeena commented Oct 28, 2019

this is my init.py file

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt

from .config import config_by_name

db = SQLAlchemy()
flask_bcrypt = Bcrypt()

authorizations = { 'Basic Auth': { 'type': 'apiKey', 'in': 'header', 'name': 'Authorization' }, }
api = Api(blueprint, doc='/documentation', title='title here', version='1.0', description='a description', security='Basic Auth', authorizations=authorizations )
def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config_by_name[config_name])
    db.init_app(app)
    flask_bcrypt.init_app(app)
    return app

@recep-yildiz
Copy link

recep-yildiz commented Oct 28, 2019

No, not into this init.py file, you should put authorizations into other init.py (located in upper package)

There is two file named init.py. One is belong to main package and other one is belong to top package. My name of top package of main is app. So i put that into app's init.py.

You should do the same. Not into inner init.py

You need an init.py file like this:

# app/__init__.py

from flask_restplus import Api
from flask import Blueprint

from .main.controller.user_controller import api as user_ns
from .main.controller.auth_controller import api as auth_ns


blueprint = Blueprint('api', __name__, url_prefix='/api')


authorizations = {
    'Basic Auth': {
        'type': 'apiKey',
        'in': 'header',
        'name': 'Authorization'
    },
}


api = Api(blueprint,
          doc='/documentation',
          title='title here',
          version='1.0',
          description='description here',
          security='Basic Auth',
          authorizations=authorizations
          )

api.add_namespace(user_ns, path='/user')
api.add_namespace(auth_ns)

@ma3achou
Copy link

@prashantjeena !! but you have to set the token in your Header request, see the screenshot

image

@Kpatric
Copy link

Kpatric commented May 26, 2020

Getting this error on auth_helper logout_user method
auth_token = data.split("")[0]
AttributeError: 'dict' object has no attribute 'split'

@ma3achou
Copy link

ma3achou commented May 30, 2020

Getting this error on auth_helper logout_user method
auth_token = data.split("")[0]
AttributeError: 'dict' object has no attribute 'split'

@Kpatric! Check your data param def logout_user(data): if it's a dict type.

EDIT :
I just saw that a blank space is missed in auth_token = data.split("")[0] it have to be auth_token = data.split(" ")[0]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants