Skip to content

Commit

Permalink
Store auth header in ResumableUpload instead of app.locals
Browse files Browse the repository at this point in the history
  • Loading branch information
tohhsinpei committed Feb 17, 2022
1 parent 4518196 commit 30ad6d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
13 changes: 4 additions & 9 deletions src/emulator/storage/apis/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,14 +452,11 @@ export function createFirebaseEndpoints(emulator: StorageEmulator): Router {
req.params.bucketId,
name,
objectContentType,
req.body
req.body,
// Store auth header for use in the finalize request
req.header("authorization")
);

// Store auth header for use in the finalize request
const app = emulator.getApp();
app.locals.authHeader = app.locals.authHeader || {};
app.locals.authHeader[upload.uploadId] = req.header("authorization");

storageLayer.uploadBytes(upload.uploadId, Buffer.alloc(0));

const emulatorInfo = EmulatorRegistry.getInfo(Emulators.STORAGE);
Expand Down Expand Up @@ -542,16 +539,14 @@ export function createFirebaseEndpoints(emulator: StorageEmulator): Router {

res.header("x-goog-upload-status", "final");

const app = emulator.getApp();

// For resumable uploads, we check auth on finalization in case of byte-dependant rules
if (
!(await isPermitted({
ruleset: emulator.rules,
// TODO This will be either create or update
method: RulesetOperationMethod.CREATE,
path: operationPath,
authorization: app.locals.authHeader[uploadId],
authorization: upload.authorization,
file: {
after: storageLayer.getMetadata(req.params.bucketId, name)?.asRulesResource(),
},
Expand Down
20 changes: 17 additions & 3 deletions src/emulator/storage/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class ResumableUpload {
private _bucketId: string;
private _objectId: string;
private _contentType: string;
private _authorization: string | undefined;
private _currentBytesUploaded = 0;
private _status: UploadStatus = UploadStatus.ACTIVE;
private _fileLocation: string;
Expand All @@ -62,13 +63,15 @@ export class ResumableUpload {
objectId: string,
uploadId: string,
contentType: string,
metadata: IncomingMetadata
metadata: IncomingMetadata,
authorization?: string
) {
this._bucketId = bucketId;
this._objectId = objectId;
this._uploadId = uploadId;
this._contentType = contentType;
this._metadata = metadata;
this._authorization = authorization;
this._fileLocation = encodeURIComponent(`${uploadId}_b_${bucketId}_o_${objectId}`);
this._currentBytesUploaded = 0;
}
Expand All @@ -91,6 +94,9 @@ export class ResumableUpload {
public set contentType(contentType: string) {
this._contentType = contentType;
}
public get authorization(): string | undefined {
return this._authorization;
}
public get currentBytesUploaded(): number {
return this._currentBytesUploaded;
}
Expand Down Expand Up @@ -190,10 +196,18 @@ export class StorageLayer {
bucket: string,
object: string,
contentType: string,
metadata: IncomingMetadata
metadata: IncomingMetadata,
authorization?: string
): ResumableUpload {
const uploadId = v4();
const upload = new ResumableUpload(bucket, object, uploadId, contentType, metadata);
const upload = new ResumableUpload(
bucket,
object,
uploadId,
contentType,
metadata,
authorization
);
this._uploads.set(uploadId, upload);
return upload;
}
Expand Down

0 comments on commit 30ad6d6

Please sign in to comment.