|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Mapping |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from aws_lambda_powertools.utilities.auth_alpha._internal.validation import string_list |
| 7 | +from aws_lambda_powertools.utilities.auth_alpha.jwt.exceptions import ( |
| 8 | + AuthError, |
| 9 | + AuthFailureReason, |
| 10 | + InvalidClaimsError, |
| 11 | + InvalidTokenError, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +class MissingTokenError(InvalidTokenError): |
| 16 | + """No authorization header was supplied.""" |
| 17 | + |
| 18 | + reason = AuthFailureReason.MISSING_TOKEN |
| 19 | + |
| 20 | + |
| 21 | +class ForbiddenError(AuthError): |
| 22 | + """A verified caller does not have permission for this operation.""" |
| 23 | + |
| 24 | + reason = AuthFailureReason.FORBIDDEN |
| 25 | + |
| 26 | + |
| 27 | +class InsufficientScopeError(ForbiddenError): |
| 28 | + """A verified caller is missing a required scope.""" |
| 29 | + |
| 30 | + reason = AuthFailureReason.INSUFFICIENT_SCOPE |
| 31 | + |
| 32 | + |
| 33 | +def bearer_token(value: Any) -> str: |
| 34 | + if value is None: |
| 35 | + raise MissingTokenError() |
| 36 | + if not isinstance(value, str): |
| 37 | + raise InvalidTokenError() |
| 38 | + parts = value.split() |
| 39 | + if len(parts) != 2 or parts[0].lower() != "bearer": |
| 40 | + raise InvalidTokenError() |
| 41 | + return parts[1] |
| 42 | + |
| 43 | + |
| 44 | +def header_token(headers: Any, multi_value_headers: Any = None) -> str: |
| 45 | + values = _authorization_values(headers) |
| 46 | + multi_values = _authorization_values(multi_value_headers) |
| 47 | + if multi_values: |
| 48 | + entries = multi_values[0] |
| 49 | + if not isinstance(entries, list) or len(entries) != 1: |
| 50 | + raise InvalidTokenError() |
| 51 | + if values and values[0] != entries[0]: |
| 52 | + raise InvalidTokenError() |
| 53 | + return bearer_token(entries[0]) |
| 54 | + return bearer_token(values[0] if values else None) |
| 55 | + |
| 56 | + |
| 57 | +def _authorization_values(headers: Any) -> list[Any]: |
| 58 | + if headers is None: |
| 59 | + return [] |
| 60 | + if not isinstance(headers, Mapping): |
| 61 | + raise InvalidTokenError() |
| 62 | + values = [value for name, value in headers.items() if isinstance(name, str) and name.lower() == "authorization"] |
| 63 | + if len(values) > 1: |
| 64 | + raise InvalidTokenError() |
| 65 | + return values |
| 66 | + |
| 67 | + |
| 68 | +def valid_scope(value: str) -> bool: |
| 69 | + return bool(value) and all(33 <= ord(character) <= 126 and character not in {'"', "\\"} for character in value) |
| 70 | + |
| 71 | + |
| 72 | +def required_scopes(scopes: list[str] | None) -> tuple[str, ...]: |
| 73 | + values = string_list(scopes if scopes is not None else []) |
| 74 | + if not all(valid_scope(value) for value in values): |
| 75 | + raise ValueError("Scopes must be valid OAuth scope tokens") |
| 76 | + return values |
| 77 | + |
| 78 | + |
| 79 | +def enforce_scopes(claims: dict[str, Any], expected: tuple[str, ...]) -> None: |
| 80 | + value: Any = next((claims[name] for name in ("scope", "scp", "scopes") if name in claims), []) |
| 81 | + if isinstance(value, str): |
| 82 | + values = [part for part in value.split(" ") if part] |
| 83 | + elif isinstance(value, list): |
| 84 | + values = value |
| 85 | + else: |
| 86 | + raise InvalidClaimsError() |
| 87 | + if any(not isinstance(scope, str) or not valid_scope(scope) for scope in values): |
| 88 | + raise InvalidClaimsError() |
| 89 | + if not set(expected).issubset(values): |
| 90 | + raise InsufficientScopeError() |
0 commit comments