@eduzz/miau-client 0.0.11 → 0.0.13
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/.turbo/turbo-build$colon$types.log +1 -1
- package/.turbo/turbo-build.log +4 -4
- package/.turbo/turbo-prepublish.log +1 -1
- package/dist/MiauClient.d.ts +3 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +70 -48
- package/dist/index.js.map +4 -4
- package/dist/miau-types/types/Application.d.ts +1 -1
- package/dist/miau-types/types/Permission.d.ts +1 -1
- package/dist/miau-types/types/Resource.d.ts +1 -1
- package/dist/middleware.d.ts +9 -0
- package/package.json +1 -1
- package/src/MiauClient.ts +10 -4
- package/src/index.ts +0 -1
- package/src/middleware.ts +93 -0
- package/dist/MiauMiddleware.d.ts +0 -3
- package/src/MiauMiddleware.ts +0 -61
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
|
|
2
|
-
> @eduzz/miau-client@0.0.
|
|
2
|
+
> @eduzz/miau-client@0.0.13 build /home/runner/work/eduzz-miau/eduzz-miau/packages/client
|
|
3
3
|
> esbuild src/index.ts --bundle --sourcemap --platform=node --target=es2020 --outfile=dist/index.js
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
dist/index.js
|
|
7
|
-
dist/index.js.map
|
|
6
|
+
dist/index.js 438.5kb
|
|
7
|
+
dist/index.js.map 735.4kb
|
|
8
8
|
|
|
9
|
-
⚡ Done in
|
|
9
|
+
⚡ Done in 459ms
|
package/dist/MiauClient.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { type RequestHandler } from 'express';
|
|
1
2
|
import { type Permission } from '@eduzz/miau-types';
|
|
3
|
+
import { type RequestAugmentation } from './middleware';
|
|
2
4
|
type MiauClientConfig = {
|
|
3
5
|
apiUrl: string;
|
|
4
6
|
appSecret: string;
|
|
@@ -13,6 +15,7 @@ export declare class MiauClient {
|
|
|
13
15
|
constructor(props: MiauClientConfig);
|
|
14
16
|
getPublicKey(kid: string): Promise<string>;
|
|
15
17
|
getToken(): Promise<string | undefined>;
|
|
18
|
+
middleware<T = Record<string, string>>(requestAugmentation?: RequestAugmentation<T>, fallbackMidlleware?: RequestHandler): RequestHandler;
|
|
16
19
|
getPermissions(targetAppId: string): Promise<Permission>;
|
|
17
20
|
private requestPermissions;
|
|
18
21
|
private getApiJwtUrl;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -11715,8 +11715,7 @@ var index_exports = {};
|
|
|
11715
11715
|
__export(index_exports, {
|
|
11716
11716
|
HttpMethods: () => HttpMethods,
|
|
11717
11717
|
MiauClient: () => MiauClient,
|
|
11718
|
-
ResourceTypes: () => ResourceTypes
|
|
11719
|
-
miauMiddleware: () => miauMiddleware
|
|
11718
|
+
ResourceTypes: () => ResourceTypes
|
|
11720
11719
|
});
|
|
11721
11720
|
module.exports = __toCommonJS(index_exports);
|
|
11722
11721
|
|
|
@@ -11725,8 +11724,71 @@ var ResourceTypes = ["http", "websocket", "grpc"];
|
|
|
11725
11724
|
var HttpMethods = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"];
|
|
11726
11725
|
|
|
11727
11726
|
// src/MiauClient.ts
|
|
11728
|
-
var
|
|
11727
|
+
var import_jsonwebtoken2 = __toESM(require_jsonwebtoken());
|
|
11729
11728
|
var import_jwks_rsa = __toESM(require_src2());
|
|
11729
|
+
|
|
11730
|
+
// src/middleware.ts
|
|
11731
|
+
var import_jsonwebtoken = __toESM(require_jsonwebtoken());
|
|
11732
|
+
var HttpError = class _HttpError extends Error {
|
|
11733
|
+
constructor(status, name, message) {
|
|
11734
|
+
super(message);
|
|
11735
|
+
this.name = name;
|
|
11736
|
+
this.status = status;
|
|
11737
|
+
Object.setPrototypeOf(this, _HttpError.prototype);
|
|
11738
|
+
}
|
|
11739
|
+
};
|
|
11740
|
+
var wildcardToRegex = (pattern) => {
|
|
11741
|
+
const escaped = pattern.replace(/[-/\\^$+?.()|[\]{}]/g, "\\$&");
|
|
11742
|
+
const withWildcards = escaped.replace(/\*/g, "[^/]+");
|
|
11743
|
+
const regexStr = `^${withWildcards}$`;
|
|
11744
|
+
return new RegExp(regexStr);
|
|
11745
|
+
};
|
|
11746
|
+
var miauMiddleware = (miauClient, requestAugmentation, fallbackMidlleware) => {
|
|
11747
|
+
return async (req, res, next) => {
|
|
11748
|
+
try {
|
|
11749
|
+
const token = req.headers.authorization?.split(" ").pop();
|
|
11750
|
+
if (!token) {
|
|
11751
|
+
throw new HttpError(401, "Invalid Token", "Token not provided");
|
|
11752
|
+
}
|
|
11753
|
+
const decodedToken = import_jsonwebtoken.default.decode(token, { complete: true });
|
|
11754
|
+
if (!decodedToken?.header?.kid) {
|
|
11755
|
+
throw new HttpError(401, "Invalid Token", "Missing kid in token header");
|
|
11756
|
+
}
|
|
11757
|
+
const publicKey = await miauClient.getPublicKey(decodedToken.header.kid);
|
|
11758
|
+
const appToken = import_jsonwebtoken.default.verify(token, publicKey, { algorithms: ["RS256"] });
|
|
11759
|
+
if (!appToken || !appToken.id || !appToken.name) {
|
|
11760
|
+
throw new HttpError(401, "Invalid Token", "Token verification failed");
|
|
11761
|
+
}
|
|
11762
|
+
const permission = await miauClient.getPermissions(appToken.id);
|
|
11763
|
+
if (!permission) {
|
|
11764
|
+
res.status(401).json({ error: "Unauthorized", message: "No permissions found for this application" });
|
|
11765
|
+
}
|
|
11766
|
+
const resources = permission?.resources || [];
|
|
11767
|
+
const isAllowed = resources.some((resource) => {
|
|
11768
|
+
return resource.method.toLowerCase() === req.method.toLowerCase() && wildcardToRegex(resource.path).test(req.path.toLowerCase());
|
|
11769
|
+
});
|
|
11770
|
+
if (!isAllowed) {
|
|
11771
|
+
throw new HttpError(403, "Forbidden", `You do not have permission to access ${req.method} ${req.path}`);
|
|
11772
|
+
}
|
|
11773
|
+
req.miauApplication = { id: appToken?.id, name: appToken?.name };
|
|
11774
|
+
req.miauMetadata = permission?.metadata || {};
|
|
11775
|
+
if (requestAugmentation) {
|
|
11776
|
+
console.log("Request augmentation is being applied");
|
|
11777
|
+
requestAugmentation({ req, app: req.miauApplication, meta: req.miauMetadata });
|
|
11778
|
+
}
|
|
11779
|
+
next();
|
|
11780
|
+
} catch (err) {
|
|
11781
|
+
if (err instanceof HttpError && err.status == 401 && fallbackMidlleware) {
|
|
11782
|
+
return fallbackMidlleware(req, res, next);
|
|
11783
|
+
}
|
|
11784
|
+
const errorStatus = err.status || 403;
|
|
11785
|
+
res.status(errorStatus).json({ error: err.name, message: err.message });
|
|
11786
|
+
return;
|
|
11787
|
+
}
|
|
11788
|
+
};
|
|
11789
|
+
};
|
|
11790
|
+
|
|
11791
|
+
// src/MiauClient.ts
|
|
11730
11792
|
var reusableFetch = async (input, init) => {
|
|
11731
11793
|
return new Promise(async (resolve, reject) => {
|
|
11732
11794
|
try {
|
|
@@ -11765,7 +11827,7 @@ var MiauClient = class {
|
|
|
11765
11827
|
}
|
|
11766
11828
|
async getToken() {
|
|
11767
11829
|
if (this.jwtToken) {
|
|
11768
|
-
const { exp } =
|
|
11830
|
+
const { exp } = import_jsonwebtoken2.default.decode(this.jwtToken);
|
|
11769
11831
|
const ONE_MINUTE_FROM_NOW = Math.floor(Date.now() / 1e3) + 60;
|
|
11770
11832
|
if (exp > ONE_MINUTE_FROM_NOW) {
|
|
11771
11833
|
return this.jwtToken;
|
|
@@ -11783,6 +11845,9 @@ var MiauClient = class {
|
|
|
11783
11845
|
this.jwtToken = (await response.json()).jwt;
|
|
11784
11846
|
return this.jwtToken;
|
|
11785
11847
|
}
|
|
11848
|
+
middleware(requestAugmentation, fallbackMidlleware) {
|
|
11849
|
+
return miauMiddleware(this, requestAugmentation, fallbackMidlleware);
|
|
11850
|
+
}
|
|
11786
11851
|
async getPermissions(targetAppId) {
|
|
11787
11852
|
if (this.permissionsCache.has(targetAppId)) {
|
|
11788
11853
|
const { data, expiresAt } = this.permissionsCache.get(targetAppId);
|
|
@@ -11814,54 +11879,11 @@ var MiauClient = class {
|
|
|
11814
11879
|
return request;
|
|
11815
11880
|
}
|
|
11816
11881
|
};
|
|
11817
|
-
|
|
11818
|
-
// src/MiauMiddleware.ts
|
|
11819
|
-
var import_jsonwebtoken2 = __toESM(require_jsonwebtoken());
|
|
11820
|
-
var wildcardToRegex = (pattern) => {
|
|
11821
|
-
const escaped = pattern.replace(/[-/\\^$+?.()|[\]{}]/g, "\\$&");
|
|
11822
|
-
const withWildcards = escaped.replace(/\*/g, "[^/]+");
|
|
11823
|
-
const regexStr = `^${withWildcards}$`;
|
|
11824
|
-
return new RegExp(regexStr);
|
|
11825
|
-
};
|
|
11826
|
-
var miauMiddleware = (miauService) => {
|
|
11827
|
-
return async (req, res, next) => {
|
|
11828
|
-
try {
|
|
11829
|
-
const token = req.headers.authorization?.split(" ").pop();
|
|
11830
|
-
if (!token) {
|
|
11831
|
-
res.status(401).json({ error: "Invalid Token", message: "Token not provided" });
|
|
11832
|
-
return;
|
|
11833
|
-
}
|
|
11834
|
-
const decodedToken = import_jsonwebtoken2.default.decode(token, { complete: true });
|
|
11835
|
-
if (!decodedToken?.header?.kid) {
|
|
11836
|
-
res.status(401).json({ error: "Invalid token", message: "Missing kid" });
|
|
11837
|
-
return;
|
|
11838
|
-
}
|
|
11839
|
-
const publicKey = await miauService.getPublicKey(decodedToken.header.kid);
|
|
11840
|
-
const appToken = import_jsonwebtoken2.default.verify(token, publicKey, { algorithms: ["RS256"] });
|
|
11841
|
-
req.miauApplication = { id: appToken?.id, name: appToken?.name };
|
|
11842
|
-
const permission = await miauService.getPermissions(req.miauApplication.id);
|
|
11843
|
-
req.miauMetadata = permission?.metadata || {};
|
|
11844
|
-
const resources = permission?.resources || [];
|
|
11845
|
-
const isAllowed = resources.some((resource) => {
|
|
11846
|
-
return resource.method.toLowerCase() === req.method.toLowerCase() && wildcardToRegex(resource.path).test(req.path.toLowerCase());
|
|
11847
|
-
});
|
|
11848
|
-
if (!isAllowed) {
|
|
11849
|
-
res.status(403).json({ error: "Forbidden", message: `You do not have permission to access ${req.method} ${req.path}` });
|
|
11850
|
-
return;
|
|
11851
|
-
}
|
|
11852
|
-
next();
|
|
11853
|
-
} catch (err) {
|
|
11854
|
-
res.status(401).json({ error: err.name, message: err.message });
|
|
11855
|
-
return;
|
|
11856
|
-
}
|
|
11857
|
-
};
|
|
11858
|
-
};
|
|
11859
11882
|
// Annotate the CommonJS export names for ESM import in node:
|
|
11860
11883
|
0 && (module.exports = {
|
|
11861
11884
|
HttpMethods,
|
|
11862
11885
|
MiauClient,
|
|
11863
|
-
ResourceTypes
|
|
11864
|
-
miauMiddleware
|
|
11886
|
+
ResourceTypes
|
|
11865
11887
|
});
|
|
11866
11888
|
/*! Bundled license information:
|
|
11867
11889
|
|