@cedarjs/auth-dbauth-middleware 5.0.7-next.336 → 5.0.7-rc.3

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.
@@ -0,0 +1,2 @@
1
+ export declare const defaultGetRoles: (decoded: Record<string, any> | undefined | null) => string[];
2
+ //# sourceMappingURL=defaultGetRoles.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaultGetRoles.d.ts","sourceRoot":"","sources":["../../src/defaultGetRoles.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe,GAC1B,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,SAAS,GAAG,IAAI,KAC9C,MAAM,EAYR,CAAA"}
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var defaultGetRoles_exports = {};
20
+ __export(defaultGetRoles_exports, {
21
+ defaultGetRoles: () => defaultGetRoles
22
+ });
23
+ module.exports = __toCommonJS(defaultGetRoles_exports);
24
+ const defaultGetRoles = (decoded) => {
25
+ try {
26
+ const roles = decoded?.currentUser?.roles;
27
+ if (Array.isArray(roles)) {
28
+ return roles;
29
+ } else {
30
+ return roles ? [roles] : [];
31
+ }
32
+ } catch {
33
+ return [];
34
+ }
35
+ };
36
+ // Annotate the CommonJS export names for ESM import in node:
37
+ 0 && (module.exports = {
38
+ defaultGetRoles
39
+ });
@@ -0,0 +1,14 @@
1
+ import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
2
+ import type { DbAuthResponse } from '@cedarjs/auth-dbauth-api';
3
+ import type { GetCurrentUser } from '@cedarjs/graphql-server';
4
+ import type { Middleware } from '@cedarjs/web/middleware';
5
+ export interface DbAuthMiddlewareOptions {
6
+ cookieName?: string;
7
+ dbAuthUrl?: string;
8
+ dbAuthHandler: (req: Request | APIGatewayProxyEvent, context?: Context) => DbAuthResponse;
9
+ getRoles?: (decoded: any) => string[];
10
+ getCurrentUser: GetCurrentUser;
11
+ }
12
+ export declare const initDbAuthMiddleware: ({ dbAuthHandler, getCurrentUser, getRoles, cookieName, dbAuthUrl, }: DbAuthMiddlewareOptions) => [Middleware, "*"];
13
+ export default initDbAuthMiddleware;
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAE/D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA;AAG9D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AAE7D,OAAO,KAAK,EAAE,UAAU,EAAqB,MAAM,yBAAyB,CAAA;AAK5E,MAAM,WAAW,uBAAuB;IACtC,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,SAAS,CAAC,EAAE,MAAM,CAAA;IAGlB,aAAa,EAAE,CACb,GAAG,EAAE,OAAO,GAAG,oBAAoB,EACnC,OAAO,CAAC,EAAE,OAAO,KACd,cAAc,CAAA;IACnB,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,MAAM,EAAE,CAAA;IACrC,cAAc,EAAE,cAAc,CAAA;CAC/B;AAED,eAAO,MAAM,oBAAoB,GAAI,qEAMlC,uBAAuB,KAAG,CAAC,UAAU,EAAE,GAAG,CAkG5C,CAAA;AA2DD,eAAe,oBAAoB,CAAA"}
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var index_exports = {};
30
+ __export(index_exports, {
31
+ default: () => index_default,
32
+ initDbAuthMiddleware: () => initDbAuthMiddleware
33
+ });
34
+ module.exports = __toCommonJS(index_exports);
35
+ var import_auth_dbauth_api = __toESM(require("@cedarjs/auth-dbauth-api"), 1);
36
+ var import_middleware = require("@cedarjs/web/middleware");
37
+ var import_defaultGetRoles = require("./defaultGetRoles.js");
38
+ const { dbAuthSession, generateCookieName } = import_auth_dbauth_api.default;
39
+ const initDbAuthMiddleware = ({
40
+ dbAuthHandler,
41
+ getCurrentUser,
42
+ getRoles = import_defaultGetRoles.defaultGetRoles,
43
+ cookieName,
44
+ dbAuthUrl = "/middleware/dbauth"
45
+ }) => {
46
+ const mw = async (req, res = import_middleware.MiddlewareResponse.next()) => {
47
+ console.log("dbAuthUrl", dbAuthUrl);
48
+ console.log("req.url", req.url);
49
+ if (req.url.includes(dbAuthUrl)) {
50
+ if (req.url.includes(`${dbAuthUrl}/currentUser`)) {
51
+ const validatedSession2 = await validateSession({
52
+ req,
53
+ cookieName,
54
+ getCurrentUser
55
+ });
56
+ if (validatedSession2) {
57
+ return new import_middleware.MiddlewareResponse(
58
+ JSON.stringify({ currentUser: validatedSession2.currentUser })
59
+ );
60
+ } else {
61
+ return new import_middleware.MiddlewareResponse(JSON.stringify({ currentUser: null }));
62
+ }
63
+ } else {
64
+ const output = await dbAuthHandler(req);
65
+ console.log("output", output);
66
+ const finalHeaders = new Headers();
67
+ Object.entries(output.headers).forEach(([key, value]) => {
68
+ if (Array.isArray(value)) {
69
+ value.forEach((mvhHeader) => finalHeaders.append(key, mvhHeader));
70
+ } else {
71
+ finalHeaders.append(key, value);
72
+ }
73
+ });
74
+ return new import_middleware.MiddlewareResponse(output.body, {
75
+ headers: finalHeaders,
76
+ status: output.statusCode
77
+ });
78
+ }
79
+ }
80
+ const cookieHeader = req.headers.get("Cookie");
81
+ if (!cookieHeader?.includes("auth-provider")) {
82
+ return res;
83
+ }
84
+ const validatedSession = await validateSession({
85
+ req,
86
+ cookieName,
87
+ getCurrentUser
88
+ });
89
+ if (validatedSession) {
90
+ const { currentUser, decryptedSession } = validatedSession;
91
+ req.serverAuthState.set({
92
+ currentUser,
93
+ loading: false,
94
+ isAuthenticated: !!currentUser,
95
+ hasError: false,
96
+ userMetadata: currentUser,
97
+ // dbAuth doesn't have userMetadata
98
+ cookieHeader,
99
+ roles: getRoles(decryptedSession)
100
+ });
101
+ } else {
102
+ req.serverAuthState.clear();
103
+ res.cookies.unset(generateCookieName(cookieName));
104
+ res.cookies.unset("auth-provider");
105
+ }
106
+ return res;
107
+ };
108
+ return [mw, "*"];
109
+ };
110
+ async function validateSession({
111
+ req,
112
+ cookieName,
113
+ getCurrentUser
114
+ }) {
115
+ let decryptedSession;
116
+ try {
117
+ decryptedSession = dbAuthSession(req, generateCookieName(cookieName));
118
+ } catch (e) {
119
+ if (process.env.NODE_ENV === "development") {
120
+ console.debug("Could not decrypt dbAuth session", e);
121
+ }
122
+ return void 0;
123
+ }
124
+ if (!decryptedSession) {
125
+ if (process.env.NODE_ENV === "development") {
126
+ console.debug(
127
+ "No dbAuth session cookie found. Looking for a cookie named:",
128
+ cookieName
129
+ );
130
+ }
131
+ return void 0;
132
+ }
133
+ const currentUser = await getCurrentUser(
134
+ decryptedSession,
135
+ {
136
+ type: "dbAuth",
137
+ schema: "cookie",
138
+ // @MARK: We pass the entire cookie header as a token. This isn't
139
+ // actually the token!
140
+ // At this point the Cookie header is guaranteed, because otherwise a
141
+ // decryptionError would have been thrown
142
+ token: req.headers.get("Cookie")
143
+ },
144
+ {
145
+ event: req,
146
+ request: req
147
+ }
148
+ );
149
+ return { currentUser, decryptedSession };
150
+ }
151
+ var index_default = initDbAuthMiddleware;
152
+ // Annotate the CommonJS export names for ESM import in node:
153
+ 0 && (module.exports = {
154
+ initDbAuthMiddleware
155
+ });
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
package/dist/index.d.ts CHANGED
@@ -1,7 +1,5 @@
1
1
  import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
