Skip to content

Commit

Permalink
feat: Create ChainedTemplateEngine for combining engines
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Aug 2, 2021
1 parent 63e8857 commit 683a4f1
Show file tree
Hide file tree
Showing 13 changed files with 392 additions and 464 deletions.
12 changes: 11 additions & 1 deletion config/identity/handler/default.json
Expand Up @@ -23,7 +23,17 @@
"providerFactory": { "@id": "urn:solid-server:default:IdentityProviderFactory" },
"templateHandler": {
"@type": "TemplateHandler",
"templateEngine": { "@type": "EjsTemplateEngine" }
"templateEngine": {
"comment": "Renders the specific page and embeds it into the main HTML body.",
"@type": "ChainedTemplateEngine",
"engines": [
{ "@type": "EjsTemplateEngine" },
{
"@type": "EjsTemplateEngine",
"template": "$PACKAGE_ROOT/templates/identity/main.html.ejs"
}
]
}
},
"interactionCompleter": {
"comment": "Responsible for finishing OIDC interactions.",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -304,6 +304,7 @@ export * from './util/locking/SingleThreadedResourceLocker';
export * from './util/locking/WrappedExpiringReadWriteLocker';

// Util/Templates
export * from './util/templates/ChainedTemplateEngine';
export * from './util/templates/EjsTemplateEngine';
export * from './util/templates/HandlebarsTemplateEngine';
export * from './util/templates/TemplateEngine';
Expand Down
37 changes: 37 additions & 0 deletions src/util/templates/ChainedTemplateEngine.ts
@@ -0,0 +1,37 @@
import type { Template, TemplateEngine } from './TemplateEngine';
import Dict = NodeJS.Dict;

/**
* Calls the given array of {@link TemplateEngine}s in the order they appear,
* feeding the output of one into the input of the next.
*
* The first engine will be called with the provided contents and template parameters.
* All subsequent engines will be called with no template parameter and contents being `{ body }`,
* with `body` the output of the previous engine.
*/
export class ChainedTemplateEngine<T extends Dict<any> = Dict<any>> implements TemplateEngine<T> {
private readonly contentsEngine: TemplateEngine<T>;
private readonly bodyEngines: TemplateEngine<{ body: string }>[];

/**
* @param engines - Engines will be executed in the same order as the array.
* Actual expected type is `[ TemplateEngine<T>, ...TemplateEngine<{ body: string }>]`
*/
public constructor(engines: TemplateEngine[]) {
if (engines.length === 0) {
throw new Error('At least 1 engine needs to be provided.');
}
this.contentsEngine = engines[0];
this.bodyEngines = engines.slice(1);
}

public async render(contents: T): Promise<string>;
public async render<TCustom = T>(contents: TCustom, template: Template): Promise<string>;
public async render<TCustom = T>(contents: TCustom, template?: Template): Promise<string> {
let body = await this.contentsEngine.render(contents, template!);
for (const engine of this.bodyEngines) {
body = await engine.render({ body });
}
return body;
}
}
31 changes: 4 additions & 27 deletions templates/identity/email-password/confirm.html.ejs
@@ -1,27 +1,4 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Authorize</title>
<link rel="stylesheet" href="/.well_known/css/styles/main.css" type="text/css">
</head>
<body>
<header>
<a href="/"><img src="/.well_known/css/images/solid.svg" alt="[Solid logo]" /></a>
<h1>Community Solid Server</h1>
</header>
<main>
<h1>Authorize</h1>
<form action="/idp/confirm" method="post">
<p class="actions"><button autofocus type="submit" name="submit" class="ids-link-filled">Continue</button></p>
</form>
</main>
<footer>
<p>
©2019–2021 <a href="https://inrupt.com/">Inrupt Inc.</a>
and <a href="https://www.imec-int.com/">imec</a>
</p>
</footer>
</body>
</html>
<h1>Authorize</h1>
<form action="/idp/confirm" method="post">
<p class="actions"><button autofocus type="submit" name="submit" class="ids-link-filled">Continue</button></p>
</form>
45 changes: 11 additions & 34 deletions templates/identity/email-password/email-sent.html.ejs
@@ -1,37 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Email sent</title>
<link rel="stylesheet" href="/.well_known/css/styles/main.css" type="text/css">
</head>
<body>
<header>
<a href="/"><img src="/.well_known/css/images/solid.svg" alt="[Solid logo]" /></a>
<h1>Community Solid Server</h1>
</header>
<main>
<h1>Email sent</h1>
<form action="/idp/forgotpassword" method="post">
<p>If your account exists, an email has been sent with a link to reset your password.</p>
<p>If you do not receive your email in a couple of minutes, check your spam folder or click the link below to send another email.</p>
<h1>Email sent</h1>
<form action="/idp/forgotpassword" method="post">
<p>If your account exists, an email has been sent with a link to reset your password.</p>
<p>If you do not receive your email in a couple of minutes, check your spam folder or click the link below to send another email.</p>

<input type="hidden" name="email" value="<%= email %>" />
<input type="hidden" name="email" value="<%= email %>" />

<p class="actions"><a href="/idp/login">Back to Log In</a></p>
<p class="actions"><a href="/idp/login">Back to Log In</a></p>

<hr />
<p class="actions">
<button type="submit" name="submit" class="link">Send Another Email</button>
</p>
</form>
</main>
<footer>
<p>
©2019–2021 <a href="https://inrupt.com/">Inrupt Inc.</a>
and <a href="https://www.imec-int.com/">imec</a>
</p>
</footer>
</body>
</html>
<hr />
<p class="actions">
<button type="submit" name="submit" class="link">Send Another Email</button>
</p>
</form>
55 changes: 16 additions & 39 deletions templates/identity/email-password/forgot-password.html.ejs
@@ -1,42 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Forgot password</title>
<link rel="stylesheet" href="/.well_known/css/styles/main.css" type="text/css">
</head>
<body>
<header>
<a href="/"><img src="/.well_known/css/images/solid.svg" alt="[Solid logo]" /></a>
<h1>Community Solid Server</h1>
</header>
<main>
<h1>Forgot password</h1>
<form action="/idp/forgotpassword" method="post">
<%if (errorMessage) { %>
<p class="error"><%= errorMessage %></p>
<% } %>
<h1>Forgot password</h1>
<form action="/idp/forgotpassword" method="post">
<%if (errorMessage) { %>
<p class="error"><%= errorMessage %></p>
<% } %>

<fieldset>
<ol>
<li>
<label for="email">Email</label>
<input id="email" type="email" name="email" autofocus>
</li>
</ol>
</fieldset>
<fieldset>
<ol>
<li>
<label for="email">Email</label>
<input id="email" type="email" name="email" autofocus>
</li>
</ol>
</fieldset>

<p class="actions"><button type="submit" name="submit">Send recovery email</button></p>
<p class="actions"><button type="submit" name="submit">Send recovery email</button></p>

<p class="actions"><a href="/idp/login" class="link">Log in</a></p>
</form>
</main>
<footer>
<p>
©2019–2021 <a href="https://inrupt.com/">Inrupt Inc.</a>
and <a href="https://www.imec-int.com/">imec</a>
</p>
</footer>
</body>
</html>
<p class="actions"><a href="/idp/login" class="link">Log in</a></p>
</form>
77 changes: 27 additions & 50 deletions templates/identity/email-password/login.html.ejs
@@ -1,53 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Log in</title>
<link rel="stylesheet" href="/.well_known/css/styles/main.css" type="text/css">
</head>
<body>
<header>
<a href="/"><img src="/.well_known/css/images/solid.svg" alt="[Solid logo]" /></a>
<h1>Community Solid Server</h1>
</header>
<main>
<h1>Log in</h1>
<form action="/idp/login" method="post">
<%if (errorMessage) { %>
<p class="error"><%= errorMessage %></p>
<% } %>
<h1>Log in</h1>
<form action="/idp/login" method="post">
<%if (errorMessage) { %>
<p class="error"><%= errorMessage %></p>
<% } %>

<fieldset>
<legend>Your account</legend>
<ol>
<li>
<label for="email">Email</label>
<input id="email" type="email" name="email" autofocus <% if (prefilled.email) { %> value="<%= prefilled.email %>" <% } %>>
</li>
<li>
<label for="password">Password</label>
<input id="password" type="password" name="password">
</li>
<li class="checkbox">
<label><input type="checkbox" name="remember" value="yes" checked>Stay logged in</label>
</li>
</ol>
</fieldset>
<fieldset>
<legend>Your account</legend>
<ol>
<li>
<label for="email">Email</label>
<input id="email" type="email" name="email" autofocus <% if (prefilled.email) { %> value="<%= prefilled.email %>" <% } %>>
</li>
<li>
<label for="password">Password</label>
<input id="password" type="password" name="password">
</li>
<li class="checkbox">
<label><input type="checkbox" name="remember" value="yes" checked>Stay logged in</label>
</li>
</ol>
</fieldset>

<p class="actions"><button type="submit" name="submit">Log in</button></p>
<p class="actions"><button type="submit" name="submit">Log in</button></p>

<ul class="actions">
<li><a href="/idp/register" class="link">Sign up</a></li>
<li><a href="/idp/forgotpassword" class="link">Forgot password</a></li>
</ul>
</form>
</main>
<footer>
<p>
©2019–2021 <a href="https://inrupt.com/">Inrupt Inc.</a>
and <a href="https://www.imec-int.com/">imec</a>
</p>
</footer>
</body>
</html>
<ul class="actions">
<li><a href="/idp/register" class="link">Sign up</a></li>
<li><a href="/idp/forgotpassword" class="link">Forgot password</a></li>
</ul>
</form>
25 changes: 1 addition & 24 deletions templates/identity/email-password/message.html.ejs
@@ -1,24 +1 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title><%= message %></title>
<link rel="stylesheet" href="/.well_known/css/styles/main.css" type="text/css">
</head>
<body>
<header>
<a href="/"><img src="/.well_known/css/images/solid.svg" alt="[Solid logo]" /></a>
<h1>Community Solid Server</h1>
</header>
<main>
<p><%= message %></p>
</main>
<footer>
<p>
©2019–2021 <a href="https://inrupt.com/">Inrupt Inc.</a>
and <a href="https://www.imec-int.com/">imec</a>
</p>
</footer>
</body>
</html>
<p><%= message %></p>

0 comments on commit 683a4f1

Please sign in to comment.