Bottle.py HTTP Auth?
Asked Answered
J

2

14

How can I get my bottle.py app (Running in Paste or Cherrypy) to do HTTP (basic or digest) authentication? - I need to secure it, but cant find a any HOWTOs.

Jezabella answered 7/11, 2012 at 15:14 Comment(2)
possible duplicate of How to implement user authentication and sessions with PythonWrangler
possible duplicate of Bottle-friendly WSGI authentication library/middlewareStokeontrent
L
26

bottle has a built in auth_basic decorator that can be used on a view:

from bottle import auth_basic, request, route

def check(user, pw):
    # Check user/pw here and return True/False

@route('/')
@auth_basic(check)
def home():
    return { 'data': request.auth }
Lowestoft answered 11/5, 2014 at 12:23 Comment(4)
Can you explain more? I am not sure how I can do check.Wallenstein
That's not a lot of information to go on, I'm afraid. Your check function depends entirely on what you want to do, so it could say e.g. if user == "user" and pw == "hello": return True – though in general of course I would not hard code a password like that!Lowestoft
What happens if you fail the check? Can you decide what is shown?Ailbert
The auth_basic parameters are the function to call, the name of the auth realm, and the body to be displayed in the 401 error, so you can pass it in there.Lowestoft
B
2

There are some libraries on GitHub like https://github.com/FedericoCeratto/bottle-cork that should help. It may be easier to integrate than the repoze library suggested in the related post.

Bitterling answered 7/11, 2012 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.