@discover-cloud/shared 1.2.4 → 1.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/middleware/index.d.ts +1 -0
- package/dist/middleware/index.js +1 -0
- package/dist/middleware/require-auth.middleware.d.ts +1 -1
- package/dist/middleware/require-auth.middleware.js +3 -3
- package/dist/middleware/require-human.middleware.d.ts +2 -0
- package/dist/middleware/require-human.middleware.js +18 -0
- package/dist/middleware/require-internal.middleware.js +11 -3
- package/dist/types/express.types.d.ts +8 -0
- package/dist/types/express.types.js +17 -0
- package/package.json +1 -1
package/dist/middleware/index.js
CHANGED
|
@@ -21,3 +21,4 @@ __exportStar(require("./authorize.middleware"), exports);
|
|
|
21
21
|
__exportStar(require("./validated-merge.middleware"), exports);
|
|
22
22
|
__exportStar(require("./require-internal.middleware"), exports);
|
|
23
23
|
__exportStar(require("./require-auth.middleware"), exports);
|
|
24
|
+
__exportStar(require("./require-human.middleware"), exports);
|
|
@@ -40,7 +40,7 @@ import { InternalJwtVerifier } from "../jwt/internal-jwt-verifier";
|
|
|
40
40
|
* const requireAuth = new RequireAuthMiddleware(jwtVerifier, logger);
|
|
41
41
|
* router.use(requireAuth.handle);
|
|
42
42
|
*/
|
|
43
|
-
export declare class
|
|
43
|
+
export declare class requireAuth {
|
|
44
44
|
private readonly verifier;
|
|
45
45
|
private readonly logger;
|
|
46
46
|
constructor(verifier: InternalJwtVerifier, logger?: ILogger);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.requireAuth = void 0;
|
|
4
4
|
const jose_1 = require("jose");
|
|
5
5
|
const utils_1 = require("../utils");
|
|
6
6
|
const internal_jwt_verifier_1 = require("../jwt/internal-jwt-verifier");
|
|
@@ -42,7 +42,7 @@ const utils_2 = require("../utils");
|
|
|
42
42
|
* const requireAuth = new RequireAuthMiddleware(jwtVerifier, logger);
|
|
43
43
|
* router.use(requireAuth.handle);
|
|
44
44
|
*/
|
|
45
|
-
class
|
|
45
|
+
class requireAuth {
|
|
46
46
|
constructor(verifier, logger = utils_1.noopLogger) {
|
|
47
47
|
this.verifier = verifier;
|
|
48
48
|
this.logger = logger;
|
|
@@ -160,4 +160,4 @@ class RequireAuthMiddleware {
|
|
|
160
160
|
(0, utils_2.failure)(res, req, "Authentication service is temporarily unavailable.", "SERVICE_UNAVAILABLE", 503);
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
|
-
exports.
|
|
163
|
+
exports.requireAuth = requireAuth;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requireHuman = void 0;
|
|
4
|
+
const utils_1 = require("../utils");
|
|
5
|
+
const types_1 = require("../types");
|
|
6
|
+
const requireHuman = (req, res, next) => {
|
|
7
|
+
const ctx = req.accessContext;
|
|
8
|
+
if (!ctx) {
|
|
9
|
+
(0, utils_1.failure)(res, req, "Unauthorized: No access context", "UNAUTHORIZED", 401);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
if (!(0, types_1.isHumanContext)(ctx)) {
|
|
13
|
+
(0, utils_1.failure)(res, req, "Human token required", "FORBIDDEN", 403);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
next();
|
|
17
|
+
};
|
|
18
|
+
exports.requireHuman = requireHuman;
|
|
@@ -35,6 +35,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.requireInternal = requireInternal;
|
|
37
37
|
const jose = __importStar(require("jose"));
|
|
38
|
+
const types_1 = require("../types");
|
|
38
39
|
const logger_utils_1 = require("../utils/logger.utils");
|
|
39
40
|
/**
|
|
40
41
|
* REQUIRE INTERNAL MIDDLEWARE (@discover-cloud/shared)
|
|
@@ -125,14 +126,20 @@ function requireInternal(options = {}) {
|
|
|
125
126
|
});
|
|
126
127
|
return;
|
|
127
128
|
}
|
|
128
|
-
|
|
129
|
-
|
|
129
|
+
try {
|
|
130
|
+
(0, types_1.assertMachinePayload)(payload);
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
130
133
|
res.status(401).json({
|
|
131
134
|
success: false,
|
|
132
|
-
error: {
|
|
135
|
+
error: {
|
|
136
|
+
code: "INVALID_TOKEN",
|
|
137
|
+
message: "Invalid machine token payload",
|
|
138
|
+
},
|
|
133
139
|
});
|
|
134
140
|
return;
|
|
135
141
|
}
|
|
142
|
+
const serviceId = payload.serviceId;
|
|
136
143
|
// Allowlist — tightest possible scope per endpoint
|
|
137
144
|
if (allowedServices && !allowedServices.includes(serviceId)) {
|
|
138
145
|
logger.warn({ requestId: req.id, serviceId }, "[requireInternal] serviceId not in allowlist");
|
|
@@ -143,6 +150,7 @@ function requireInternal(options = {}) {
|
|
|
143
150
|
return;
|
|
144
151
|
}
|
|
145
152
|
const context = { kind: "machine", serviceId };
|
|
153
|
+
req.internalAuth = payload;
|
|
146
154
|
req.accessContext = context;
|
|
147
155
|
logger.debug({ requestId: req.id, serviceId, jti: payload.jti }, "[requireInternal] Machine token verified");
|
|
148
156
|
next();
|
|
@@ -96,6 +96,14 @@ export interface MachineAccessContext {
|
|
|
96
96
|
export type AccessContext = HumanAccessContext | MachineAccessContext;
|
|
97
97
|
export declare function isHumanPayload(payload: InternalJwtPayload): payload is HumanInternalJwtPayload;
|
|
98
98
|
export declare function isMachinePayload(payload: InternalJwtPayload): payload is MachineInternalJwtPayload;
|
|
99
|
+
/**
|
|
100
|
+
* Runtime assertion for jose.jwtVerify() results.
|
|
101
|
+
*
|
|
102
|
+
* jose returns a generic JWTPayload because it cannot know
|
|
103
|
+
* our custom claims. After verification + this assertion,
|
|
104
|
+
* TypeScript safely treats it as MachineInternalJwtPayload.
|
|
105
|
+
*/
|
|
106
|
+
export declare function assertMachinePayload(payload: JWTPayload): asserts payload is MachineInternalJwtPayload;
|
|
99
107
|
export declare function isHumanContext(ctx: AccessContext): ctx is HumanAccessContext;
|
|
100
108
|
export declare function isMachineContext(ctx: AccessContext): ctx is MachineAccessContext;
|
|
101
109
|
export interface VerifiedAccessPayload {
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
21
|
exports.isHumanPayload = isHumanPayload;
|
|
22
22
|
exports.isMachinePayload = isMachinePayload;
|
|
23
|
+
exports.assertMachinePayload = assertMachinePayload;
|
|
23
24
|
exports.isHumanContext = isHumanContext;
|
|
24
25
|
exports.isMachineContext = isMachineContext;
|
|
25
26
|
/* ====================================================================
|
|
@@ -35,6 +36,22 @@ function isHumanPayload(payload) {
|
|
|
35
36
|
function isMachinePayload(payload) {
|
|
36
37
|
return payload.isMachine === true;
|
|
37
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Runtime assertion for jose.jwtVerify() results.
|
|
41
|
+
*
|
|
42
|
+
* jose returns a generic JWTPayload because it cannot know
|
|
43
|
+
* our custom claims. After verification + this assertion,
|
|
44
|
+
* TypeScript safely treats it as MachineInternalJwtPayload.
|
|
45
|
+
*/
|
|
46
|
+
function assertMachinePayload(payload) {
|
|
47
|
+
if (payload["typ"] !== "internal" ||
|
|
48
|
+
payload["isMachine"] !== true ||
|
|
49
|
+
typeof payload["serviceId"] !== "string" ||
|
|
50
|
+
typeof payload["jti"] !== "string" ||
|
|
51
|
+
typeof payload["caller"] !== "string") {
|
|
52
|
+
throw new Error("Invalid machine JWT payload");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
38
55
|
function isHumanContext(ctx) {
|
|
39
56
|
return ctx.kind === "human";
|
|
40
57
|
}
|