Repository navigation
Support content negotiation for IDP requests - #898
Conversation
Can/should we guard against this? Setting the typings such that the request is read-only? |
| } from './interaction/email-password/handler/InteractionHandler'; | ||
| import { IdpInteractionError } from './interaction/util/IdpInteractionError'; | ||
| import type { InteractionCompleter } from './interaction/util/InteractionCompleter'; | ||
| import namedNode = DataFactory.namedNode; |
There was a problem hiding this comment.
Without having studied the details: that is a ton of diverse imports.
Is that a sign that we might need to split off some functionality into another class?
There was a problem hiding this comment.
The RDF stuff is what seems too much (DataFactory). Perhaps a metadata helper somewhere?
There was a problem hiding this comment.
Is that a sign that we might need to split off some functionality into another class?
Probably, I already give some ideas above of how this could be done, but my plan was to first get the main behaviour right and then split it off. Not exactly the best architectural design method, but it doesn't impact any other components for now and I felt there already were plenty of changes here for 1 PR.
The RDF stuff is what seems too much (DataFactory). Perhaps a metadata helper somewhere?
This is a common problem in the entire project actually, many places where we need RDF terms have this. Also related: we actually have 2 libraries for creating terms: N3.js and @rdfjs/data-model. Having a metadata helper somewhere to have a consistent solution for this would probably be good (and would allow us to remove the second dependency). Would create a separate issue for this instead of fixing this here though.
| // .ejs templates errors on undefined variables | ||
| return await this.handleTemplateResponse(response, route.viewTemplate, { errorMessage: '', prefilled: {}}); | ||
| return { | ||
| result: { type: 'response', details: { errorMessage: '', prefilled: {}}}, |
There was a problem hiding this comment.
Weird to have those empty things there. Omit?
There was a problem hiding this comment.
They're not here because of typing issues, they're here because the EJS templates crash if the variables are not defined. if (errorMessage) errors if errorMessage was not passed as a parameter to the template. Didn't look into this fully, but I'm guessing there are probably ways to get around this by editting the EJS templates.
There was a problem hiding this comment.
Yeah, with if (typeof errorMessage !== 'undefined') is pretty inelegant.
But can whatever component that calls the template not fill those out?
There was a problem hiding this comment.
We don't need it for all templates though, and once out of this function the next step is the TemplateEngine itself. Will probably still look into a way to get this out of the JSON response body at some point but that is less urgent.
| @@ -1,5 +1,5 @@ | |||
| import type { KoaContextWithOIDC } from 'oidc-provider'; | |||
| import type { HttpRequest } from '../../../../server/HttpRequest'; | |||
| import type { Operation } from '../../../../ldp/operations/Operation'; | |||
There was a problem hiding this comment.
Maybe LDP is no longer the right namespace then.
There was a problem hiding this comment.
True, this has actually been the case for several things in that folder for some time. Now we're just reaching the point where it pretty is no longer unique for most things in there. So we can probably just rename the folder to something more generic (which seems an easier transition than moving everything in it around). core perhaps.
| * Returns the URL the request should be redirected to. | ||
| */ | ||
| export class InteractionCompleter extends AsyncHandler<InteractionCompleterInput> { | ||
| export class InteractionCompleter extends AsyncHandler<InteractionCompleterInput, string> { |
There was a problem hiding this comment.
I wonder if we want UrlString or so.
There was a problem hiding this comment.
We could do that. I'm guessing we probably have several places where we can make good use of template literal types.
There was a problem hiding this comment.
Template literals might be hard; typing a URL is non-trivial.
| private readonly templateEngine: TemplateEngine; | ||
| private readonly extension: string; | ||
|
|
||
| public constructor(templateEngine: TemplateEngine, extension = '') { |
There was a problem hiding this comment.
I find the extension weird. Is this a proxy for mime types?
There was a problem hiding this comment.
Sort of I guess. The idea was that I wanted to prevent the template engine being called on templates it didn't support (so only use the engine on .ejs templates if it is an EJS engine). But thinking about it again now, this should not be here. Instead, this is a very good case on why we should make the template engines AsyncHandlers. Then the engine itself can check the file extension in its canHandle function so the EJS engine only supports .ejs templates. We could then even have a waterfall handler with both EJS and Handlebars engine so we have a single engine that supports both.
The request being read is the issue. What would break this solution is if the body parser reads out the stream. The problem is that the OIDC library also wants to read out the stream so then it would error since you can't read a stream twice. Technically we could create a complex class that prevents a stream from being read unless we want it to, but that would be overkill for this situation. It's not needed now, and if it is ever needed there are simpler solutions. |
|
Can this be merged? |
I assumed there would be some replies to my comments and was waiting on that before finishing up this PR 😅 . Unless the reply everywhere is "you're right, you do what you think is best!" |
|
All good! I had approved the PR, so that's a sign that you can do what's best 🙂 |
ee8f6e1 to
5de056e
Compare
5de056e to
91d0913
Compare
Merging this into
feat/idp-architecturebranch instead of main due to breaking changes in some IDP classes. Going to push future architecture changes there as well.Now uses RequestParser on input data and ReponseWriter for all requests handled by our handlers.
The one issue here is that we still need to pass the original request object to the OIDC library in case it needs to handle something. Fortunately our default BodyParser doesn't actually read data from the input request and re-uses it in the Representation it creates, so the input request isn't actually modified. Should this ever change we either need to do the parsing later (but we still want to parse preferences early for error handling), or create a mock request with the correct data for the OIDC library.
The default response data is now JSON, on which we apply a converter to do content negotiation. For this I created a new converter that takes a JSON Representation and uses that JSON as input parameter for rendering a template. The metadata object gets used to pass the template file paths to the converter. This is a bit ugly perhaps but I think it is the cleanest way to handle this. Another unfortunate issue is that this is not a
TypedRepresentationConvertersince its supported output types depend on the input Representation. This means we can't use it in the ChainedConverter for now. A solution would be to update theTypedRepresentationConverterinterface so its functions can take a Representation or other arguments as input.The position of the converter in the config is also because of #834 .
The templates field for every IDP handler entry is now an object with the key being the content-type, making it easy to add other template types in the future if needed.
This is an example JSON output when registering:
{ "webId": "http://localhost:3000/joachimvh/profile/card#me", "email": "[email protected]", "oidcIssuer": "http://localhost:3000/", "podBaseUrl": "http://localhost:3000/joachimvh/", "createWebId": true, "register": true, "createPod": true, "apiVersion": "0.1" }The plan in the future is to add an extra
controlsfield (or other name) in every response object that links to all the relevant pages.When doing a GET request you always get the following for now:
{ "errorMessage": "", "prefilled": {}, "apiVersion": "0.1" }Probably want to clean that up in the future. Not sure what to add instead besides the controls. A list of all supported fields and a description perhaps.
The integration tests currently still use the HTML responses, but now they could be completely rewritten to use the JSON API instead.
Still need to move the input parsing to the main handler so the IDP handlers don't have to do it themselves yet and we can support JSON input besides form data.
It's also getting to the point where it might make sense to split the main handler up in a few separate classes, perhaps one for the route handling, and another one for operation handling, but I think it might be best to first get to a state where we support the requests we want to support.