Skip to content

Support content negotiation for IDP requests - #898

Merged
jvherweg merged 4 commits into
versions/2.0.0from
feat/idp-request
Aug 18, 2021
Merged

jvherweg merged 4 commits into
versions/2.0.0from
feat/idp-request

Conversation

@jvherweg

@jvherweg jvherweg commented Aug 10, 2021 •

Copy link
Copy Markdown
Member

Merging this into feat/idp-architecture branch 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 TypedRepresentationConverter since 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 the TypedRepresentationConverter interface 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 controls field (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.

@RubenVerborgh

Copy link
Copy Markdown
Member

Should this ever change we either need to do the parsing later (but we still want to parse preferences early for error handling),

Can/should we guard against this? Setting the typings such that the request is read-only?

@RubenVerborgh RubenVerborgh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good stuff!!

} from './interaction/email-password/handler/InteractionHandler';
import { IdpInteractionError } from './interaction/util/IdpInteractionError';
import type { InteractionCompleter } from './interaction/util/InteractionCompleter';
import namedNode = DataFactory.namedNode;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RDF stuff is what seems too much (DataFactory). Perhaps a metadata helper somewhere?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: {}}},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird to have those empty things there. Omit?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Union typing on result?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, with if (typeof errorMessage !== 'undefined') is pretty inelegant.

But can whatever component that calls the template not fill those out?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe LDP is no longer the right namespace then.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we want UrlString or so.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do that. I'm guessing we probably have several places where we can make good use of template literal types.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Template literals might be hard; typing a URL is non-trivial.

private readonly templateEngine: TemplateEngine;
private readonly extension: string;

public constructor(templateEngine: TemplateEngine, extension = '') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the extension weird. Is this a proxy for mime types?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/util/Vocabularies.ts Resolved
@RubenVerborgh RubenVerborgh added the semver.major Requires a major version bump label Aug 11, 2021
@jvherweg

Copy link
Copy Markdown
Member Author

Should this ever change we either need to do the parsing later (but we still want to parse preferences early for error handling),

Can/should we guard against this? Setting the typings such that the request is read-only?

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.

@RubenVerborgh

Copy link
Copy Markdown
Member

Can this be merged?

@jvherweg

Copy link
Copy Markdown
Member Author

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!"

@RubenVerborgh

Copy link
Copy Markdown
Member

All good! I had approved the PR, so that's a sign that you can do what's best 🙂

@jvherweg
jvherweg changed the base branch from feat/idp-architecture to versions/2.0.0 August 18, 2021 11:45
@jvherweg
jvherweg merged commit f0f59a8 into versions/2.0.0 Aug 18, 2021
@jvherweg
jvherweg deleted the feat/idp-request branch August 18, 2021 13:39
@jvherweg jvherweg added this to the v2.0.0 milestone Oct 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver.major Requires a major version bump

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants