@eduzz/miau-client 0.0.18 → 0.0.20
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 +9 -3
- package/dist/index.js +154 -16
- package/dist/index.js.map +3 -3
- package/dist/miau-types/index.d.ts +2 -1
- package/dist/miau-types/index.d.ts.map +1 -0
- package/dist/miau-types/types/Application.d.ts +2 -1
- package/dist/miau-types/types/Application.d.ts.map +1 -0
- package/dist/miau-types/types/Environments.d.ts +8 -0
- package/dist/miau-types/types/Environments.d.ts.map +1 -0
- package/dist/miau-types/types/Permission.d.ts +3 -2
- package/dist/miau-types/types/Permission.d.ts.map +1 -0
- package/dist/miau-types/types/Request.d.ts +10 -1
- package/dist/miau-types/types/Request.d.ts.map +1 -0
- package/dist/miau-types/types/Resource.d.ts +5 -4
- package/dist/miau-types/types/Resource.d.ts.map +1 -0
- package/dist/miau-types/types/Secret.d.ts +16 -0
- package/dist/miau-types/types/Secret.d.ts.map +1 -0
- package/package.json +3 -3
- package/scripts/prepare-publish.sh +0 -0
- package/src/MiauClient.ts +21 -14
- package/src/middleware.ts +25 -7
- package/.turbo/turbo-build.log +0 -9
- package/dist/miau-types/types/Organization.d.ts +0 -8
package/dist/MiauClient.d.ts
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
import { type RequestHandler } from 'express';
|
|
2
|
-
import { type Permission } from '@eduzz/miau-types';
|
|
2
|
+
import { type SecretEnv, type Permission } from '@eduzz/miau-types';
|
|
3
3
|
import { type RequestAugmentation } from './middleware';
|
|
4
4
|
type MiauClientConfig = {
|
|
5
5
|
apiUrl: string;
|
|
6
6
|
appSecret: string;
|
|
7
|
+
environment: SecretEnv;
|
|
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(): SecretEnv;
|
|
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
|
@@ -11715,14 +11715,133 @@ var index_exports = {};
|
|
|
11715
11715
|
__export(index_exports, {
|
|
11716
11716
|
HttpMethods: () => HttpMethods,
|
|
11717
11717
|
MiauClient: () => MiauClient,
|
|
11718
|
-
ResourceProtocols: () => ResourceProtocols
|
|
11718
|
+
ResourceProtocols: () => ResourceProtocols,
|
|
11719
|
+
envMap: () => envMap,
|
|
11720
|
+
envs: () => envs,
|
|
11721
|
+
expirationOptions: () => expirationOptions,
|
|
11722
|
+
expirationOptionsValues: () => expirationOptionsValues,
|
|
11723
|
+
expirationTimeToDate: () => expirationTimeToDate
|
|
11719
11724
|
});
|
|
11720
11725
|
module.exports = __toCommonJS(index_exports);
|
|
11721
11726
|
|
|
11722
11727
|
// ../types/src/types/Resource.ts
|
|
11723
|
-
var ResourceProtocols = ["http"
|
|
11728
|
+
var ResourceProtocols = ["http"];
|
|
11724
11729
|
var HttpMethods = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"];
|
|
11725
11730
|
|
|
11731
|
+
// ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/constants.js
|
|
11732
|
+
var daysInYear = 365.2425;
|
|
11733
|
+
var maxTime = Math.pow(10, 8) * 24 * 60 * 60 * 1e3;
|
|
11734
|
+
var minTime = -maxTime;
|
|
11735
|
+
var millisecondsInHour = 36e5;
|
|
11736
|
+
var secondsInHour = 3600;
|
|
11737
|
+
var secondsInDay = secondsInHour * 24;
|
|
11738
|
+
var secondsInWeek = secondsInDay * 7;
|
|
11739
|
+
var secondsInYear = secondsInDay * daysInYear;
|
|
11740
|
+
var secondsInMonth = secondsInYear / 12;
|
|
11741
|
+
var secondsInQuarter = secondsInMonth * 3;
|
|
11742
|
+
var constructFromSymbol = Symbol.for("constructDateFrom");
|
|
11743
|
+
|
|
11744
|
+
// ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/constructFrom.js
|
|
11745
|
+
function constructFrom(date, value) {
|
|
11746
|
+
if (typeof date === "function") return date(value);
|
|
11747
|
+
if (date && typeof date === "object" && constructFromSymbol in date)
|
|
11748
|
+
return date[constructFromSymbol](value);
|
|
11749
|
+
if (date instanceof Date) return new date.constructor(value);
|
|
11750
|
+
return new Date(value);
|
|
11751
|
+
}
|
|
11752
|
+
|
|
11753
|
+
// ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/toDate.js
|
|
11754
|
+
function toDate(argument, context) {
|
|
11755
|
+
return constructFrom(context || argument, argument);
|
|
11756
|
+
}
|
|
11757
|
+
|
|
11758
|
+
// ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/addDays.js
|
|
11759
|
+
function addDays(date, amount, options) {
|
|
11760
|
+
const _date = toDate(date, options?.in);
|
|
11761
|
+
if (isNaN(amount)) return constructFrom(options?.in || date, NaN);
|
|
11762
|
+
if (!amount) return _date;
|
|
11763
|
+
_date.setDate(_date.getDate() + amount);
|
|
11764
|
+
return _date;
|
|
11765
|
+
}
|
|
11766
|
+
|
|
11767
|
+
// ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/addMilliseconds.js
|
|
11768
|
+
function addMilliseconds(date, amount, options) {
|
|
11769
|
+
return constructFrom(options?.in || date, +toDate(date) + amount);
|
|
11770
|
+
}
|
|
11771
|
+
|
|
11772
|
+
// ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/addHours.js
|
|
11773
|
+
function addHours(date, amount, options) {
|
|
11774
|
+
return addMilliseconds(date, amount * millisecondsInHour, options);
|
|
11775
|
+
}
|
|
11776
|
+
|
|
11777
|
+
// ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/addMonths.js
|
|
11778
|
+
function addMonths(date, amount, options) {
|
|
11779
|
+
const _date = toDate(date, options?.in);
|
|
11780
|
+
if (isNaN(amount)) return constructFrom(options?.in || date, NaN);
|
|
11781
|
+
if (!amount) {
|
|
11782
|
+
return _date;
|
|
11783
|
+
}
|
|
11784
|
+
const dayOfMonth = _date.getDate();
|
|
11785
|
+
const endOfDesiredMonth = constructFrom(options?.in || date, _date.getTime());
|
|
11786
|
+
endOfDesiredMonth.setMonth(_date.getMonth() + amount + 1, 0);
|
|
11787
|
+
const daysInMonth = endOfDesiredMonth.getDate();
|
|
11788
|
+
if (dayOfMonth >= daysInMonth) {
|
|
11789
|
+
return endOfDesiredMonth;
|
|
11790
|
+
} else {
|
|
11791
|
+
_date.setFullYear(
|
|
11792
|
+
endOfDesiredMonth.getFullYear(),
|
|
11793
|
+
endOfDesiredMonth.getMonth(),
|
|
11794
|
+
dayOfMonth
|
|
11795
|
+
);
|
|
11796
|
+
return _date;
|
|
11797
|
+
}
|
|
11798
|
+
}
|
|
11799
|
+
|
|
11800
|
+
// ../../node_modules/.pnpm/date-fns@4.1.0/node_modules/date-fns/addYears.js
|
|
11801
|
+
function addYears(date, amount, options) {
|
|
11802
|
+
return addMonths(date, amount * 12, options);
|
|
11803
|
+
}
|
|
11804
|
+
|
|
11805
|
+
// ../types/src/types/Secret.ts
|
|
11806
|
+
var expirationOptions = {
|
|
11807
|
+
"Forever": "forever",
|
|
11808
|
+
"One hour": "oneHour",
|
|
11809
|
+
"Six hours": "sixHours",
|
|
11810
|
+
"Twelve hours": "twelveHours",
|
|
11811
|
+
"One day": "oneDay",
|
|
11812
|
+
"One week": "sevenDays",
|
|
11813
|
+
"One month": "oneMonth",
|
|
11814
|
+
"Six months": "sixMonths",
|
|
11815
|
+
"One year": "oneYear"
|
|
11816
|
+
};
|
|
11817
|
+
var expirationOptionsValues = Object.values(expirationOptions);
|
|
11818
|
+
var conversionMap = {
|
|
11819
|
+
forever: () => void 0,
|
|
11820
|
+
oneHour: () => addHours(/* @__PURE__ */ new Date(), 1),
|
|
11821
|
+
sixHours: () => addHours(/* @__PURE__ */ new Date(), 6),
|
|
11822
|
+
twelveHours: () => addHours(/* @__PURE__ */ new Date(), 12),
|
|
11823
|
+
oneDay: () => addDays(/* @__PURE__ */ new Date(), 1),
|
|
11824
|
+
sevenDays: () => addDays(/* @__PURE__ */ new Date(), 7),
|
|
11825
|
+
oneMonth: () => addMonths(/* @__PURE__ */ new Date(), 1),
|
|
11826
|
+
sixMonths: () => addMonths(/* @__PURE__ */ new Date(), 6),
|
|
11827
|
+
oneYear: () => addYears(/* @__PURE__ */ new Date(), 1)
|
|
11828
|
+
};
|
|
11829
|
+
var expirationTimeToDate = (option) => {
|
|
11830
|
+
return conversionMap[option]() ?? void 0;
|
|
11831
|
+
};
|
|
11832
|
+
|
|
11833
|
+
// ../types/src/types/Environments.ts
|
|
11834
|
+
var envs = {
|
|
11835
|
+
development: "Development",
|
|
11836
|
+
test: "Testing",
|
|
11837
|
+
production: "Production"
|
|
11838
|
+
};
|
|
11839
|
+
var envMap = {
|
|
11840
|
+
development: "d",
|
|
11841
|
+
test: "q",
|
|
11842
|
+
production: "p"
|
|
11843
|
+
};
|
|
11844
|
+
|
|
11726
11845
|
// src/MiauClient.ts
|
|
11727
11846
|
var import_node_crypto = __toESM(require("node:crypto"));
|
|
11728
11847
|
var import_jsonwebtoken2 = __toESM(require_jsonwebtoken());
|
|
@@ -11756,11 +11875,19 @@ var miauMiddleware = (miauClient, requestAugmentation, fallbackMidlleware) => {
|
|
|
11756
11875
|
throw new HttpError(400, "Invalid Token", "Missing kid in token header");
|
|
11757
11876
|
}
|
|
11758
11877
|
const publicKey = await miauClient.getPublicKey(decodedToken.header.kid);
|
|
11759
|
-
const
|
|
11760
|
-
if (!
|
|
11878
|
+
const clientToken = import_jsonwebtoken.default.verify(token, publicKey, { algorithms: ["RS256"] });
|
|
11879
|
+
if (!clientToken || !clientToken.application || !clientToken.secret || !clientToken.application.id || !clientToken.secret.id || !clientToken.secret.environment) {
|
|
11761
11880
|
throw new HttpError(400, "Invalid Token", "Token verification failed");
|
|
11762
11881
|
}
|
|
11763
|
-
const
|
|
11882
|
+
const { application, secret } = clientToken;
|
|
11883
|
+
if (secret.environment != miauClient.getEnvironment()) {
|
|
11884
|
+
throw new HttpError(
|
|
11885
|
+
400,
|
|
11886
|
+
"Invalid Environment",
|
|
11887
|
+
`Secret environment ${secret.environment} does not match client environment ${miauClient.getEnvironment()}`
|
|
11888
|
+
);
|
|
11889
|
+
}
|
|
11890
|
+
const permission = await miauClient.getPermissions(application.id);
|
|
11764
11891
|
if (!permission) {
|
|
11765
11892
|
throw new HttpError(401, "Unauthorized", "No permissions found for this application");
|
|
11766
11893
|
}
|
|
@@ -11771,8 +11898,9 @@ var miauMiddleware = (miauClient, requestAugmentation, fallbackMidlleware) => {
|
|
|
11771
11898
|
if (!isAllowed) {
|
|
11772
11899
|
throw new HttpError(403, "Forbidden", `You do not have permission to access ${req.method} ${req.path}`);
|
|
11773
11900
|
}
|
|
11774
|
-
req.miauApplication = { id:
|
|
11775
|
-
|
|
11901
|
+
req.miauApplication = { id: application.id, name: application.name };
|
|
11902
|
+
const environment = miauClient.getEnvironment();
|
|
11903
|
+
req.miauMetadata = permission?.metadata?.[environment] || {};
|
|
11776
11904
|
if (requestAugmentation) {
|
|
11777
11905
|
requestAugmentation({ req, app: req.miauApplication, meta: req.miauMetadata });
|
|
11778
11906
|
}
|
|
@@ -11802,7 +11930,7 @@ var reusableFetch = async (input, init) => {
|
|
|
11802
11930
|
});
|
|
11803
11931
|
};
|
|
11804
11932
|
var MiauClient = class {
|
|
11805
|
-
constructor(
|
|
11933
|
+
constructor(config) {
|
|
11806
11934
|
this.permissionsCache = /* @__PURE__ */ new Map();
|
|
11807
11935
|
this.permissionsRequests = /* @__PURE__ */ new Map();
|
|
11808
11936
|
this.getApiJwtUrl = () => {
|
|
@@ -11814,11 +11942,15 @@ var MiauClient = class {
|
|
|
11814
11942
|
this.getJwksUrl = () => {
|
|
11815
11943
|
return `${this.apiUrl}/v1/jwks.json`;
|
|
11816
11944
|
};
|
|
11817
|
-
this.apiUrl =
|
|
11818
|
-
|
|
11819
|
-
const
|
|
11945
|
+
this.apiUrl = config.apiUrl;
|
|
11946
|
+
this.environment = config.environment;
|
|
11947
|
+
const apiKey = config.appSecret.substring(7, 32);
|
|
11948
|
+
const hashedSecret = import_node_crypto.default.createHash("sha256").update(config.appSecret).digest("hex");
|
|
11820
11949
|
this.basicAuthToken = Buffer.from(`${apiKey}:${hashedSecret}`).toString("base64");
|
|
11821
11950
|
}
|
|
11951
|
+
getEnvironment() {
|
|
11952
|
+
return this.environment;
|
|
11953
|
+
}
|
|
11822
11954
|
async getPublicKey(kid) {
|
|
11823
11955
|
if (!this.jwksClient) {
|
|
11824
11956
|
this.jwksClient = new import_jwks_rsa.JwksClient({ jwksUri: this.getJwksUrl(), cache: true });
|
|
@@ -11840,14 +11972,15 @@ var MiauClient = class {
|
|
|
11840
11972
|
"Content-Type": "application/json"
|
|
11841
11973
|
}
|
|
11842
11974
|
});
|
|
11975
|
+
const data = await response.json();
|
|
11843
11976
|
if (response.status !== 200) {
|
|
11844
|
-
throw new Error("Failed to fetch token");
|
|
11977
|
+
throw new Error(data.message || "Failed to fetch JWT token");
|
|
11845
11978
|
}
|
|
11846
|
-
this.jwtToken =
|
|
11979
|
+
this.jwtToken = data.jwt;
|
|
11847
11980
|
return this.jwtToken;
|
|
11848
11981
|
}
|
|
11849
|
-
middleware(
|
|
11850
|
-
return miauMiddleware(this, requestAugmentation, fallbackMidlleware);
|
|
11982
|
+
middleware(config) {
|
|
11983
|
+
return miauMiddleware(this, config?.requestAugmentation, config?.fallbackMidlleware);
|
|
11851
11984
|
}
|
|
11852
11985
|
async getPermissions(targetAppId) {
|
|
11853
11986
|
if (this.permissionsCache.has(targetAppId)) {
|
|
@@ -11884,7 +12017,12 @@ var MiauClient = class {
|
|
|
11884
12017
|
0 && (module.exports = {
|
|
11885
12018
|
HttpMethods,
|
|
11886
12019
|
MiauClient,
|
|
11887
|
-
ResourceProtocols
|
|
12020
|
+
ResourceProtocols,
|
|
12021
|
+
envMap,
|
|
12022
|
+
envs,
|
|
12023
|
+
expirationOptions,
|
|
12024
|
+
expirationOptionsValues,
|
|
12025
|
+
expirationTimeToDate
|
|
11888
12026
|
});
|
|
11889
12027
|
/*! Bundled license information:
|
|
11890
12028
|
|