Source: encrypt/group-manager-db.js

  1. /**
  2. * Copyright (C) 2015-2018 Regents of the University of California.
  3. * @author: Jeff Thompson <jefft0@remap.ucla.edu>
  4. * @author: From ndn-group-encrypt src/group-manager-db https://github.com/named-data/ndn-group-encrypt
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * A copy of the GNU Lesser General Public License is in the file COPYING.
  19. */
  20. /** @ignore */
  21. var SyncPromise = require('../util/sync-promise.js').SyncPromise;
  22. /**
  23. * GroupManagerDb is a base class for the storage of data used by the
  24. * GroupManager. It contains two tables to store Schedules and Members.
  25. * This is an abstract base class. A subclass must implement the methods.
  26. * For example, see Sqlite3GroupManagerDb (for Nodejs) or IndexedDbGroupManagerDb
  27. * (for the browser).
  28. * @note This class is an experimental feature. The API may change.
  29. * @constructor
  30. */
  31. var GroupManagerDb = function GroupManagerDb()
  32. {
  33. };
  34. exports.GroupManagerDb = GroupManagerDb;
  35. /**
  36. * Create a new GroupManagerDb.Error to report an error using GroupManagerDb
  37. * methods, wrapping the given error object.
  38. * Call with: throw new GroupManagerDb.Error(new Error("message")).
  39. * @constructor
  40. * @param {Error} error The exception created with new Error.
  41. */
  42. GroupManagerDb.Error = function GroupManagerDbError(error)
  43. {
  44. if (error) {
  45. error.__proto__ = GroupManagerDb.Error.prototype;
  46. return error;
  47. }
  48. };
  49. GroupManagerDb.Error.prototype = new Error();
  50. GroupManagerDb.Error.prototype.name = "GroupManagerDbError";
  51. ////////////////////////////////////////////////////// Schedule management.
  52. /**
  53. * Check if there is a schedule with the given name.
  54. * @param {string} name The name of the schedule.
  55. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  56. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  57. * an async Promise.
  58. * @return {Promise|SyncPromise} A promise that returns true if there is a
  59. * schedule (else false), or that is rejected with GroupManagerDb.Error for a
  60. * database error.
  61. */
  62. GroupManagerDb.prototype.hasSchedulePromise = function(name, useSync)
  63. {
  64. return SyncPromise.reject(new Error
  65. ("GroupManagerDb.hasSchedulePromise is not implemented"));
  66. };
  67. /**
  68. * List all the names of the schedules.
  69. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  70. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  71. * an async Promise.
  72. * @return {Promise|SyncPromise} A promise that returns a new array of string
  73. * with the names of all schedules, or that is rejected with
  74. * GroupManagerDb.Error for a database error.
  75. */
  76. GroupManagerDb.prototype.listAllScheduleNamesPromise = function(useSync)
  77. {
  78. return SyncPromise.reject(new Error
  79. ("GroupManagerDb.listAllScheduleNamesPromise is not implemented"));
  80. };
  81. /**
  82. * Get a schedule with the given name.
  83. * @param {string} name The name of the schedule.
  84. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  85. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  86. * an async Promise.
  87. * @return {Promise|SyncPromise} A promise that returns a new Schedule object,
  88. * or that is rejected with GroupManagerDb.Error if the schedule does not exist
  89. * or other database error.
  90. */
  91. GroupManagerDb.prototype.getSchedulePromise = function(name, useSync)
  92. {
  93. return SyncPromise.reject(new Error
  94. ("GroupManagerDb.getSchedulePromise is not implemented"));
  95. };
  96. /**
  97. * For each member using the given schedule, get the name and public key DER
  98. * of the member's key.
  99. * @param {string} name The name of the schedule.
  100. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  101. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  102. * an async Promise.
  103. * @return {Promise|SyncPromise} A promise that returns a new array of object
  104. * (where "keyName" is the Name of the public key and "publicKey" is the Blob of
  105. * the public key DER), or that is rejected with GroupManagerDb.Error for a
  106. * database error. Note that the member's identity name is keyName.getPrefix(-1).
  107. * If the schedule name is not found, the list is empty.
  108. */
  109. GroupManagerDb.prototype.getScheduleMembersPromise = function
  110. (name, useSync)
  111. {
  112. return SyncPromise.reject(new Error
  113. ("GroupManagerDb.getScheduleMembersPromise is not implemented"));
  114. };
  115. /**
  116. * Add a schedule with the given name.
  117. * @param {string} name The name of the schedule. The name cannot be empty.
  118. * @param {Schedule} schedule The Schedule to add.
  119. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  120. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  121. * an async Promise.
  122. * @return {Promise|SyncPromise} A promise that fulfills when the schedule is
  123. * added, or that is rejected with GroupManagerDb.Error if a schedule with the
  124. * same name already exists, if the name is empty, or other database error.
  125. */
  126. GroupManagerDb.prototype.addSchedulePromise = function(name, schedule, useSync)
  127. {
  128. return SyncPromise.reject(new Error
  129. ("GroupManagerDb.addSchedulePromise is not implemented"));
  130. };
  131. /**
  132. * Delete the schedule with the given name. Also delete members which use this
  133. * schedule. If there is no schedule with the name, then do nothing.
  134. * @param {string} name The name of the schedule.
  135. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  136. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  137. * an async Promise.
  138. * @return {Promise|SyncPromise} A promise that fulfills when the schedule is
  139. * deleted (or there is no such schedule), or that is rejected with
  140. * GroupManagerDb.Error for a database error.
  141. */
  142. GroupManagerDb.prototype.deleteSchedulePromise = function(name, useSync)
  143. {
  144. return SyncPromise.reject(new Error
  145. ("GroupManagerDb.deleteSchedulePromise is not implemented"));
  146. };
  147. /**
  148. * Rename a schedule with oldName to newName.
  149. * @param {string} oldName The name of the schedule to be renamed.
  150. * @param {string} newName The new name of the schedule. The name cannot be empty.
  151. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  152. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  153. * an async Promise.
  154. * @return {Promise|SyncPromise} A promise that fulfills when the schedule is
  155. * renamed, or that is rejected with GroupManagerDb.Error if a schedule with
  156. * newName already exists, if the schedule with oldName does not exist, if
  157. * newName is empty, or other database error.
  158. */
  159. GroupManagerDb.prototype.renameSchedulePromise = function
  160. (oldName, newName, useSync)
  161. {
  162. return SyncPromise.reject(new Error
  163. ("GroupManagerDb.renameSchedulePromise is not implemented"));
  164. };
  165. /**
  166. * Update the schedule with name and replace the old object with the given
  167. * schedule. Otherwise, if no schedule with name exists, a new schedule
  168. * with name and the given schedule will be added to database.
  169. * @param {string} name The name of the schedule. The name cannot be empty.
  170. * @param {Schedule} schedule The Schedule to update or add.
  171. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  172. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  173. * an async Promise.
  174. * @return {Promise|SyncPromise} A promise that fulfills when the schedule is
  175. * updated, or that is rejected with GroupManagerDb.Error if the name is empty,
  176. * or other database error.
  177. */
  178. GroupManagerDb.prototype.updateSchedulePromise = function
  179. (name, schedule, useSync)
  180. {
  181. return SyncPromise.reject(new Error
  182. ("GroupManagerDb.updateSchedulePromise is not implemented"));
  183. };
  184. ////////////////////////////////////////////////////// Member management.
  185. /**
  186. * Check if there is a member with the given identity name.
  187. * @param {Name} identity The member's identity name.
  188. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  189. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  190. * an async Promise.
  191. * @return {Promise|SyncPromise} A promise that returns true if there is a
  192. * member (else false), or that is rejected with GroupManagerDb.Error for a
  193. * database error.
  194. */
  195. GroupManagerDb.prototype.hasMemberPromise = function(identity, useSync)
  196. {
  197. return SyncPromise.reject(new Error
  198. ("GroupManagerDb.hasMemberPromise is not implemented"));
  199. };
  200. /**
  201. * List all the members.
  202. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  203. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  204. * an async Promise.
  205. * @return {Promise|SyncPromise} A promise that returns a new array of Name with
  206. * the names of all members, or that is rejected with GroupManagerDb.Error for a
  207. * database error.
  208. */
  209. GroupManagerDb.prototype.listAllMembersPromise = function(useSync)
  210. {
  211. return SyncPromise.reject(new Error
  212. ("GroupManagerDb.listAllMembersPromise is not implemented"));
  213. };
  214. /**
  215. * Get the name of the schedule for the given member's identity name.
  216. * @param {Name} identity The member's identity name.
  217. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  218. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  219. * an async Promise.
  220. * @return {Promise|SyncPromise} A promise that returns the string schedule name,
  221. * or that is rejected with GroupManagerDb.Error if there's no member with the
  222. * given identity name in the database, or other database error.
  223. */
  224. GroupManagerDb.prototype.getMemberSchedulePromise = function(identity, useSync)
  225. {
  226. return SyncPromise.reject(new Error
  227. ("GroupManagerDb.getMemberSchedulePromise is not implemented"));
  228. };
  229. /**
  230. * Add a new member with the given key named keyName into a schedule named
  231. * scheduleName. The member's identity name is keyName.getPrefix(-1).
  232. * @param {string} scheduleName The schedule name.
  233. * @param {Name} keyName The name of the key.
  234. * @param {Blob} key A Blob of the public key DER.
  235. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  236. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  237. * an async Promise.
  238. * @return {Promise|SyncPromise} A promise that fulfills when the member is
  239. * added, or that is rejected with GroupManagerDb.Error if there's no schedule
  240. * named scheduleName, if the member's identity name already exists, or other
  241. * database error.
  242. */
  243. GroupManagerDb.prototype.addMemberPromise = function
  244. (scheduleName, keyName, key, useSync)
  245. {
  246. return SyncPromise.reject(new Error
  247. ("GroupManagerDb.addMemberPromise is not implemented"));
  248. };
  249. /**
  250. * Change the name of the schedule for the given member's identity name.
  251. * @param {Name} identity The member's identity name.
  252. * @param {string} scheduleName The new schedule name.
  253. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  254. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  255. * an async Promise.
  256. * @return {Promise|SyncPromise} A promise that fulfills when the member is
  257. * updated, or that is rejected with GroupManagerDb.Error if there's no member
  258. * with the given identity name in the database, or there's no schedule named
  259. * scheduleName, or other database error.
  260. */
  261. GroupManagerDb.prototype.updateMemberSchedulePromise = function
  262. (identity, scheduleName, useSync)
  263. {
  264. return SyncPromise.reject(new Error
  265. ("GroupManagerDb.updateMemberSchedulePromise is not implemented"));
  266. };
  267. /**
  268. * Delete a member with the given identity name. If there is no member with
  269. * the identity name, then do nothing.
  270. * @param {Name} identity The member's identity name.
  271. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  272. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  273. * an async Promise.
  274. * @return {Promise|SyncPromise} A promise that fulfills when the member is
  275. * deleted (or there is no such member), or that is rejected with
  276. * GroupManagerDb.Error for a database error.
  277. */
  278. GroupManagerDb.prototype.deleteMemberPromise = function(identity, useSync)
  279. {
  280. return SyncPromise.reject(new Error
  281. ("GroupManagerDb.deleteMemberPromise is not implemented"));
  282. };
  283. /**
  284. * Check if there is an EKey with the name eKeyName in the database.
  285. * @param {Name} eKeyName The name of the EKey.
  286. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  287. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  288. * an async Promise.
  289. * @return {Promise|SyncPromise} A promise that returns true if the EKey exists
  290. * (else false), or that is rejected with GroupManagerDb.Error for a database
  291. * error.
  292. */
  293. GroupManagerDb.prototype.hasEKeyPromise = function(eKeyName, useSync)
  294. {
  295. return SyncPromise.reject(new Error
  296. ("GroupManagerDb.hasEKeyPromise is not implemented"));
  297. };
  298. /**
  299. * Add the EKey with name eKeyName to the database.
  300. * @param {Name} eKeyName The name of the EKey. This copies the Name.
  301. * @param {Blob} publicKey The encoded public Key of the group key pair.
  302. * @param {Blob} privateKey The encoded private Key of the group key pair.
  303. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  304. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  305. * an async Promise.
  306. * @return {Promise|SyncPromise} A promise that fulfills when the EKey is added,
  307. * or that is rejected with GroupManagerDb.Error if a key with name eKeyName
  308. * already exists in the database, or other database error.
  309. */
  310. GroupManagerDb.prototype.addEKeyPromise = function
  311. (eKeyName, publicKey, privateKey, useSync)
  312. {
  313. return SyncPromise.reject(new Error
  314. ("GroupManagerDb.addEKeyPromise is not implemented"));
  315. };
  316. /**
  317. * Get the group key pair with the name eKeyName from the database.
  318. * @param {Name} eKeyName The name of the EKey.
  319. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  320. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  321. * an async Promise.
  322. * @return {Promise|SyncPromise} A promise that returns an object (where
  323. * "publicKey" is the public key Blob and "privateKey" is the private key Blob),
  324. * or that is rejected with GroupManagerDb.Error for a database error.
  325. */
  326. GroupManagerDb.prototype.getEKeyPromise = function(eKeyName, useSync)
  327. {
  328. return SyncPromise.reject(new Error
  329. ("GroupManagerDb.getEKeyPromise is not implemented"));
  330. };
  331. /**
  332. * Delete all the EKeys in the database. The database will keep growing because
  333. * EKeys will keep being added, so this method should be called periodically.
  334. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  335. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  336. * an async Promise.
  337. * @return {Promise|SyncPromise} A promise that fulfills when the EKeys are
  338. * deleted, or that is rejected with GroupManagerDb.Error for a database error.
  339. */
  340. GroupManagerDb.prototype.cleanEKeysPromise = function(useSync)
  341. {
  342. return SyncPromise.reject(new Error
  343. ("GroupManagerDb.cleanEKeysPromise is not implemented"));
  344. };
  345. /**
  346. * Delete the EKey with name eKeyName from the database. If no key with the
  347. * name exists in the database, do nothing.
  348. * @param {Name} eKeyName The name of the EKey.
  349. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  350. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  351. * an async Promise.
  352. * @return {Promise|SyncPromise} A promise that fulfills when the EKey is
  353. * deleted (or there is no such key), or that is rejected with
  354. * GroupManagerDb.Error for a database error.
  355. */
  356. GroupManagerDb.prototype.deleteEKeyPromise = function(eKeyName, useSync)
  357. {
  358. return SyncPromise.reject(new Error
  359. ("GroupManagerDb.deleteEKeyPromise is not implemented"));
  360. };