|
1 | 1 | import logging |
2 | 2 | import re |
3 | 3 | import time |
| 4 | +import urllib.parse |
4 | 5 | from io import BytesIO |
5 | 6 | from queue import Queue |
6 | 7 |
|
@@ -818,3 +819,95 @@ def post_token(request, context): |
818 | 819 |
|
819 | 820 | assert tokens.access_token == "6cce5-t0k3n" |
820 | 821 | assert len(adapter.request_history) == 2 |
| 822 | + |
| 823 | + |
| 824 | +class TestOidcProviderInfoAuthorizationParameters: |
| 825 | + """Tests for the authorization_parameters feature (openEO API 1.3.0, issue #534)""" |
| 826 | + |
| 827 | + def test_from_dict_with_authorization_parameters(self, requests_mock): |
| 828 | + requests_mock.get("https://authit.test/.well-known/openid-configuration", json={"scopes_supported": ["openid"]}) |
| 829 | + data = { |
| 830 | + "id": "google", |
| 831 | + "title": "Google", |
| 832 | + "issuer": "https://authit.test", |
| 833 | + "scopes": ["openid"], |
| 834 | + "authorization_parameters": {"access_type": "offline", "prompt": "consent"}, |
| 835 | + } |
| 836 | + info = OidcProviderInfo.from_dict(data) |
| 837 | + assert info.authorization_parameters == {"access_type": "offline", "prompt": "consent"} |
| 838 | + |
| 839 | + def test_from_dict_without_authorization_parameters(self, requests_mock): |
| 840 | + requests_mock.get("https://authit.test/.well-known/openid-configuration", json={"scopes_supported": ["openid"]}) |
| 841 | + data = { |
| 842 | + "id": "egi", |
| 843 | + "title": "EGI", |
| 844 | + "issuer": "https://authit.test", |
| 845 | + } |
| 846 | + info = OidcProviderInfo.from_dict(data) |
| 847 | + assert info.authorization_parameters == {} |
| 848 | + |
| 849 | + def test_device_code_request_includes_authorization_parameters(self, requests_mock): |
| 850 | + """Checks whether the authorization_parameters ends up in the device code POST body.""" |
| 851 | + oidc_issuer = "https://authit.test" |
| 852 | + requests_mock.get( |
| 853 | + f"{oidc_issuer}/.well-known/openid-configuration", |
| 854 | + json={ |
| 855 | + "scopes_supported": ["openid"], |
| 856 | + "device_authorization_endpoint": f"{oidc_issuer}/device_code", |
| 857 | + "token_endpoint": f"{oidc_issuer}/token", |
| 858 | + }, |
| 859 | + ) |
| 860 | + device_code_mock = requests_mock.post( |
| 861 | + f"{oidc_issuer}/device_code", |
| 862 | + json={ |
| 863 | + "device_code": "d3v1c3", |
| 864 | + "user_code": "US3R", |
| 865 | + "verification_uri": f"{oidc_issuer}/dc", |
| 866 | + "interval": 5, |
| 867 | + }, |
| 868 | + ) |
| 869 | + provider = OidcProviderInfo( |
| 870 | + issuer=oidc_issuer, |
| 871 | + authorization_parameters={"access_type": "offline", "prompt": "consent"}, |
| 872 | + ) |
| 873 | + authenticator = OidcDeviceAuthenticator( |
| 874 | + client_info=OidcClientInfo(client_id="myclient", provider=provider, client_secret="s3cr3t"), |
| 875 | + ) |
| 876 | + authenticator._get_verification_info() |
| 877 | + |
| 878 | + assert device_code_mock.call_count == 1 |
| 879 | + post_body = urllib.parse.parse_qs(device_code_mock.last_request.text) |
| 880 | + assert post_body["client_id"] == ["myclient"] |
| 881 | + assert post_body["access_type"] == ["offline"] |
| 882 | + assert post_body["prompt"] == ["consent"] |
| 883 | + |
| 884 | + def test_device_code_request_without_authorization_parameters(self, requests_mock): |
| 885 | + """Verify no extra params when authorization_parameters is empty.""" |
| 886 | + oidc_issuer = "https://authit.test" |
| 887 | + requests_mock.get( |
| 888 | + f"{oidc_issuer}/.well-known/openid-configuration", |
| 889 | + json={ |
| 890 | + "scopes_supported": ["openid"], |
| 891 | + "device_authorization_endpoint": f"{oidc_issuer}/device_code", |
| 892 | + "token_endpoint": f"{oidc_issuer}/token", |
| 893 | + }, |
| 894 | + ) |
| 895 | + device_code_mock = requests_mock.post( |
| 896 | + f"{oidc_issuer}/device_code", |
| 897 | + json={ |
| 898 | + "device_code": "d3v1c3", |
| 899 | + "user_code": "US3R", |
| 900 | + "verification_uri": f"{oidc_issuer}/dc", |
| 901 | + "interval": 5, |
| 902 | + }, |
| 903 | + ) |
| 904 | + provider = OidcProviderInfo(issuer=oidc_issuer) |
| 905 | + authenticator = OidcDeviceAuthenticator( |
| 906 | + client_info=OidcClientInfo(client_id="myclient", provider=provider, client_secret="s3cr3t"), |
| 907 | + ) |
| 908 | + authenticator._get_verification_info() |
| 909 | + |
| 910 | + post_body = urllib.parse.parse_qs(device_code_mock.last_request.text) |
| 911 | + assert "access_type" not in post_body |
| 912 | + assert "prompt" not in post_body |
| 913 | + assert set(post_body.keys()) == {"client_id", "scope"} |
0 commit comments