@eduzz/miau-client 0.0.17 → 0.0.19
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-prepublish.log +1 -1
- package/dist/MiauClient.d.ts +8 -2
- package/dist/index.js +25 -14
- package/dist/index.js.map +2 -2
- package/dist/miau-types/index.d.ts +0 -1
- package/dist/miau-types/types/Application.d.ts +0 -1
- package/dist/miau-types/types/Permission.d.ts +0 -1
- package/dist/miau-types/types/Request.d.ts +7 -0
- package/dist/miau-types/types/Secret.d.ts +1 -0
- package/package.json +5 -3
- package/scripts/prepare-publish.sh +0 -0
- package/src/MiauClient.ts +20 -12
- package/src/middleware.ts +23 -8
- package/.turbo/turbo-build.log +0 -9
- package/dist/miau-types/types/Organization.d.ts +0 -8
package/dist/MiauClient.d.ts
CHANGED
|
@@ -4,18 +4,24 @@ import { type RequestAugmentation } from './middleware';
|
|
|
4
4
|
type MiauClientConfig = {
|
|
5
5
|
apiUrl: string;
|
|
6
6
|
appSecret: string;
|
|
7
|
+
environment: string;
|
|
7
8
|
};
|
|
8
9
|
export declare class MiauClient {
|
|
9
10
|
private apiUrl;
|
|
11
|
+
private environment;
|
|
10
12
|
private jwtToken;
|
|
11
13
|
private jwksClient;
|
|
12
14
|
private basicAuthToken;
|
|
13
15
|
private permissionsCache;
|
|
14
16
|
private permissionsRequests;
|
|
15
|
-
constructor(
|
|
17
|
+
constructor(config: MiauClientConfig);
|
|
18
|
+
getEnvironment(): string;
|
|
16
19
|
getPublicKey(kid: string): Promise<string>;
|
|
17
20
|
getToken(): Promise<string | undefined>;
|
|
18
|
-
middleware<T = Record<string, string>>(
|
|
21
|
+
middleware<T = Record<string, string>>(config?: {
|
|
22
|
+
requestAugmentation?: RequestAugmentation<T>;
|
|
23
|
+
fallbackMidlleware?: RequestHandler;
|
|
24
|
+
}): RequestHandler;
|
|
19
25
|
getPermissions(targetAppId: string): Promise<Permission>;
|
|
20
26
|
private requestPermissions;
|
|
21
27
|
private getApiJwtUrl;
|
package/dist/index.js
CHANGED
|
@@ -11756,11 +11756,19 @@ var miauMiddleware = (miauClient, requestAugmentation, fallbackMidlleware) => {
|
|
|
11756
11756
|
throw new HttpError(400, "Invalid Token", "Missing kid in token header");
|
|
11757
11757
|
}
|
|
11758
11758
|
const publicKey = await miauClient.getPublicKey(decodedToken.header.kid);
|
|
11759
|
-
const
|
|
11760
|
-
if (!
|
|
11759
|
+
const clientToken = import_jsonwebtoken.default.verify(token, publicKey, { algorithms: ["RS256"] });
|
|
11760
|
+
if (!clientToken || !clientToken.application || !clientToken.secret || !clientToken.application.id || !clientToken.secret.id || !clientToken.secret.environment) {
|
|
11761
11761
|
throw new HttpError(400, "Invalid Token", "Token verification failed");
|
|
11762
11762
|
}
|
|
11763
|
-
const
|
|
11763
|
+
const { application, secret } = clientToken;
|
|
11764
|
+
if (secret.environment != miauClient.getEnvironment()) {
|
|
11765
|
+
throw new HttpError(
|
|
11766
|
+
400,
|
|
11767
|
+
"Invalid Environment",
|
|
11768
|
+
`Secret environment ${secret.environment} does not match client environment ${miauClient.getEnvironment()}`
|
|
11769
|
+
);
|
|
11770
|
+
}
|
|
11771
|
+
const permission = await miauClient.getPermissions(application.id);
|
|
11764
11772
|
if (!permission) {
|
|
11765
11773
|
throw new HttpError(401, "Unauthorized", "No permissions found for this application");
|
|
11766
11774
|
}
|
|
@@ -11771,16 +11779,14 @@ var miauMiddleware = (miauClient, requestAugmentation, fallbackMidlleware) => {
|
|
|
11771
11779
|
if (!isAllowed) {
|
|
11772
11780
|
throw new HttpError(403, "Forbidden", `You do not have permission to access ${req.method} ${req.path}`);
|
|
11773
11781
|
}
|
|
11774
|
-
req.miauApplication = { id:
|
|
11782
|
+
req.miauApplication = { id: application.id, name: application.name };
|
|
11775
11783
|
req.miauMetadata = permission?.metadata || {};
|
|
11776
11784
|
if (requestAugmentation) {
|
|
11777
|
-
console.log("Request augmentation is being applied");
|
|
11778
11785
|
requestAugmentation({ req, app: req.miauApplication, meta: req.miauMetadata });
|
|
11779
11786
|
}
|
|
11780
11787
|
next();
|
|
11781
11788
|
} catch (err) {
|
|
11782
11789
|
if (err instanceof HttpError && err.status == 400 && fallbackMidlleware) {
|
|
11783
|
-
console.log("Using fallback middleware for 400 error");
|
|
11784
11790
|
return fallbackMidlleware(req, res, next);
|
|
11785
11791
|
}
|
|
11786
11792
|
const errorStatus = err.status || 403;
|
|
@@ -11804,7 +11810,7 @@ var reusableFetch = async (input, init) => {
|
|
|
11804
11810
|
});
|
|
11805
11811
|
};
|
|
11806
11812
|
var MiauClient = class {
|
|
11807
|
-
constructor(
|
|
11813
|
+
constructor(config) {
|
|
11808
11814
|
this.permissionsCache = /* @__PURE__ */ new Map();
|
|
11809
11815
|
this.permissionsRequests = /* @__PURE__ */ new Map();
|
|
11810
11816
|
this.getApiJwtUrl = () => {
|
|
@@ -11816,11 +11822,15 @@ var MiauClient = class {
|
|
|
11816
11822
|
this.getJwksUrl = () => {
|
|
11817
11823
|
return `${this.apiUrl}/v1/jwks.json`;
|
|
11818
11824
|
};
|
|
11819
|
-
this.apiUrl =
|
|
11820
|
-
|
|
11821
|
-
const
|
|
11825
|
+
this.apiUrl = config.apiUrl;
|
|
11826
|
+
this.environment = config.environment;
|
|
11827
|
+
const apiKey = config.appSecret.substring(7, 32);
|
|
11828
|
+
const hashedSecret = import_node_crypto.default.createHash("sha256").update(config.appSecret).digest("hex");
|
|
11822
11829
|
this.basicAuthToken = Buffer.from(`${apiKey}:${hashedSecret}`).toString("base64");
|
|
11823
11830
|
}
|
|
11831
|
+
getEnvironment() {
|
|
11832
|
+
return this.environment;
|
|
11833
|
+
}
|
|
11824
11834
|
async getPublicKey(kid) {
|
|
11825
11835
|
if (!this.jwksClient) {
|
|
11826
11836
|
this.jwksClient = new import_jwks_rsa.JwksClient({ jwksUri: this.getJwksUrl(), cache: true });
|
|
@@ -11842,14 +11852,15 @@ var MiauClient = class {
|
|
|
11842
11852
|
"Content-Type": "application/json"
|
|
11843
11853
|
}
|
|
11844
11854
|
});
|
|
11855
|
+
const data = await response.json();
|
|
11845
11856
|
if (response.status !== 200) {
|
|
11846
|
-
throw new Error("Failed to fetch token");
|
|
11857
|
+
throw new Error(data.message || "Failed to fetch JWT token");
|
|
11847
11858
|
}
|
|
11848
|
-
this.jwtToken =
|
|
11859
|
+
this.jwtToken = data.jwt;
|
|
11849
11860
|
return this.jwtToken;
|
|
11850
11861
|
}
|
|
11851
|
-
middleware(
|
|
11852
|
-
return miauMiddleware(this, requestAugmentation, fallbackMidlleware);
|
|
11862
|
+
middleware(config) {
|
|
11863
|
+
return miauMiddleware(this, config?.requestAugmentation, config?.fallbackMidlleware);
|
|
11853
11864
|
}
|
|
11854
11865
|
async getPermissions(targetAppId) {
|
|
11855
11866
|
if (this.permissionsCache.has(targetAppId)) {
|