oidcauthlib 3.1.0__tar.gz → 3.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. {oidcauthlib-3.1.0/oidcauthlib.egg-info → oidcauthlib-3.1.1}/PKG-INFO +10 -3
  2. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/README.md +9 -2
  3. oidcauthlib-3.1.1/VERSION +1 -0
  4. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/authorization/admin_access.py +26 -8
  5. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/authorization/client_keys.py +9 -4
  6. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/authorization/client_slugs.py +21 -7
  7. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/authorization/reader_configuration.py +23 -8
  8. oidcauthlib-3.1.1/oidcauthlib/authorization/scopes.py +51 -0
  9. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1/oidcauthlib.egg-info}/PKG-INFO +10 -3
  10. oidcauthlib-3.1.0/VERSION +0 -1
  11. oidcauthlib-3.1.0/oidcauthlib/authorization/scopes.py +0 -30
  12. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/LICENSE +0 -0
  13. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/MANIFEST.in +0 -0
  14. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/__init__.py +0 -0
  15. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/__init__.py +0 -0
  16. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/auth_helper.py +0 -0
  17. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/auth_manager.py +0 -0
  18. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/cache/__init__.py +0 -0
  19. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/cache/oauth_cache.py +0 -0
  20. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/cache/oauth_memory_cache.py +0 -0
  21. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/cache/oauth_mongo_cache.py +0 -0
  22. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/config/__init__.py +0 -0
  23. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/config/auth_config.py +0 -0
  24. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/config/auth_config_reader.py +0 -0
  25. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/dcr/__init__.py +0 -0
  26. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/dcr/dcr_client.py +0 -0
  27. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/dcr/dcr_manager.py +0 -0
  28. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/dcr/dcr_registration.py +0 -0
  29. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/exceptions/__init__.py +0 -0
  30. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/exceptions/authorization_bearer_token_expired_exception.py +0 -0
  31. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/exceptions/authorization_bearer_token_invalid_exception.py +0 -0
  32. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/exceptions/authorization_bearer_token_missing_exception.py +0 -0
  33. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/exceptions/authorization_needed_exception.py +0 -0
  34. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/fastapi_auth_manager.py +0 -0
  35. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/middleware/__init__.py +0 -0
  36. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/middleware/request_scope_middleware.py +0 -0
  37. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/middleware/token_reader_middleware.py +0 -0
  38. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/models/__init__.py +0 -0
  39. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/models/auth.py +0 -0
  40. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/models/base_db_model.py +0 -0
  41. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/models/cache_item.py +0 -0
  42. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/models/client_key_set.py +0 -0
  43. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/models/token.py +0 -0
  44. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/repository/__init__.py +0 -0
  45. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/repository/base_repository.py +0 -0
  46. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/repository/memory/__init__.py +0 -0
  47. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/repository/memory/memory_repository.py +0 -0
  48. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/repository/mongo/__init__.py +0 -0
  49. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/repository/mongo/mongo_repository.py +0 -0
  50. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/repository/repository_factory.py +0 -0
  51. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/routers/__init__.py +0 -0
  52. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/routers/auth_router.py +0 -0
  53. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/token_reader.py +0 -0
  54. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/userinfo/__init__.py +0 -0
  55. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/userinfo/oidc_userinfo_manager.py +0 -0
  56. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/well_known_configuration/__init__.py +0 -0
  57. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/well_known_configuration/auth_server_metadata.py +0 -0
  58. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/well_known_configuration/auth_server_metadata_discovery.py +0 -0
  59. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/well_known_configuration/well_known_configuration_cache.py +0 -0
  60. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/well_known_configuration/well_known_configuration_cache_result.py +0 -0
  61. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/auth/well_known_configuration/well_known_configuration_manager.py +0 -0
  62. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/authorization/__init__.py +0 -0
  63. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/cache_gate/__init__.py +0 -0
  64. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/cache_gate/advisory_lock.py +0 -0
  65. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/cache_gate/version_gate.py +0 -0
  66. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/container/__init__.py +0 -0
  67. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/container/oidc_authlib_container_factory.py +0 -0
  68. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/open_telemetry/__init__.py +0 -0
  69. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/open_telemetry/attribute_names.py +0 -0
  70. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/open_telemetry/configurator.py +0 -0
  71. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/open_telemetry/filtering_sampler.py +0 -0
  72. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/open_telemetry/filtering_span_exporter.py +0 -0
  73. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/open_telemetry/filtering_span_processor.py +0 -0
  74. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/open_telemetry/otel_setup.py +0 -0
  75. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/open_telemetry/span_names.py +0 -0
  76. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/py.typed +0 -0
  77. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/storage/__init__.py +0 -0
  78. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/storage/cache_to_collection_mapper.py +0 -0
  79. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/storage/memory_storage_factory.py +0 -0
  80. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/storage/mongo_gridfs_db.py +0 -0
  81. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/storage/mongo_gridfs_exception.py +0 -0
  82. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/storage/mongo_storage_factory.py +0 -0
  83. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/storage/storage_factory.py +0 -0
  84. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/storage/storage_factory_creator.py +0 -0
  85. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/utilities/__init__.py +0 -0
  86. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/utilities/cached.py +0 -0
  87. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/utilities/environment/__init__.py +0 -0
  88. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/utilities/environment/abstract_environment_variables.py +0 -0
  89. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/utilities/environment/oidc_environment_variables.py +0 -0
  90. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/utilities/logger/__init__.py +0 -0
  91. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/utilities/logger/log_levels.py +0 -0
  92. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/utilities/logger/logging_response.py +0 -0
  93. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/utilities/logger/logging_transport.py +0 -0
  94. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/utilities/mongo_url_utils.py +0 -0
  95. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib/utilities/url_validator.py +0 -0
  96. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib.egg-info/SOURCES.txt +0 -0
  97. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib.egg-info/dependency_links.txt +0 -0
  98. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib.egg-info/requires.txt +0 -0
  99. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/oidcauthlib.egg-info/top_level.txt +0 -0
  100. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/pyproject.toml +0 -0
  101. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/setup.cfg +0 -0
  102. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/tests/test_mongo_url_utils.py +0 -0
  103. {oidcauthlib-3.1.0 → oidcauthlib-3.1.1}/tests/test_simple.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: oidcauthlib
