Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8999161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:08:36+00:00 2026-06-16T00:08:36+00:00

I used a flask snippet for my flask-login that checks that a user is

  • 0

I used a flask snippet for my flask-login that checks that a user is logged in:

from functools import wraps

def logged_in(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if session.get('logged_in') is not None:
            return f(*args, **kwargs)
        else:
            flash('Please log in first.', 'error')
            return redirect(url_for('login'))
    return decorated_function

And I decorate views like so:

@app.route('/secrets', methods=['GET', 'POST'])
@logged_in
def secrets():
    error = None

I’d like to do something similar for authorization, too. Right now, I have many views to check that a user owns a resource, let’s say the hotdogs resource.

If the logged_in user is the owner of that particular hotdog, he can edit and manage his hotdogs. If he isn’t, I kick him out to the unauthorized screen.

@app.route('/<hotdog>/addmustard/',methods=["GET"])
@logged_in
def addmustard(hotdog):
    if not (authorizeowner(hotdog)):
        return redirect(url_for('unauthorized'))
    do_stuff()

authorizeowner() takes a hotdog as input and checks that the recorded hotdog owner matches the owner name listed in the session variable.

I tried making a owns_hotdog wrapper/decorator function similar to my logged in one, but it complained that it didn’t accept arguments. How can I achieve something similar? Something like…

def owns_hotdog(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if not authorizeowner(hotdog):
            return f(*args, **kwargs)
        else:
            flash('Please log in first.', 'error')
            return redirect(url_for('login'))
    return decorated_function

From the error message, decorator seems not to be receiving the hotdog argument that Flask views have access to from the variable in the route. My hope is for something like…

@app.route('/<hotdog>/addmustard/',methods=["GET"])
@logged_in
@owns_hotdog(hotdog)
def addmustard(hotdog):
    do_stuff()

Everything works with my current authorizeowner(hotdog) function, but it just seems cleaner to have this in place as a wrapper on top of my route, rather than as the first line inside the route.

Some other notes:

  • I know that Flask-Security and Flask-Principal can manage
    authorization for me. Unfortunately, I’m using an unsupported
    database back-end and am unable to use these extensions. So, I’m
    forced to do authentication without them.
  • If you see any glaring holes in doing authorization this way, please let me know!
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:08:37+00:00Added an answer on June 16, 2026 at 12:08 am

    Here’s how to do it:

    from functools import update_wrapper
    
    def owns_hotdog(hotdog):
        def decorator(fn):
            def wrapped_function(*args, **kwargs):
                # First check if user is authenticated.
                if not logged_in():
                    return redirect(url_for('login'))
                # For authorization error it is better to return status code 403
                # and handle it in errorhandler separately, because the user could
                # be already authenticated, but lack the privileges.
                if not authorizeowner(hotdog):
                    abort(403)
                return fn(*args, **kwargs)
            return update_wrapper(wrapped_function, fn)
        return decorator
    
    @app.errorhandler(403)
    def forbidden_403(exception):
        return 'No hotdogs for you!', 403
    

    When decorator takes arguments, it’s not really a decorator, but a factory function which returns the real decorator.

    But if I were you, I would use Flask-Login for authentication and augment it with custom decorators and functions as yours to handle authorization.

    I looked into Flask-Principal, but found it overly complicated for my tastes. Haven’t checked Flask-Security, but I believe it uses Flask-Principal for authorization. Overall I think that Flask-Login with some custom code is enough most of the time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to use Flask and the Flask-Login extension to implement user authentication in
I'm trying to understand how Flask-Login works. I see in their documentation that they
I have a Flask web app, that shows information from a rss feed. I
I'm working on a flask app that needs authentication. I've hooked up flask-login but
How can I specify the port used for the Flask url_for method? Or, can
been a while since i used flash... the minor problem im having is that,
There are many sites claming that adobe flash and flex can be used an
I've embedded a video from YouTube via a snippet I've found on the Internet,
I'm running a Flask-based web app that uses Mongodb (with Pymongo for use in
I understand that flash is quite new to android. Has anyone actually used flash

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.