Skip to content

HTTP:Authentication

HTTP는 액세스 제어와 인증을 위한 framework 를 제공합니다. 가장 일반적인 인증 방식은 "Basic" 인증 방식입니다. 이 페이지에서는 일반적인 HTTP 인증 framework를 소개하고 서버에 HTTP의 Basic 인증 방식으로 접근을 제한하는 것을 보여 줍니다.

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

Bearer type 경우, 서버에서 지정한 어떠한 문자열도 입력할 수 있습니다. 예를 들어 서버에서 hello-world-token이라는 Bearer 토큰을 인증키로 지정한 경우, 사용자는 다음과 같이 헤더를 구성하여 서버에 전송하면 인증을 받을 수 있습니다.

Authorization: Bearer hello-world-token

Bearer 토큰 인증 방식 경우, 굉장히 허술한 느낌을 받습니다. 이를 보완하고자 쿠버네티스에서 Bearer 토큰을 전송할 때 주로 jwt (JSON Web Token) 토큰을 사용합니다.

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