Skip to content
Permalink
Browse files
[CB-4094] Add whitelist API plugin
  • Loading branch information
clelland committed Jul 11, 2013
1 parent 7102bbf commit 2412466266d2529557a461c3383f8171f546d283
Showing 24 changed files with 4,297 additions and 0 deletions.
@@ -0,0 +1,4 @@
cordova-plugin-whitelist
------------------------

This is an optional JavaScript interface to the core Cordova Whitelist functionality. It is currently used by Mobile Spec to test the whitelist.
@@ -0,0 +1,51 @@
---
license: Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
---

Whitelist
=========

> The `whitelist` object provides an interface for testing whether arbitrary
> URLs are allowed by the currently active configuration, or would be allowed
> by a given set of whitelist patterns.
Methods
-------

- cordova.whitelist.match
- cordova.whitelist.test

### cordova.whitelist.match

Indicates whether a given URL would be allowed by a set of Whitelist URL
patterns.

cordova.whitelist.match(url, patterns, callback);

`callback` will be invoked with a boolean argument indicating whether the
url matches the set of patterns.

### cordova.whitelist.test

Indicates whether a given URL would be allowed by the current application
configuration.

cordova.whitelist.test(url, callback);

`callback` will be invoked with a boolean argument indicating whether the
url is currently whitelisted.
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>

<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0"
id="org.apache.cordova.test.whitelist"
version="0.1.0">
<name>Whitelist</name>

<js-module src="www/whitelist.js" name="whitelist">
<clobbers target="cordova.whitelist" />
</js-module>

<!-- android -->
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="WhitelistAPI" >
<param name="android-package" value="org.apache.cordova.test.WhitelistAPI"/>
</feature>
</config-file>

<source-file src="src/android/WhitelistAPI.java" target-dir="src/org/apache/cordova/test" />
</platform>

<!-- ios -->
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="WhitelistAPI">
<param name="ios-package" value="CDVWhitelistAPI"/>
</feature>
</config-file>

<header-file src="src/ios/CDVWhitelistAPI.h" />
<source-file src="src/ios/CDVWhitelistAPI.m" />
</platform>

</plugin>
@@ -0,0 +1,59 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package org.apache.cordova.test;

import org.apache.cordova.Whitelist;
import org.apache.cordova.Config;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;

public class WhitelistAPI extends CordovaPlugin {
/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArry of arguments for the plugin.
* @param callbackContext The callback id used when calling back into JavaScript.
* @return True if the action was valid, false if not.
*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("URLMatchesPatterns")) {
String url = args.getString(0);
JSONArray patterns = args.getJSONArray(1);
Whitelist whitelist = new Whitelist();
for (int i=0; i < patterns.length(); i++) {
String pattern = patterns.getString(i);
whitelist.addWhiteListEntry(pattern, false);
}
boolean isAllowed = whitelist.isUrlWhiteListed(url);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, isAllowed));
return true;
} else if (action.equals("URLIsAllowed")) {
String url = args.getString(0);
boolean isAllowed = Config.isUrlWhiteListed(url);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, isAllowed));
return true;
}
return false;
}
}
@@ -0,0 +1,29 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

#import <UIKit/UIKit.h>
#import <Cordova/CDVPlugin.h>

@interface CDVWhitelistAPI : CDVPlugin
{}

- (void)URLMatchesPatterns:(CDVInvokedUrlCommand*)command;
- (void)URLIsAllowed:(CDVInvokedUrlCommand*)command;

@end
@@ -0,0 +1,48 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

#import <Cordova/CDV.h>
#import "CDVWhitelistAPI.h"

@implementation CDVWhitelistAPI

- (void)URLMatchesPatterns:(CDVInvokedUrlCommand*)command
{
NSString *url = [command argumentAtIndex:0];
NSArray *allowedHosts = [command argumentAtIndex:1];

CDVWhitelist* whitelist = [[CDVWhitelist alloc] initWithArray:allowedHosts];

bool isAllowed = [whitelist URLIsAllowed:[NSURL URLWithString:url]];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:isAllowed];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)URLIsAllowed:(CDVInvokedUrlCommand*)command
{
NSString *url = [command argumentAtIndex:0];

CDVWhitelist* whitelist = [(CDVViewController *)self.viewController whitelist];

bool isAllowed = [whitelist URLIsAllowed:[NSURL URLWithString:url]];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:isAllowed];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

@end
@@ -0,0 +1,101 @@
jasmine.HtmlReporter = function(_doc) {
var self = this;
var doc = _doc || window.document;

var reporterView;

var dom = {};

// Jasmine Reporter Public Interface
self.logRunningSpecs = false;

self.reportRunnerStarting = function(runner) {
var specs = runner.specs() || [];

if (specs.length == 0) {
return;
}

createReporterDom(runner.env.versionString());
doc.body.appendChild(dom.reporter);

reporterView = new jasmine.HtmlReporter.ReporterView(dom);
reporterView.addSpecs(specs, self.specFilter);
};

self.reportRunnerResults = function(runner) {
reporterView && reporterView.complete();
};

self.reportSuiteResults = function(suite) {
reporterView.suiteComplete(suite);
};

self.reportSpecStarting = function(spec) {
if (self.logRunningSpecs) {
self.log('>> Jasmine Running ' + spec.suite.description + ' ' + spec.description + '...');
}
};

self.reportSpecResults = function(spec) {
reporterView.specComplete(spec);
};

self.log = function() {
var console = jasmine.getGlobal().console;
if (console && console.log) {
if (console.log.apply) {
console.log.apply(console, arguments);
} else {
console.log(arguments); // ie fix: console.log.apply doesn't exist on ie
}
}
};

self.specFilter = function(spec) {
if (!focusedSpecName()) {
return true;
}

return spec.getFullName().indexOf(focusedSpecName()) === 0;
};

return self;

function focusedSpecName() {
var specName;

(function memoizeFocusedSpec() {
if (specName) {
return;
}

var paramMap = [];
var params = doc.location.search.substring(1).split('&');

for (var i = 0; i < params.length; i++) {
var p = params[i].split('=');
paramMap[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
}

specName = paramMap.spec;
})();

return specName;
}

function createReporterDom(version) {
dom.reporter = self.createDom('div', { id: 'HTMLReporter', className: 'jasmine_reporter' },
dom.banner = self.createDom('div', { className: 'banner' },
self.createDom('span', { className: 'title' }, "Jasmine "),
self.createDom('span', { className: 'version' }, version)),

dom.symbolSummary = self.createDom('ul', {className: 'symbolSummary'}),
dom.alert = self.createDom('div', {className: 'alert'}),
dom.results = self.createDom('div', {className: 'results'},
dom.summary = self.createDom('div', { className: 'summary' }),
dom.details = self.createDom('div', { id: 'details' }))
);
}
};
jasmine.HtmlReporterHelpers.addHelpers(jasmine.HtmlReporter);
@@ -0,0 +1,60 @@
jasmine.HtmlReporterHelpers = {};

jasmine.HtmlReporterHelpers.createDom = function(type, attrs, childrenVarArgs) {
var el = document.createElement(type);

for (var i = 2; i < arguments.length; i++) {
var child = arguments[i];

if (typeof child === 'string') {
el.appendChild(document.createTextNode(child));
} else {
if (child) {
el.appendChild(child);
}
}
}

for (var attr in attrs) {
if (attr == "className") {
el[attr] = attrs[attr];
} else {
el.setAttribute(attr, attrs[attr]);
}
}

return el;
};

jasmine.HtmlReporterHelpers.getSpecStatus = function(child) {
var results = child.results();
var status = results.passed() ? 'passed' : 'failed';
if (results.skipped) {
status = 'skipped';
}

return status;
};

jasmine.HtmlReporterHelpers.appendToSummary = function(child, childElement) {
var parentDiv = this.dom.summary;
var parentSuite = (typeof child.parentSuite == 'undefined') ? 'suite' : 'parentSuite';
var parent = child[parentSuite];

if (parent) {
if (typeof this.views.suites[parent.id] == 'undefined') {
this.views.suites[parent.id] = new jasmine.HtmlReporter.SuiteView(parent, this.dom, this.views);
}
parentDiv = this.views.suites[parent.id].element;
}

parentDiv.appendChild(childElement);
};


jasmine.HtmlReporterHelpers.addHelpers = function(ctor) {
for(var fn in jasmine.HtmlReporterHelpers) {
ctor.prototype[fn] = jasmine.HtmlReporterHelpers[fn];
}
};

0 comments on commit 2412466

Please sign in to comment.