Skip to content

Commit 516c1be

Browse files
committed
Use aliases by default
1 parent f156d7c commit 516c1be

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

constants.ts

+8
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ export const MAILING_DOMAIN = "to.pocketletter.cc";
99

1010
export const SIGNING_METHOD = "SHA256";
1111
export const CODE_COOKIE_NAME = "code";
12+
13+
export type IPocketLetterEnv = {
14+
POCKET_CONSUMER_KEY: string;
15+
POCKET_TOKEN_KEY: string;
16+
SIGNING_SECRET: string;
17+
DATA: KVNamespace;
18+
ALIASES: KVNamespace;
19+
};

functions/digest.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Toucan from "toucan-js";
2-
import {POCKET_ADD_URL} from "../constants";
2+
import {POCKET_ADD_URL, IPocketLetterEnv} from "../constants";
33
import {signature, decrypt} from "../crypto";
44

55
// Adapted from https://stackoverflow.com/a/66481918/90297
@@ -13,14 +13,6 @@ const makeHTML = (title: string, body: string) =>
1313
`<!DOCTYPE html>\n<html><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1"/><title>${title}</title></head><body>${body}</body></html>`;
1414
const SUBJECT_CLEANER = /^((Re|Fwd?):\s*)+/i;
1515

16-
type IPocketLetterEnv = {
17-
POCKET_CONSUMER_KEY: string;
18-
POCKET_TOKEN_KEY: string;
19-
SIGNING_SECRET: string;
20-
DATA: KVNamespace;
21-
ALIASES: KVNamespace;
22-
};
23-
2416
class LinkFinder {
2517
textMatcher =
2618
/(?:read|view) (?:\w+ )*(?:browser|online|web(?: ?page)?)|webpage|Share/i;
@@ -123,9 +115,10 @@ export const onRequest: PagesFunction<IPocketLetterEnv> = async ({
123115
][0].split("@", 1);
124116
sentry.setUser({id: fromName});
125117

126-
const resolvedName = (await env.ALIASES.get(fromName)) || fromName;
118+
const access_token =
119+
(await env.ALIASES.get(fromName)) ||
120+
decrypt(fromName, env.POCKET_TOKEN_KEY);
127121

128-
const pocketToken = decrypt(resolvedName, env.POCKET_TOKEN_KEY);
129122
const {html, directLink} = await processHTML(rawHtml);
130123

131124
const url = directLink || (await storeLetter({env, request}, html));
@@ -134,8 +127,8 @@ export const onRequest: PagesFunction<IPocketLetterEnv> = async ({
134127
const postData = {
135128
url,
136129
title,
130+
access_token,
137131
tags: "pocketletter",
138-
access_token: pocketToken,
139132
consumer_key: env.POCKET_CONSUMER_KEY,
140133
};
141134

functions/token/callback.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import {
22
POCKET_OAUTH_AUTHORIZE_URL,
33
CODE_COOKIE_NAME,
44
MAILING_DOMAIN,
5+
IPocketLetterEnv,
56
} from "../../constants";
6-
import {encrypt} from "../../crypto";
77

8-
export const onRequest: PagesFunction<{
9-
POCKET_CONSUMER_KEY: string;
10-
POCKET_TOKEN_KEY: string;
11-
}> = async ({request, env}) => {
8+
export const onRequest: PagesFunction<IPocketLetterEnv> = async ({
9+
request,
10+
env,
11+
}) => {
1212
const cookies = Object.fromEntries(
1313
request.headers
1414
.get("Cookie")
@@ -32,13 +32,11 @@ export const onRequest: PagesFunction<{
3232
});
3333

3434
const {username, access_token} = await response.json();
35-
const address = `${encrypt(
36-
access_token,
37-
env.POCKET_TOKEN_KEY,
38-
)}@${MAILING_DOMAIN}`;
35+
await env.ALIASES.put(username, access_token);
36+
const address = `${username}@${MAILING_DOMAIN}`;
3937

4038
return new Response(
41-
`Hi ${username}, your PocketLetter address is ${address}`,
39+
`Hi ${username}, your just registered your PocketLetter address: ${address}`,
4240
{
4341
headers: {
4442
"Set-Cookie": `${CODE_COOKIE_NAME}=deleted; expires=Thu, 01 Jan 1970 00:00:00 GMT`,

0 commit comments

Comments
 (0)