@axah/koa
Advanced tools
Comparing version 2.1.1 to 2.2.0
@@ -0,6 +1,18 @@ | ||
export declare enum UserRole { | ||
ADMIN = "ADMIN", | ||
FEIGN = "FEIGN", | ||
DIRECT_SALE = "DIRECT-SALE", | ||
DEVOPS = "DEVOPS", | ||
CUSTOMER_SERVICE = "CUSTOMER_SERVICE", | ||
BENEFITS_PROCESSING = "BENEFITS_PROCESSING", | ||
CUSTOMER = "CUSTOMER", | ||
ADVISORY = "ADVISORY", | ||
ADVISORY_BACKOFFICE = "ADVISORY_BACKOFFICE", | ||
ADVISORY_PARTNER_AGENCY = "ADVISORY_PARTNER_AGENCY" | ||
} | ||
export declare type Authentication = { | ||
userId: string; | ||
userRoles: ReadonlyArray<string>; | ||
hasRole: (...roles: ReadonlyArray<string>) => boolean; | ||
userRoles: ReadonlyArray<UserRole>; | ||
hasRole: (...roles: ReadonlyArray<UserRole>) => boolean; | ||
}; | ||
export default function createAuth(headers: Object): Authentication; | ||
export default function createAuth(headers: Record<string, string | ReadonlyArray<string> | undefined>): Authentication; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function createAuth( | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
headers) { | ||
exports.UserRole = void 0; | ||
var UserRole; | ||
(function (UserRole) { | ||
UserRole["ADMIN"] = "ADMIN"; | ||
UserRole["FEIGN"] = "FEIGN"; | ||
UserRole["DIRECT_SALE"] = "DIRECT-SALE"; | ||
UserRole["DEVOPS"] = "DEVOPS"; | ||
UserRole["CUSTOMER_SERVICE"] = "CUSTOMER_SERVICE"; | ||
UserRole["BENEFITS_PROCESSING"] = "BENEFITS_PROCESSING"; | ||
UserRole["CUSTOMER"] = "CUSTOMER"; | ||
UserRole["ADVISORY"] = "ADVISORY"; | ||
UserRole["ADVISORY_BACKOFFICE"] = "ADVISORY_BACKOFFICE"; | ||
UserRole["ADVISORY_PARTNER_AGENCY"] = "ADVISORY_PARTNER_AGENCY"; | ||
})(UserRole = exports.UserRole || (exports.UserRole = {})); | ||
function notEmpty(value) { | ||
return !!value; | ||
} | ||
function createAuth(headers) { | ||
const userRoleHeader = headers['cp-axa-user-role']; | ||
@@ -14,7 +29,22 @@ const userIdHeader = headers['cp-axa-user-id']; | ||
} | ||
const userRoles = userRoleHeader | ||
.split(',') | ||
.map((r) => r.trim()) | ||
.filter((r) => r); | ||
if (userRoles.length === 0) { | ||
if (typeof userIdHeader !== 'string') { | ||
throw new Error('cp-axa-user-id header is not a string'); | ||
} | ||
let userRoles; | ||
if (typeof userRoleHeader === 'string') { | ||
userRoles = userRoleHeader | ||
.split(',') | ||
.map((r) => r.trim()) | ||
.filter((r) => r); | ||
} | ||
else { | ||
userRoles = userRoleHeader; | ||
} | ||
const userRolesToUse = userRoles | ||
.map((userRoleString) => { | ||
const match = Object.entries(UserRole).find(([key]) => key === userRoleString); | ||
return match?.[1]; | ||
}) | ||
.filter(notEmpty); | ||
if (userRolesToUse.length === 0) { | ||
throw new Error('Empty cp-axa-user-role header'); | ||
@@ -24,3 +54,3 @@ } | ||
userId: userIdHeader, | ||
userRoles, | ||
userRoles: userRolesToUse, | ||
hasRole(...roles) { | ||
@@ -27,0 +57,0 @@ return roles.some((role) => userRoles.indexOf(role) !== -1); |
import { Context, Next, Middleware } from 'koa'; | ||
import { ZodType } from 'zod'; | ||
import { Authentication } from './auth'; | ||
import { Authentication, UserRole } from './auth'; | ||
declare type Input<BV, QV, PV> = { | ||
allowedRoles: ReadonlyArray<string>; | ||
allowedRoles: ReadonlyArray<UserRole>; | ||
body: BV; | ||
@@ -7,0 +7,0 @@ query: QV; |
@@ -41,3 +41,6 @@ "use strict"; | ||
try { | ||
bodyValidationResult = bodyValidator.parse(Object.assign(Object.assign({}, ctx.request.body), (allowFiles ? ctx.request.files : {}))); | ||
bodyValidationResult = bodyValidator.parse({ | ||
...ctx.request.body, | ||
...(allowFiles ? ctx.request.files : {}), | ||
}); | ||
} | ||
@@ -44,0 +47,0 @@ catch (e) { |
@@ -5,2 +5,4 @@ "use strict"; | ||
const pino_1 = require("pino"); | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
const log_1 = require("@axah/log"); | ||
@@ -85,5 +87,8 @@ function parseJwt(token) { | ||
} | ||
return Object.assign({ messageId, | ||
return { | ||
messageId, | ||
initialMessageId, | ||
sessionId }, authInfo); | ||
sessionId, | ||
...authInfo, | ||
}; | ||
} | ||
@@ -90,0 +95,0 @@ function createLogMiddleware(logger, logRequests = true) { |
@@ -5,4 +5,4 @@ declare const _default: { | ||
[name: string]: string | readonly string[]; | ||
}) => {}; | ||
}) => Record<string, string | readonly string[]>; | ||
}; | ||
export default _default; |
{ | ||
"name": "@axah/koa", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"main": "lib/index.js", | ||
@@ -13,4 +13,4 @@ "license": "UNLICENSED", | ||
"prepublishOnly": "yarn build", | ||
"build": "yarn lint && yarn test && yarn clean && npm-run-all -p build:*", | ||
"build-with-lcov": "yarn lint && yarn test --coverage --coverageReporters lcov && yarn clean && npm-run-all -p build:*", | ||
"build": "yarn clean && yarn lint && yarn test && yarn build:typescript", | ||
"build-with-lcov": "yarn clean && yarn lint && yarn test --coverage --coverageReporters lcov && yarn build:typescript", | ||
"clean": "rimraf lib", | ||
@@ -23,23 +23,22 @@ "build:typescript": "tsc --build tsconfig.build.json", | ||
"@shopify/jest-koa-mocks": "^2.2.3", | ||
"@types/jest": "^26.0.13", | ||
"@types/jest": "^26.0.19", | ||
"@types/koa": "^2.11.4", | ||
"@types/node": "^14.10.1", | ||
"@types/pino": "^6.3.2", | ||
"@types/node": "^14.14.13", | ||
"@types/pino": "^6.3.4", | ||
"@types/uuid": "^8.3.0", | ||
"@typescript-eslint/eslint-plugin": "^4.1.0", | ||
"@typescript-eslint/parser": "^4.1.0", | ||
"eslint": "^7.9.0", | ||
"eslint-config-airbnb-base": "^14.2.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"@typescript-eslint/eslint-plugin": "^4.9.1", | ||
"@typescript-eslint/parser": "^4.9.1", | ||
"eslint": "^7.15.0", | ||
"eslint-config-airbnb-base": "^14.2.1", | ||
"eslint-config-prettier": "^7.0.0", | ||
"eslint-plugin-import": "^2.22.0", | ||
"eslint-plugin-prettier": "^3.1.4", | ||
"flowgen": "^1.11.0", | ||
"jest": "^26.4.2", | ||
"eslint-plugin-prettier": "^3.3.0", | ||
"flowgen": "^1.12.1", | ||
"jest": "^26.6.3", | ||
"koa": "^2.13.0", | ||
"nodemon": "^2.0.4", | ||
"npm-run-all": "^4.1.5", | ||
"pino": "^6.7.0", | ||
"prettier": "^2.1.1", | ||
"ts-jest": "^26.3.0", | ||
"typescript": "^4.0.2" | ||
"pino": "^6.8.0", | ||
"prettier": "^2.2.1", | ||
"ts-jest": "^26.4.4", | ||
"typescript": "^4.1.3" | ||
}, | ||
@@ -50,4 +49,4 @@ "dependencies": { | ||
"koa-body": "^4.2.0", | ||
"uuid": "^8.3.1", | ||
"zod": "2.0.0-beta.16" | ||
"uuid": "^8.3.2", | ||
"zod": "2.0.0-beta.29" | ||
}, | ||
@@ -57,3 +56,6 @@ "peerDependencies": { | ||
"pino": "^6.0.0" | ||
}, | ||
"engines": { | ||
"node": ">=14" | ||
} | ||
} |
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
Sorry, the diff of this file is not supported yet
28258
21
406
+ Addedzod@2.0.0-beta.29(transitive)
- Removedzod@2.0.0-beta.16(transitive)
Updateduuid@^8.3.2
Updatedzod@2.0.0-beta.29