[Python] Best approach to auth using JWT in API

Discussão em 'Python' iniciado por Stack, Setembro 25, 2024.

  1. Stack

    Stack Membro Participativo

    I have a small API written in flask. I am authenticating users with JWT token sent in headers. Now what I do is:

    my_func():
    if auth(request.headers):
    # do sth
    else:
    return jsonify("msg": "Unautorized"), 401


    That works for me, but... Is that nice? My idea was to use wrapper, to have something like

    @check_auth(request.headers):
    my_func():
    # do sth


    but of course request is out of scope and it will not work this way.

    Any suggestions on how to make it simple but elegant? Or maybe I'm just trying to overengeneer it and should leave it like it is?

    Continue reading...

Compartilhe esta Página