aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2022-01-13 14:17:17 +0000
committers-ol <s+removethis@s-ol.nu>2022-01-13 14:17:17 +0000
commit0cd2992defecc563a9ac9b28f2dc91294096b73d (patch)
treeb2a8b19ff93bfcb35ef46b7d15bbb3a8e7afe04e
parentinitial commit - read-only DiscDag integration (diff)
downloadfedidag-0cd2992defecc563a9ac9b28f2dc91294096b73d.tar.gz
fedidag-0cd2992defecc563a9ac9b28f2dc91294096b73d.zip
better auth, list
-rw-r--r--main.ts66
1 files changed, 51 insertions, 15 deletions
diff --git a/main.ts b/main.ts
index d8767b0..0bb805d 100644
--- a/main.ts
+++ b/main.ts
@@ -13,13 +13,15 @@ const PUBLIC_URL = 'http://localhost:3000';
const app = express();
-app.use(cors({ origin: 'http://localhost:8080' }));
+app.use(cors({ origin: 'http://localhost:8080', credentials: true }));
app.use(express.json());
app.use(cookieParser());
const discdag = express()
.post('/login', wrap(async ($req, $res) => {
const { username, password } = $req.body;
+ if (!username || !password)
+ throw new Error("no credentials");
const res = await fetch(DISCDAG_URL, {
method: 'POST',
@@ -36,24 +38,58 @@ const discdag = express()
if (res.status !== 200)
throw new Error("bad response");
- const cookies = (res.headers.get('set-cookie') ?? '').split(' ');
- let digest: string | null = null;
- for (const cookiestr of cookies) {
- if (!cookiestr.startsWith('digest=')) continue;
+ const cookiestr = res.headers.get('set-cookie') ?? '';
+ const match = cookiestr.match(/digest=([^ ]+);/);
+ if (!match || !match[1].length) throw new Error("wrong credentials");
- digest = cookiestr.split('=')[1];
- break;
- }
+ const doc = cheerio.load(await res.text());
+ const name = doc("font b").text().match(/browsing as (.*)$/)![1];
+ const user = `${PUBLIC_URL}/discdag/user/${name}`;
+
+ return $res
+ .cookie('digest', match[1])
+ .json({
+ '@context': 'https://www.w3.org/ns/activitystreams',
+ id: user,
+ type: 'User',
+ name,
+ });
+ }))
+
+ .get('/list', wrap(async ($req, $res) => {
+ const { digest } = $req.cookies;
- if (!digest)
- throw new Error("no digest cookie");
+ const res = await fetch(DISCDAG_URL + '?Action=ListDiscussions', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/x-www-form-urlencoded',
+ 'Cookie': `digest=${digest}`,
+ },
+ });
- const body = await res.text();
- const discussions = [...body.matchAll(/"\/cgi-bin\/DiscDAG.py\?DiscussionID=(.+?)"/g)].map(m => m[1]);
+ const doc = cheerio.load(await res.text());
+ const name = doc("font b").text().match(/browsing as (.*)$/)![1];
+ const user = `${PUBLIC_URL}/discdag/user/${name}`;
+
+ const discussions = doc("ul > li").map(function(this: any) {
+ const writeAccess = this.children[0].data.indexOf('(W)') > -1;
+ const href = doc("a", this).attr("href")!;
+ const id = href.match(/cgi-bin\/DiscDAG.py\?DiscussionID=(.+)/)![1];
+
+ return {
+ id: `${PUBLIC_URL}/discdag/${id}`,
+ type: 'Document',
+ name: id,
+ url: {
+ type: 'Link',
+ href,
+ },
+ attributedTo: writeAccess ? user : null,
+ audience: user,
+ };
+ }).toArray();
- return $res
- .cookie('digest', digest)
- .json({ discussions });
+ return $res.json({ discussions });
}))
.get('/user/:name', wrap(async ($req, $res) => {