2
0
mirror of synced 2025-01-11 14:44:12 +00:00

move invalid parameters validation to javascript and remove validation from android

This commit is contained in:
Omer Levy 2017-10-09 03:42:57 +03:00
parent 29ebbbcc00
commit a085733e03
4 changed files with 83 additions and 46 deletions

View File

@ -196,23 +196,10 @@ public class RNFirebaseLinks extends ReactContextBaseJavaModule implements Activ
if (dynamicLinkInfo != null) { if (dynamicLinkInfo != null) {
try { try {
parametersBuilder.setLink(Uri.parse((String) dynamicLinkInfo.get("link"))); parametersBuilder.setLink(Uri.parse((String) dynamicLinkInfo.get("link")));
dynamicLinkInfo.remove("link");
parametersBuilder.setDynamicLinkDomain((String) dynamicLinkInfo.get("dynamicLinkDomain")); parametersBuilder.setDynamicLinkDomain((String) dynamicLinkInfo.get("dynamicLinkDomain"));
dynamicLinkInfo.remove("dynamicLinkDomain");
setAndroidParameters(dynamicLinkInfo, parametersBuilder); setAndroidParameters(dynamicLinkInfo, parametersBuilder);
dynamicLinkInfo.remove("androidInfo");
setIosParameters(dynamicLinkInfo, parametersBuilder); setIosParameters(dynamicLinkInfo, parametersBuilder);
dynamicLinkInfo.remove("iosInfo");
setSocialMetaTagParameters(dynamicLinkInfo, parametersBuilder); setSocialMetaTagParameters(dynamicLinkInfo, parametersBuilder);
dynamicLinkInfo.remove("socialMetaTagInfo");
if (dynamicLinkInfo.size() > 0) {
throw new IllegalArgumentException("Invalid arguments: " + dynamicLinkInfo.keySet().toString());
}
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "error while building parameters " + e.getMessage()); Log.e(TAG, "error while building parameters " + e.getMessage());
throw e; throw e;
@ -244,19 +231,12 @@ public class RNFirebaseLinks extends ReactContextBaseJavaModule implements Activ
DynamicLink.AndroidParameters.Builder androidParametersBuilder = DynamicLink.AndroidParameters.Builder androidParametersBuilder =
new DynamicLink.AndroidParameters.Builder((String) androidParameters.get("androidPackageName")); new DynamicLink.AndroidParameters.Builder((String) androidParameters.get("androidPackageName"));
androidParameters.remove("androidPackageName");
if (androidParameters.containsKey("androidFallbackLink")) { if (androidParameters.containsKey("androidFallbackLink")) {
androidParametersBuilder.setFallbackUrl(Uri.parse((String) androidParameters.get("androidFallbackLink"))); androidParametersBuilder.setFallbackUrl(Uri.parse((String) androidParameters.get("androidFallbackLink")));
androidParameters.remove("androidFallbackLink");
} }
if (androidParameters.containsKey("androidMinPackageVersionCode")) { if (androidParameters.containsKey("androidMinPackageVersionCode")) {
androidParametersBuilder.setMinimumVersion(Integer.parseInt((String) androidParameters.get("androidMinPackageVersionCode"))); androidParametersBuilder.setMinimumVersion(Integer.parseInt((String) androidParameters.get("androidMinPackageVersionCode")));
androidParameters.remove("androidMinPackageVersionCode");
}
if (androidParameters.size() > 0) {
throw new IllegalArgumentException("Invalid arguments: " + androidParameters.keySet().toString());
} }
parametersBuilder.setAndroidParameters(androidParametersBuilder.build()); parametersBuilder.setAndroidParameters(androidParametersBuilder.build());
} }
@ -270,35 +250,24 @@ public class RNFirebaseLinks extends ReactContextBaseJavaModule implements Activ
} }
DynamicLink.IosParameters.Builder iosParametersBuilder = DynamicLink.IosParameters.Builder iosParametersBuilder =
new DynamicLink.IosParameters.Builder((String) iosParameters.get("iosBundleId")); new DynamicLink.IosParameters.Builder((String) iosParameters.get("iosBundleId"));
iosParameters.remove("iosBundleId");
if (iosParameters.containsKey("iosAppStoreId")) { if (iosParameters.containsKey("iosAppStoreId")) {
iosParametersBuilder.setAppStoreId((String) iosParameters.get("iosAppStoreId")); iosParametersBuilder.setAppStoreId((String) iosParameters.get("iosAppStoreId"));
iosParameters.remove("iosAppStoreId");
} }
if (iosParameters.containsKey("iosCustomScheme")) { if (iosParameters.containsKey("iosCustomScheme")) {
iosParametersBuilder.setCustomScheme((String) iosParameters.get("iosCustomScheme")); iosParametersBuilder.setCustomScheme((String) iosParameters.get("iosCustomScheme"));
iosParameters.remove("iosCustomScheme");
} }
if (iosParameters.containsKey("iosFallbackLink")) { if (iosParameters.containsKey("iosFallbackLink")) {
iosParametersBuilder.setFallbackUrl(Uri.parse((String) iosParameters.get("iosFallbackLink"))); iosParametersBuilder.setFallbackUrl(Uri.parse((String) iosParameters.get("iosFallbackLink")));
iosParameters.remove("iosFallbackLink");
} }
if (iosParameters.containsKey("iosIpadBundleId")) { if (iosParameters.containsKey("iosIpadBundleId")) {
iosParametersBuilder.setIpadBundleId((String) iosParameters.get("iosIpadBundleId")); iosParametersBuilder.setIpadBundleId((String) iosParameters.get("iosIpadBundleId"));
iosParameters.remove("iosIpadBundleId");
} }
if (iosParameters.containsKey("iosIpadFallbackLink")) { if (iosParameters.containsKey("iosIpadFallbackLink")) {
iosParametersBuilder.setIpadFallbackUrl(Uri.parse((String) iosParameters.get("iosIpadFallbackLink"))); iosParametersBuilder.setIpadFallbackUrl(Uri.parse((String) iosParameters.get("iosIpadFallbackLink")));
iosParameters.remove("iosIpadFallbackLink");
} }
if (iosParameters.containsKey("iosMinPackageVersionCode")) { if (iosParameters.containsKey("iosMinPackageVersionCode")) {
iosParametersBuilder.setMinimumVersion((String) iosParameters.get("iosMinPackageVersionCode")); iosParametersBuilder.setMinimumVersion((String) iosParameters.get("iosMinPackageVersionCode"));
iosParameters.remove("iosMinPackageVersionCode");
}
if (iosParameters.size() > 0) {
throw new IllegalArgumentException("Invalid arguments: " + iosParameters.keySet().toString());
} }
parametersBuilder.setIosParameters(iosParametersBuilder.build()); parametersBuilder.setIosParameters(iosParametersBuilder.build());
} }
@ -312,19 +281,12 @@ public class RNFirebaseLinks extends ReactContextBaseJavaModule implements Activ
if (socialMetaTagParameters.containsKey("socialDescription")) { if (socialMetaTagParameters.containsKey("socialDescription")) {
socialMetaTagParametersBuilder.setDescription((String) socialMetaTagParameters.get("socialDescription")); socialMetaTagParametersBuilder.setDescription((String) socialMetaTagParameters.get("socialDescription"));
socialMetaTagParameters.remove("socialDescription");
} }
if (socialMetaTagParameters.containsKey("socialImageLink")) { if (socialMetaTagParameters.containsKey("socialImageLink")) {
socialMetaTagParametersBuilder.setImageUrl(Uri.parse((String) socialMetaTagParameters.get("socialImageLink"))); socialMetaTagParametersBuilder.setImageUrl(Uri.parse((String) socialMetaTagParameters.get("socialImageLink")));
socialMetaTagParameters.remove("socialImageLink");
} }
if (socialMetaTagParameters.containsKey("socialTitle")) { if (socialMetaTagParameters.containsKey("socialTitle")) {
socialMetaTagParametersBuilder.setTitle((String) socialMetaTagParameters.get("socialTitle")); socialMetaTagParametersBuilder.setTitle((String) socialMetaTagParameters.get("socialTitle"));
socialMetaTagParameters.remove("socialTitle");
}
if (socialMetaTagParameters.size() > 0) {
throw new IllegalArgumentException("Invalid arguments: " + socialMetaTagParameters.keySet().toString());
} }
parametersBuilder.setSocialMetaTagParameters(socialMetaTagParametersBuilder.build()); parametersBuilder.setSocialMetaTagParameters(socialMetaTagParametersBuilder.build());
} }

