Skip to content

Commit

Permalink
feat(buckets): add sign token to upload
Browse files Browse the repository at this point in the history
  • Loading branch information
sg-gs committed Dec 4, 2022
1 parent b829de7 commit 182408d
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lib/core/buckets/usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import _ from 'lodash';
import { sign as signJwt } from 'jsonwebtoken';
import { getEnv } from '../../server/env';

function signTokenWithSize(size: number) {
return signJwt({ payload: { size } }, getEnv().storage.jwtSecret);
function signTokenWith(payload: Record<string, unknown>) {
return signJwt({ payload }, getEnv().storage.jwtSecret, { expiresIn: '30d' });
}

export class BucketEntryNotFoundError extends Error {
Expand Down Expand Up @@ -289,7 +289,8 @@ export class BucketsUsecase {
cluster: string[],
uploads: { index: number; size: number }[],
auth: { username: string; password: string },
multiparts = 1
multiparts = 1,
withToken = true
) {
const [bucket, user] = await Promise.all([
this.bucketsRepository.findOne({ id: bucketId }),
Expand Down Expand Up @@ -386,7 +387,9 @@ export class BucketsUsecase {
index,
uuid,
url: null,
urls: urls.map((url) => url + '&token=' + signTokenWithSize(size)),
urls: withToken ? urls.map((url) => url + '?token=' + signTokenWith({
size, uuid
})) : urls,
UploadId
};
}
Expand All @@ -395,7 +398,9 @@ export class BucketsUsecase {
return {
index,
uuid,
url: objectStorageUrl + '&token=' + signTokenWithSize(size),
url: withToken ? objectStorageUrl + '?token=' + signTokenWith({
size, uuid
}): objectStorageUrl,
urls: null
};
});
Expand All @@ -409,13 +414,13 @@ export class BucketsUsecase {
auth: { username: string; password: string }
): Promise<string> {
const { address, port } = contact;
const farmerUrl = `http://${address}:${port}/v2/upload/link/${uuid}`;
const objectStorageUrl = `http://${address}:${port}/v2/upload/${uuid}`;

const { username, password } = auth;
const farmerRes = await axios.get<{ result: string }>(farmerUrl, {
auth: { username, password },
});
const objectStorageUrl = farmerRes.data.result;
// const { username, password } = auth;
// const farmerRes = await axios.get<{ result: string }>(farmerUrl, {
// auth: { username, password },
// });
// const objectStorageUrl = farmerRes.data.result;

return objectStorageUrl;
}
Expand Down

0 comments on commit 182408d

Please sign in to comment.