Skip to content

Commit

Permalink
Use real env vars for access token
Browse files Browse the repository at this point in the history
  • Loading branch information
brookback committed Mar 18, 2024
1 parent 5581aa6 commit 53311c3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
24 changes: 16 additions & 8 deletions api/auth.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import { ProblemKind } from './problem.ts';
import { ProblemError } from './problem.ts';
import { getConfig } from './config.ts';
import { ProblemError, ProblemKind } from './problem.ts';

export interface Client {
id: string; // unique
name: string;
token: string; // unique
}

const IOS = {
const IOS: Client = {
id: 'ios-shortcut',
token: 'aaa',
name: 'iOS Shortcut',
token: getConfig('IOS_SHORTCUT_TOKEN'),
};

const MAC = {
const MAC: Client = {
id: 'mac-shortcut',
token: 'bbb',
name: 'Mac Shortcut',
token: getConfig('MAC_SHORTCUT_TOKEN'),
};

type Token = string;

const CLIENTS: Record<Token, Client> = {
'aaa': IOS,
'bbb': MAC,
// 🙃
[IOS.token]: IOS,
[MAC.token]: MAC,
};

export const checkAuth = (req: Request): Client => {
Expand All @@ -30,6 +34,10 @@ export const checkAuth = (req: Request): Client => {
throw new ProblemError(ProblemKind.BadInput, 'Missing important credentials');
}

if (!token.startsWith('API-Token')) {
throw new ProblemError(ProblemKind.BadInput, 'Malformed auth token');
}

const client = CLIENTS[token.replace('API-Token', '').trim()];

if (!client) {
Expand Down
5 changes: 5 additions & 0 deletions api/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
interface Config {
/** For authing with the GitHub API. */
GITHUB_TOKEN: string;

// Access tokens for this API
IOS_SHORTCUT_TOKEN: string;
MAC_SHORTCUT_TOKEN: string;
}

export const getConfig = <K extends keyof Config>(key: K, def?: Config[K]): Config[K] => {
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lume": "echo \"import 'lume/cli.ts'\" | deno run --unstable-temporal -A -",
"build": "deno task lume",
"serve": "deno task build -s",
"test": "deno test --allow-env --unstable-temporal --location https://johan.im",
"test": "export IOS_SHORTCUT_TOKEN=aaa && export MAC_SHORTCUT_TOKEN=aaa && TEST=1 deno test --allow-env --unstable-temporal --location https://johan.im",
"api": "deno run --unstable-temporal --allow-env --allow-net --allow-read --allow-write --location https://johan.im --watch api/server.ts"
},
"compilerOptions": {
Expand Down
4 changes: 2 additions & 2 deletions test/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Deno.test('API /post-note fails on bad auth header', async () => {
}),
);

assertEquals(res.status, 401);
assertEquals(res.status, 400);
const body = await res.text();
assertMatch(body, /Bad auth token/);
assertMatch(body, /Malformed auth token/);
});

0 comments on commit 53311c3

Please sign in to comment.