Source: encrypt/group-manager-db.js

  1. /**
  2. * Copyright (C) 2015-2016 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. // Copy lineNumber, etc. from where new Error was called.
  46. for (var prop in error)
  47. this[prop] = error[prop];
  48. // Make sure these are copied.
  49. this.message = error.message;
  50. this.stack = error.stack;
  51. }
  52. }
  53. GroupManagerDb.Error.prototype = new Error();
  54. GroupManagerDb.Error.prototype.name = "GroupManagerDbError";
  55. ////////////////////////////////////////////////////// Schedule management.
  56. /**
  57. * Check if there is a schedule with the given name.
  58. * @param {string} name The name of the schedule.
  59. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  60. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  61. * an async Promise.
  62. * @return {Promise|SyncPromise} A promise that returns true if there is a
  63. * schedule (else false), or that is rejected with GroupManagerDb.Error for a
  64. * database error.
  65. */
  66. GroupManagerDb.prototype.hasSchedulePromise = function(name, useSync)
  67. {
  68. return SyncPromise.reject(new Error
  69. ("GroupManagerDb.hasSchedulePromise is not implemented"));
  70. };
  71. /**
  72. * List all the names of the schedules.
  73. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  74. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  75. * an async Promise.
  76. * @return {Promise|SyncPromise} A promise that returns a new array of string
  77. * with the names of all schedules, or that is rejected with
  78. * GroupManagerDb.Error for a database error.
  79. */
  80. GroupManagerDb.prototype.listAllScheduleNamesPromise = function(useSync)
  81. {
  82. return SyncPromise.reject(new Error
  83. ("GroupManagerDb.listAllScheduleNamesPromise is not implemented"));
  84. };
  85. /**
  86. * Get a schedule with the given name.
  87. * @param {string} name The name of the schedule.
  88. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  89. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  90. * an async Promise.
  91. * @return {Promise|SyncPromise} A promise that returns a new Schedule object,
  92. * or that is rejected with GroupManagerDb.Error if the schedule does not exist
  93. * or other database error.
  94. */
  95. GroupManagerDb.prototype.getSchedulePromise = function(name, useSync)
  96. {
  97. return SyncPromise.reject(new Error
  98. ("GroupManagerDb.getSchedulePromise is not implemented"));
  99. };
  100. /**
  101. * For each member using the given schedule, get the name and public key DER
  102. * of the member's key.
  103. * @param {string} name The name of the schedule.
  104. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  105. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  106. * an async Promise.
  107. * @return {Promise|SyncPromise} A promise that returns a new array of object
  108. * (where "keyName" is the Name of the public key and "publicKey" is the Blob of
  109. * the public key DER), or that is rejected with GroupManagerDb.Error for a
  110. * database error. Note that the member's identity name is keyName.getPrefix(-1).
  111. * If the schedule name is not found, the list is empty.
  112. */
  113. GroupManagerDb.prototype.getScheduleMembersPromise = function
  114. (name, useSync)
  115. {
  116. return SyncPromise.reject(new Error
  117. ("GroupManagerDb.getScheduleMembersPromise is not implemented"));
  118. };
  119. /**
  120. * Add a schedule with the given name.
  121. * @param {string} name The name of the schedule. The name cannot be empty.
  122. * @param {Schedule} schedule The Schedule to add.
  123. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  124. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  125. * an async Promise.
  126. * @return {Promise|SyncPromise} A promise that fulfills when the schedule is
  127. * added, or that is rejected with GroupManagerDb.Error if a schedule with the
  128. * same name already exists, if the name is empty, or other database error.
  129. */
  130. GroupManagerDb.prototype.addSchedulePromise = function(name, schedule, useSync)
  131. {
  132. return SyncPromise.reject(new Error
  133. ("GroupManagerDb.addSchedulePromise is not implemented"));
  134. };
  135. /**
  136. * Delete the schedule with the given name. Also delete members which use this
  137. * schedule. If there is no schedule with the name, then do nothing.
  138. * @param {string} name The name of the schedule.
  139. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  140. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  141. * an async Promise.
  142. * @return {Promise|SyncPromise} A promise that fulfills when the schedule is
  143. * deleted (or there is no such schedule), or that is rejected with
  144. * GroupManagerDb.Error for a database error.
  145. */
  146. GroupManagerDb.prototype.deleteSchedulePromise = function(name, useSync)
  147. {
  148. return SyncPromise.reject(new Error
  149. ("GroupManagerDb.deleteSchedulePromise is not implemented"));
  150. };
  151. /**
  152. * Rename a schedule with oldName to newName.
  153. * @param {string} oldName The name of the schedule to be renamed.
  154. * @param {string} newName The new name of the schedule. The name cannot be empty.
  155. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  156. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  157. * an async Promise.
  158. * @return {Promise|SyncPromise} A promise that fulfills when the schedule is
  159. * renamed, or that is rejected with GroupManagerDb.Error if a schedule with
  160. * newName already exists, if the schedule with oldName does not exist, if
  161. * newName is empty, or other database error.
  162. */
  163. GroupManagerDb.prototype.renameSchedulePromise = function
  164. (oldName, newName, useSync)
  165. {
  166. return SyncPromise.reject(new Error
  167. ("GroupManagerDb.renameSchedulePromise is not implemented"));
  168. };
  169. /**
  170. * Update the schedule with name and replace the old object with the given
  171. * schedule. Otherwise, if no schedule with name exists, a new schedule
  172. * with name and the given schedule will be added to database.
  173. * @param {string} name The name of the schedule. The name cannot be empty.
  174. * @param {Schedule} schedule The Schedule to update or add.
  175. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  176. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  177. * an async Promise.
  178. * @return {Promise|SyncPromise} A promise that fulfills when the schedule is
  179. * updated, or that is rejected with GroupManagerDb.Error if the name is empty,
  180. * or other database error.
  181. */
  182. GroupManagerDb.prototype.updateSchedulePromise = function
  183. (name, schedule, useSync)
  184. {
  185. return SyncPromise.reject(new Error
  186. ("GroupManagerDb.updateSchedulePromise is not implemented"));
  187. };
  188. ////////////////////////////////////////////////////// Member management.
  189. /**
  190. * Check if there is a member with the given identity name.
  191. * @param {Name} identity The member's identity name.
  192. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  193. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  194. * an async Promise.
  195. * @return {Promise|SyncPromise} A promise that returns true if there is a
  196. * member (else false), or that is rejected with GroupManagerDb.Error for a
  197. * database error.
  198. */
  199. GroupManagerDb.prototype.hasMemberPromise = function(identity, useSync)
  200. {
  201. return SyncPromise.reject(new Error
  202. ("GroupManagerDb.hasMemberPromise is not implemented"));
  203. };
  204. /**
  205. * List all the members.
  206. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  207. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  208. * an async Promise.
  209. * @return {Promise|SyncPromise} A promise that returns a new array of Name with
  210. * the names of all members, or that is rejected with GroupManagerDb.Error for a
  211. * database error.
  212. */
  213. GroupManagerDb.prototype.listAllMembersPromise = function(useSync)
  214. {
  215. return SyncPromise.reject(new Error
  216. ("GroupManagerDb.listAllMembersPromise is not implemented"));
  217. };
  218. /**
  219. * Get the name of the schedule for the given member's identity name.
  220. * @param {Name} identity The member's identity name.
  221. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  222. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  223. * an async Promise.
  224. * @return {Promise|SyncPromise} A promise that returns the string schedule name,
  225. * or that is rejected with GroupManagerDb.Error if there's no member with the
  226. * given identity name in the database, or other database error.
  227. */
  228. GroupManagerDb.prototype.getMemberSchedulePromise = function(identity, useSync)
  229. {
  230. return SyncPromise.reject(new Error
  231. ("GroupManagerDb.getMemberSchedulePromise is not implemented"));
  232. };
  233. /**
  234. * Add a new member with the given key named keyName into a schedule named
  235. * scheduleName. The member's identity name is keyName.getPrefix(-1).
  236. * @param {string} scheduleName The schedule name.
  237. * @param {Name} keyName The name of the key.
  238. * @param {Blob} key A Blob of the public key DER.
  239. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  240. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  241. * an async Promise.
  242. * @return {Promise|SyncPromise} A promise that fulfills when the member is
  243. * added, or that is rejected with GroupManagerDb.Error if there's no schedule
  244. * named scheduleName, if the member's identity name already exists, or other
  245. * database error.
  246. */
  247. GroupManagerDb.prototype.addMemberPromise = function
  248. (scheduleName, keyName, key, useSync)
  249. {
  250. return SyncPromise.reject(new Error
  251. ("GroupManagerDb.addMemberPromise is not implemented"));
  252. };
  253. /**
  254. * Change the name of the schedule for the given member's identity name.
  255. * @param {Name} identity The member's identity name.
  256. * @param {string} scheduleName The new schedule name.
  257. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  258. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  259. * an async Promise.
  260. * @return {Promise|SyncPromise} A promise that fulfills when the member is
  261. * updated, or that is rejected with GroupManagerDb.Error if there's no member
  262. * with the given identity name in the database, or there's no schedule named
  263. * scheduleName, or other database error.
  264. */
  265. GroupManagerDb.prototype.updateMemberSchedulePromise = function
  266. (identity, scheduleName, useSync)
  267. {
  268. return SyncPromise.reject(new Error
  269. ("GroupManagerDb.updateMemberSchedulePromise is not implemented"));
  270. };
  271. /**
  272. * Delete a member with the given identity name. If there is no member with
  273. * the identity name, then do nothing.
  274. * @param {Name} identity The member's identity name.
  275. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  276. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  277. * an async Promise.
  278. * @return {Promise|SyncPromise} A promise that fulfills when the member is
  279. * deleted (or there is no such member), or that is rejected with
  280. * GroupManagerDb.Error for a database error.
  281. */
  282. GroupManagerDb.prototype.deleteMemberPromise = function(identity, useSync)
  283. {
  284. return SyncPromise.reject(new Error
  285. ("GroupManagerDb.deleteMemberPromise is not implemented"));
  286. };