|
13 | 13 | from scim2_models import Error |
14 | 14 | from scim2_models import ListResponse |
15 | 15 | from scim2_models import PatchOp |
| 16 | +from scim2_models import ResourceType |
16 | 17 | from scim2_models import ResponseParameters |
| 18 | +from scim2_models import Schema |
17 | 19 | from scim2_models import SearchRequest |
18 | 20 | from scim2_models import UniquenessException |
19 | 21 | from scim2_models import User |
20 | 22 |
|
21 | 23 | from .integrations import delete_record |
22 | 24 | from .integrations import from_scim_user |
23 | 25 | from .integrations import get_record |
| 26 | +from .integrations import get_resource_types |
| 27 | +from .integrations import get_schemas |
24 | 28 | from .integrations import list_records |
25 | 29 | from .integrations import save_record |
| 30 | +from .integrations import service_provider_config |
26 | 31 | from .integrations import to_scim_user |
27 | 32 |
|
28 | 33 | # -- setup-start -- |
@@ -209,4 +214,116 @@ def post(self, request): |
209 | 214 | path("scim/v2/Users/<user:app_record>", UserView.as_view(), name="scim_user"), |
210 | 215 | ] |
211 | 216 | # -- collection-end -- |
| 217 | + |
| 218 | + |
| 219 | +# -- discovery-start -- |
| 220 | +# -- schemas-start -- |
| 221 | +class SchemasView(View): |
| 222 | + """Handle GET on the SCIM schemas collection.""" |
| 223 | + |
| 224 | + def get(self, request): |
| 225 | + try: |
| 226 | + req = SearchRequest.model_validate(request.GET.dict()) |
| 227 | + except ValidationError as error: |
| 228 | + return scim_validation_error(error) |
| 229 | + |
| 230 | + all_schemas = get_schemas() |
| 231 | + page = all_schemas[req.start_index_0 : req.stop_index_0] |
| 232 | + response = ListResponse[Schema]( |
| 233 | + total_results=len(all_schemas), |
| 234 | + start_index=req.start_index or 1, |
| 235 | + items_per_page=len(page), |
| 236 | + resources=page, |
| 237 | + ) |
| 238 | + return scim_response( |
| 239 | + response.model_dump_json(scim_ctx=Context.RESOURCE_QUERY_RESPONSE) |
| 240 | + ) |
| 241 | + |
| 242 | + |
| 243 | +class SchemaView(View): |
| 244 | + """Handle GET on a single SCIM schema.""" |
| 245 | + |
| 246 | + def get(self, request, schema_id): |
| 247 | + for schema in get_schemas(): |
| 248 | + if schema.id == schema_id: |
| 249 | + return scim_response( |
| 250 | + schema.model_dump_json(scim_ctx=Context.RESOURCE_QUERY_RESPONSE) |
| 251 | + ) |
| 252 | + scim_error = Error(status=404, detail=f"Schema {schema_id!r} not found") |
| 253 | + return scim_response(scim_error.model_dump_json(), HTTPStatus.NOT_FOUND) |
| 254 | +# -- schemas-end -- |
| 255 | + |
| 256 | + |
| 257 | +# -- resource-types-start -- |
| 258 | +class ResourceTypesView(View): |
| 259 | + """Handle GET on the SCIM resource types collection.""" |
| 260 | + |
| 261 | + def get(self, request): |
| 262 | + try: |
| 263 | + req = SearchRequest.model_validate(request.GET.dict()) |
| 264 | + except ValidationError as error: |
| 265 | + return scim_validation_error(error) |
| 266 | + |
| 267 | + all_resource_types = get_resource_types() |
| 268 | + page = all_resource_types[req.start_index_0 : req.stop_index_0] |
| 269 | + response = ListResponse[ResourceType]( |
| 270 | + total_results=len(all_resource_types), |
| 271 | + start_index=req.start_index or 1, |
| 272 | + items_per_page=len(page), |
| 273 | + resources=page, |
| 274 | + ) |
| 275 | + return scim_response( |
| 276 | + response.model_dump_json(scim_ctx=Context.RESOURCE_QUERY_RESPONSE) |
| 277 | + ) |
| 278 | + |
| 279 | + |
| 280 | +class ResourceTypeView(View): |
| 281 | + """Handle GET on a single SCIM resource type.""" |
| 282 | + |
| 283 | + def get(self, request, resource_type_id): |
| 284 | + for rt in get_resource_types(): |
| 285 | + if rt.id == resource_type_id: |
| 286 | + return scim_response( |
| 287 | + rt.model_dump_json(scim_ctx=Context.RESOURCE_QUERY_RESPONSE) |
| 288 | + ) |
| 289 | + scim_error = Error( |
| 290 | + status=404, detail=f"ResourceType {resource_type_id!r} not found" |
| 291 | + ) |
| 292 | + return scim_response(scim_error.model_dump_json(), HTTPStatus.NOT_FOUND) |
| 293 | +# -- resource-types-end -- |
| 294 | + |
| 295 | + |
| 296 | +# -- service-provider-config-start -- |
| 297 | +class ServiceProviderConfigView(View): |
| 298 | + """Handle GET on the SCIM service provider configuration.""" |
| 299 | + |
| 300 | + def get(self, request): |
| 301 | + return scim_response( |
| 302 | + service_provider_config.model_dump_json( |
| 303 | + scim_ctx=Context.RESOURCE_QUERY_RESPONSE |
| 304 | + ) |
| 305 | + ) |
| 306 | +# -- service-provider-config-end -- |
| 307 | + |
| 308 | + |
| 309 | +discovery_urlpatterns = [ |
| 310 | + path("scim/v2/Schemas", SchemasView.as_view(), name="scim_schemas"), |
| 311 | + path("scim/v2/Schemas/<path:schema_id>", SchemaView.as_view(), name="scim_schema"), |
| 312 | + path( |
| 313 | + "scim/v2/ResourceTypes", |
| 314 | + ResourceTypesView.as_view(), |
| 315 | + name="scim_resource_types", |
| 316 | + ), |
| 317 | + path( |
| 318 | + "scim/v2/ResourceTypes/<resource_type_id>", |
| 319 | + ResourceTypeView.as_view(), |
| 320 | + name="scim_resource_type", |
| 321 | + ), |
| 322 | + path( |
| 323 | + "scim/v2/ServiceProviderConfig", |
| 324 | + ServiceProviderConfigView.as_view(), |
| 325 | + name="scim_service_provider_config", |
| 326 | + ), |
| 327 | +] |
| 328 | +# -- discovery-end -- |
212 | 329 | # -- endpoints-end -- |
0 commit comments