@authdog/remix-node 0.0.2
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/dist/index.d.ts +13 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +32 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as _remix_run_node from '@remix-run/node';
|
|
2
|
+
|
|
3
|
+
declare const remixAuthLoader: ({ request, context }: {
|
|
4
|
+
request: Request;
|
|
5
|
+
context: Record<string, any>;
|
|
6
|
+
}) => Promise<_remix_run_node.TypedResponse<{
|
|
7
|
+
user: any;
|
|
8
|
+
isAuthenticated: boolean;
|
|
9
|
+
}> | _remix_run_node.TypedResponse<{
|
|
10
|
+
loading: boolean;
|
|
11
|
+
}>>;
|
|
12
|
+
|
|
13
|
+
export { remixAuthLoader };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{json as a}from"@remix-run/node";var l=async({request:i,context:u})=>{var p;let d=process.env.PK_AUTHDOG;if(!d)throw new Error("Public key is not defined");if(!d.startsWith("pk_"))throw new Error("Invalid public key");let e=JSON.parse(Buffer.from(d.replace("pk_",""),"base64").toString("utf-8")),h=new URL(i.url).searchParams.get("token");if(h){let r=await fetch(`${e==null?void 0:e.identityHost}/oidc/${e==null?void 0:e.environmentId}/userinfo`,{headers:{authorization:`Bearer ${h}`}});if(!r.ok)throw new Error("Failed to fetch user info");let o=await r.json();if(o!=null&&o.meta&&((p=o==null?void 0:o.meta)==null?void 0:p.code)===200){let c=a({user:o.user,isAuthenticated:!0}),n=new Headers(c.headers),t=JSON.stringify(o==null?void 0:o.user),s=h;n.append("Set-Cookie",`user_session_${e==null?void 0:e.environmentId}=${encodeURIComponent(t)}; Path=/; HttpOnly; Secure; SameSite=Strict`),n.append("Set-Cookie",`user_session_hash_${e==null?void 0:e.environmentId}=${encodeURIComponent(s)}; Path=/; HttpOnly; Secure; SameSite=Strict`),n.append("Location",new URL(i.url).toString().replace(/([?&])token=[^&]*/,"$1").replace(/([?&])$/,"").replace(/([?&])$/,"")),u[`user_session_${e==null?void 0:e.environmentId}`]=t,u[`user_session_hash_${e==null?void 0:e.environmentId}`]=s;let m=new URL(i.url);return m.searchParams.delete("token"),n.append("Location",m.toString()),n.append("Cache-Control","no-store, no-cache, must-revalidate, proxy-revalidate"),n.append("Pragma","no-cache"),n.append("Expires","0"),n.append("Vary","Cookie"),n.append("Content-Type","application/json"),n.append("Access-Control-Allow-Origin","*"),n.append("Access-Control-Allow-Credentials","true"),a({user:o.user,isAuthenticated:!0},{headers:n})}}else try{let r=i.headers.get("Cookie");if(!r)return a({user:null,isAuthenticated:!1});let c=r.split(";").map(n=>{let[t,s]=n.trim().split("=");return{name:t,value:s}}).find(n=>n.name===`user_session_hash_${e==null?void 0:e.environmentId}`);if(c){let n=c.value,t=await fetch(`${e==null?void 0:e.identityHost}/oidc/${e==null?void 0:e.environmentId}/userinfo`,{headers:{authorization:`Bearer ${n}`}});if(!t.ok)throw new Error("Failed to fetch user info");let s=await t.json();return a({user:s.user,isAuthenticated:!0})}}catch(r){console.error("Error authenticating with cookies:",r)}return a({loading:!0})};export{l as remixAuthLoader};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n json\n} from \"@remix-run/node\";\n\nexport const remixAuthLoader = async ({ request, context }: { request: Request; context: Record<string, any> }) => { \n const publicKey = process.env.PK_AUTHDOG as string;\n \n if (!publicKey) {\n throw new Error(\"Public key is not defined\");\n }\n \n if (!publicKey.startsWith(\"pk_\")) {\n throw new Error(\"Invalid public key\");\n }\n \n // Decode Base64-encoded publicKey\n const publicKeyObj = JSON.parse(\n Buffer.from(publicKey.replace(\"pk_\", \"\"), \"base64\").toString(\"utf-8\"),\n );\n \n // First check if we have a token in the URL\n const tokenFromUri = new URL(request.url).searchParams.get(\"token\");\n \n if (tokenFromUri) {\n // Handle token from URL\n const userData = await fetch(\n `${publicKeyObj?.identityHost}/oidc/${publicKeyObj?.environmentId}/userinfo`,\n {\n headers: {\n authorization: `Bearer ${tokenFromUri}`,\n },\n },\n );\n \n if (!userData.ok) {\n throw new Error(\"Failed to fetch user info\");\n }\n \n const authenticatedUser = await userData.json();\n \n // console.log(\"authenticatedUser\", authenticatedUser);\n \n if (authenticatedUser?.meta && authenticatedUser?.meta?.code === 200) {\n // Create response with cookies\n const response = json({\n user: authenticatedUser.user,\n isAuthenticated: true,\n });\n \n // Set cookies in the response headers\n const headers = new Headers(response.headers);\n \n // Serialize the user object separately\n const userSessionValue = JSON.stringify(authenticatedUser?.user);\n const userSessionHashValue = tokenFromUri;\n \n // Create cookie instances\n // const userSessionCookie = createCookie(`user_session_${publicKeyObj?.environmentId}`, {\n // path: \"/\",\n // httpOnly: true,\n // secure: true,\n // sameSite: \"strict\",\n // });\n \n // const userSessionHashCookie = createCookie(`user_session_hash_${publicKeyObj?.environmentId}`, {\n // path: \"/\",\n // httpOnly: true,\n // secure: true,\n // sameSite: \"strict\",\n // });\n \n headers.append(\n \"Set-Cookie\",\n `user_session_${publicKeyObj?.environmentId}=${encodeURIComponent(userSessionValue)}; Path=/; HttpOnly; Secure; SameSite=Strict`,\n // await userSessionCookie.serialize(userSessionValue)\n );\n headers.append(\n \"Set-Cookie\",\n `user_session_hash_${publicKeyObj?.environmentId}=${encodeURIComponent(userSessionHashValue)}; Path=/; HttpOnly; Secure; SameSite=Strict`,\n // await userSessionHashCookie.serialize(userSessionHashValue)\n );\n \n // newUrl = url without token\n \n // newUrl.searchParams.delete(\"token\");\n \n headers.append(\n \"Location\",\n // same uri without token\n new URL(request.url)\n .toString()\n .replace(/([?&])token=[^&]*/, \"$1\")\n .replace(/([?&])$/, \"\")\n .replace(/([?&])$/, \"\"),\n // new URL(request.url).toString()\n // .replace(/([?&])token=[^&]*/, \"$1\")\n );\n \n // Store in context for later use\n context[`user_session_${publicKeyObj?.environmentId}`] = userSessionValue;\n context[`user_session_hash_${publicKeyObj?.environmentId}`] =\n userSessionHashValue;\n \n // redirect to new URL without token\n const newUrl = new URL(request.url);\n newUrl.searchParams.delete(\"token\");\n headers.append(\"Location\", newUrl.toString());\n \n headers.append(\n \"Cache-Control\",\n \"no-store, no-cache, must-revalidate, proxy-revalidate\",\n );\n headers.append(\"Pragma\", \"no-cache\");\n headers.append(\"Expires\", \"0\");\n headers.append(\"Vary\", \"Cookie\");\n headers.append(\"Content-Type\", \"application/json\");\n headers.append(\"Access-Control-Allow-Origin\", \"*\");\n headers.append(\"Access-Control-Allow-Credentials\", \"true\");\n \n return json(\n {\n user: authenticatedUser.user,\n isAuthenticated: true,\n },\n {\n headers,\n },\n );\n \n // return json({\n // user: authenticatedUser.user,\n // isAuthenticated: true\n // }, {\n // headers\n // });\n }\n \n // return json({\n // user: null,\n // isAuthenticated: false\n // });\n } else {\n // Try to authenticate using cookies\n try {\n // Get cookies from request\n const cookieHeader = request.headers.get(\"Cookie\");\n \n // console.log(cookieHeader);\n \n if (!cookieHeader) {\n return json({\n user: null,\n isAuthenticated: false,\n });\n }\n \n // Parse cookies\n const cookies = cookieHeader.split(\";\").map((cookie) => {\n const [name, value] = cookie.trim().split(\"=\");\n return { name, value };\n });\n \n // console.log(\"cookies\", cookies);\n \n // Find our specific cookies\n // const userSessionCookie = cookies.find(c => c.name === `user_session_${publicKeyObj?.environmentId}`);\n const userSessionHashCookie = cookies.find(\n (c) => c.name === `user_session_hash_${publicKeyObj?.environmentId}`,\n );\n \n if (userSessionHashCookie) {\n // console.log(\"has cookies\");\n \n // Decode the URL-encoded cookie values\n const userSessionHashValue = userSessionHashCookie.value;\n \n // console.log(\"userSessionValue\", userSessionValue);\n // console.log(\"userSessionHashValue\", userSessionHashValue);\n \n const userData = await fetch(\n `${publicKeyObj?.identityHost}/oidc/${publicKeyObj?.environmentId}/userinfo`,\n {\n headers: {\n authorization: `Bearer ${userSessionHashValue}`,\n },\n },\n );\n if (!userData.ok) {\n throw new Error(\"Failed to fetch user info\");\n }\n const authenticatedUser = await userData.json();\n \n return json({\n user: authenticatedUser.user,\n isAuthenticated: true,\n });\n }\n } catch (error) {\n console.error(\"Error authenticating with cookies:\", error);\n }\n }\n \n return json({\n loading: true,\n });\n };"],"mappings":"AAAA,OACI,QAAAA,MACG,kBAEA,IAAMC,EAAkB,MAAO,CAAE,QAAAC,EAAS,QAAAC,CAAQ,IAA0D,CAJnH,IAAAC,EAKI,IAAMC,EAAY,QAAQ,IAAI,WAE9B,GAAI,CAACA,EACH,MAAM,IAAI,MAAM,2BAA2B,EAG7C,GAAI,CAACA,EAAU,WAAW,KAAK,EAC7B,MAAM,IAAI,MAAM,oBAAoB,EAItC,IAAMC,EAAe,KAAK,MACxB,OAAO,KAAKD,EAAU,QAAQ,MAAO,EAAE,EAAG,QAAQ,EAAE,SAAS,OAAO,CACtE,EAGME,EAAe,IAAI,IAAIL,EAAQ,GAAG,EAAE,aAAa,IAAI,OAAO,EAElE,GAAIK,EAAc,CAEhB,IAAMC,EAAW,MAAM,MACrB,GAAGF,GAAA,YAAAA,EAAc,YAAY,SAASA,GAAA,YAAAA,EAAc,aAAa,YACjE,CACE,QAAS,CACP,cAAe,UAAUC,CAAY,EACvC,CACF,CACF,EAEA,GAAI,CAACC,EAAS,GACZ,MAAM,IAAI,MAAM,2BAA2B,EAG7C,IAAMC,EAAoB,MAAMD,EAAS,KAAK,EAI9C,GAAIC,GAAA,MAAAA,EAAmB,QAAQL,EAAAK,GAAA,YAAAA,EAAmB,OAAnB,YAAAL,EAAyB,QAAS,IAAK,CAEpE,IAAMM,EAAWV,EAAK,CACpB,KAAMS,EAAkB,KACxB,gBAAiB,EACnB,CAAC,EAGKE,EAAU,IAAI,QAAQD,EAAS,OAAO,EAGtCE,EAAmB,KAAK,UAAUH,GAAA,YAAAA,EAAmB,IAAI,EACzDI,EAAuBN,EAiB7BI,EAAQ,OACN,aACA,gBAAgBL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AE,OAAS,qBAAqBd,GAAA,YAAAA,EAAc,aAAa,EACpE,EAEA,GAAIU,EAAuB,CAIzB,IAAMH,EAAuBG,EAAsB,MAK7CR,EAAW,MAAM,MACrB,GAAGF,GAAA,YAAAA,EAAc,YAAY,SAASA,GAAA,YAAAA,EAAc,aAAa,YACjE,CACE,QAAS,CACP,cAAe,UAAUO,CAAoB,EAC/C,CACF,CACF,EACA,GAAI,CAACL,EAAS,GACZ,MAAM,IAAI,MAAM,2BAA2B,EAE7C,IAAMC,EAAoB,MAAMD,EAAS,KAAK,EAE9C,OAAOR,EAAK,CACV,KAAMS,EAAkB,KACxB,gBAAiB,EACnB,CAAC,CACH,CACF,OAASY,EAAO,CACd,QAAQ,MAAM,qCAAsCA,CAAK,CAC3D,CAGF,OAAOrB,EAAK,CACV,QAAS,EACX,CAAC,CACH","names":["json","remixAuthLoader","request","context","_a","publicKey","publicKeyObj","tokenFromUri","userData","authenticatedUser","response","headers","userSessionValue","userSessionHashValue","newUrl","cookieHeader","userSessionHashCookie","cookie","name","value","c","error"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@authdog/remix-node",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Authdog Remix SDK",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"source": "src/index.ts",
|
|
7
|
+
"main": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.mts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.mts",
|
|
12
|
+
"import": "./dist/index.mjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"registry": "https://registry.npmjs.org/",
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@remix-run/node": "^2.15.2"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"format": "prettier --config .prettierrc.json --write \"**/*.{ts,md}\"",
|
|
27
|
+
"type-check": "tsc",
|
|
28
|
+
"clean": "rm -rf dist",
|
|
29
|
+
"build": "pnpm clean && tsup",
|
|
30
|
+
"ship": "pnpm build && pnpm publish --access public --no-git-checks"
|
|
31
|
+
}
|
|
32
|
+
}
|