View File

@ -1,4 +1,5 @@
import ModuleBase from './../../utils/ModuleBase'; import ModuleBase from './../../utils/ModuleBase';
import { areObjectKeysContainedInOther } from './../../utils';
const EVENT_TYPE = { const EVENT_TYPE = {
Link: 'dynamic_link_received', Link: 'dynamic_link_received',
@ -43,6 +44,9 @@ export default class Links extends ModuleBase {
* @returns {Promise.<String>} * @returns {Promise.<String>}
*/ */
createDynamicLink(parameters: Object = {}): Promise<String> { createDynamicLink(parameters: Object = {}): Promise<String> {
if (!this._validateParameters(parameters)) {
return Promise.reject(new Error('Invalid Parameters.'));
}
return this._native.createDynamicLink(parameters); return this._native.createDynamicLink(parameters);
} }
@ -52,8 +56,43 @@ export default class Links extends ModuleBase {
* @returns {Promise.<String>} * @returns {Promise.<String>}
*/ */
createShortDynamicLink(parameters: Object = {}): Promise<String> { createShortDynamicLink(parameters: Object = {}): Promise<String> {
if (!this._validateParameters(parameters)) {
return Promise.reject(new Error('Invalid Parameters.'));
}
return this._native.createShortDynamicLink(parameters); return this._native.createShortDynamicLink(parameters);
} }
_validateParameters(parameters: Object): boolean {
const suportedParametersObject = {
dynamicLinkInfo: {
dynamicLinkDomain: 'string',
link: 'string',
androidInfo: {
androidPackageName: 'string',
androidFallbackLink: 'string',
androidMinPackageVersionCode: 'string',
androidLink: 'string',
},
iosInfo: {
iosBundleId: 'string',
iosFallbackLink: 'string',
iosCustomScheme: 'string',
iosIpadFallbackLink: 'string',
iosIpadBundleId: 'string',
iosAppStoreId: 'string',
},
socialMetaTagInfo: {
socialTitle: 'string',
socialDescription: 'string',
socialImageLink: 'string',
},
},
suffix: {
option: 'string',
},
};
return areObjectKeysContainedInOther(parameters, suportedParametersObject);
}
} }
export const statics = { export const statics = {

View File

@ -70,6 +70,43 @@ export function deepExists(object: Object,
return tmp !== undefined; return tmp !== undefined;
} }
/**
* Deep Check if obj1 keys are contained in obj2
* @param obj1
* @param obj2
* @returns {boolean}
*/
export function areObjectKeysContainedInOther(obj1 : Object, obj2: Object): boolean {
if (!isObject(obj1) || !isObject(obj2)) {
return false;
}
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (isArrayContainedInOther(keys1, keys2)) {
return keys1.filter((key) => {
return isObject(obj1[key]);
}).reduce((acc, cur) => {
return acc && areObjectKeysContainedInOther(obj1[cur], obj2[cur]);
}, true);
}
return false;
}
/**
* Check if arr1 is contained in arr2
* @param arr1
* @param arr2
* @returns {boolean}
*/
export function isArrayContainedInOther(arr1: Array, arr2: Array): boolean {
if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
return false;
}
return arr1.reduce((acc, cur) => {
return acc && arr2.includes(cur);
}, true);
}
/** /**
* Simple is object check. * Simple is object check.
* @param item * @param item

View File

@ -101,9 +101,8 @@ function linksTests({ describe, it, firebase, tryCatch }) {
reject(new Error('createDynamicLink did not fail.')); reject(new Error('createDynamicLink did not fail.'));
}, reject); }, reject);
const failure = tryCatch((error) => { const failure = tryCatch(() => {
// Assertion // Assertion
error.code.includes('links/failure').should.be.true();
resolve(); resolve();
}, reject); }, reject);
@ -126,7 +125,7 @@ function linksTests({ describe, it, firebase, tryCatch }) {
const failure = tryCatch((error) => { const failure = tryCatch((error) => {
// Assertion // Assertion
error.code.includes('links/failure').should.be.true(); error.message.should.equal('no iosBundleId was specified.');
resolve(); resolve();
}, reject); }, reject);
@ -172,7 +171,7 @@ function linksTests({ describe, it, firebase, tryCatch }) {
const failure = tryCatch((error) => { const failure = tryCatch((error) => {
// Assertion // Assertion
error.code.includes('links/failure').should.be.true(); error.message.should.equal('no androidPackageName was specified.');
resolve(); resolve();
}, reject); }, reject);
@ -218,7 +217,7 @@ function linksTests({ describe, it, firebase, tryCatch }) {
const failure = tryCatch((error) => { const failure = tryCatch((error) => {
// Assertion // Assertion
error.code.includes('links/failure').should.be.true(); error.message.should.equal('Invalid Parameters.');
resolve(); resolve();
}, reject); }, reject);
@ -247,7 +246,7 @@ function linksTests({ describe, it, firebase, tryCatch }) {
const failure = tryCatch((error) => { const failure = tryCatch((error) => {
// Assertion // Assertion
error.code.includes('links/failure').should.be.true(); error.message.should.equal('Invalid Parameters.');
resolve(); resolve();
}, reject); }, reject);
@ -283,7 +282,7 @@ function linksTests({ describe, it, firebase, tryCatch }) {
const failure = tryCatch((error) => { const failure = tryCatch((error) => {
// Assertion // Assertion
error.code.includes('links/failure').should.be.true(); error.message.should.equal('Invalid Parameters.');
resolve(); resolve();
}, reject); }, reject);
@ -319,7 +318,7 @@ function linksTests({ describe, it, firebase, tryCatch }) {
const failure = tryCatch((error) => { const failure = tryCatch((error) => {
// Assertion // Assertion
error.code.includes('links/failure').should.be.true(); error.message.should.equal('Invalid Parameters.');
resolve(); resolve();
}, reject); }, reject);