Skip to main content

bareASGI middleware for SSPI

Project description

bareASGI-sspi

ASGI middleware for the bareASGI framework providing SSPI authentication on Windows.

The implementation uses the pyspnego package.

There is also a generic ASGI server middleware implementation in the package jetblack-asgi-sspi.

Installation

Install from the pie store.

pip install bareasgi-sspi

Usage

The following program uses the Hypercorn ASGI server.

import asyncio
import logging
from typing import Optional

from bareasgi import Application, HttpRequest, HttpResponse
from bareutils import text_writer
from hypercorn import Config
from hypercorn.asyncio import serve

from bareasgi_sspi import add_sspi_middleware, sspi_details

# A callback to display the results of the SSPI middleware.
async def http_request_callback(request: HttpRequest) -> HttpResponse:
    # Get the details from the request context request['sspi']. Note if
    # authentication failed this might be absent or empty.
    sspi = sspi_details(request)
    client_principal = (
        sspi['client_principal']
        if sspi is not None
        else 'unknown'
    )
    return HttpResponse(
        200,
        [(b'content-type', b'text/plain')],
        text_writer(f"Authenticated as '{client_principal}'")
    )


async def main_async():
    # Make the ASGI application using the middleware.
    app = Application()
    app.http_router.add({'GET'}, '/', http_request_callback)

    # Add the middleware. Change the protocol from Negotiate to NTLM,
    # and allow unauthenticated requests to pass through.
    add_sspi_middleware(
        app,
        protocol=b'NTLM',
        forbid_unauthenticated=False
    )

    # Start the ASGI server.
    config = Config()
    config.bind = ['localhost:9023']
    await serve(app, config)

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    asyncio.run(main_async())

Arguments

Optional arguments include:

  • protocol (bytes): Either b"Negotiate" or b"NTLM" (for systems not part of a domain).
  • service (str): The SPN service. Defaults to "HTTP".
  • hostname (str, optional): The hostname. Defaults to he result of socket.gethostname().
  • session_duration (timedelta, optional): The duration of a session. Defaults to 1 hour.
  • forbid_unauthenticated (bool): If true, and authentication fails, send 403 (Forbidden). Otherwise handle the request unauthenticated.
  • context_key (str, optional): The key used in the request context. Defaults to sspi.
  • whitelist (Sequence[str], optional): Paths not to authenticate. Defaults to ().

Results

If the authentication is successful the SSPI details are added to the context dictionary of the HttpRequest object with the key "sspi" (if not overridden). There is a helper method sspi_details for this.

The following properties are set:

  • "client_principal" (str): The username of the client.
  • "negotiated_protocol" (str): The negotiated protocol.
  • "protocol" (str): The requested protocol.
  • "spn" (str): The SPN of the server.

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

bareasgi-sspi-4.2.0rc0.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bareasgi_sspi-4.2.0rc0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file bareasgi-sspi-4.2.0rc0.tar.gz.

File metadata

  • Download URL: bareasgi-sspi-4.2.0rc0.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.14 Darwin/21.6.0

File hashes

Hashes for bareasgi-sspi-4.2.0rc0.tar.gz
Algorithm Hash digest
SHA256 6b001de7152c38f0e04a12b6aacc94376838af6eb8b64ce0fffbf68301464b87
MD5 869e447f5f662bb4167c7b84d8e13a99
BLAKE2b-256 43552351eb0b13b9d585607b27c70f0021687ec197a5fd762bb525ede1041db2

See more details on using hashes here.

File details

Details for the file bareasgi_sspi-4.2.0rc0-py3-none-any.whl.

File metadata

  • Download URL: bareasgi_sspi-4.2.0rc0-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.14 Darwin/21.6.0

File hashes

Hashes for bareasgi_sspi-4.2.0rc0-py3-none-any.whl
Algorithm Hash digest
SHA256 6e5813afdf0efe07618eebb5564b4a36de47a7a5003ed538bf0322a943faf199
MD5 a2ff65cef8ea25cb62c0de15f5cfc64c
BLAKE2b-256 de00d2f75f11d7658b9a468058cf69c51dfcfde819592eca94a8aa0aafda451f

See more details on using hashes here.

Supported by

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