Skip to content

Commit

Permalink
Add push secret validation
Browse files Browse the repository at this point in the history
  • Loading branch information
K0IN committed Jul 22, 2023
1 parent 51ba83a commit 640c006
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
13 changes: 13 additions & 0 deletions app/backend/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,16 @@ export async function validateDeviceSecret<CTX extends RouterContext<A, B, C>, A
return;
}
}


export async function validatePushSecret<CTX extends RouterContext<A, B, C>, A extends string, B extends RouteParams<A>, C extends State>(context: CTX, next: () => Promise<unknown>) {
const { sendkey } = context.state;
const authHeader = context.request.headers.get('authorization');
if (!sendkey || validateAuthHeader(authHeader, sendkey)) {
return next();
} else {
const response = failure({ type: 'auth_required', message: 'Authorization header invalid' }, { status: 401 });
responseToContext(context, response)
return;
}
}
15 changes: 7 additions & 8 deletions app/backend/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ export async function serve(params: AppParameters, listen = true): Promise<Appli
}

const { port, vapidKey, sub, frontend, cors, sendkey, loginkey } = parsed.data;

const app = new Application();

app.state.vapidKey = vapidKey;
app.state.sub = sub;
app.state.frontend = frontend;
app.state.sendkey = sendkey;
app.state.loginkey = loginkey;

if (cors) {
app.use(oakCors());
Expand All @@ -35,13 +41,6 @@ export async function serve(params: AppParameters, listen = true): Promise<Appli
app.use(serveStaticFilesMiddleware);
}

// inject global settings
app.state.vapidKey = vapidKey;
app.state.sub = sub;
app.state.frontend = frontend;
app.state.sendkey = sendkey;
app.state.loginkey = loginkey;

console.log(`Listening on http://localhost:${port}/ Config: ${JSON.stringify(params)}`);

if (listen) {
Expand Down
3 changes: 2 additions & 1 deletion app/backend/routes/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { MessageValidator } from "../types/message.ts";
import { failure, success } from "../types/apiresponse.ts";
import { notifyAll } from "../logic/project/notify.ts";
import { toReturn } from "../util/oakreturn.ts";
import { validatePushSecret } from "../middleware/auth.ts";

export const notificationRouter = new Router({ prefix: '/notify' });

notificationRouter.post('/', // authFactory(SERVERPWD ?? AUTHPWD), // if no Server password is set, use the user password
notificationRouter.post('/', validatePushSecret,
toReturn(async (context): Promise<Response> => {
const body = context.request.body({ type: 'json' });
const rawMessage = await body.value;
Expand Down

0 comments on commit 640c006

Please sign in to comment.