Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic auth authentication in Bottle

Tags:

python

bottle

How can i perform basic authentication in bottle framework? in flask i used to:

def check( username, password ):
    # This function is called to check if a username/password combination is valid
    return username == 'nikos' and password == '******'


def authenticate():
    # Sends a 401 response that enables basic auth
    return Response( 'Credentials of a registered user required!', 401, {'WWW-Authenticate': 'Basic realm="User!"'} )

and called as:

auth = request.authorization
if not auth or not counters.check( auth.username, auth.password ):
    return counters.authenticate()

How can i achieve the same in Bottle framework?

like image 539
Νικόλαος Βέργος Avatar asked Sep 22 '18 23:09

Νικόλαος Βέργος


People also ask

How do I use basic auth in Fetch?

To use basic authentication with Fetch, all you need is a little Base64 encoding and the Authorization header. Try changing the login and password below; values other than “user” and “passwd” will result in a 401 error. Learn new data visualization techniques. Perform complex data analysis.

Where do you store basic authentication?

If you're on Windows, you can put the cert in the user store for the app domain service account. Encrypt the basic auth credentials using the public key of the certificate. Store the encrypted credentials in your database, configuration file, or whatever your REST client uses.

How does basic auth work in Postman?

Basic authentication involves sending a verified username and password with your request. In the request Authorization tab, select Basic Auth from the Type dropdown list. Enter your API username and password in the Username and Password fields. For additional security, store these in variables.


2 Answers

As reported here, Bottle natively contains a decorator that makes Basic Auth pretty straightforward:

from bottle import auth_basic, request, route

def is_authenticated_user(user, password):
    # You write this function. It must return
    # True if user/password is authenticated, or False to deny access.

@route('/')
@auth_basic(is_authenticated_user)
def home():
    return ['hooray, you are authenticated! your info is: {}'.format(request.auth)]
like image 161
ron rothman Avatar answered Oct 17 '22 14:10

ron rothman


Adapted from ron rothman with a basic auth solution using werkzeug.

from bottle import auth_basic, request, route
from werkzeug.security import generate_password_hash, check_password_hash


users = {'user1': generate_password_hash('pwd!')}


def is_authenticated_user(user, password):
    # You write this function. It must return
    # True if user/password is authenticated, or False to deny access.
    return user in users and check_password_hash(users[user], password)

@route('/')
@auth_basic(is_authenticated_user)
def home():
    return ['hooray, you are authenticated! your info is: {}'.format(request.auth)]
like image 31
Ben Avatar answered Oct 17 '22 12:10

Ben