3
- Version: 3.1.0
3
+ Version: 3.1.1
4
4
  Summary: oidcauthlib
5
5
  Author-email: Imran Qureshi <imran.qureshi@bwell.com>
6
6
  License: Apache-2.0
@@ -75,7 +75,10 @@ scopes = await manager.get_scopes_from_user_info_async(access_token=token)
75
75
  Pure helpers that turn a caller's merged scopes (for example the result of `OidcUserInfoManager`) into a
76
76
  tier: `admin` (unrestricted), `reader` (limited to the client keys its `access/<slug>.*` scopes select), or
77
77
  denied. The subpackage reads no environment variable, does no I/O and imports nothing else from
78
- `oidcauthlib`; the caller verifies the token, looks up the user and passes the configuration in.
78
+ `oidcauthlib`; the caller verifies the token, looks up the user and passes the configuration in. The
79
+ `access/<slug>.*` and `patient/` scope convention is the FHIR server's, described in its
80
+ [security guide](https://github.com/icanbwell/fhir-server/blob/main/readme/security.md) (section 5); the tier
81
+ model is recorded in [ADR 0001](adrs/0001-authorization-tiers-from-scopes.md).
79
82
 
80
83
  ```python
81
84
  from oidcauthlib.authorization import (
@@ -110,7 +113,11 @@ access = resolve_admin_access(
110
113
  `reader`; anything else is denied with a `DenialReason`. An empty admin or reader requirement admits nobody.
111
114
  - **Slugs:** `access/<slug>.*` selects the client whose name, stripped and casefolded, equals the slug. Two
112
115
  names that casefold alike make the slug ambiguous and it selects nothing. The suffix must be exactly `.*`.
113
- `access/*.*` selects every client only when `allow_all_tenants=True`.
116
+ `access/*.*` selects every client only when `allow_all_tenants=True`, including a client whose own slug is
117
+ ambiguous. An ambiguous slug is logged by slug only, through the standard `logging` module (the package
118
+ configures no logger of its own).
119
+ - **Blank scopes:** an admin or reader scope list that is empty, or has a blank entry, admits nobody, and
120
+ `validate_reader_configuration` refuses a blank or padded entry in either list.
114
121
  - **No default for "all tenants":** `allowed_client_keys` is `None` only for `admin`. A reader always has a
115
122
  non-empty set and a denied caller always has `frozenset()`, so a denial can never be read as unrestricted.
116
123
  `AdminAccess` enforces this when it is constructed.
@@ -47,7 +47,10 @@ scopes = await manager.get_scopes_from_user_info_async(access_token=token)
47
47
  Pure helpers that turn a caller's merged scopes (for example the result of `OidcUserInfoManager`) into a
48
48
  tier: `admin` (unrestricted), `reader` (limited to the client keys its `access/<slug>.*` scopes select), or
49
49
  denied. The subpackage reads no environment variable, does no I/O and imports nothing else from
50
- `oidcauthlib`; the caller verifies the token, looks up the user and passes the configuration in.
50
+ `oidcauthlib`; the caller verifies the token, looks up the user and passes the configuration in. The
51
+ `access/<slug>.*` and `patient/` scope convention is the FHIR server's, described in its
52
+ [security guide](https://github.com/icanbwell/fhir-server/blob/main/readme/security.md) (section 5); the tier
53
+ model is recorded in [ADR 0001](adrs/0001-authorization-tiers-from-scopes.md).
51
54
 
52
55
  ```python
53
56
  from oidcauthlib.authorization import (
@@ -82,7 +85,11 @@ access = resolve_admin_access(
82
85
  `reader`; anything else is denied with a `DenialReason`. An empty admin or reader requirement admits nobody.
83
86
  - **Slugs:** `access/<slug>.*` selects the client whose name, stripped and casefolded, equals the slug. Two
84
87
  names that casefold alike make the slug ambiguous and it selects nothing. The suffix must be exactly `.*`.
85
- `access/*.*` selects every client only when `allow_all_tenants=True`.
88
+ `access/*.*` selects every client only when `allow_all_tenants=True`, including a client whose own slug is
89
+ ambiguous. An ambiguous slug is logged by slug only, through the standard `logging` module (the package
90
+ configures no logger of its own).
91
+ - **Blank scopes:** an admin or reader scope list that is empty, or has a blank entry, admits nobody, and
92
+ `validate_reader_configuration` refuses a blank or padded entry in either list.
86
93
  - **No default for "all tenants":** `allowed_client_keys` is `None` only for `admin`. A reader always has a
87
94
  non-empty set and a denied caller always has `frozenset()`, so a denial can never be read as unrestricted.
88
95
  `AdminAccess` enforces this when it is constructed.
@@ -0,0 +1 @@
1
+ 3.1.1
@@ -17,7 +17,7 @@ grants the reader tier.
17
17
  """
18
18
 
19
19
  from collections.abc import Iterable, Sequence
20
- from dataclasses import dataclass
20
+ from dataclasses import dataclass, field
21
21
  from enum import StrEnum
22
22
 
23
23
  from oidcauthlib.authorization.client_slugs import (
@@ -25,10 +25,12 @@ from oidcauthlib.authorization.client_slugs import (
25
25
  ClientSlugResolver,
26
26
  slug_from_access_scope,
27
27
  )
28
- from oidcauthlib.authorization.scopes import has_admin_scopes, has_patient_scope
28
+ from oidcauthlib.authorization.scopes import has_admin_scopes, has_blank_scope, has_patient_scope
29
29
 
30
30
 
31
31
  class AdminTier(StrEnum):
32
+ """The tier a granted caller holds; a denied caller has no tier."""
33
+
32
34
  ADMIN = "admin"
33
35
  READER = "reader"
34
36
 
@@ -58,10 +60,14 @@ class AdminAccess:
58
60
  """
59
61
 
60
62
  tier: AdminTier | None
61
- allowed_client_keys: frozenset[str] | None
63
+ # repr=False: client keys identify tenants and stay out of logs and tracebacks.
64
+ allowed_client_keys: frozenset[str] | None = field(repr=False)
62
65
  denial_reason: DenialReason | None
63
66
 
64
67
  def __post_init__(self) -> None:
68
+ if self.allowed_client_keys is not None and not isinstance(self.allowed_client_keys, frozenset):
69
+ # A mutable set would let a caller widen the decision after it was made.
70
+ object.__setattr__(self, "allowed_client_keys", frozenset(self.allowed_client_keys))
65
71
  if self.tier is AdminTier.READER and not self.allowed_client_keys:
66
72
  raise ValueError("a reader needs a non-empty set of client keys it may read")
67
73
  if self.tier is AdminTier.ADMIN and self.allowed_client_keys is not None:
@@ -74,10 +80,12 @@ class AdminAccess:
74
80
 
75
81
  @classmethod
76
82
  def admin(cls) -> "AdminAccess":
83
+ """An unrestricted administrator."""
77
84
  return cls(tier=AdminTier.ADMIN, allowed_client_keys=None, denial_reason=None)
78
85
 
79
86
  @classmethod
80
87
  def reader(cls, *, allowed_client_keys: frozenset[str]) -> "AdminAccess":
88
+ """A tenant reader limited to ``allowed_client_keys`` (which must not be empty)."""
81
89
  return cls(
82
90
  tier=AdminTier.READER,
83
91
  allowed_client_keys=allowed_client_keys,
@@ -86,6 +94,7 @@ class AdminAccess:
86
94
 
87
95
  @classmethod
88
96
  def denied(cls, *, reason: DenialReason) -> "AdminAccess":
97
+ """A denied caller: no tier, an empty key set and the ``reason``."""
89
98
  return cls(tier=None, allowed_client_keys=frozenset(), denial_reason=reason)
90
99
 
91
100
 
@@ -99,17 +108,26 @@ def resolve_admin_access(
99
108
  ) -> AdminAccess:
100
109
  """The tier ``scopes`` (the merged token and userinfo scopes) grant.
101
110
 
102
- ``admin_scopes`` and ``reader_scopes`` are all-of requirements; an empty one
103
- admits nobody. ``allow_all_tenants`` lets ``access/*.*`` select every client
104
- in ``clients``. Never raises.
111
+ ``admin_scopes`` and ``reader_scopes`` are all-of requirements; an empty one,
112
+ or one with a blank entry, admits nobody. Entries of ``scopes`` that are not
113
+ strings are ignored. ``allow_all_tenants`` lets ``access/*.*`` select every
114
+ client in ``clients``, including a client whose own slug is ambiguous. Never
115
+ raises.
116
+
117
+ Args:
118
+ scopes: the merged token and userinfo scopes of the caller.
119
+ admin_scopes: every one of these must be held for the administrator tier.
120
+ reader_scopes: every one of these must be held for the reader tier.
121
+ clients: resolves ``access/<slug>.*`` scopes to client keys.
122
+ allow_all_tenants: whether ``access/*.*`` selects every client.
105
123
  """
106
- held = set(scopes)
124
+ held = {scope for scope in scopes if isinstance(scope, str)}
107
125
  if has_patient_scope(scopes=held):
108
126
  return AdminAccess.denied(reason=DenialReason.PATIENT_SCOPE)
109
127
  if has_admin_scopes(scopes=held, required_scopes=admin_scopes):
110
128
  return AdminAccess.admin()
111
129
  # An empty requirement would admit everyone (``issubset`` of the empty set).
112
- if not reader_scopes or not set(reader_scopes).issubset(held):
130
+ if not reader_scopes or has_blank_scope(scopes=reader_scopes) or not set(reader_scopes).issubset(held):
113
131
  return AdminAccess.denied(reason=DenialReason.NOT_A_READER)
114
132
  slugs = {slug for scope in held if (slug := slug_from_access_scope(scope=scope)) is not None}
115
133
  if not slugs:
@@ -13,7 +13,6 @@ Error messages name the rule that failed, never a client name or a key.
13
13
  """
14
14
 
15
15
  import json
16
- from typing import Any
17
16
 
18
17
 
19
18
  class ClientKeysFormatError(ValueError):
@@ -25,6 +24,9 @@ def parse_client_keys_lenient(*, raw: str) -> dict[str, str]:
25
24
 
26
25
  An empty value gives ``{}``. A JSON object parses as is; invalid JSON falls
27
26
  back to comma parsing. In the comma form, an entry without ``=`` is dropped.
27
+
28
+ Args:
29
+ raw: the setting's value, a JSON object or a comma-separated ``name=key`` list.
28
30
  """
29
31
  if not raw:
30
32
  return {}
@@ -45,6 +47,9 @@ def parse_client_keys_lenient(*, raw: str) -> dict[str, str]:
45
47
  def parse_client_keys_strict(*, raw: str) -> dict[str, str]:
46
48
  """Parse the mapping and refuse anything the lenient parser would repair.
47
49
 
50
+ Args:
51
+ raw: the setting's value, a JSON object or a comma-separated ``name=key`` list.
52
+
48
53
  Raises:
49
54
  ClientKeysFormatError: invalid JSON (no fall-back to comma parsing), a
50
55
  JSON value that is not an object of strings, duplicate JSON keys, a
@@ -71,12 +76,12 @@ def parse_client_keys_strict(*, raw: str) -> dict[str, str]:
71
76
 
72
77
 
73
78
  def _json_pairs(text: str) -> list[tuple[str, str]]:
79
+ # The caller passes text that starts with "{", so a successful parse is always an
80
+ # object, and ``object_pairs_hook`` gives its (name, value) pairs in order.
74
81
  try:
75
- parsed: Any = json.loads(text, object_pairs_hook=lambda pairs: pairs)
82
+ parsed: list[tuple[str, object]] = json.loads(text, object_pairs_hook=lambda pairs: pairs)
76
83
  except json.JSONDecodeError as exc:
77
84
  raise ClientKeysFormatError("the value is not valid JSON") from exc
78
- if not isinstance(parsed, list):
79
- raise ClientKeysFormatError("the JSON value is not an object")
80
85
  pairs: list[tuple[str, str]] = []
81
86
  for position, (name, key) in enumerate(parsed, start=1):
82
87
  if not isinstance(key, str):
@@ -13,6 +13,8 @@ from collections.abc import Callable, Mapping
13
13
  from dataclasses import dataclass, field
14
14
  from types import MappingProxyType
15
15
 
16
+ from oidcauthlib.authorization.scopes import normalize_scope
17
+
16
18
  logger = logging.getLogger(__name__)
17
19
 
18
20
  ACCESS_SCOPE_PREFIX = "access/"
@@ -23,7 +25,7 @@ WILDCARD_SLUG = "*"
23
25
 
24
26
  def normalize_slug(*, slug: str) -> str:
25
27
  """Strip and casefold a slug or a client name so they compare equal."""
26
- return slug.strip().casefold()
28
+ return normalize_scope(scope=slug)
27
29
 
28
30
 
29
31
  def slug_from_access_scope(*, scope: str) -> str | None:
@@ -32,8 +34,11 @@ def slug_from_access_scope(*, scope: str) -> str | None:
32
34
  ``access/tenant.*`` gives ``tenant`` and ``access/*.*`` gives ``*``. The suffix
33
35
  must be exactly ``.*``: ``access/demo.read``, ``access/*.read``, ``access/demo``
34
36
  and an empty slug (``access/.*``) all give ``None``.
37
+
38
+ Args:
39
+ scope: any scope string.
35
40
  """
36
- normalized = scope.strip().casefold()
41
+ normalized = normalize_scope(scope=scope)
37
42
  if not normalized.startswith(ACCESS_SCOPE_PREFIX):
38
43
  return None
39
44
  if not normalized.endswith(ACCESS_SCOPE_SUFFIX):
@@ -45,10 +50,16 @@ def slug_from_access_scope(*, scope: str) -> str | None:
45
50
 
46
51
  @dataclass(frozen=True)
47
52
  class ClientSlugResolver:
48
- """Slug to its one client key, and every eligible client key."""
53
+ """Slug to its one client key, and every eligible client key.
49
54
 
50
- keys_by_slug: Mapping[str, str] = field(default_factory=lambda: MappingProxyType({}))
51
- all_keys: frozenset[str] = frozenset()
55
+ ``all_keys`` is what ``access/*.*`` selects when a caller opts in. It holds every
56
+ eligible client's key, including the keys of clients whose slug is ambiguous (which
57
+ ``keys_for`` refuses one by one). Client keys identify tenants, so both fields stay
58
+ out of ``repr``.
59
+ """
60
+
61
+ keys_by_slug: Mapping[str, str] = field(default_factory=lambda: MappingProxyType({}), repr=False)
62
+ all_keys: frozenset[str] = field(default=frozenset(), repr=False)
52
63
 
53
64
  @classmethod
54
65
  def from_client_keys(
@@ -60,7 +71,8 @@ class ClientSlugResolver:
60
71
  """Build the resolver from a ``name -> key`` map.
61
72
 
62
73
  Args:
63
- clients: the client-key mapping. Entries with a blank name or key are
74
+ clients: the client-key mapping. Entries with a blank name or key, or a
75
+ key that is not a string (for example a JSON ``null`` or number), are
64
76
  skipped.
65
77
  is_eligible: optional strategy ``(name, key) -> bool``. A client it
66
78
  rejects is in neither ``keys_by_slug`` nor ``all_keys``, and does
@@ -68,8 +80,10 @@ class ClientSlugResolver:
68
80
  """
69
81
  keys_by_slug_candidates: dict[str, list[str]] = {}
70
82
  for name, key in clients.items():
83
+ if not isinstance(key, str):
84
+ continue
71
85
  slug = normalize_slug(slug=str(name))
72
- clean_key = str(key).strip()
86
+ clean_key = key.strip()
73
87
  if not slug or not clean_key:
74
88
  continue
75
89
  if is_eligible is not None and not is_eligible(str(name), clean_key):
@@ -11,10 +11,12 @@ from oidcauthlib.authorization.client_keys import (
11
11
  parse_client_keys_strict,
12
12
  )
13
13
  from oidcauthlib.authorization.client_slugs import ACCESS_SCOPE_PREFIX, WILDCARD_SLUG
14
- from oidcauthlib.authorization.scopes import PATIENT_SCOPE_PREFIX
14
+ from oidcauthlib.authorization.scopes import PATIENT_SCOPE_PREFIX, normalize_scope
15
15
 
16
16
  # Scopes every token carries; as a reader requirement they would admit everyone.
17
17
  GENERIC_SCOPES = frozenset({"openid", "profile", "email", "groups"})
18
+ # A reader scope with this suffix would read like the all-tenants access scope.
19
+ WILDCARD_SCOPE_SUFFIX = "*.*"
18
20
 
19
21
 
20
22
  class ReaderConfigurationError(ValueError):
@@ -27,10 +29,20 @@ def validate_reader_configuration(
27
29
  admin_scopes: Sequence[str],
28
30
  client_keys_raw: str,
29
31
  ) -> None:
30
- """Refuse an unsafe reader-tier configuration. A no-op when no reader scopes are set.
32
+ """Refuse an unsafe reader-tier configuration.
33
+
34
+ A blank or padded entry in either scope list is always refused, because scopes
35
+ match exactly and a blank requirement is satisfiable by a blank held scope. The
36
+ remaining checks are skipped when no reader scopes are set.
37
+
38
+ Args:
39
+ reader_scopes: the scopes that together grant the reader tier.
40
+ admin_scopes: the scopes that together grant the administrator tier.
41
+ client_keys_raw: the raw client-key mapping setting.
31
42
 
32
43
  Raises:
33
- ReaderConfigurationError: when reader scopes are set and the client-key
44
+ ReaderConfigurationError: when either scope list has a blank entry or an
45
+ entry with surrounding whitespace; when reader scopes are set and the client-key
34
46
  mapping fails the strict parser, is empty, or has a name containing
35
47
  ``*``; a reader scope overlaps an administrator scope, starts with
36
48
  ``access/`` or ``patient/``, is generic (``openid``, ``profile``,
@@ -38,6 +50,9 @@ def validate_reader_configuration(
38
50
  starts with ``access/`` (a reader holding ``access/*.*`` would then be
39
51
  an administrator).
40
52
  """
53
+ for scopes in (reader_scopes, admin_scopes):
54
+ if any(scope != scope.strip() or not scope for scope in scopes):
55
+ raise ReaderConfigurationError("a scope is blank or has surrounding whitespace")
41
56
  if not reader_scopes:
42
57
  return
43
58
  try:
@@ -50,9 +65,9 @@ def validate_reader_configuration(
50
65
  raise ReaderConfigurationError(
51
66
  f"a client name contains '{WILDCARD_SLUG}', which collides with the wildcard slug"
52
67
  )
53
- admin_normalized = {scope.strip().casefold() for scope in admin_scopes}
68
+ admin_normalized = {normalize_scope(scope=scope) for scope in admin_scopes}
54
69
  for scope in reader_scopes:
55
- normalized = scope.strip().casefold()
70
+ normalized = normalize_scope(scope=scope)
56
71
  if normalized in admin_normalized:
57
72
  raise ReaderConfigurationError("a reader scope overlaps an administrator scope")
58
73
  if normalized.startswith(ACCESS_SCOPE_PREFIX) or normalized.startswith(PATIENT_SCOPE_PREFIX):
@@ -61,9 +76,9 @@ def validate_reader_configuration(
61
76
  )
62
77
  if normalized in GENERIC_SCOPES:
63
78
  raise ReaderConfigurationError("a reader scope is a generic scope every token carries")
64
- if normalized.endswith("*.*"):
65
- raise ReaderConfigurationError("a reader scope ends in '*.*'")
66
- if any(scope.strip().casefold().startswith(ACCESS_SCOPE_PREFIX) for scope in admin_scopes):
79
+ if normalized.endswith(WILDCARD_SCOPE_SUFFIX):
80
+ raise ReaderConfigurationError(f"a reader scope ends in '{WILDCARD_SCOPE_SUFFIX}'")
81
+ if any(normalize_scope(scope=scope).startswith(ACCESS_SCOPE_PREFIX) for scope in admin_scopes):
67
82
  raise ReaderConfigurationError(
68
83
  f"an administrator scope starts with '{ACCESS_SCOPE_PREFIX}': "
69
84
  "a reader holding the wildcard access scope would be an administrator"
@@ -0,0 +1,51 @@
1
+ """Scope predicates: the patient deny and the all-of match on required scopes."""
2
+
3
+ from collections.abc import Collection, Iterable
4
+
5
+ # A token carrying any patient-scoped grant is a patient or patient-app token and
6
+ # is never an administrator, even if it also holds an administrator scope. The
7
+ # check is a prefix match on the trimmed, casefolded scope, so ``Patient/*.read``
8
+ # matches but ``Patient.read`` (no slash) does not.
9
+ PATIENT_SCOPE_PREFIX = "patient/"
10
+
11
+
12
+ def normalize_scope(*, scope: str) -> str:
13
+ """Strip and casefold a scope so scopes that differ only by padding or case compare equal."""
14
+ return scope.strip().casefold()
15
+
16
+
17
+ def has_blank_scope(*, scopes: Iterable[str]) -> bool:
18
+ """True when any scope is empty or whitespace only.
19
+
20
+ A blank required scope, for example the ``[""]`` that ``"".split(",")`` gives for an
21
+ unset setting, is satisfied by any held scope list that contains ``""`` (a token
22
+ scope claim split on single spaces with a double or trailing space does). Callers
23
+ treat a requirement list with a blank entry as unsatisfiable.
24
+ """
25
+ return any(not scope.strip() for scope in scopes)
26
+
27
+
28
+ def has_patient_scope(*, scopes: Iterable[str]) -> bool:
29
+ """True when any scope starts with ``patient/`` (trimmed, case-insensitive)."""
30
+ return any(normalize_scope(scope=scope).startswith(PATIENT_SCOPE_PREFIX) for scope in scopes)
31
+
32
+
33
+ def has_admin_scopes(*, scopes: Iterable[str], required_scopes: Collection[str]) -> bool:
34
+ """True when ``scopes`` holds every required scope exactly and no ``patient/`` scope.
35
+
36
+ An empty ``required_scopes`` is always False: ``set().issubset(anything)`` is
37
+ True, so an empty requirement (for example an unset setting) would otherwise
38
+ admit every verified token. A requirement list with a blank entry is False for
39
+ the same reason (see ``has_blank_scope``). This is the one check every
40
+ administrator path applies to the merged scope set.
41
+
42
+ Args:
43
+ scopes: the scopes the caller holds.
44
+ required_scopes: every one of these must be held, matched exactly.
45
+ """
46
+ if not required_scopes or has_blank_scope(scopes=required_scopes):
47
+ return False
48
+ held = set(scopes)
49
+ if has_patient_scope(scopes=held):
50
+ return False
51
+ return set(required_scopes).issubset(held)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: oidcauthlib
3
- Version: 3.1.0
3
+ Version: 3.1.1
4
4
  Summary: oidcauthlib
5
5
  Author-email: Imran Qureshi <imran.qureshi@bwell.com>
6
6
  License: Apache-2.0
@@ -75,7 +75,10 @@ scopes = await manager.get_scopes_from_user_info_async(access_token=token)
75
75
  Pure helpers that turn a caller's merged scopes (for example the result of `OidcUserInfoManager`) into a
76
76
  tier: `admin` (unrestricted), `reader` (limited to the client keys its `access/<slug>.*` scopes select), or
77
77
  denied. The subpackage reads no environment variable, does no I/O and imports nothing else from
78
- `oidcauthlib`; the caller verifies the token, looks up the user and passes the configuration in.
78
+ `oidcauthlib`; the caller verifies the token, looks up the user and passes the configuration in. The
79
+ `access/<slug>.*` and `patient/` scope convention is the FHIR server's, described in its
80
+ [security guide](https://github.com/icanbwell/fhir-server/blob/main/readme/security.md) (section 5); the tier
81
+ model is recorded in [ADR 0001](adrs/0001-authorization-tiers-from-scopes.md).
79
82
 
80
83
  ```python
81
84
  from oidcauthlib.authorization import (
@@ -110,7 +113,11 @@ access = resolve_admin_access(
110
113
  `reader`; anything else is denied with a `DenialReason`. An empty admin or reader requirement admits nobody.
111
114
  - **Slugs:** `access/<slug>.*` selects the client whose name, stripped and casefolded, equals the slug. Two
112
115
  names that casefold alike make the slug ambiguous and it selects nothing. The suffix must be exactly `.*`.
113
- `access/*.*` selects every client only when `allow_all_tenants=True`.
116
+ `access/*.*` selects every client only when `allow_all_tenants=True`, including a client whose own slug is
117
+ ambiguous. An ambiguous slug is logged by slug only, through the standard `logging` module (the package
118
+ configures no logger of its own).
119
+ - **Blank scopes:** an admin or reader scope list that is empty, or has a blank entry, admits nobody, and
120
+ `validate_reader_configuration` refuses a blank or padded entry in either list.
114
121
  - **No default for "all tenants":** `allowed_client_keys` is `None` only for `admin`. A reader always has a
115
122
  non-empty set and a denied caller always has `frozenset()`, so a denial can never be read as unrestricted.
116
123
  `AdminAccess` enforces this when it is constructed.
oidcauthlib-3.1.0/VERSION DELETED
@@ -1 +0,0 @@
1
- 3.1.0
@@ -1,30 +0,0 @@
1
- """Scope predicates: the patient deny and the all-of match on required scopes."""
2
-
3
- from collections.abc import Collection, Iterable
4
-
5
- # A token carrying any patient-scoped grant is a patient or patient-app token and
6
- # is never an administrator, even if it also holds an administrator scope. The
7
- # check is a prefix match on the trimmed, casefolded scope, so ``Patient/*.read``
8
- # matches but ``Patient.read`` (no slash) does not.
9
- PATIENT_SCOPE_PREFIX = "patient/"
10
-
11
-
12
- def has_patient_scope(*, scopes: Iterable[str]) -> bool:
13
- """True when any scope starts with ``patient/`` (trimmed, case-insensitive)."""
14
- return any(scope.strip().casefold().startswith(PATIENT_SCOPE_PREFIX) for scope in scopes)
15
-
16
-
17
- def has_admin_scopes(*, scopes: Iterable[str], required_scopes: Collection[str]) -> bool:
18
- """True when ``scopes`` holds every required scope exactly and no ``patient/`` scope.
19
-
20
- An empty ``required_scopes`` is always False: ``set().issubset(anything)`` is
21
- True, so an empty requirement (for example an unset setting) would otherwise
22
- admit every verified token. This is the one check every administrator path
23
- applies to the merged scope set.
24
- """
25
- if not required_scopes:
26
- return False
27
- held = set(scopes)
28
- if has_patient_scope(scopes=held):
29
- return False
30
- return set(required_scopes).issubset(held)
File without changes
File without changes
File without changes
File without changes