Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class BaseBearerTokenAuthenticationProvider(AuthenticationProvider):
CLAIMS_KEY = "claims"

def __init__(self, access_token_provider: AccessTokenProvider) -> None:
if not access_token_provider:
raise ValueError("Access token provider cannot be null")
self.access_token_provider = access_token_provider

async def authenticate_request(
Expand All @@ -35,6 +37,10 @@ async def authenticate_request(
if not request:
raise ValueError("Request cannot be null")

validator = self.access_token_provider.get_allowed_hosts_validator()
if validator and not validator.is_url_host_valid(request.url):
return

if request.headers is None:
request.headers = HeadersCollection()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import pytest

from kiota_abstractions.authentication import BaseBearerTokenAuthenticationProvider
from kiota_abstractions.request_information import RequestInformation


def test_initialization(mock_access_token_provider):
auth = BaseBearerTokenAuthenticationProvider(mock_access_token_provider)
assert auth.access_token_provider == mock_access_token_provider


def test_initialization_null_provider_raises_value_error():
with pytest.raises(ValueError, match="^Access token provider cannot be null$"):
BaseBearerTokenAuthenticationProvider(None)


@pytest.mark.asyncio
async def test_authenticate_request_null_request_information(mock_access_token_provider):
auth = BaseBearerTokenAuthenticationProvider(mock_access_token_provider)
Expand Down Expand Up @@ -81,3 +87,28 @@ async def test_authenticate_request_preserves_existing_authorization_without_cla
await auth.authenticate_request(mock_request_information)

assert mock_request_information.headers.get("Authorization") == {"Bearer ExistingToken"}


@pytest.mark.asyncio
async def test_authenticate_request_unallowed_host_returns_early(mock_access_token_provider):
auth = BaseBearerTokenAuthenticationProvider(mock_access_token_provider)
request = RequestInformation()
request.url = "https://unallowed.com/me"
await auth.authenticate_request(request)

assert not request.headers.contains("Authorization")


@pytest.mark.asyncio
async def test_authenticate_request_unallowed_host_preserves_existing_headers_with_claims(
mock_access_token_provider,
):
auth = BaseBearerTokenAuthenticationProvider(mock_access_token_provider)
request = RequestInformation()
request.url = "https://unallowed.com/me"
request.headers.add("Authorization", "Bearer ExistingToken")
await auth.authenticate_request(
request, additional_authentication_context={"claims": "some_claims"}
)

assert request.headers.get("Authorization") == {"Bearer ExistingToken"}
Loading