Save permissions grant / deny per domain

This commit is contained in:
Alice Gaudon 2020-04-01 13:39:20 +02:00
parent 2a3d8cf065
commit 42920d7260
3 changed files with 61 additions and 18 deletions

View File

@ -384,24 +384,56 @@ function loadService(serviceId, service) {
setContextMenu(webContents);
// Set permission request handler
session.fromPartition(service.view.partition)
.setPermissionRequestHandler(((webContents, permission, callback, details) => {
dialog.showMessageBox(remote.getCurrentWindow(), {
type: 'question',
title: 'Grant ' + permission + ' permission',
message: 'Do you wish to grant the ' + permission + ' permission to ' + details.requestingUrl + '?',
buttons: ['Deny', 'Authorize'],
cancelId: 0,
}).then(result => {
if (result.response === 1) {
console.log('Granted', permission, 'for service', details.requestingUrl);
callback(true);
} else {
console.log('Denied', permission, 'for service', details.requestingUrl);
callback(false);
}
}).catch(console.error);
}));
function getUrlDomain(url) {
let domain = url.match(/^https?:\/\/((.+?)\/|(.+))/i)[1];
if (domain.endsWith('/')) domain = domain.substr(0, domain.length - 1);
return domain;
}
function getDomainPermissions(domain) {
let domainPermissions = service.permissions[domain];
if (!domainPermissions) domainPermissions = service.permissions[domain] = [];
return domainPermissions;
}
let serviceSession = session.fromPartition(service.view.partition);
serviceSession.setPermissionRequestHandler(((webContents, permissionName, callback, details) => {
let domain = getUrlDomain(details.requestingUrl);
let domainPermissions = getDomainPermissions(domain);
let existingPermissions = domainPermissions.filter(p => p.name === permissionName);
if (existingPermissions.length > 0) {
callback(existingPermissions[0].authorized);
return;
}
dialog.showMessageBox(remote.getCurrentWindow(), {
type: 'question',
title: 'Grant ' + permissionName + ' permission',
message: 'Do you wish to grant the ' + permissionName + ' permission to ' + domain + '?',
buttons: ['Deny', 'Authorize'],
cancelId: 0,
}).then(result => {
const authorized = result.response === 1;
domainPermissions.push({
name: permissionName,
authorized: authorized,
});
updateServicePermissions(serviceId);
console.log(authorized ? 'Granted' : 'Denied', permissionName, 'for domain', domain);
callback(authorized);
}).catch(console.error);
}));
serviceSession.setPermissionCheckHandler((webContents1, permissionName, requestingOrigin, details) => {
console.log('Permission check', permissionName, requestingOrigin, details);
let domain = getUrlDomain(details.requestingUrl);
let domainPermissions = getDomainPermissions(domain);
let existingPermissions = domainPermissions.filter(p => p.name === permissionName);
return existingPermissions.length > 0 && existingPermissions[0].authorized;
});
service.view.setAttribute('src', service.url);
});
@ -469,6 +501,11 @@ function reloadService(serviceId) {
}
}
function updateServicePermissions(serviceId) {
const service = services[serviceId];
ipcRenderer.send('updateServicePermissions', serviceId, service.permissions);
}
function updateNavigation() {
console.debug('Updating navigation');
// Update active list element

View File

@ -36,6 +36,7 @@ Service.requiredProperties = {
'autoLoad': false,
'customCSS': null,
'customUserAgent': null,
'permissions': {},
};
export default Service;

View File

@ -183,6 +183,11 @@ function createWindow() {
config.save();
});
ipcMain.on('updateServicePermissions', (e, serviceId, permissions) => {
config.services[serviceId].permissions = permissions;
config.save();
});
ipcMain.on('updateWindowTitle', (event, serviceId, viewTitle) => {
if (serviceId === null) {
window.setTitle(Meta.title);