Skip to content

Commit 2ed58aa

Browse files
authored
Merge pull request #49 from alan-turing-institute/46-restrict-users-to-selected-domain
Restrict users to selected domain
2 parents eaa4d51 + ebb79a2 commit 2ed58aa

File tree

2 files changed

+75
-68
lines changed

2 files changed

+75
-68
lines changed

apricot/oauth/oauth_data_adaptor.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __init__(
4747
# Retrieve and validate user and group information
4848
annotated_groups, annotated_users = self._retrieve_entries()
4949
self.validated_groups = self._validate_groups(annotated_groups)
50-
self.validated_users = self._validate_users(annotated_users)
50+
self.validated_users = self._validate_users(annotated_users, domain)
5151
if self.debug:
5252
log.msg(
5353
f"Validated {len(self.validated_groups)} groups and {len(self.validated_users)} users.",
@@ -195,34 +195,41 @@ def _validate_groups(
195195
)
196196
except ValidationError as exc:
197197
name = group_dict.get("cn", "unknown")
198-
log.msg(f"Validation failed for group '{name}'.")
198+
log.msg(f"... group '{name}' failed validation.")
199199
for error in exc.errors():
200200
log.msg(
201-
f"... '{error['loc'][0]}': {error['msg']} but '{error['input']}' was provided.",
201+
f" -> '{error['loc'][0]}': {error['msg']} but '{error['input']}' was provided.",
202202
)
203203
return output
204204

205205
def _validate_users(
206206
self: Self,
207207
annotated_users: list[tuple[JSONDict, list[type[LDAPObjectClass]]]],
208+
domain: str,
208209
) -> list[LDAPAttributeAdaptor]:
209210
"""Return a list of LDAPAttributeAdaptors representing validated user data."""
210211
if self.debug:
211212
log.msg(f"Attempting to validate {len(annotated_users)} users.")
212213
output = []
213214
for user_dict, required_classes in annotated_users:
215+
name = user_dict.get("cn", "unknown")
214216
try:
215-
output.append(
216-
LDAPAttributeAdaptor.from_attributes(
217-
user_dict,
218-
required_classes=required_classes,
219-
),
220-
)
217+
if (user_domain := user_dict.get("domain", None)) == domain:
218+
output.append(
219+
LDAPAttributeAdaptor.from_attributes(
220+
user_dict,
221+
required_classes=required_classes,
222+
),
223+
)
224+
else:
225+
log.msg(f"... user '{name}' failed validation.")
226+
log.msg(
227+
f" -> 'domain': expected '{domain}' but '{user_domain}' was provided.",
228+
)
221229
except ValidationError as exc:
222-
name = user_dict.get("cn", "unknown")
223-
log.msg(f"Validation failed for user '{name}'.")
230+
log.msg(f"... user '{name}' failed validation.")
224231
for error in exc.errors():
225232
log.msg(
226-
f"... '{error['loc'][0]}': {error['msg']} but '{error['input']}' was provided.",
233+
f" -> '{error['loc'][0]}': {error['msg']} but '{error['input']}' was provided.",
227234
)
228235
return output

pyproject.toml

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -80,62 +80,62 @@ target-version = ["py310", "py311"]
8080
[tool.ruff.lint]
8181
select = [
8282
# See https://beta.ruff.rs/docs/rules/
83-
"A", # flake8-builtins
84-
"AIR", # Airflow
85-
"ANN", # flake8-annotations
86-
"ARG", # flake8-unused-arguments
87-
"ASYNC", # flake8-async
88-
"B", # flake8-bugbear
89-
"BLE", # flake8-blind-except
90-
"C", # complexity, mcabe and flake8-comprehensions
91-
"COM", # flake8-commas
92-
"D", # pydocstyle
93-
"DTZ", # flake8-datetimez
94-
"E", # pycodestyle errors
95-
"EM", # flake8-errmsg
96-
"ERA", # eradicate
97-
"EXE", # flake8-executable
98-
"F", # pyflakes
99-
"FA", # flake8-future-annotations
100-
"FBT", # flake8-boolean-trap
101-
"FIX", # flake8-fixme
102-
"FLY", # flynt
103-
"FURB", # refurb
104-
"G", # flake8-logging-format
105-
"I", # isort
106-
"ICN", # flake8-import-conventions
107-
"INP", # flake8-no-pep420
108-
"INT", # flake8-gettext
109-
"ISC", # flake8-implicit-str-concat
110-
"LOG", # flake8-logging
111-
"N", # pep8-naming
112-
"NPY", # numpy-specific-rules
113-
"PD", # pandas-vet
114-
"PGH", # pygrep-hooks
115-
"PIE", # flake8-pie
116-
"PLC", # pylint convention
117-
"PLE", # pylint error
118-
"PLR", # pylint refactor
119-
"PLW", # pylint warning
120-
"PT", # flake8-pytest-style
121-
"PTH", # flake8-use-pathlib
122-
"PYI", # flake8-pyi
123-
"Q", # flake8-quotes
124-
"RET", # flake8-return
125-
"RSE", # flake8-raise
126-
"RUF", # ruff rules
127-
"S", # flake8-bandit
128-
"SIM", # flake8-simplify
129-
"SLOT", # flake8-slot
130-
"T", # flake8-debugger and flake8-print
131-
"TCH", # flake8-type-checking
132-
"TD", # flake8-todos
133-
"TID", # flake8-tidy-imports
134-
"TRIO", # flake8-trio
135-
"TRY", # tryceratops
136-
"UP", # pyupgrade
137-
"W", # pycodestyle warnings
138-
"YTT", # flake8-2020
83+
"A", # flake8-builtins
84+
"AIR", # Airflow
85+
"ANN", # flake8-annotations
86+
"ARG", # flake8-unused-arguments
87+
"ASYNC", # flake8-async
88+
"ASYNC1", # flake8-trio
89+
"B", # flake8-bugbear
90+
"BLE", # flake8-blind-except
91+
"C", # complexity, mcabe and flake8-comprehensions
92+
"COM", # flake8-commas
93+
"D", # pydocstyle
94+
"DTZ", # flake8-datetimez
95+
"E", # pycodestyle errors
96+
"EM", # flake8-errmsg
97+
"ERA", # eradicate
98+
"EXE", # flake8-executable
99+
"F", # pyflakes
100+
"FA", # flake8-future-annotations
101+
"FBT", # flake8-boolean-trap
102+
"FIX", # flake8-fixme
103+
"FLY", # flynt
104+
"FURB", # refurb
105+
"G", # flake8-logging-format
106+
"I", # isort
107+
"ICN", # flake8-import-conventions
108+
"INP", # flake8-no-pep420
109+
"INT", # flake8-gettext
110+
"ISC", # flake8-implicit-str-concat
111+
"LOG", # flake8-logging
112+
"N", # pep8-naming
113+
"NPY", # numpy-specific-rules
114+
"PD", # pandas-vet
115+
"PGH", # pygrep-hooks
116+
"PIE", # flake8-pie
117+
"PLC", # pylint convention
118+
"PLE", # pylint error
119+
"PLR", # pylint refactor
120+
"PLW", # pylint warning
121+
"PT", # flake8-pytest-style
122+
"PTH", # flake8-use-pathlib
123+
"PYI", # flake8-pyi
124+
"Q", # flake8-quotes
125+
"RET", # flake8-return
126+
"RSE", # flake8-raise
127+
"RUF", # ruff rules
128+
"S", # flake8-bandit
129+
"SIM", # flake8-simplify
130+
"SLOT", # flake8-slot
131+
"T", # flake8-debugger and flake8-print
132+
"TCH", # flake8-type-checking
133+
"TD", # flake8-todos
134+
"TID", # flake8-tidy-imports
135+
"TRY", # tryceratops
136+
"UP", # pyupgrade
137+
"W", # pycodestyle warnings
138+
"YTT", # flake8-2020
139139
]
140140
ignore = [
141141
"D100", # missing-docstring-in-module

0 commit comments

Comments
 (0)