2
- import type { DbAuthResponse } from '@cedarjs/auth-dbauth-api' with {
3
- 'resolution-mode': 'import'
4
- };
2
+ import type { DbAuthResponse } from '@cedarjs/auth-dbauth-api';
5
3
  import type { GetCurrentUser } from '@cedarjs/graphql-server';
6
4
  import type { Middleware } from '@cedarjs/web/middleware';
7
5
  export interface DbAuthMiddlewareOptions {
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAE/D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;IAC7D,iBAAiB,EAAE,QAAQ;CAC5B,CAAA;AACD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AAE7D,OAAO,KAAK,EAAE,UAAU,EAAqB,MAAM,yBAAyB,CAAA;AAI5E,MAAM,WAAW,uBAAuB;IACtC,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,SAAS,CAAC,EAAE,MAAM,CAAA;IAGlB,aAAa,EAAE,CACb,GAAG,EAAE,OAAO,GAAG,oBAAoB,EACnC,OAAO,CAAC,EAAE,OAAO,KACd,cAAc,CAAA;IACnB,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,MAAM,EAAE,CAAA;IACrC,cAAc,EAAE,cAAc,CAAA;CAC/B;AAED,eAAO,MAAM,oBAAoB,GAAI,qEAMlC,uBAAuB,KAAG,CAAC,UAAU,EAAE,GAAG,CAmG5C,CAAA;AA8DD,eAAe,oBAAoB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAE/D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA;AAG9D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AAE7D,OAAO,KAAK,EAAE,UAAU,EAAqB,MAAM,yBAAyB,CAAA;AAK5E,MAAM,WAAW,uBAAuB;IACtC,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,SAAS,CAAC,EAAE,MAAM,CAAA;IAGlB,aAAa,EAAE,CACb,GAAG,EAAE,OAAO,GAAG,oBAAoB,EACnC,OAAO,CAAC,EAAE,OAAO,KACd,cAAc,CAAA;IACnB,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,MAAM,EAAE,CAAA;IACrC,cAAc,EAAE,cAAc,CAAA;CAC/B;AAED,eAAO,MAAM,oBAAoB,GAAI,qEAMlC,uBAAuB,KAAG,CAAC,UAAU,EAAE,GAAG,CAkG5C,CAAA;AA2DD,eAAe,oBAAoB,CAAA"}
package/dist/index.js CHANGED
@@ -1,4 +1,6 @@
1
+ import dbAuthApi from "@cedarjs/auth-dbauth-api";
1
2
  import { MiddlewareResponse } from "@cedarjs/web/middleware";
3
+ const { dbAuthSession, generateCookieName } = dbAuthApi;
2
4
  import { defaultGetRoles } from "./defaultGetRoles.js";
3
5
  const initDbAuthMiddleware = ({
4
6
  dbAuthHandler,
@@ -64,7 +66,6 @@ const initDbAuthMiddleware = ({
64
66
  });
65
67
  } else {
66
68
  req.serverAuthState.clear();
67
- const { generateCookieName } = await import("@cedarjs/auth-dbauth-api");
68
69
  res.cookies.unset(generateCookieName(cookieName));
69
70
  res.cookies.unset("auth-provider");
70
71
  }
@@ -78,7 +79,6 @@ async function validateSession({
78
79
  getCurrentUser
79
80
  }) {
80
81
  let decryptedSession;
81
- const { dbAuthSession, generateCookieName } = await import("@cedarjs/auth-dbauth-api");
82
82
  try {
83
83
  decryptedSession = dbAuthSession(req, generateCookieName(cookieName));
84
84
  } catch (e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/auth-dbauth-middleware",
3
- "version": "5.0.7-next.336",
3
+ "version": "5.0.7-rc.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/cedarjs/cedar.git",
@@ -10,9 +10,13 @@
10
10
  "type": "module",
11
11
  "exports": {
12
12
  ".": {
13
- "default": {
13
+ "import": {
14
14
  "types": "./dist/index.d.ts",
15
15
  "default": "./dist/index.js"
16
+ },
17
+ "require": {
18
+ "types": "./dist/cjs/index.d.ts",
19
+ "default": "./dist/cjs/index.js"
16
20
  }
17
21
  }
18
22
  },
@@ -25,6 +29,7 @@
25
29
  "build": "node ./build.mts",
26
30
  "build:pack": "yarn pack -o cedarjs-auth-dbauth-middleware.tgz",
27
31
  "build:types": "tsc --build --verbose ./tsconfig.build.json",
32
+ "build:types-cjs": "tsc --build --verbose tsconfig.cjs.json",
28
33
  "check:attw": "yarn cedar-fwtools-attw",
29
34
  "check:package": "concurrently npm:check:attw yarn:publint",
30
35
  "prepublishOnly": "NODE_ENV=production yarn build",
@@ -32,19 +37,20 @@
32
37
  "test:watch": "vitest watch"
33
38
  },
34
39
  "dependencies": {
35
- "@cedarjs/auth-dbauth-api": "5.0.7-next.336",
36
- "@cedarjs/web": "5.0.7-next.336"
40
+ "@cedarjs/auth-dbauth-api": "5.0.7-rc.3",
41
+ "@cedarjs/web": "5.0.7-rc.3"
37
42
  },
38
43
  "devDependencies": {
39
- "@cedarjs/api": "5.0.7-next.336",
40
- "@cedarjs/framework-tools": "5.0.7-next.336",
41
- "@cedarjs/graphql-server": "5.0.7-next.336",
44
+ "@arethetypeswrong/cli": "0.18.4",
45
+ "@cedarjs/api": "5.0.7-rc.3",
46
+ "@cedarjs/framework-tools": "5.0.7-rc.3",
47
+ "@cedarjs/graphql-server": "5.0.7-rc.3",
42
48
  "@types/aws-lambda": "8.10.162",
43
49
  "concurrently": "9.2.4",
44
- "publint": "0.3.23",
50
+ "publint": "0.3.21",
45
51
  "ts-toolbelt": "9.6.0",
46
52
  "typescript": "5.9.3",
47
- "vitest": "4.1.10"
53
+ "vitest": "3.2.6"
48
54
  },
49
55
  "engines": {
50
56
  "node": ">=24"