Comparing version 1.3.0-beta6 to 1.3.0-beta7
@@ -5,5 +5,7 @@ import Addon from "../addon"; | ||
import JsonApiUserProcessor from "../processors/user-processor"; | ||
import JsonApiSessionProcessor from "../processors/session-processor"; | ||
export type UserManagementAddonOptions = AddonOptions & { | ||
userResource: typeof User; | ||
userProcessor?: typeof JsonApiUserProcessor; | ||
sessionProcessor?: typeof JsonApiSessionProcessor; | ||
userEncryptPasswordCallback?: (op: Operation) => Promise<ResourceAttributes>; | ||
@@ -16,2 +18,3 @@ userLoginCallback?: (op: Operation, userDataSource: ResourceAttributes) => Promise<boolean>; | ||
passwordRequestParameter?: string; | ||
jwtClaimForUserID?: string; | ||
}; | ||
@@ -18,0 +21,0 @@ export default class UserManagementAddon extends Addon { |
@@ -12,2 +12,3 @@ "use strict"; | ||
userProcessor: user_processor_1.default, | ||
sessionProcessor: session_processor_1.default, | ||
userRolesProvider: async () => [], | ||
@@ -28,2 +29,3 @@ userPermissionsProvider: async () => [], | ||
passwordRequestParameter: "password", | ||
jwtClaimForUserID: "id", | ||
}; | ||
@@ -71,9 +73,18 @@ class UserManagementAddon extends addon_1.default { | ||
createSessionProcessor(sessionResourceType) { | ||
return ((options) => { var _a; return _a = class SessionProcessor extends session_processor_1.default { | ||
async login(op, userDataSource) { | ||
return (options.userLoginCallback || ((_, __) => true))(op, userDataSource); | ||
} | ||
}, | ||
_a.resourceClass = sessionResourceType, | ||
_a; })(this.options); | ||
const { sessionProcessor } = this.options; | ||
if (sessionProcessor === session_processor_1.default) { | ||
return ((options) => { var _a; return _a = class SessionProcessor extends session_processor_1.default { | ||
async login(op, userDataSource) { | ||
return (options.userLoginCallback || ((_, __) => true))(op, userDataSource); | ||
} | ||
}, | ||
_a.resourceClass = sessionResourceType, | ||
_a; })(this.options); | ||
} | ||
if (sessionProcessor !== undefined) { | ||
return ((options) => { var _a; return _a = class SessionProcessor extends (options.sessionProcessor) { | ||
}, | ||
_a.resourceClass = sessionResourceType, | ||
_a; })(this.options); | ||
} | ||
} | ||
@@ -80,0 +91,0 @@ createSessionResource() { |
@@ -5,2 +5,3 @@ "use strict"; | ||
const json_api_errors_1 = require("./errors/json-api-errors"); | ||
const user_management_1 = require("./addons/user-management"); | ||
class ApplicationInstance { | ||
@@ -15,5 +16,17 @@ constructor(app) { | ||
const tokenPayload = (0, jsonwebtoken_1.decode)(token); | ||
if (!tokenPayload || !tokenPayload["id"]) { | ||
let userIdSourceKey = ""; | ||
const userManagementAddonOptions = this.app.getAddonOptions(user_management_1.default); | ||
if (userManagementAddonOptions) { | ||
userIdSourceKey = userManagementAddonOptions.jwtClaimForUserID; | ||
} | ||
else { | ||
userIdSourceKey = "id"; | ||
} | ||
if (!tokenPayload) { | ||
throw json_api_errors_1.default.InvalidToken(); | ||
} | ||
const id = tokenPayload[userIdSourceKey]; | ||
if (!id) { | ||
throw json_api_errors_1.default.InvalidToken(); | ||
} | ||
const op = { | ||
@@ -23,3 +36,3 @@ op: "identify", | ||
type: "user", | ||
id: tokenPayload["id"], | ||
id, | ||
}, | ||
@@ -26,0 +39,0 @@ params: {}, |
@@ -27,2 +27,3 @@ import { Knex } from "knex"; | ||
use(addon: typeof Addon, options?: AddonOptions): void; | ||
getAddonOptions<T extends AddonOptions = AddonOptions>(addon: typeof Addon): AddonOptions | undefined; | ||
registerAttributeType(attributeDefinition: ApplicationAttributeTypeFactory): void; | ||
@@ -29,0 +30,0 @@ executeOperations(ops: Operation[], applicationInstance?: ApplicationInstanceInterface): Promise<OperationResponse[]>; |
@@ -37,6 +37,11 @@ "use strict"; | ||
} | ||
new addon(this, options).install().then(() => { | ||
this.addons.push({ addon, options }); | ||
const addonToInstall = new addon(this, options); | ||
addonToInstall.install().then(() => { | ||
this.addons.push({ addon, options: addonToInstall.options }); | ||
}); | ||
} | ||
getAddonOptions(addon) { | ||
const installedAddon = this.addons.find((installedAddon) => installedAddon.addon === addon); | ||
return installedAddon === null || installedAddon === void 0 ? void 0 : installedAddon.options; | ||
} | ||
registerAttributeType(attributeDefinition) { | ||
@@ -43,0 +48,0 @@ this.serializer.registerAttributeType(attributeDefinition); |
{ | ||
"name": "kurier", | ||
"version": "1.3.0-beta6", | ||
"version": "1.3.0-beta7", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -20,2 +20,3 @@ import Addon from "../addon"; | ||
userProcessor?: typeof JsonApiUserProcessor; | ||
sessionProcessor?: typeof JsonApiSessionProcessor; | ||
userEncryptPasswordCallback?: (op: Operation) => Promise<ResourceAttributes>; | ||
@@ -28,2 +29,3 @@ userLoginCallback?: (op: Operation, userDataSource: ResourceAttributes) => Promise<boolean>; | ||
passwordRequestParameter?: string; | ||
jwtClaimForUserID?: string; | ||
}; | ||
@@ -34,2 +36,3 @@ | ||
userProcessor: JsonApiUserProcessor, | ||
sessionProcessor: JsonApiSessionProcessor, | ||
userRolesProvider: async () => [], | ||
@@ -55,2 +58,3 @@ userPermissionsProvider: async () => [], | ||
passwordRequestParameter: "password", | ||
jwtClaimForUserID: "id", | ||
}; | ||
@@ -76,3 +80,3 @@ | ||
this.createUserProcessor() as unknown as typeof JsonApiUserProcessor, | ||
this.createSessionProcessor(sessionResourceType), | ||
this.createSessionProcessor(sessionResourceType) as typeof JsonApiSessionProcessor, | ||
); | ||
@@ -116,10 +120,21 @@ } | ||
private createSessionProcessor(sessionResourceType: typeof Resource) { | ||
return ((options) => | ||
class SessionProcessor<T extends Session> extends JsonApiSessionProcessor<T> { | ||
public static resourceClass = sessionResourceType as typeof Session; | ||
const { sessionProcessor } = this.options; | ||
protected async login(op: Operation, userDataSource: ResourceAttributes): Promise<boolean> { | ||
return (options.userLoginCallback || ((_, __) => true))(op, userDataSource); | ||
} | ||
})(this.options); | ||
if (sessionProcessor === JsonApiSessionProcessor) { | ||
return ((options) => | ||
class SessionProcessor<T extends Session> extends JsonApiSessionProcessor<T> { | ||
public static resourceClass = sessionResourceType as typeof Session; | ||
protected async login(op: Operation, userDataSource: ResourceAttributes): Promise<boolean> { | ||
return (options.userLoginCallback || ((_, __) => true))(op, userDataSource); | ||
} | ||
})(this.options); | ||
} | ||
if (sessionProcessor !== undefined) { | ||
return ((options) => | ||
class SessionProcessor<T extends Session> extends (options.sessionProcessor!)<T> { | ||
public static resourceClass = sessionResourceType as typeof Session; | ||
})(this.options); | ||
} | ||
} | ||
@@ -126,0 +141,0 @@ |
@@ -9,2 +9,3 @@ import { decode } from "jsonwebtoken"; | ||
import User from "./resources/user"; | ||
import UserManagementAddon, { UserManagementAddonOptions } from "./addons/user-management"; | ||
@@ -23,7 +24,22 @@ export default class ApplicationInstance { | ||
const tokenPayload = decode(token); | ||
let userIdSourceKey = ""; | ||
if (!tokenPayload || !tokenPayload["id"]) { | ||
const userManagementAddonOptions = this.app.getAddonOptions<UserManagementAddonOptions>(UserManagementAddon); | ||
if (userManagementAddonOptions) { | ||
userIdSourceKey = userManagementAddonOptions.jwtClaimForUserID as string; | ||
} else { | ||
userIdSourceKey = "id"; | ||
} | ||
if (!tokenPayload) { | ||
throw jsonApiErrors.InvalidToken(); | ||
} | ||
const id = tokenPayload[userIdSourceKey]; | ||
if (!id) { | ||
throw jsonApiErrors.InvalidToken(); | ||
} | ||
const op = { | ||
@@ -33,3 +49,3 @@ op: "identify", | ||
type: "user", | ||
id: tokenPayload["id"], | ||
id, | ||
}, | ||
@@ -36,0 +52,0 @@ params: {}, |
@@ -78,7 +78,14 @@ import { Knex } from "knex"; | ||
new addon(this as ApplicationInterface, options).install().then(() => { | ||
this.addons.push({ addon, options }); | ||
const addonToInstall = new addon(this as ApplicationInterface, options); | ||
addonToInstall.install().then(() => { | ||
this.addons.push({ addon, options: addonToInstall.options as AddonOptions }); | ||
}); | ||
} | ||
getAddonOptions<T extends AddonOptions = AddonOptions>(addon: typeof Addon) { | ||
const installedAddon = this.addons.find((installedAddon) => installedAddon.addon === addon); | ||
return installedAddon?.options; | ||
} | ||
registerAttributeType(attributeDefinition: ApplicationAttributeTypeFactory) { | ||
@@ -85,0 +92,0 @@ this.serializer.registerAttributeType(attributeDefinition); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
564187
8302