@eduzz/miau-client 0.0.18 → 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.
@@ -1,4 +1,4 @@
1
1
 
2
- > @eduzz/miau-client@0.0.18 build:types /home/runner/work/eduzz-miau/eduzz-miau/packages/client
2
+ > @eduzz/miau-client@0.0.19 build:types /home/runner/work/eduzz-miau/eduzz-miau/packages/client
3
3
  > tsc --emitDeclarationOnly --outDir dist
4
4
 
@@ -1,4 +1,4 @@
1
1
 
2
- > @eduzz/miau-client@0.0.18 prepublish /home/runner/work/eduzz-miau/eduzz-miau/packages/client
2
+ > @eduzz/miau-client@0.0.19 prepublish /home/runner/work/eduzz-miau/eduzz-miau/packages/client
3
3
  > sh ./scripts/prepare-publish.sh
4
4
 
@@ -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(props: MiauClientConfig);
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>>(requestAugmentation?: RequestAugmentation<T>, fallbackMidlleware?: RequestHandler): RequestHandler;
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 appToken = import_jsonwebtoken.default.verify(token, publicKey, { algorithms: ["RS256"] });
11760
- if (!appToken || !appToken.id || !appToken.name) {
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 permission = await miauClient.getPermissions(appToken.id);
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,7 +11779,7 @@ 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: appToken?.id, name: appToken?.name };
11782
+ req.miauApplication = { id: application.id, name: application.name };
11775
11783
  req.miauMetadata = permission?.metadata || {};
11776
11784
  if (requestAugmentation) {
11777
11785
  requestAugmentation({ req, app: req.miauApplication, meta: req.miauMetadata });
@@ -11802,7 +11810,7 @@ var reusableFetch = async (input, init) => {
11802
11810
  });
11803
11811
  };
11804
11812
  var MiauClient = class {
11805
- constructor(props) {
11813
+ constructor(config) {
11806
11814
  this.permissionsCache = /* @__PURE__ */ new Map();
11807
11815
  this.permissionsRequests = /* @__PURE__ */ new Map();
11808
11816
  this.getApiJwtUrl = () => {
@@ -11814,11 +11822,15 @@ var MiauClient = class {
11814
11822
  this.getJwksUrl = () => {
11815
11823
  return `${this.apiUrl}/v1/jwks.json`;
11816
11824
  };
11817
- this.apiUrl = props.apiUrl;
11818
- const apiKey = props.appSecret.substring(7, 32);
11819
- const hashedSecret = import_node_crypto.default.createHash("sha256").update(props.appSecret).digest("hex");
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");
11820
11829
  this.basicAuthToken = Buffer.from(`${apiKey}:${hashedSecret}`).toString("base64");
11821
11830
  }
11831
+ getEnvironment() {
11832
+ return this.environment;
11833
+ }
11822
11834
  async getPublicKey(kid) {
11823
11835
  if (!this.jwksClient) {
11824
11836
  this.jwksClient = new import_jwks_rsa.JwksClient({ jwksUri: this.getJwksUrl(), cache: true });
@@ -11840,14 +11852,15 @@ var MiauClient = class {
11840
11852
  "Content-Type": "application/json"
11841
11853
  }
11842
11854
  });
11855
+ const data = await response.json();
11843
11856
  if (response.status !== 200) {
11844
- throw new Error("Failed to fetch token");
11857
+ throw new Error(data.message || "Failed to fetch JWT token");
11845
11858
  }
11846
- this.jwtToken = (await response.json()).jwt;
11859
+ this.jwtToken = data.jwt;
11847
11860
  return this.jwtToken;
11848
11861
  }
11849
- middleware(requestAugmentation, fallbackMidlleware) {
11850
- return miauMiddleware(this, requestAugmentation, fallbackMidlleware);
11862
+ middleware(config) {
11863
+ return miauMiddleware(this, config?.requestAugmentation, config?.fallbackMidlleware);
11851
11864
  }
11852
11865
  async getPermissions(targetAppId) {
11853
11866
  if (this.permissionsCache.has(targetAppId)) {