Skip to content
This repository has been archived by the owner on May 13, 2021. It is now read-only.

Commit

Permalink
Implemented mjs_api_access setting for Windows 10 Hosted Web Apps pla…
Browse files Browse the repository at this point in the history
…tform
  • Loading branch information
mrodriguez committed Nov 18, 2015
1 parent 5714110 commit 00abe4b
Show file tree
Hide file tree
Showing 2 changed files with 332 additions and 36 deletions.
108 changes: 74 additions & 34 deletions lib/platformUtils/windows10Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,46 @@ var metadataItemTemplate= '\r\n\t\t<build:Item Name ="{0}" Version ="{1}" />';

var serviceEndpoint = 'http://cloudappx.azurewebsites.net';

var baseAcurMatch;

function findRuleByMatch(acurList, match) {
for (var i = 0; i < acurList.length; i++) {
if (acurList[i].match === match) {
return acurList[i];
}
}
}

function tryAddAcurToList(acurList, acur) {
// if match is '*', replace match with base match
if (acur.match === '*') {
acur.match = baseAcurMatch;
}

// if the match url ends with '/*', remove the '*'.
if (acur.match.indexOf('/*', acur.match.length - 2) !== -1) {
acur.match = acur.match.substring(0, acur.match.length - 1);
}

// ensure rule is not duplicated
var rule = findRuleByMatch(acurList, acur.match);
if (!rule) {
// if no type is specified in rule and access is 'none', ignore the rule
if (!acur.type && acur.runtimeAccess === 'none') {
return;
}

rule = { match: acur.match };
acurList.push(rule);
}

// override the runtimeAccess property (if any) or use default value ('none')
rule.runtimeAccess = acur.runtimeAccess || rule.runtimeAccess || 'none';

// override the type (if any) or use default value ('include')
rule.type = acur.type || rule.type || 'include';
}

function replaceManifestValues(w3cManifestInfo, content) {
var w3cManifest = w3cManifestInfo.content;
var timestamp = w3cManifestInfo.timestamp || new Date().toISOString().replace(/T/, ' ').replace(/\.[0-9]+/, ' ');
Expand Down Expand Up @@ -45,54 +85,54 @@ function replaceManifestValues(w3cManifestInfo, content) {

replacedContent = replacedContent.replace(/{MetadataItems}/g, metadataItems);

// Update access rules
// Update ACURs
var indentationChars = '\r\n\t\t\t\t';
var applicationContentUriRules = '';
var acurList = [];

// Set the base access rule using the start_url's base url
var baseUrlPattern = url.resolve(w3cManifest.start_url, '/');
var baseApiAccess = 'none';
// Set the base acur rule using the start_url's base url
baseAcurMatch = url.resolve(w3cManifest.start_url, '/');
if (w3cManifest.scope && w3cManifest.scope.length) {
// If the scope is defined, the base access rule is defined by the scope
var parsedScopeUrl = url.parse(w3cManifest.scope);

if (parsedScopeUrl.host && parsedScopeUrl.protocol) {
baseUrlPattern = w3cManifest.scope;
baseAcurMatch = w3cManifest.scope;
} else {
baseUrlPattern = url.resolve(baseUrlPattern, w3cManifest.scope);
baseAcurMatch = url.resolve(baseAcurMatch, w3cManifest.scope);
}
}

// If the base access rule ends with '/*', remove the '*'.
if (baseUrlPattern.indexOf('/*', baseUrlPattern.length - 2) !== -1) {
baseUrlPattern = baseUrlPattern.substring(0, baseUrlPattern.length - 1);
}
// Add base rule to ACUR list
tryAddAcurToList(acurList, { 'match': baseAcurMatch, 'type': 'include' });

var applicationContentUriRules = '';

// Add additional access rules
if (w3cManifest.mjs_access_whitelist && w3cManifest.mjs_access_whitelist instanceof Array) {
for (var j = 0; j < w3cManifest.mjs_access_whitelist.length; j++) {
var accessUrl = w3cManifest.mjs_access_whitelist[j].url;
// Ignore the '*' rule
if (accessUrl !== '*') {
// If the access url ends with '/*', remove the '*'.
if (accessUrl.indexOf('/*', accessUrl.length - 2) !== -1) {
accessUrl = accessUrl.substring(0, accessUrl.length - 1);
}

var apiAccess = w3cManifest.mjs_access_whitelist[j].apiAccess || 'none';

if (accessUrl === baseUrlPattern) {
baseApiAccess = apiAccess;
} else {
applicationContentUriRules += indentationChars + '<uap:Rule Type="include" WindowsRuntimeAccess="' + apiAccess + '" Match="' + accessUrl + '" />';
}
}
}
// Add rules from mjs_access_whitelist to ACUR list
if (w3cManifest.mjs_access_whitelist) {
w3cManifest.mjs_access_whitelist.forEach(function(whitelistRule) {
tryAddAcurToList(acurList, { 'match': whitelistRule.url, 'type': 'include', 'runtimeAccess': whitelistRule.apiAccess });
});
}

// TODO: Add rules from mjs_extended_scope to ACUR list

// Add rules from mjs_api_access to ACUR list
if (w3cManifest.mjs_api_access) {
w3cManifest.mjs_api_access.forEach(function (apiRule) {
// ensure rule applies to current platform
if (apiRule.platform && apiRule.platform.split(';')
.map(function (item) { return item.trim(); })
.indexOf('windows10') < 0) {
return false;
}

tryAddAcurToList(acurList, { match: apiRule.match, runtimeAccess: apiRule.access });
});
}

// Added base rule
applicationContentUriRules = '<uap:Rule Type="include" WindowsRuntimeAccess="' + baseApiAccess + '" Match="' + baseUrlPattern + '" />' + applicationContentUriRules;
// Create XML entries for ACUR rules
acurList.forEach(function (acur) {
applicationContentUriRules += indentationChars + '<uap:Rule Type="' + acur.type + '" WindowsRuntimeAccess="' + acur.runtimeAccess + '" Match="' + acur.match + '" />';
});

replacedContent = replacedContent.replace(/{ApplicationContentUriRules}/g, applicationContentUriRules);

Expand Down
Loading

0 comments on commit 00abe4b

Please sign in to comment.