Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/facade/cfg/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,12 @@
"maxFiles": 20
},
"endpoint": "os"
},
"odic": {
"templates": {
"layout": "./node_modules/@restorecommerce/facade/templates/layout.hbs",
"login": "./node_modules/@restorecommerce/facade/templates/login.hbs",
"consent": "./node_modules/@restorecommerce/facade/templates/consent.hbs"
}
}
}
9 changes: 9 additions & 0 deletions packages/facade/cfg/config_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"odic": {
"templates": {
"layout": "./templates/layout.hbs",
"login": "./templates/login.hbs",
"consent": "./templates/consent.hbs"
}
}
}
6 changes: 0 additions & 6 deletions packages/facade/copy-hbs.sh

This file was deleted.

3 changes: 1 addition & 2 deletions packages/facade/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"vitest": "^3.2.4"
},
"scripts": {
"build": "npm-run-all build:clean generate build:compile build:codegen:clean build:codegen:compile copyhbs",
"build": "npm-run-all build:clean generate build:compile build:codegen:clean build:codegen:compile",
"build:clean": "rimraf -rf ./dist",
"build:compile": "tsc -p tsconfig.lib.json",
"build:codegen:clean": "rimraf -rf ./codegen",
Expand All @@ -117,7 +117,6 @@
"generate": "tsx --tsconfig tsconfig.generate.json generate.ts",
"prepublishOnly": "npm run build",
"debug-run": "tsx --tsconfig tsconfig.debug.json debug-run.ts",
"copyhbs": "sh copy-hbs.sh",
"lint": "eslint './**/*.ts'"
},
"publishConfig": {
Expand Down
1 change: 0 additions & 1 deletion packages/facade/src/modules/identity/oidc/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Provider from 'oidc-provider';
import helmet from 'koa-helmet';
import { type Logger } from 'winston';
import { type IdentityContext } from '../interfaces.js';
import type { OIDCConfig } from './interfaces.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/facade/src/modules/identity/oidc/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type InteractionResults } from 'oidc-provider';
import type Provider from 'oidc-provider';
import { type Logger } from 'winston';
import { type IdentityContext } from '../interfaces.js';
import { OIDCTemplateEngine, OIDCTemplateError } from './templates.js';
import { OIDCTemplateEngine } from './templates.js';
// import { AuthUser, loginUser } from './user/index.js';
import { type OIDCError, type OIDCHbsTemplates, type OIDCBodyLoginFn } from './interfaces.js';
import { koaBody } from 'koa-body';
Expand Down
55 changes: 21 additions & 34 deletions packages/facade/src/modules/identity/oidc/templates.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import path from 'node:path';
import fs from 'node:fs';
import { IdentityContext } from '../interfaces.js';
import hbs from 'handlebars';
import { type OIDCHbsTemplates } from './interfaces.js';

const __dirname = import.meta.dirname;

export interface OIDCTemplateError {
key: string;
message?: string;
Expand Down Expand Up @@ -38,51 +35,41 @@ hbs.registerHelper('json', (object) => {
});

export class OIDCTemplateEngine {

private layoutHbs?: HandlebarsTemplateDelegate<any>;
private loginHbs?: HandlebarsTemplateDelegate<OIDCTemplateLoginContext>;
private consentHbs?: HandlebarsTemplateDelegate<OIDCTemplateConsentContext>;

constructor(private templates: OIDCHbsTemplates | undefined) { }

async layout(context: OIDCTemplateContext & { body: string }) {
if (!this.layoutHbs) {
const layoutTpl = this.templates?.login ?? await new Promise<string>((resolve, reject) => {
fs.readFile(path.resolve(__dirname, 'views/layout.hbs'), (err, data) => err ? reject(err) : resolve(data.toString()));
async load(target: string) {
const template = this.templates?.[target];
if (template) {
const layout = await new Promise<string>((resolve, reject) => {
fs.readFile(
path.resolve(template),
(err, data) => err ? reject(err) : resolve(data.toString())
);
});
this.layoutHbs = hbs.compile(layoutTpl);
return hbs.compile(layout);
}
else {
throw new Error(`OIDC 'odic.template.${target}' not configured!`);
}
return this.layoutHbs(context);
}

async layout(context: OIDCTemplateContext & { body: string }) {
this.layoutHbs ??= await this.load('layout');
return this.layoutHbs(context);
}

async login(context: OIDCTemplateLoginContext) {
if (!this.loginHbs) {
const loginTpl = this.templates?.login ?? await new Promise<string>((resolve, reject) => {
fs.readFile(path.resolve(__dirname, 'views/login.hbs'), (err, data) => err ? reject(err) : resolve(data.toString()));
});
this.loginHbs = hbs.compile(loginTpl);
}

let html = this.loginHbs(context);
return this.layout({
...context,
body: html
});
this.loginHbs ??= await this.load('login');
return this.loginHbs(context);
}
async consent(context: OIDCTemplateConsentContext) {
if (!this.consentHbs) {
const consentTpl = this.templates?.consent ?? await new Promise<string>((resolve, reject) => {
fs.readFile(path.resolve(__dirname, 'views/consent.hbs'), (err, data) => err ? reject(err) : resolve(data.toString()));
});
this.consentHbs = hbs.compile(consentTpl);
}

let html = this.consentHbs(context);
return this.layout({
...context,
body: html
});
async consent(context: OIDCTemplateConsentContext) {
this.consentHbs ??= await this.load('consent');
return this.consentHbs(context);
}

}
Loading