Skip to content
Search

HTTP:Authentication

lastmod 2024-01-09created 2017-12-0716 edits

HTTP provides a framework for access control and authentication. The most common authentication scheme is the "Basic" authentication scheme. This page introduces the general HTTP authentication framework and shows how to restrict access to a server with HTTP's Basic authentication scheme.

Authentication schemes

  • Basic access authentication (see RFC 7617, base64-encoded credentials. See below for more information.),
  • Bearer (see RFC 6750, bearer tokens to access OAuth 2.0-protected resources),
  • Digest access authentication (HTTP Digest) (see RFC 7616, only md5 hashing is supported in Firefox, see bug 472823 for SHA encryption support),
  • vHOBA (see RFC 7486 (draft), HTTP Origin-Bound Authentication, digital-signature-based),
  • Mutual Authentication Protocol for HTTP (see draft-ietf-httpauth-mutual),
  • AWS4-HMAC-SHA256 (see AWS docs).
  • NT LAN Manager (NTLM)
  • Negotiate

Basic

Python example

# -*- coding: utf-8 -*-

import binascii
from base64 import b64decode, b64encode


class BasicAuth:
    """
    Http basic authentication.
    """

    TYPE = "Basic"
    TYPE_LOWER = TYPE.lower()

    def __init__(self, login: str, password="", encoding="latin1"):
        if ":" in login:
            raise ValueError('A ":" is not allowed in login RFC 1945#section-11.1)')

        self.login = login
        self.password = password
        self.encoding = encoding

    @classmethod
    def decode_from_authorization_header(cls, auth_header, encoding="latin1"):
        """
        Create a :class:`BasicAuth` object from an ``Authorization`` HTTP header.
        """

        split = auth_header.strip().split(" ")
        if len(split) != 2:
            raise ValueError("Could not parse authorization header.")

        if split[0].strip().lower() != cls.TYPE_LOWER:
            raise ValueError(f"Unknown authorization method {split[0]}")

        to_decode = split[1].strip()

        try:
            username, _, password = (
                b64decode(to_decode.encode("ascii")).decode(encoding).partition(":")
            )
        except binascii.Error:
            raise ValueError("Invalid base64 encoding.")

        return cls(username, password, encoding)

    def encode(self):
        """
        Encode credentials.
        """

        credentials = f"{self.login}:{self.password}".encode(self.encoding)
        encoded_credentials = b64encode(credentials).decode(self.encoding)
        return f"{self.TYPE} {encoded_credentials}"

Bearer

With the Bearer type, any string specified by the server can be used. For example, if the server specifies a Bearer token called hello-world-token as the authentication key, the user can be authenticated by building the header as follows and sending it to the server.

Authorization: Bearer hello-world-token

The Bearer token authentication scheme feels very loose. To compensate for this, Kubernetes mainly uses jwt (JSON Web Token) tokens when sending Bearer tokens.

Keywords

  • WWW-Authenticate
  • Authorization
  • Proxy-Authorization
  • Proxy-Authenticate
  • 401, 403, 407

See also

Favorite site

References


  1. C-612.tistory.com_-HTTP_Authorization-_Basic.pdf ↩

author Zer0 sitemap