From 11cea332ec9db43a2bda89a5d40bba0e7a0c063f Mon Sep 17 00:00:00 2001 From: Harry Kodden Date: Fri, 13 Jan 2023 00:07:05 +0100 Subject: [PATCH] Adjusted SCIM templates and added Resource Type Template --- server/api/scim.py | 20 ++++++++++ server/scim/group_template.py | 2 +- server/scim/resource_type_template.py | 40 +++++++++++++++++++ server/scim/schema_template.py | 12 +++--- server/scim/user_template.py | 4 +- .../public/paths/get_resource_types.yml | 17 ++++++++ .../public/schemas/ScimResourceTypes.yml | 25 ++++++++++++ 7 files changed, 110 insertions(+), 10 deletions(-) create mode 100644 server/scim/resource_type_template.py create mode 100644 server/swagger/public/paths/get_resource_types.yml create mode 100644 server/swagger/public/schemas/ScimResourceTypes.yml diff --git a/server/api/scim.py b/server/api/scim.py index 4002fa152..8d7ee242b 100644 --- a/server/api/scim.py +++ b/server/api/scim.py @@ -12,6 +12,7 @@ from server.scim.group_template import find_groups_template, find_group_by_id_template from server.scim.repo import all_scim_users_by_service, all_scim_groups_by_service from server.scim.schema_template import SCIM_SCHEMA_CORE, schemas_template, schema_user_template, schema_group_template +from server.scim.resource_type_template import resource_type_template, resource_type_user_template, resource_type_group_template from server.scim.sweep import perform_sweep from server.scim.user_template import find_users_template, find_user_by_id_template, version_value @@ -45,6 +46,25 @@ def schemas(): return schemas_template(), 200 +@scim_api.route("/ResourceTypes/User", methods=["GET"], strict_slashes=False) +@json_endpoint +def resource_types_user(): + return resource_type_user_template(), 200 + + +@scim_api.route("/ResourceTypes/Group", methods=["GET"], strict_slashes=False) +@json_endpoint +def resource_types_group(): + return resource_type_group_template(), 200 + + +@scim_api.route("/ResourceTypes", methods=["GET"], strict_slashes=False) +@swag_from("../swagger/public/paths/get_resource_types.yml") +@json_endpoint +def resource_types(): + return resource_type_template(), 200 + + @scim_api.route("/Users", methods=["GET"], strict_slashes=False) @swag_from("../swagger/public/paths/get_users.yml") @json_endpoint diff --git a/server/scim/group_template.py b/server/scim/group_template.py index ab6b9543e..801345423 100644 --- a/server/scim/group_template.py +++ b/server/scim/group_template.py @@ -12,7 +12,7 @@ def _meta_info(group: Union[Group, Collaboration]): "created": date_time_format(group.created_at), "lastModified": date_time_format(group.updated_at), "version": version_value(group), - "location": f"{SCIM_URL_PREFIX}/Groups/{group.identifier}{EXTERNAL_ID_POST_FIX}"} + "location": f"/Groups/{group.identifier}{EXTERNAL_ID_POST_FIX}"} def create_group_template(group: Union[Group, Collaboration], membership_scim_objects): diff --git a/server/scim/resource_type_template.py b/server/scim/resource_type_template.py new file mode 100644 index 000000000..f6f4c8c53 --- /dev/null +++ b/server/scim/resource_type_template.py @@ -0,0 +1,40 @@ +from server.scim.schema_template import SCIM_SCHEMA_CORE, SCIM_API_MESSAGES + + +def _resource_type(name): + return { + "description": f"Defined resource types for the {name} schema", + "endpoint": f"/{name}s", + "id": f"{name}", + "meta": { + "location": f"/ResourceTypes/{name}", + "resourceType": "ResourceType" + }, + "name": f"{name}", + "schema": f"{SCIM_SCHEMA_CORE}:{name}", + "schemas": [ + f"{SCIM_SCHEMA_CORE}:ResourceType" + ] + } + + +def resource_type_user_template(): + return _resource_type("User") + + +def resource_type_group_template(): + return _resource_type("Group") + + +def resource_type_template(): + resources = [resource_type_user_template(), resource_type_group_template()] + + return { + "schemas": [ + f"{SCIM_API_MESSAGES}:ListResponse" + ], + "totalResults": len(resources), + "startIndex": 1, + "itemsPerPage": len(resources), + "Resources": resources + } diff --git a/server/scim/schema_template.py b/server/scim/schema_template.py index 9bd252fed..48be00630 100644 --- a/server/scim/schema_template.py +++ b/server/scim/schema_template.py @@ -1,5 +1,3 @@ -from server.scim import SCIM_URL_PREFIX - SCIM_SCHEMA_CORE = "urn:ietf:params:scim:schemas:core:2.0" SCIM_API_MESSAGES = "urn:ietf:params:scim:api:messages:2.0" @@ -12,7 +10,7 @@ def _schema(name, schema, attributes): "id": schema, "meta": { "resourceType": "Schema", - "location": f"{SCIM_URL_PREFIX}/Schemas/{schema}" + "location": f"/Schemas/{schema}" }, "name": name, "Description": f"Defined attributes for the {name} schema", @@ -199,14 +197,14 @@ def schema_group_template(): def schemas_template(): - schemas = [schema_user_template(), schema_group_template()] + resources = [schema_user_template(), schema_group_template()] return { "schemas": [ f"{SCIM_API_MESSAGES}:ListResponse" ], - "totalResults": len(schemas), + "totalResults": len(resources), "startIndex": 1, - "itemsPerPage": len(schemas), - "Resources": schemas + "itemsPerPage": len(resources), + "Resources": resources } diff --git a/server/scim/user_template.py b/server/scim/user_template.py index 0c5d55292..9482554fb 100644 --- a/server/scim/user_template.py +++ b/server/scim/user_template.py @@ -3,7 +3,7 @@ from typing import List, Union from server.db.domain import User, Group, Collaboration -from server.scim import SCIM_URL_PREFIX, EXTERNAL_ID_POST_FIX +from server.scim import EXTERNAL_ID_POST_FIX from server.scim.schema_template import SCIM_SCHEMA_CORE, SCIM_API_MESSAGES @@ -29,7 +29,7 @@ def _meta_info(user: User): "created": date_time_format(user.created_at), "lastModified": date_time_format(user.updated_at), "version": version_value(user), - "location": f"{SCIM_URL_PREFIX}/Users/{user.external_id}{EXTERNAL_ID_POST_FIX}"} + "location": f"/Users/{user.external_id}{EXTERNAL_ID_POST_FIX}"} def create_user_template(user: User): diff --git a/server/swagger/public/paths/get_resource_types.yml b/server/swagger/public/paths/get_resource_types.yml new file mode 100644 index 000000000..dc86e5ee6 --- /dev/null +++ b/server/swagger/public/paths/get_resource_types.yml @@ -0,0 +1,17 @@ +summary: "Get Resource Types." +description: "Retrieves the SCIM Resource Types provided by SRAM." + +tags: + - "SCIM Services" + +consumes: + - "application/json" + +produces: + - "application/json" + +responses: + 200: + description: All users + schema: + $ref: "/swagger/schemas/ScimResourceTypes.yml" diff --git a/server/swagger/public/schemas/ScimResourceTypes.yml b/server/swagger/public/schemas/ScimResourceTypes.yml new file mode 100644 index 000000000..4cec238fc --- /dev/null +++ b/server/swagger/public/schemas/ScimResourceTypes.yml @@ -0,0 +1,25 @@ +--- +type: object +properties: + itemsPerPage: + type: number + description: "Number of item per page" + example: 15 + startIndex: + type: number + description: "Zero-based start index of the result for pagination" + example: 0 + totalResults: + type: number + description: "Total number of results" + example: 15 + schemas: + type: array + description: "SCIM schema's" + items: + type: string + example: "urn:ietf:params:scim:api:messages:2.0:ListResponse" + Resources: + type: array + items: + type: object