@cedarjs/auth-dbauth-middleware 7.0.0-rc.128 → 8.0.0-canary.3263

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/README.md CHANGED
@@ -39,6 +39,27 @@ export const ServerEntry: React.FC<Props> = ({ css, meta }) => {
39
39
  }
40
40
  ```
41
41
 
42
+ ### OAuth routing
43
+
44
+ Apps using [`@cedarjs/auth-dbauth-oauth`](../oauth) opt in by also passing an `oauthHandler`, built the same way `dbAuthHandler` is — by wrapping `OAuthHandler`'s `invoke()`:
45
+
46
+ ```tsx
47
+ import { OAuthHandler } from '@cedarjs/auth-dbauth-oauth'
48
+
49
+ import { handler as dbAuthHandler } from '$api/src/functions/auth'
50
+ import { oauthOptions } from '$api/src/lib/auth'
51
+
52
+ const authMw = initDbAuthMiddleware({
53
+ dbAuthHandler,
54
+ getCurrentUser,
55
+ oauthHandler: (req, context) =>
56
+ new OAuthHandler(req, context, oauthOptions).invoke(),
57
+ // oauthUrl? optional, defaults to '/auth/oauth' (OAuthHandlerOptions.basePath's default)
58
+ })
59
+ ```
60
+
61
+ Requests whose URL falls under `oauthUrl` (`authorize`/`callback`/`unlink`) are dispatched to `oauthHandler` instead of the normal dbAuth session-validation path; its 302 redirects, `Set-Cookie` headers (session, `auth-provider`, and the OAuth transaction cookie), and JSON bodies are carried over to the middleware response unchanged. Omitting `oauthHandler` entirely disables OAuth routing — requests under `oauthUrl` simply fall through to the normal dbAuth handling below.
62
+
42
63
  ### Roles handling
43
64
 
44
65
  By default the middleware assumes your roles will be in `currentUser.roles` - either as a string or an array of strings.
package/dist/index.d.ts CHANGED
@@ -8,9 +8,31 @@ export interface DbAuthMiddlewareOptions {
8
8
  cookieName?: string;
9
9
  dbAuthUrl?: string;
10
10
  dbAuthHandler: (req: Request | APIGatewayProxyEvent, context?: Context) => DbAuthResponse;
11
- getRoles?: (decoded: any) => string[];
11
+ /**
12
+ * Handler for dbAuth's OAuth routes (`/auth/oauth/...` by default),
13
+ * mirroring `dbAuthHandler`. Typically built by wrapping `OAuthHandler`
14
+ * from `@cedarjs/auth-dbauth-oauth`:
15
+ *
16
+ * ```ts
17
+ * oauthHandler: (req, context) =>
18
+ * new OAuthHandler(req, context, oauthOptions).invoke()
19
+ * ```
20
+ *
21
+ * Omitting this option leaves OAuth routing disabled entirely — requests
22
+ * under `oauthUrl` simply fall through to the normal dbAuth handling
23
+ * below.
24
+ */
25
+ oauthHandler?: (req: Request | APIGatewayProxyEvent, context?: Context) => DbAuthResponse;
26
+ /**
27
+ * URL segment routed to `oauthHandler`. Defaults to `/auth/oauth`,
28
+ * matching `OAuthHandlerOptions.basePath`'s default in
29
+ * `@cedarjs/auth-dbauth-oauth`. Only takes effect when `oauthHandler` is
30
+ * set.
31
+ */
32
+ oauthUrl?: string;
33
+ getRoles?: (decoded: Parameters<GetCurrentUser>[0]) => string[];
12
34
  getCurrentUser: GetCurrentUser;
13
35
  }
14
- export declare const initDbAuthMiddleware: ({ dbAuthHandler, getCurrentUser, getRoles, cookieName, dbAuthUrl, }: DbAuthMiddlewareOptions) => [Middleware, "*"];
36
+ export declare const initDbAuthMiddleware: ({ dbAuthHandler, oauthHandler, oauthUrl, getCurrentUser, getRoles, cookieName, dbAuthUrl, }: DbAuthMiddlewareOptions) => [Middleware, "*"];
15
37
  export default initDbAuthMiddleware;
16
38
  //# sourceMappingURL=index.d.ts.map
@@ -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,CA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,cAAc,CAAC,CAAC,CAAC,CAAC,KAAK,MAAM,EAAE,CAAA;IAC/D,cAAc,EAAE,cAAc,CAAA;CAC/B;AA6BD,eAAO,MAAM,oBAAoB,GAAI,6FAQlC,uBAAuB,KAAG,CAAC,UAAU,EAAE,GAAG,CA+G5C,CAAA;AA8DD,eAAe,oBAAoB,CAAA"}
package/dist/index.js CHANGED
@@ -1,7 +1,23 @@
1
1
  import { MiddlewareResponse } from "@cedarjs/web/middleware";
2
2
  import { defaultGetRoles } from "./defaultGetRoles.js";
3
+ function toMiddlewareResponse(output) {
4
+ const finalHeaders = new Headers();
5
+ Object.entries(output.headers).forEach(([key, value]) => {
6
+ if (Array.isArray(value)) {
7
+ value.forEach((mvhHeader) => finalHeaders.append(key, mvhHeader));
8
+ } else {
9
+ finalHeaders.append(key, value);
10
+ }
11
+ });
12
+ return new MiddlewareResponse(output.body, {
13
+ headers: finalHeaders,
14
+ status: output.statusCode
15
+ });
16
+ }
3
17
  const initDbAuthMiddleware = ({
4
18
  dbAuthHandler,
19
+ oauthHandler,
20
+ oauthUrl = "/auth/oauth",
5
21
  getCurrentUser,
6
22
  getRoles = defaultGetRoles,
7
23
  cookieName,
@@ -10,6 +26,15 @@ const initDbAuthMiddleware = ({
10
26
  const mw = async (req, res = MiddlewareResponse.next()) => {
11
27
  console.log("dbAuthUrl", dbAuthUrl);
12
28
  console.log("req.url", req.url);
29
+ const pathname = new URL(req.url).pathname;
30
+ let oauthBasePath = oauthUrl;
31
+ while (oauthBasePath.length > 1 && oauthBasePath.endsWith("/")) {
32
+ oauthBasePath = oauthBasePath.slice(0, -1);
33
+ }
34
+ if (oauthHandler && (pathname === oauthBasePath || pathname.startsWith(`${oauthBasePath}/`))) {
35
+ const output = await oauthHandler(req);
36
+ return toMiddlewareResponse(output);
37
+ }
13
38
  if (req.url.includes(dbAuthUrl)) {
14
39
  if (req.url.includes(`${dbAuthUrl}/currentUser`)) {
15
40
  const validatedSession2 = await validateSession({
@@ -27,18 +52,7 @@ const initDbAuthMiddleware = ({
27
52
  } else {
28
53
  const output = await dbAuthHandler(req);
29
54
  console.log("output", output);
30
- const finalHeaders = new Headers();
31
- Object.entries(output.headers).forEach(([key, value]) => {
32
- if (Array.isArray(value)) {
33
- value.forEach((mvhHeader) => finalHeaders.append(key, mvhHeader));
34
- } else {
35
- finalHeaders.append(key, value);
36
- }
37
- });
38
- return new MiddlewareResponse(output.body, {
39
- headers: finalHeaders,
40
- status: output.statusCode
41
- });
55
+ return toMiddlewareResponse(output);
42
56
  }
43
57
  }
44
58
  const cookieHeader = req.headers.get("Cookie");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/auth-dbauth-middleware",
3
- "version": "7.0.0-rc.128",
3
+ "version": "8.0.0-canary.3263",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/cedarjs/cedar.git",
@@ -32,13 +32,14 @@
32
32
  "test:watch": "vitest watch"
33
33
  },
34
34
  "dependencies": {
35
- "@cedarjs/auth-dbauth-api": "7.0.0-rc.128",
36
- "@cedarjs/web": "7.0.0-rc.128"
35
+ "@cedarjs/auth-dbauth-api": "8.0.0-canary.3263",
36
+ "@cedarjs/web": "8.0.0-canary.3263"
37
37
  },
38
38
  "devDependencies": {
39
- "@cedarjs/api": "7.0.0-rc.128",
40
- "@cedarjs/framework-tools": "7.0.0-rc.128",
41
- "@cedarjs/graphql-server": "7.0.0-rc.128",
39
+ "@cedarjs/api": "8.0.0-canary.3263",
40
+ "@cedarjs/auth-dbauth-oauth": "8.0.0-canary.3263",
41
+ "@cedarjs/framework-tools": "8.0.0-canary.3263",
42
+ "@cedarjs/graphql-server": "8.0.0-canary.3263",
42
43
  "@types/aws-lambda": "8.10.163",
43
44
  "concurrently": "9.2.4",
44
45
  "publint": "0.3.24",