Skip to content

Commit

Permalink
feat: Indicate to templates if this is part of an auth request
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Aug 13, 2021
1 parent efb70d6 commit 89a1d42
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
18 changes: 12 additions & 6 deletions src/identity/IdentityProviderHttpHandler.ts
Expand Up @@ -168,7 +168,9 @@ export class IdentityProviderHttpHandler extends HttpHandler {
oidcInteraction?: Interaction): Promise<void> {
if (request.method === 'GET') {
// .ejs templates errors on undefined variables
return await this.handleTemplateResponse(response, route.viewTemplate, { errorMessage: '', prefilled: {}});
return await this.handleTemplateResponse(
response, route.viewTemplate, { errorMessage: '', prefilled: {}}, oidcInteraction,
);
}

if (request.method === 'POST') {
Expand All @@ -179,7 +181,9 @@ export class IdentityProviderHttpHandler extends HttpHandler {
// Render error in the view
const prefilled = IdpInteractionError.isInstance(error) ? error.prefilled : {};
const errorMessage = createErrorMessage(error);
return await this.handleTemplateResponse(response, route.viewTemplate, { errorMessage, prefilled });
return await this.handleTemplateResponse(
response, route.viewTemplate, { errorMessage, prefilled }, oidcInteraction,
);
}

if (result.type === 'complete') {
Expand All @@ -192,15 +196,17 @@ export class IdentityProviderHttpHandler extends HttpHandler {
return await this.interactionCompleter.handleSafe({ ...result.details, request, response });
}
if (result.type === 'response' && route.responseTemplate) {
return await this.handleTemplateResponse(response, route.responseTemplate, result.details);
return await this.handleTemplateResponse(response, route.responseTemplate, result.details, oidcInteraction);
}
}
throw new BadRequestHttpError(`Unsupported request: ${request.method} ${request.url}`);
}

private async handleTemplateResponse(response: HttpResponse, templateFile: string, contents?: NodeJS.Dict<any>):
Promise<void> {
await this.templateHandler.handleSafe({ response, templateFile, contents: contents ?? {}});
private async handleTemplateResponse(response: HttpResponse, templateFile: string, data?: NodeJS.Dict<any>,
oidcInteraction?: Interaction): Promise<void> {
const contents = data ?? {};
contents.authenticating = Boolean(oidcInteraction);
await this.templateHandler.handleSafe({ response, templateFile, contents });
}

/**
Expand Down
6 changes: 5 additions & 1 deletion templates/identity/email-password/register-response.html.ejs
Expand Up @@ -26,7 +26,11 @@
<h2>Your new account</h2>
<p>
Via your email address <em><%= email %></em>,
this server lets you <a href="./login">log in</a> to Solid apps
<% if (authenticating) { %>
you can now <a href="./login">log in</a>
<% } else { %>
this server lets you log in to Solid apps
<% } %>
with your WebID <a href="<%= webId %>" class="link"><%= webId %></a>
</p>
<% if (!createWebId) { %>
Expand Down
21 changes: 12 additions & 9 deletions test/unit/identity/IdentityProviderHttpHandler.test.ts
Expand Up @@ -83,9 +83,9 @@ describe('An IdentityProviderHttpHandler', (): void => {
request.url = '/idp/routeResponse';
await expect(handler.handle({ request, response })).resolves.toBeUndefined();
expect(templateHandler.handleSafe).toHaveBeenCalledTimes(1);
expect(templateHandler.handleSafe).toHaveBeenLastCalledWith(
{ response, templateFile: routes.response.viewTemplate, contents: { errorMessage: '', prefilled: {}}},
);
expect(templateHandler.handleSafe).toHaveBeenLastCalledWith({ response,
templateFile: routes.response.viewTemplate,
contents: { errorMessage: '', prefilled: {}, authenticating: false }});
});

it('calls the templateHandler for InteractionResponseResults.', async(): Promise<void> => {
Expand All @@ -96,20 +96,22 @@ describe('An IdentityProviderHttpHandler', (): void => {
expect(routes.response.handler.handleSafe).toHaveBeenLastCalledWith({ request });
expect(templateHandler.handleSafe).toHaveBeenCalledTimes(1);
expect(templateHandler.handleSafe).toHaveBeenLastCalledWith(
{ response, templateFile: routes.response.responseTemplate, contents: { key: 'val' }},
{ response, templateFile: routes.response.responseTemplate, contents: { key: 'val', authenticating: false }},
);
});

it('supports InteractionResponseResults without details.', async(): Promise<void> => {
it('indicates to the templates if the request is part of an auth flow.', async(): Promise<void> => {
request.url = '/idp/routeResponse';
request.method = 'POST';
const oidcInteraction = { session: { accountId: 'account' }} as any;
provider.interactionDetails.mockResolvedValueOnce(oidcInteraction);
(routes.response.handler as jest.Mocked<InteractionHandler>).handleSafe.mockResolvedValueOnce({ type: 'response' });
await expect(handler.handle({ request, response })).resolves.toBeUndefined();
expect(routes.response.handler.handleSafe).toHaveBeenCalledTimes(1);
expect(routes.response.handler.handleSafe).toHaveBeenLastCalledWith({ request });
expect(routes.response.handler.handleSafe).toHaveBeenLastCalledWith({ request, oidcInteraction });
expect(templateHandler.handleSafe).toHaveBeenCalledTimes(1);
expect(templateHandler.handleSafe).toHaveBeenLastCalledWith(
{ response, templateFile: routes.response.responseTemplate, contents: {}},
{ response, templateFile: routes.response.responseTemplate, contents: { authenticating: true }},
);
});

Expand Down Expand Up @@ -175,7 +177,8 @@ describe('An IdentityProviderHttpHandler', (): void => {
expect(templateHandler.handleSafe).toHaveBeenLastCalledWith({
response,
templateFile: routes.response.viewTemplate,
contents: { errorMessage: 'handle error', prefilled: { name: 'name' }},
contents: { errorMessage: 'handle error', prefilled: { name: 'name' }, authenticating: false },

});
});

Expand All @@ -188,7 +191,7 @@ describe('An IdentityProviderHttpHandler', (): void => {
expect(templateHandler.handleSafe).toHaveBeenLastCalledWith({
response,
templateFile: routes.response.viewTemplate,
contents: { errorMessage: 'handle error', prefilled: { }},
contents: { errorMessage: 'handle error', prefilled: {}, authenticating: false },
});
});

Expand Down

0 comments on commit 89a1d42

Please sign in to comment.