Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set x_mtok Cookie on Cordova #676

Merged
merged 8 commits into from
Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 10 additions & 18 deletions client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Mongo } from 'meteor/mongo';
import { Meteor } from 'meteor/meteor';
import { DDP } from 'meteor/ddp-client';
import { Cookies } from 'meteor/ostrio:cookies';
import { check, Match } from 'meteor/check';
import { UploadInstance } from './upload.js';
Expand Down Expand Up @@ -113,29 +114,20 @@ export class FilesCollection extends FilesCollectionCore {
}

const setTokenCookie = () => {
if ((!cookie.has('x_mtok') && Meteor.connection._lastSessionId) || (cookie.has('x_mtok') && (cookie.get('x_mtok') !== Meteor.connection._lastSessionId))) {
cookie.set('x_mtok', Meteor.connection._lastSessionId, {
path: '/'
});
}
};

const unsetTokenCookie = () => {
dr-dimitru marked this conversation as resolved.
Show resolved Hide resolved
if (cookie.has('x_mtok')) {
cookie.remove('x_mtok', '/');
if (Meteor.connection._lastSessionId) {
cookie.set('x_mtok', Meteor.connection._lastSessionId, { path: '/' });
if (Meteor.isCordova) {
cookie.send();
}
}
};

if (typeof Accounts !== 'undefined' && Accounts !== null) {
Meteor.startup(() => {
setTokenCookie();
});
Accounts.onLogin(() => {
setTokenCookie();
});
Accounts.onLogout(() => {
unsetTokenCookie();
DDP.onReconnect((conn) => {
conn.onReconnect = setTokenCookie;
});
Meteor.startup(setTokenCookie);
Accounts.onLogin(setTokenCookie);
}

check(this.onbeforeunloadMessage, Match.OneOf(String, Function));
Expand Down
19 changes: 18 additions & 1 deletion docs/constructor.md
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,24 @@
Use for security reasons when only upload from <em>Client</em> to <em>Server</em> usage is needed, and files shouldn't be downloaded by any user.
</td>
</tr>
<tr>
<tr>
<td align="right">
<code>config.allowedOrigins</code> {<em>Regex</em>|<em>Boolean</em>}
</td>
<td>
Server
</td>
<td>
Regex of Origins that are allowed CORS access or `false` to disable completely.
</td>
<td>
<code>/^http:\/\/localhost:12\d\d\d$/</code>
</td>
<td>
Defaults to `localhost:12000`-`localhost:13000` for allowing Meteor-Cordova builds access.
</td>
</tr>
<tr>
<td align="right">
<code>config._preCollection</code> {<em>Mongo.Collection</em>}
</td>
Expand Down
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Npm.depends({
Package.onUse(function(api) {
api.versionsFrom('1.6.1');
api.use('webapp', 'server');
api.use(['reactive-var', 'tracker', 'http'], 'client');
api.use(['reactive-var', 'tracker', 'http', 'ddp-client'], 'client');
api.use(['mongo', 'check', 'random', 'ecmascript', 'ostrio:cookies@2.3.0'], ['client', 'server']);
api.addAssets('worker.min.js', 'client');
api.mainModule('server.js', 'server');
Expand Down
25 changes: 24 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ const NOOP = () => { };
* @param config.downloadCallback {Function} - [Server] Callback triggered each time file is requested, return truthy value to continue download, or falsy to abort
* @param config.interceptDownload {Function} - [Server] Intercept download request, so you can serve file from third-party resource, arguments {http: {request: {...}, response: {...}}, fileRef: {...}}
* @param config.disableUpload {Boolean} - Disable file upload, useful for server only solutions
* @param config.disableDownload {Boolean} - Disable file download (serving), useful for file management only solutions
* @param config.disableDownload {Boolean} - Disable file download (serving), useful for file management only solutions
* @param config.allowedOrigins {Regex|Boolean} - [Server] Regex of Origins that are allowed CORS access or `false` to disable completely. Defaults to `localhost:12000`-`localhost:13000` for allowing Meteor-Cordova builds access.
* @param config._preCollection {Mongo.Collection} - [Server] Mongo preCollection Instance
* @param config._preCollectionName {String} - [Server] preCollection name
* @summary Create new instance of FilesCollection
Expand Down Expand Up @@ -90,6 +91,7 @@ export class FilesCollection extends FilesCollectionCore {
namingFunction: this.namingFunction,
responseHeaders: this.responseHeaders,
disableDownload: this.disableDownload,
allowedOrigins: this.allowedOrigins,
allowClientCode: this.allowClientCode,
downloadCallback: this.downloadCallback,
onInitiateUpload: this.onInitiateUpload,
Expand Down Expand Up @@ -205,6 +207,10 @@ export class FilesCollection extends FilesCollectionCore {
this.disableDownload = false;
}

if (!this.allowedOrigins) {
this.allowedOrigins = /^http:\/\/localhost:12\d\d\d$/;
}

if (!helpers.isObject(this._currentUploads)) {
this._currentUploads = {};
}
Expand Down Expand Up @@ -435,6 +441,23 @@ export class FilesCollection extends FilesCollectionCore {
return;
}
WebApp.connectHandlers.use((httpReq, httpResp, next) => {
if (this.allowedOrigins && !!~httpReq._parsedUrl.path.indexOf(`${this.downloadRoute}/`) && !httpResp.headersSent) {
if (this.allowedOrigins.test(httpReq.headers.origin)) {
httpResp.setHeader('Access-Control-Allow-Credentials', 'true');
httpResp.setHeader('Access-Control-Allow-Origin', httpReq.headers.origin);
dr-dimitru marked this conversation as resolved.
Show resolved Hide resolved
}

if (httpReq.method === 'OPTIONS') {
httpResp.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
httpResp.setHeader('Access-Control-Allow-Headers', 'Range, Content-Type, x-mtok, x-start, x-chunkid, x-fileid, x-eof');
s-ol marked this conversation as resolved.
Show resolved Hide resolved
httpResp.setHeader('Access-Control-Expose-Headers', 'Accept-Ranges, Content-Encoding, Content-Length, Content-Range');
dr-dimitru marked this conversation as resolved.
Show resolved Hide resolved
httpResp.setHeader('Allow', 'GET, POST, OPTIONS');
httpResp.writeHead(200);
httpResp.end();
return;
}
}

if (!this.disableUpload && !!~httpReq._parsedUrl.path.indexOf(`${this.downloadRoute}/${this.collectionName}/__upload`)) {
if (httpReq.method === 'POST') {
const handleError = (_error) => {
Expand Down
12 changes: 12 additions & 0 deletions upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ export class UploadInstance extends EventEmitter {
'x-fileid': opts.fileId,
'x-chunkid': opts.chunkId,
'content-type': 'text/plain'
},
beforeSend(xhr) {
xhr.withCredentials = true;
return true;
}
}, (error) => {
this.transferTime += +new Date() - this.startTime[opts.chunkId];
Expand Down Expand Up @@ -423,6 +427,10 @@ export class UploadInstance extends EventEmitter {
'x-mtok': (helpers.isObject(Meteor.connection) ? Meteor.connection._lastSessionId : void 0) || null,
'x-fileId': opts.fileId,
'content-type': 'text/plain'
},
beforeSend(xhr) {
xhr.withCredentials = true;
return true;
}
}, (error, _result) => {
let result;
Expand Down Expand Up @@ -607,6 +615,10 @@ export class UploadInstance extends EventEmitter {
headers: {
'x-start': '1',
'x-mtok': (helpers.isObject(Meteor.connection) ? Meteor.connection._lastSessionId : void 0) || null
},
beforeSend(xhr) {
xhr.withCredentials = true;
return true;
}
}, handleStart);
}
Expand Down