simpleidserver / SimpleIdServer

OpenID, OAuth 2.0, SCIM2.0, UMA2.0, FAPI, CIBA & OPENBANKING Framework for ASP.NET Core
https://simpleidserver.com/
Apache License 2.0
737 stars 99 forks source link

Adding a new resourceType #23

Closed bzuidgeest closed 4 years ago

bzuidgeest commented 4 years ago

Sorry to bother you with this, but need a few pointers.

I need to add a new resource type. I took the following steps.

I created a schema to add to the collection. `SCIMSchema eduUserSchema = SCIMSchemaBuilder.Create("urn:ietf:params:scim:schemas:extension:nleducation:1.0:eduuser", "eduUser", "eduUsers", "SCIM extended resource for representing users for digital educational content in the Netherlands", true ) .AddStringAttribute("eckId", multiValued: false, description: "The unique identification of the user for digital content", mutability: SCIMSchemaAttributeMutabilities.IMMUTABLE, required: true, caseExact: false, returned: SCIMSchemaAttributeReturned.REQUEST, uniqueness: SCIMSchemaAttributeUniqueness.GLOBAL) .AddStringAttribute("externalId", multiValued: false, description: "The identifaction of the user as defined by Kennisnet: nlEduPersonRealId", mutability: SCIMSchemaAttributeMutabilities.READWRITE, required: false, caseExact: true, returned: SCIMSchemaAttributeReturned.ALWAYS, uniqueness: SCIMSchemaAttributeUniqueness.GLOBAL) .AddComplexAttribute("name", c => { c.AddStringAttribute("familyName", multiValued: false, description: "The surname of the EduUser, including prefixes like 'van' or 'van der'", mutability: SCIMSchemaAttributeMutabilities.READWRITE, required: true, caseExact: false, returned: SCIMSchemaAttributeReturned.ALWAYS, uniqueness: SCIMSchemaAttributeUniqueness.NONE); c.AddStringAttribute("givenName", multiValued: false, description: "The given name of the EduUser", mutability: SCIMSchemaAttributeMutabilities.READWRITE, required: true, caseExact: false, returned: SCIMSchemaAttributeReturned.ALWAYS, uniqueness: SCIMSchemaAttributeUniqueness.NONE);

            },
            multiValued: false,
            description: "A complex type containing the name of the EduUser",
            mutability: SCIMSchemaAttributeMutabilities.READWRITE,
            required: true,
            caseExact: false,
            //returned: SCIMSchemaAttributeReturned.REQUEST,
            uniqueness: SCIMSchemaAttributeUniqueness.NONE
        ).Build();`

This shows nicely in both schemas and resourcetype endpoints.

I than made a copy of groupscontroller source into my project and renamed it EduUsersController

`using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using SimpleIdServer.Scim; using SimpleIdServer.Scim.Api; using SimpleIdServer.Scim.Commands.Handlers; using SimpleIdServer.Scim.Persistence;

namespace SimpleIdServer { [Route("EduUsers")] public class EduUsersController : BaseApiController { public EduUsersController(IAddRepresentationCommandHandler addRepresentationCommandHandler, IDeleteRepresentationCommandHandler deleteRepresentationCommandHandler, IReplaceRepresentationCommandHandler replaceRepresentationCommandHandler, IPatchRepresentationCommandHandler patchRepresentationCommandHandler, ISCIMRepresentationQueryRepository scimRepresentationQueryRepository, ISCIMSchemaQueryRepository scimSchemaQueryRepository, IOptionsMonitor options) : base(SCIMConstants.SCIMEndpoints.Groups, addRepresentationCommandHandler, deleteRepresentationCommandHandler, replaceRepresentationCommandHandler, patchRepresentationCommandHandler, scimRepresentationQueryRepository, scimSchemaQueryRepository, options) { } }

}`

The controller is found, but I get an error while trying to create a new "eduUser"

Request:

User-Agent: Fiddler Host: localhost:44328 Authorization: Bearer ............. Content-Type: application/json Content-Length: 406 { "schemas": [ "urn:ietf:params:scim:schemas:extension:nleducation:1.0:eduuser" ], "eckId": "https://eckid.nl/8e0a9f57fc76854d3dd2d3c4fa732feaf7b7a2d5f549a5458ce300223b83172f5074aa88a8cef0712aca19b62e9b90d0352e98fc76f498cd3947e7cc810f03fa", "externalId": "12345@magis.main", "name": { "givenName": "Voornaam", "familyName": "Achternaam" } }

response:

HTTP/1.1 400 Bad Request Content-Length: 266 Content-Type: application/json Server: Microsoft-IIS/10.0 X-Powered-By: ASP.NET Date: Thu, 05 Mar 2020 14:33:56 GMT

{ "status": "400", "response": { "schemas": [ "urn:ietf:params:scim:api:messages:2.0:Error" ], "status": "400", "scimType": "invalidSyntax", "detail": "Request is unparsable, syntactically incorrect, or violates schema." } }

So the error supplies me with three possible reasons for the failure, but I don't know where to start debugging this/where to look. I hate to admit I get somewhat confused with all the dependencyinjection stuff. And I could be not so much the request failing a me missing some needed step to add the new resource type.

Hope you have any pointers for me on how to fix this.

bzuidgeest commented 4 years ago

I found the problem. I missed editing the resourcetype on the base(....) part making it switch to groups instead of eduUserController.

I would like to request a modification of the baseapicontroller on for example line 203.

try { var command = new AddRepresentationCommand(_resourceType, jobj); var scimRepresentation = await _addRepresentationCommandHandler.Handle(command); return BuildHTTPResult(scimRepresentation, HttpStatusCode.Created, false); } catch (**SCIMBadRequestException ex)** { return this.BuildError(HttpStatusCode.BadRequest, "Request is unparsable, syntactically incorrect, or violates schema.", "invalidSyntax"); } catch (SCIMUniquenessAttributeException) { return this.BuildError(HttpStatusCode.Conflict, "One or more of the attribute values are already in use or are reserved.", "uniqueness"); }

the message from SCIMBadRequestException is not exported in the error message, but it contains much useful information making the error much more specific. It really is a shame this is not exported in the error response. That would make diagnosis much easier.

simpleidserver commented 4 years ago

Hello,

We are going to check what we can do to enrich the error message, give a better insight into the errors.

At the moment, the error messages present in the project are coming from this RFC : https://tools.ietf.org/html/rfc7644#section-3.12

Kind regards

bzuidgeest commented 4 years ago

I can see the changes, thank you. It would have been a waste if you made so much work of providing clear exceptions and then have them hidden. Off course we do not want exceptions happening, but when they happen, nice error info makes live a lot easier.

simpleidserver commented 4 years ago

The error is fixed in the commit : "https://github.com/simpleidserver/SimpleIdServer/commit/0450d48945467df9fb5d12e108436e395e1776fa"

bzuidgeest commented 4 years ago

Looking at the commit I see a lot of lines like: [Authorize("QueryScimResource")] // [Authorize("QueryScimResource")]

Is that right? Seems you might have accidentally turned of authorization... Or has it moved elsewhere?

simpleidserver commented 4 years ago

"Authorization" attributes have been commented by accident... The modifications have been rolled back in the commit "https://github.com/simpleidserver/SimpleIdServer/commit/96a30352dead9ac2562b1daf93193b01038bf97e"