2 * Copyright 2013, 2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 var when = require('when');
20 var settingsAvailable;
22 function moduleSelector(aSettings) {
24 if (aSettings.storageModule) {
25 if (typeof aSettings.storageModule === "string") {
26 // TODO: allow storage modules to be specified by absolute path
27 toReturn = require("./"+aSettings.storageModule);
29 toReturn = aSettings.storageModule;
32 toReturn = require("./localfilesystem");
37 function is_malicious(path) {
38 return path.indexOf('../') != -1 || path.indexOf('..\\') != -1;
41 var storageModuleInterface = {
42 init: function(settings) {
44 storageModule = moduleSelector(settings);
45 settingsAvailable = storageModule.hasOwnProperty("getSettings") && storageModule.hasOwnProperty("saveSettings");
47 return when.reject(e);
49 return storageModule.init(settings);
51 getFlows: function() {
52 return storageModule.getFlows();
54 saveFlows: function(flows) {
55 return storageModule.saveFlows(flows);
57 getCredentials: function() {
58 return storageModule.getCredentials();
60 saveCredentials: function(credentials) {
61 return storageModule.saveCredentials(credentials);
63 getSettings: function() {
64 if (settingsAvailable) {
65 return storageModule.getSettings();
67 return when.resolve(null);
70 saveSettings: function(settings) {
71 if (settingsAvailable) {
72 return storageModule.saveSettings(settings);
74 return when.resolve();
77 /* Library Functions */
78 getAllFlows: function() {
79 return storageModule.getAllFlows();
81 getFlow: function(fn) {
82 if (is_malicious(fn)) {
83 return when.reject(new Error('forbidden flow name'));
85 return storageModule.getFlow(fn);
87 saveFlow: function(fn, data) {
88 if (is_malicious(fn)) {
89 return when.reject(new Error('forbidden flow name'));
91 return storageModule.saveFlow(fn, data);
93 getLibraryEntry: function(type, path) {
94 if (is_malicious(path)) {
95 return when.reject(new Error('forbidden flow name'));
97 return storageModule.getLibraryEntry(type, path);
99 saveLibraryEntry: function(type, path, meta, body) {
100 if (is_malicious(path)) {
101 return when.reject(new Error('forbidden flow name'));
103 return storageModule.saveLibraryEntry(type, path, meta, body);
107 module.exports = storageModuleInterface;