Skip to main content

Basic Auth Identity Policy for Morepath

Project description

more.basicauth: HTTP Basic Authentication integration for Morepath

Overview

This is a Morepath authentication extension for HTTP Basic Authentication. It was originally part of Morepath, but because it’s not really the best choice, we decide to extract in in a separate extension.

Some Pros and Cons:

Alternative authentication extensions for morepath are:

  • more.jwtauth, a token based authentication system using JSON Web Token (JWT)

  • more.isdangerous, a cookie based identity policy using isdangerous.

Introduction

Basic authentication is special in a number of ways:

  • The HTTP response status that triggers basic auth is Unauthorized (401), not the default Forbidden (403). This needs to be sent back to the browser each time login fails, so that the browser asks the user for a username and a password.

  • The username and password combination is sent to the server by the browser automatically; there is no need to set some type of cookie on the response. Therefore remember_identity does nothing.

  • With basic auth, there is no universal way for a web application to trigger a log out. Therefore forget_identity does nothing either.

To trigger a 401 status when time Morepath raises a 403 status, we can use an exception view, something like this:

from webob.exc import HTTPForbidden

@App.view(model=HTTPForbidden)
def make_unauthorized(self, request):
    @request.after
    def set_status_code(response):
        response.status_code = 401
    return "Unauthorized"

For the login code, as remember_identity is not an option, you can just check the password:

# check whether user has password, using password hash and database
if not user_has_password(username, password):
    return "Sorry, login failed" # or something more fancy

Usage

Here a full example for a basic setup:

import morepath
from more.basicauth import BasicAuthIdentityPolicy
from webob.exc import HTTPForbidden


class App(morepath.App):
    pass


@App.identity_policy()
def get_identity_policy():
    return BasicAuthIdentityPolicy()


@App.verify_identity()
def verify_identity(identity):
    # Do the password validation.
    return user_has_password(identity.username, identity.password)


@App.view(model=HTTPForbidden)
def make_unauthorized(self, request):
    @request.after
    def set_status_code(response):
        response.status_code = 401

    return "Unauthorized"

The login form could just be:

from webob.exc import HTTPProxyAuthenticationRequired


class Login(object):
    pass


@App.path(model=Login, path='login')
def get_login():
    return Login()


@App.view(model=Login, request_method='POST')
def login(self, request):
    username = request.POST['username']
    password = request.POST['password']

    # Do the password validation.
    if not user_has_password(username, password):
        raise HTTPProxyAuthenticationRequired('Invalid username/password')

    return "You're logged in."  # or something more fancy

Requirements

  • Python (2.7, 3.3, 3.4, 3.5)

  • morepath (>= 0.13.2)

CHANGES

0.1 (2016-04-16)

  • Extract Basic Auth from Morepath.

  • Return NO_IDENTITY instead of None, if user cannot identify.

  • Replace class ‘app’ with ‘App’ in tests.

  • Add a login test.

  • Enhance documentation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

more.basicauth-0.1.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

more.basicauth-0.1-py2.py3-none-any.whl (9.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file more.basicauth-0.1.tar.gz.

File metadata

File hashes

Hashes for more.basicauth-0.1.tar.gz
Algorithm Hash digest
SHA256 2e64de5e2290e7016037891e2b452aa0f32d7929e08f98a559cf0e365a2a00c8
MD5 0e29abd0d1b88fa2f42ef67c0005dcde
BLAKE2b-256 40e79cdbec5e08f75453f11e34ff18f04a5fd5f4b122fbb81c60c9534e9f9fdc

See more details on using hashes here.

Provenance

File details

Details for the file more.basicauth-0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for more.basicauth-0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6a90095416599ba5faeca36d28b135d4370708133cf0df2addb1c73d69cc7171
MD5 97730b0ee9c9c56dbf7426455a8eaf20
BLAKE2b-256 5272fbd4813aa4d03c96a6c323a5b1f93c67579bc337133056e06d56423fd91c

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page