added hmac auth to report route

This commit is contained in:
Danny Coates
2020-07-25 15:36:09 -07:00
parent 2f6119e2f1
commit d9cbe058ab
7 changed files with 39 additions and 50 deletions

View File

@@ -61,7 +61,10 @@ async function fetchWithAuth(url, params, keychain) {
const result = {};
params = params || {};
const h = await keychain.authHeader();
params.headers = new Headers({ Authorization: h });
params.headers = new Headers({
Authorization: h,
'Content-Type': 'application/json'
});
const response = await fetch(url, params);
result.response = response;
result.ok = response.ok;
@@ -439,15 +442,19 @@ export async function getConstants() {
throw new Error(response.status);
}
export async function reportLink(id, key, reason) {
const response = await fetch(
export async function reportLink(id, keychain, reason) {
const result = await fetchWithAuthAndRetry(
getApiUrl(`/api/report/${id}`),
post({ key, reason })
{
method: 'POST',
body: JSON.stringify({ reason })
},
keychain
);
if (response.ok) {
if (result.ok) {
return;
}
throw new Error(response.status);
throw new Error(result.response.status);
}

View File

@@ -1,6 +1,5 @@
import FileSender from './fileSender';
import FileReceiver from './fileReceiver';
import { reportLink } from './api';
import { copyToClipboard, delay, openLinksInNewTab, percent } from './utils';
import * as metrics from './metrics';
import { bytes, locale } from './utils';
@@ -315,13 +314,7 @@ export default function(state, emitter) {
emitter.on('report', async ({ reason }) => {
try {
const file = state.fileInfo;
if (!file) {
// TODO
emitter.emit('pushState', '/error');
return render();
}
await reportLink(file.id, file.secretKey, reason);
await state.transfer.reportLink(reason);
render();
} catch (err) {
console.error(err);

View File

@@ -1,7 +1,7 @@
import Nanobus from 'nanobus';
import Keychain from './keychain';
import { delay, bytes, streamToArrayBuffer } from './utils';
import { downloadFile, metadata, getApiUrl } from './api';
import { downloadFile, metadata, getApiUrl, reportLink } from './api';
import { blobStream } from './streams';
import Zip from './zip';
@@ -53,6 +53,10 @@ export default class FileReceiver extends Nanobus {
this.state = 'ready';
}
async reportLink(reason) {
await reportLink(this.fileInfo.id, this.keychain, reason);
}
sendMessageToSw(msg) {
return new Promise((resolve, reject) => {
const channel = new MessageChannel();

View File

@@ -9,7 +9,7 @@ import contentDisposition from 'content-disposition';
let noSave = false;
const map = new Map();
const IMAGES = /.*\.(png|svg|jpg)$/;
const VERSIONED_ASSET = /\.[A-Fa-f0-9]{8}\.(js|css|png|svg|jpg)$/;
const VERSIONED_ASSET = /\.[A-Fa-f0-9]{8}\.(js|css|png|svg|jpg)(#\w+)?$/;
const DOWNLOAD_URL = /\/api\/download\/([A-Fa-f0-9]{4,})/;
const FONT = /\.woff2?$/;