MCPcopy Index your code
hub / github.com/python-websockets/websockets / parse_authorization_basic

Function parse_authorization_basic

src/websockets/headers.py:527–572  ·  view source on GitHub ↗

Parse an ``Authorization`` header for HTTP Basic Auth. Return a ``(username, password)`` tuple. Args: header: Value of the ``Authorization`` header. Raises: InvalidHeaderFormat: On invalid inputs. InvalidHeaderValue: On unsupported inputs.

(header: str)

Source from the content-addressed store, hash-verified

525
526
527def parse_authorization_basic(header: str) -> tuple[str, str]:
528 """
529 Parse an ``Authorization`` header for HTTP Basic Auth.
530
531 Return a ``(username, password)`` tuple.
532
533 Args:
534 header: Value of the ``Authorization`` header.
535
536 Raises:
537 InvalidHeaderFormat: On invalid inputs.
538 InvalidHeaderValue: On unsupported inputs.
539
540 """
541 # https://datatracker.ietf.org/doc/html/rfc7235#section-2.1
542 # https://datatracker.ietf.org/doc/html/rfc7617#section-2
543 scheme, pos = parse_token(header, 0, "Authorization")
544 if scheme.lower() != "basic":
545 raise InvalidHeaderValue(
546 "Authorization",
547 f"unsupported scheme: {scheme}",
548 )
549 if peek_ahead(header, pos) != " ":
550 raise InvalidHeaderFormat(
551 "Authorization", "expected space after scheme", header, pos
552 )
553 pos += 1
554 basic_credentials, pos = parse_token68(header, pos, "Authorization")
555 parse_end(header, pos, "Authorization")
556
557 try:
558 user_pass = base64.b64decode(basic_credentials.encode()).decode()
559 except binascii.Error:
560 raise InvalidHeaderValue(
561 "Authorization",
562 "expected base64-encoded credentials",
563 ) from None
564 try:
565 username, password = user_pass.split(":", 1)
566 except ValueError:
567 raise InvalidHeaderValue(
568 "Authorization",
569 "expected username:password credentials",
570 ) from None
571
572 return username, password
573
574
575def build_authorization_basic(username: str, password: str) -> str:

Calls 8

parse_tokenFunction · 0.85
InvalidHeaderValueClass · 0.85
peek_aheadFunction · 0.85
InvalidHeaderFormatClass · 0.85
parse_token68Function · 0.85
parse_endFunction · 0.85
decodeMethod · 0.45
encodeMethod · 0.45

Used in the wild real call sites across dependent graphs

searching dependent graphs…