Comparing version 1.3.0-beta7 to 1.3.0-beta8
@@ -18,2 +18,3 @@ import Addon from "../addon"; | ||
jwtClaimForUserID?: string; | ||
includeTokenInIdentifyOpDataPayload?: boolean; | ||
}; | ||
@@ -20,0 +21,0 @@ export default class UserManagementAddon extends Addon { |
@@ -29,2 +29,3 @@ "use strict"; | ||
jwtClaimForUserID: "id", | ||
includeTokenInIdentifyOpDataPayload: false, | ||
}; | ||
@@ -31,0 +32,0 @@ class UserManagementAddon extends addon_1.default { |
import { Knex } from "knex"; | ||
import Resource from "./resource"; | ||
import OperationProcessor from "./processors/operation-processor"; | ||
import { NoOpTransaction, ApplicationInterface } from "./types"; | ||
import { NoOpTransaction, ApplicationInterface, VendorRequest } from "./types"; | ||
import User from "./resources/user"; | ||
@@ -12,3 +12,3 @@ export default class ApplicationInstance { | ||
processorFor(resourceType: string): Promise<OperationProcessor<Resource> | undefined>; | ||
getUserFromToken(token: string): Promise<User | undefined>; | ||
getUserFromToken(token: string, request?: VendorRequest): Promise<User | undefined>; | ||
} |
@@ -6,2 +6,3 @@ "use strict"; | ||
const user_management_1 = require("./addons/user-management"); | ||
const hooks_1 = require("./utils/hooks"); | ||
class ApplicationInstance { | ||
@@ -14,3 +15,3 @@ constructor(app) { | ||
} | ||
async getUserFromToken(token) { | ||
async getUserFromToken(token, request) { | ||
const tokenPayload = (0, jsonwebtoken_1.decode)(token); | ||
@@ -32,3 +33,3 @@ let userIdSourceKey = ""; | ||
} | ||
const op = { | ||
let op = { | ||
op: "identify", | ||
@@ -41,2 +42,15 @@ ref: { | ||
}; | ||
if (userManagementAddonOptions.includeTokenInIdentifyOpDataPayload) { | ||
op.data = { | ||
type: "user", | ||
attributes: { | ||
token, | ||
}, | ||
relationships: {}, | ||
}; | ||
} | ||
const updateOperation = (updatedOperation) => { | ||
op = updatedOperation; | ||
}; | ||
(0, hooks_1.runHookFunctions)(this, "beforeExecutingIdentifyOperation", { op, request, updateOperation }); | ||
let user; | ||
@@ -43,0 +57,0 @@ try { |
@@ -23,2 +23,3 @@ "use strict"; | ||
beforeRequestHandling: [], | ||
beforeExecutingIdentifyOperation: [], | ||
}; | ||
@@ -25,0 +26,0 @@ if (settings.attributeTypes) { |
@@ -214,2 +214,3 @@ /// <reference types="node" /> | ||
beforeRequestHandling: HookFunction[]; | ||
beforeExecutingIdentifyOperation: HookFunction[]; | ||
} | ||
@@ -216,0 +217,0 @@ export interface ApplicationInterface extends Application { |
@@ -22,3 +22,3 @@ "use strict"; | ||
const [, token] = authHeader.split(" "); | ||
currentUser = await appInstance.getUserFromToken(token); | ||
currentUser = await appInstance.getUserFromToken(token, request); | ||
} | ||
@@ -25,0 +25,0 @@ appInstance.user = currentUser; |
{ | ||
"name": "kurier", | ||
"version": "1.3.0-beta7", | ||
"version": "1.3.0-beta8", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -29,2 +29,3 @@ import Addon from "../addon"; | ||
jwtClaimForUserID?: string; | ||
includeTokenInIdentifyOpDataPayload?: boolean; | ||
}; | ||
@@ -57,2 +58,3 @@ | ||
jwtClaimForUserID: "id", | ||
includeTokenInIdentifyOpDataPayload: false, | ||
}; | ||
@@ -59,0 +61,0 @@ |
@@ -6,6 +6,7 @@ import { decode } from "jsonwebtoken"; | ||
import OperationProcessor from "./processors/operation-processor"; | ||
import { Operation, OperationResponse, NoOpTransaction, ApplicationInterface } from "./types"; | ||
import { Operation, OperationResponse, NoOpTransaction, ApplicationInterface, VendorRequest } from "./types"; | ||
import jsonApiErrors from "./errors/json-api-errors"; | ||
import User from "./resources/user"; | ||
import UserManagementAddon, { UserManagementAddonOptions } from "./addons/user-management"; | ||
import { runHookFunctions } from "./utils/hooks"; | ||
@@ -22,7 +23,7 @@ export default class ApplicationInstance { | ||
async getUserFromToken(token: string): Promise<User | undefined> { | ||
async getUserFromToken(token: string, request?: VendorRequest): Promise<User | undefined> { | ||
const tokenPayload = decode(token); | ||
let userIdSourceKey = ""; | ||
const userManagementAddonOptions = this.app.getAddonOptions<UserManagementAddonOptions>(UserManagementAddon); | ||
const userManagementAddonOptions = this.app.getAddonOptions(UserManagementAddon) as UserManagementAddonOptions; | ||
@@ -45,3 +46,3 @@ if (userManagementAddonOptions) { | ||
const op = { | ||
let op = { | ||
op: "identify", | ||
@@ -55,2 +56,18 @@ ref: { | ||
if (userManagementAddonOptions.includeTokenInIdentifyOpDataPayload) { | ||
op.data = { | ||
type: "user", | ||
attributes: { | ||
token, | ||
}, | ||
relationships: {}, | ||
}; | ||
} | ||
const updateOperation = (updatedOperation: Operation) => { | ||
op = updatedOperation; | ||
}; | ||
runHookFunctions(this, "beforeExecutingIdentifyOperation", { op, request, updateOperation }); | ||
let user: OperationResponse; | ||
@@ -57,0 +74,0 @@ |
@@ -60,2 +60,3 @@ import { Knex } from "knex"; | ||
beforeRequestHandling: [], | ||
beforeExecutingIdentifyOperation: [], | ||
}; | ||
@@ -62,0 +63,0 @@ |
@@ -255,2 +255,3 @@ import { IncomingHttpHeaders, IncomingMessage, ServerResponse } from "http"; | ||
beforeRequestHandling: HookFunction[]; | ||
beforeExecutingIdentifyOperation: HookFunction[]; | ||
} | ||
@@ -257,0 +258,0 @@ |
@@ -25,3 +25,3 @@ import * as escapeStringRegexp from "escape-string-regexp"; | ||
const [, token] = authHeader.split(" "); | ||
currentUser = await appInstance.getUserFromToken(token); | ||
currentUser = await appInstance.getUserFromToken(token, request); | ||
} | ||
@@ -28,0 +28,0 @@ |
@@ -26,8 +26,11 @@ import { KnexProcessor, Application, ApplicationInstanceInterface } from "../../../src"; | ||
app.hook("beforeRequestHandling", hook); | ||
app.hook("beforeExecutingIdentifyOperation", hook); | ||
expect(app.hooks.beforeAuthentication).toHaveLength(1); | ||
expect(app.hooks.beforeRequestHandling).toHaveLength(1); | ||
expect(app.hooks.beforeExecutingIdentifyOperation).toHaveLength(1); | ||
expect(app.hooks.beforeAuthentication[0]).toEqual(hook); | ||
expect(app.hooks.beforeRequestHandling[0]).toEqual(hook); | ||
expect(app.hooks.beforeExecutingIdentifyOperation[0]).toEqual(hook); | ||
}); | ||
}); |
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
Sorry, the diff of this file is not supported yet
566277
8341