Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Get current_identity outside of the @jwt_required #106

Open
hirecom opened this issue Nov 10, 2016 · 11 comments
Open

Get current_identity outside of the @jwt_required #106

hirecom opened this issue Nov 10, 2016 · 11 comments

Comments

@hirecom
Copy link

hirecom commented Nov 10, 2016

I'd like to get the current_identity in routes where authorization is not required. For instance a list of news items, if they are logged in they can comment, otherwise no commenting allowed.

Is there another way to get the current_identity of a user if they are logged in from a route that's not decorated with @jwt_required?

@jarmani
Copy link
Contributor

jarmani commented Nov 10, 2016

Quick hack by implementing your own decorator :

from functools import wraps
from flask import current_app
from flask_jwt import _jwt_required, current_identity


def jwt_optionnal(realm=None):
    def wrapper(fn):
        @wraps(fn)
        def decorator(*args, **kwargs):
            try:
                _jwt_required(realm or current_app.config['JWT_DEFAULT_REALM'])
            except:
                pass
            return fn(*args, **kwargs)
        return decorator
    return wrapper

then :

from flask_jwt import current_identity


@jwt_optionnal()
def view():
  if current_identity:
    [...]

@hirecom
Copy link
Author

hirecom commented Nov 10, 2016

👍 Wow - thanks! I'll give it a try tonight!

@hirecom
Copy link
Author

hirecom commented Nov 11, 2016

@jarmani - Awesome, this worked great!

@Garito
Copy link

Garito commented Sep 22, 2017

Is this being added to the library?
I would like to have it officially

@vimalloc
Copy link

This library has been abandoned for a while now. Check out flask-jwt-extended (better if you are creating the JWTs in your flask app) or flask-jwt-simple (better if you are consuming tokens from another JWT provider). Both of them have @jwt_optional built into the extension, and are still actively maintained.

Full disclosure, I'm the author of those extensions.

@Garito
Copy link

Garito commented Sep 22, 2017

Thanks

@Garito
Copy link

Garito commented Sep 22, 2017

By the way: could you modify the readme to point this fact?

@vimalloc
Copy link

vimalloc commented Sep 22, 2017

Nope, I don't have access to this repo (Flask-JWT). The owner has been AWAL for a while, and at this point it seems like the project has been entirely abandoned.

@Garito
Copy link

Garito commented Sep 22, 2017

Oh, boy...
That kind of things piss me off a lot
It makes a bunch of people lost their time...
I'm going to put an issue explaining that

Thanks

@aidik
Copy link

aidik commented Jun 8, 2018

@Garito I'm sorry I don't want to start a flame, but I strongly disagree.

The authors of this and in fact any Open Source project are not responsible for anything including your lost time. It was their choice to create the project and they still retain ownership of the project and therefore are able to abandon it (ius dereliquendi) or even destroy it (ius abutendi). It was solely their goodwill that they licensed their work in such a way that you could gain some of the rights of their ownership of this project: use it (ius utendi) and even profit from it (ius fruendi) all at your own risk. Please reevaluate your view of this issue and also do your due diligence; a project without a commit for three years is probably dead.

@Garito
Copy link

Garito commented Jun 9, 2018

Yeah, I totally reevaluate my view: change of library, so long and thanks for all the fish

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

No branches or pull requests

5 participants