python pyramid set cookie - redirect url

Viewed 32

I have a python pyramid application with multiple resources, Each resource if its not authenticated need to be redirected to a 3rd party for authentication. The 3rd party(once authenticated) will redirect to a callback URL that I use to extract the user information, once I extract the userinformation I will redirect to the redirect_url that is set in the cookie. I see request.set_cookie is a kind of duplicate code for accessing each resource. Is there a better way to handle this?

def resource_1(request)
    response = HTTPSeeOther(location="/oauth2")
    response.set_cookie('redirect_url', request.url)
    return response

def resource_2(request)
    response = HTTPSeeOther(location="/oauth2")
    response.set_cookie('redirect_url', request.url)
    return response
1 Answers

I believe you can do this using Pyramid's ACL and permission mechanism.

  • Set a permission on view
  • If the user is unauthenticated permission exception is raised
  • You can capture this exception in tweet
  • You can redirect user to the login screen with an URL parameter telling where the user should land post login

Unfortunately I do not have a code example in hand.

Related