@celljs/security 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (344) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/cell.yml +36 -0
  4. package/lib/browser/index.d.ts +2 -0
  5. package/lib/browser/index.d.ts.map +1 -0
  6. package/lib/browser/index.js +18 -0
  7. package/lib/browser/index.js.map +1 -0
  8. package/lib/browser/module.d.ts +4 -0
  9. package/lib/browser/module.d.ts.map +1 -0
  10. package/lib/browser/module.js +6 -0
  11. package/lib/browser/module.js.map +1 -0
  12. package/lib/browser/user/index.d.ts +3 -0
  13. package/lib/browser/user/index.d.ts.map +1 -0
  14. package/lib/browser/user/index.js +19 -0
  15. package/lib/browser/user/index.js.map +1 -0
  16. package/lib/browser/user/user-manager.d.ts +23 -0
  17. package/lib/browser/user/user-manager.d.ts.map +1 -0
  18. package/lib/browser/user/user-manager.js +125 -0
  19. package/lib/browser/user/user-manager.js.map +1 -0
  20. package/lib/browser/user/user-protocol.d.ts +10 -0
  21. package/lib/browser/user/user-protocol.d.ts.map +1 -0
  22. package/lib/browser/user/user-protocol.js +5 -0
  23. package/lib/browser/user/user-protocol.js.map +1 -0
  24. package/lib/common/index.d.ts +2 -0
  25. package/lib/common/index.d.ts.map +1 -0
  26. package/lib/common/index.js +18 -0
  27. package/lib/common/index.js.map +1 -0
  28. package/lib/common/security-protocol.d.ts +60 -0
  29. package/lib/common/security-protocol.d.ts.map +1 -0
  30. package/lib/common/security-protocol.js +33 -0
  31. package/lib/common/security-protocol.js.map +1 -0
  32. package/lib/node/access/access-decision-manager.d.ts +9 -0
  33. package/lib/node/access/access-decision-manager.d.ts.map +1 -0
  34. package/lib/node/access/access-decision-manager.js +56 -0
  35. package/lib/node/access/access-decision-manager.js.map +1 -0
  36. package/lib/node/access/access-decision-utils.d.ts +5 -0
  37. package/lib/node/access/access-decision-utils.d.ts.map +1 -0
  38. package/lib/node/access/access-decision-utils.js +23 -0
  39. package/lib/node/access/access-decision-utils.js.map +1 -0
  40. package/lib/node/access/access-decision-voter.d.ts +14 -0
  41. package/lib/node/access/access-decision-voter.d.ts.map +1 -0
  42. package/lib/node/access/access-decision-voter.js +86 -0
  43. package/lib/node/access/access-decision-voter.js.map +1 -0
  44. package/lib/node/access/access-protocol.d.ts +75 -0
  45. package/lib/node/access/access-protocol.d.ts.map +1 -0
  46. package/lib/node/access/access-protocol.js +20 -0
  47. package/lib/node/access/access-protocol.js.map +1 -0
  48. package/lib/node/access/action-name-resolver.d.ts +5 -0
  49. package/lib/node/access/action-name-resolver.d.ts.map +1 -0
  50. package/lib/node/access/action-name-resolver.js +30 -0
  51. package/lib/node/access/action-name-resolver.js.map +1 -0
  52. package/lib/node/access/index.d.ts +12 -0
  53. package/lib/node/access/index.d.ts.map +1 -0
  54. package/lib/node/access/index.js +28 -0
  55. package/lib/node/access/index.js.map +1 -0
  56. package/lib/node/access/jexl-engine-provider.d.ts +6 -0
  57. package/lib/node/access/jexl-engine-provider.d.ts.map +1 -0
  58. package/lib/node/access/jexl-engine-provider.js +25 -0
  59. package/lib/node/access/jexl-engine-provider.js.map +1 -0
  60. package/lib/node/access/method-advice.d.ts +17 -0
  61. package/lib/node/access/method-advice.d.ts.map +1 -0
  62. package/lib/node/access/method-advice.js +81 -0
  63. package/lib/node/access/method-advice.js.map +1 -0
  64. package/lib/node/access/policy-provider.d.ts +17 -0
  65. package/lib/node/access/policy-provider.d.ts.map +1 -0
  66. package/lib/node/access/policy-provider.js +84 -0
  67. package/lib/node/access/policy-provider.js.map +1 -0
  68. package/lib/node/access/policy-resolver.d.ts +15 -0
  69. package/lib/node/access/policy-resolver.d.ts.map +1 -0
  70. package/lib/node/access/policy-resolver.js +91 -0
  71. package/lib/node/access/policy-resolver.js.map +1 -0
  72. package/lib/node/access/resource-name-resolver.d.ts +7 -0
  73. package/lib/node/access/resource-name-resolver.d.ts.map +1 -0
  74. package/lib/node/access/resource-name-resolver.js +57 -0
  75. package/lib/node/access/resource-name-resolver.js.map +1 -0
  76. package/lib/node/access/security-metadata-source.d.ts +7 -0
  77. package/lib/node/access/security-metadata-source.d.ts.map +1 -0
  78. package/lib/node/access/security-metadata-source.js +58 -0
  79. package/lib/node/access/security-metadata-source.js.map +1 -0
  80. package/lib/node/annotation/action.d.ts +2 -0
  81. package/lib/node/annotation/action.d.ts.map +1 -0
  82. package/lib/node/annotation/action.js +11 -0
  83. package/lib/node/annotation/action.js.map +1 -0
  84. package/lib/node/annotation/anonymous.d.ts +2 -0
  85. package/lib/node/annotation/anonymous.d.ts.map +1 -0
  86. package/lib/node/annotation/anonymous.js +10 -0
  87. package/lib/node/annotation/anonymous.js.map +1 -0
  88. package/lib/node/annotation/authenticated.d.ts +2 -0
  89. package/lib/node/annotation/authenticated.d.ts.map +1 -0
  90. package/lib/node/annotation/authenticated.js +10 -0
  91. package/lib/node/annotation/authenticated.js.map +1 -0
  92. package/lib/node/annotation/authorize.d.ts +11 -0
  93. package/lib/node/annotation/authorize.d.ts.map +1 -0
  94. package/lib/node/annotation/authorize.js +40 -0
  95. package/lib/node/annotation/authorize.js.map +1 -0
  96. package/lib/node/annotation/index.d.ts +8 -0
  97. package/lib/node/annotation/index.d.ts.map +1 -0
  98. package/lib/node/annotation/index.js +24 -0
  99. package/lib/node/annotation/index.js.map +1 -0
  100. package/lib/node/annotation/post-authorize.d.ts +2 -0
  101. package/lib/node/annotation/post-authorize.d.ts.map +1 -0
  102. package/lib/node/annotation/post-authorize.js +10 -0
  103. package/lib/node/annotation/post-authorize.js.map +1 -0
  104. package/lib/node/annotation/pre-authorize.d.ts +2 -0
  105. package/lib/node/annotation/pre-authorize.d.ts.map +1 -0
  106. package/lib/node/annotation/pre-authorize.js +10 -0
  107. package/lib/node/annotation/pre-authorize.js.map +1 -0
  108. package/lib/node/annotation/resource.d.ts +2 -0
  109. package/lib/node/annotation/resource.d.ts.map +1 -0
  110. package/lib/node/annotation/resource.js +16 -0
  111. package/lib/node/annotation/resource.js.map +1 -0
  112. package/lib/node/authentication/authentication-manager.d.ts +10 -0
  113. package/lib/node/authentication/authentication-manager.d.ts.map +1 -0
  114. package/lib/node/authentication/authentication-manager.js +81 -0
  115. package/lib/node/authentication/authentication-manager.js.map +1 -0
  116. package/lib/node/authentication/authentication-middleware.d.ts +8 -0
  117. package/lib/node/authentication/authentication-middleware.d.ts.map +1 -0
  118. package/lib/node/authentication/authentication-middleware.js +36 -0
  119. package/lib/node/authentication/authentication-middleware.js.map +1 -0
  120. package/lib/node/authentication/authentication-protocol.d.ts +30 -0
  121. package/lib/node/authentication/authentication-protocol.d.ts.map +1 -0
  122. package/lib/node/authentication/authentication-protocol.js +12 -0
  123. package/lib/node/authentication/authentication-protocol.js.map +1 -0
  124. package/lib/node/authentication/authentication-provider.d.ts +33 -0
  125. package/lib/node/authentication/authentication-provider.d.ts.map +1 -0
  126. package/lib/node/authentication/authentication-provider.js +174 -0
  127. package/lib/node/authentication/authentication-provider.js.map +1 -0
  128. package/lib/node/authentication/authentication-success-handler.d.ts +16 -0
  129. package/lib/node/authentication/authentication-success-handler.d.ts.map +1 -0
  130. package/lib/node/authentication/authentication-success-handler.js +97 -0
  131. package/lib/node/authentication/authentication-success-handler.js.map +1 -0
  132. package/lib/node/authentication/index.d.ts +6 -0
  133. package/lib/node/authentication/index.d.ts.map +1 -0
  134. package/lib/node/authentication/index.js +22 -0
  135. package/lib/node/authentication/index.js.map +1 -0
  136. package/lib/node/cache/cache-protocol.d.ts +16 -0
  137. package/lib/node/cache/cache-protocol.d.ts.map +1 -0
  138. package/lib/node/cache/cache-protocol.js +6 -0
  139. package/lib/node/cache/cache-protocol.js.map +1 -0
  140. package/lib/node/cache/index.d.ts +3 -0
  141. package/lib/node/cache/index.d.ts.map +1 -0
  142. package/lib/node/cache/index.js +19 -0
  143. package/lib/node/cache/index.js.map +1 -0
  144. package/lib/node/cache/request-cache.d.ts +10 -0
  145. package/lib/node/cache/request-cache.d.ts.map +1 -0
  146. package/lib/node/cache/request-cache.js +52 -0
  147. package/lib/node/cache/request-cache.js.map +1 -0
  148. package/lib/node/constants.d.ts +6 -0
  149. package/lib/node/constants.d.ts.map +1 -0
  150. package/lib/node/constants.js +9 -0
  151. package/lib/node/constants.js.map +1 -0
  152. package/lib/node/context/context-protocol.d.ts +22 -0
  153. package/lib/node/context/context-protocol.d.ts.map +1 -0
  154. package/lib/node/context/context-protocol.js +30 -0
  155. package/lib/node/context/context-protocol.js.map +1 -0
  156. package/lib/node/context/index.d.ts +5 -0
  157. package/lib/node/context/index.d.ts.map +1 -0
  158. package/lib/node/context/index.js +21 -0
  159. package/lib/node/context/index.js.map +1 -0
  160. package/lib/node/context/security-context-middleware.d.ts +9 -0
  161. package/lib/node/context/security-context-middleware.d.ts.map +1 -0
  162. package/lib/node/context/security-context-middleware.js +44 -0
  163. package/lib/node/context/security-context-middleware.js.map +1 -0
  164. package/lib/node/context/security-context-store.d.ts +8 -0
  165. package/lib/node/context/security-context-store.d.ts.map +1 -0
  166. package/lib/node/context/security-context-store.js +40 -0
  167. package/lib/node/context/security-context-store.js.map +1 -0
  168. package/lib/node/context/security-context-strategy.d.ts +9 -0
  169. package/lib/node/context/security-context-strategy.d.ts.map +1 -0
  170. package/lib/node/context/security-context-strategy.js +32 -0
  171. package/lib/node/context/security-context-strategy.js.map +1 -0
  172. package/lib/node/crypto/index.d.ts +3 -0
  173. package/lib/node/crypto/index.d.ts.map +1 -0
  174. package/lib/node/crypto/index.js +19 -0
  175. package/lib/node/crypto/index.js.map +1 -0
  176. package/lib/node/crypto/keygen/base64-string-key-generator.d.ts +6 -0
  177. package/lib/node/crypto/keygen/base64-string-key-generator.d.ts.map +1 -0
  178. package/lib/node/crypto/keygen/base64-string-key-generator.js +30 -0
  179. package/lib/node/crypto/keygen/base64-string-key-generator.js.map +1 -0
  180. package/lib/node/crypto/keygen/index.d.ts +3 -0
  181. package/lib/node/crypto/keygen/index.d.ts.map +1 -0
  182. package/lib/node/crypto/keygen/index.js +19 -0
  183. package/lib/node/crypto/keygen/index.js.map +1 -0
  184. package/lib/node/crypto/keygen/keygen-protocol.d.ts +5 -0
  185. package/lib/node/crypto/keygen/keygen-protocol.d.ts.map +1 -0
  186. package/lib/node/crypto/keygen/keygen-protocol.js +5 -0
  187. package/lib/node/crypto/keygen/keygen-protocol.js.map +1 -0
  188. package/lib/node/crypto/password/index.d.ts +3 -0
  189. package/lib/node/crypto/password/index.d.ts.map +1 -0
  190. package/lib/node/crypto/password/index.js +19 -0
  191. package/lib/node/crypto/password/index.js.map +1 -0
  192. package/lib/node/crypto/password/password-encoder.d.ts +9 -0
  193. package/lib/node/crypto/password/password-encoder.d.ts.map +1 -0
  194. package/lib/node/crypto/password/password-encoder.js +51 -0
  195. package/lib/node/crypto/password/password-encoder.js.map +1 -0
  196. package/lib/node/crypto/password/password-protocol.d.ts +6 -0
  197. package/lib/node/crypto/password/password-protocol.d.ts.map +1 -0
  198. package/lib/node/crypto/password/password-protocol.js +5 -0
  199. package/lib/node/crypto/password/password-protocol.js.map +1 -0
  200. package/lib/node/error/error-handler.d.ts +19 -0
  201. package/lib/node/error/error-handler.d.ts.map +1 -0
  202. package/lib/node/error/error-handler.js +81 -0
  203. package/lib/node/error/error-handler.js.map +1 -0
  204. package/lib/node/error/error-protocol.d.ts +3 -0
  205. package/lib/node/error/error-protocol.d.ts.map +1 -0
  206. package/lib/node/error/error-protocol.js +7 -0
  207. package/lib/node/error/error-protocol.js.map +1 -0
  208. package/lib/node/error/error.d.ts +21 -0
  209. package/lib/node/error/error.d.ts.map +1 -0
  210. package/lib/node/error/error.js +35 -0
  211. package/lib/node/error/error.js.map +1 -0
  212. package/lib/node/error/index.d.ts +4 -0
  213. package/lib/node/error/index.d.ts.map +1 -0
  214. package/lib/node/error/index.js +20 -0
  215. package/lib/node/error/index.js.map +1 -0
  216. package/lib/node/index.d.ts +11 -0
  217. package/lib/node/index.d.ts.map +1 -0
  218. package/lib/node/index.js +27 -0
  219. package/lib/node/index.js.map +1 -0
  220. package/lib/node/logout/index.d.ts +5 -0
  221. package/lib/node/logout/index.d.ts.map +1 -0
  222. package/lib/node/logout/index.js +21 -0
  223. package/lib/node/logout/index.js.map +1 -0
  224. package/lib/node/logout/logout-handler.d.ts +5 -0
  225. package/lib/node/logout/logout-handler.d.ts.map +1 -0
  226. package/lib/node/logout/logout-handler.js +22 -0
  227. package/lib/node/logout/logout-handler.js.map +1 -0
  228. package/lib/node/logout/logout-middleware.d.ts +16 -0
  229. package/lib/node/logout/logout-middleware.d.ts.map +1 -0
  230. package/lib/node/logout/logout-middleware.js +66 -0
  231. package/lib/node/logout/logout-middleware.js.map +1 -0
  232. package/lib/node/logout/logout-protocol.d.ts +12 -0
  233. package/lib/node/logout/logout-protocol.d.ts.map +1 -0
  234. package/lib/node/logout/logout-protocol.js +9 -0
  235. package/lib/node/logout/logout-protocol.js.map +1 -0
  236. package/lib/node/logout/logout-success-handler-provider.d.ts +8 -0
  237. package/lib/node/logout/logout-success-handler-provider.d.ts.map +1 -0
  238. package/lib/node/logout/logout-success-handler-provider.js +35 -0
  239. package/lib/node/logout/logout-success-handler-provider.js.map +1 -0
  240. package/lib/node/logout/logout-success-handler.d.ts +9 -0
  241. package/lib/node/logout/logout-success-handler.d.ts.map +1 -0
  242. package/lib/node/logout/logout-success-handler.js +39 -0
  243. package/lib/node/logout/logout-success-handler.js.map +1 -0
  244. package/lib/node/module.d.ts +5 -0
  245. package/lib/node/module.d.ts.map +1 -0
  246. package/lib/node/module.js +7 -0
  247. package/lib/node/module.js.map +1 -0
  248. package/lib/node/user/index.d.ts +6 -0
  249. package/lib/node/user/index.d.ts.map +1 -0
  250. package/lib/node/user/index.js +22 -0
  251. package/lib/node/user/index.js.map +1 -0
  252. package/lib/node/user/user-checker.d.ts +8 -0
  253. package/lib/node/user/user-checker.d.ts.map +1 -0
  254. package/lib/node/user/user-checker.js +44 -0
  255. package/lib/node/user/user-checker.js.map +1 -0
  256. package/lib/node/user/user-mapper.d.ts +9 -0
  257. package/lib/node/user/user-mapper.d.ts.map +1 -0
  258. package/lib/node/user/user-mapper.js +40 -0
  259. package/lib/node/user/user-mapper.js.map +1 -0
  260. package/lib/node/user/user-middleware.d.ts +11 -0
  261. package/lib/node/user/user-middleware.d.ts.map +1 -0
  262. package/lib/node/user/user-middleware.js +55 -0
  263. package/lib/node/user/user-middleware.js.map +1 -0
  264. package/lib/node/user/user-protocol.d.ts +15 -0
  265. package/lib/node/user/user-protocol.d.ts.map +1 -0
  266. package/lib/node/user/user-protocol.js +9 -0
  267. package/lib/node/user/user-protocol.js.map +1 -0
  268. package/lib/node/user/user-service.d.ts +7 -0
  269. package/lib/node/user/user-service.d.ts.map +1 -0
  270. package/lib/node/user/user-service.js +42 -0
  271. package/lib/node/user/user-service.js.map +1 -0
  272. package/lib/package.spec.d.ts +1 -0
  273. package/lib/package.spec.d.ts.map +1 -0
  274. package/lib/package.spec.js +4 -0
  275. package/lib/package.spec.js.map +1 -0
  276. package/package.json +53 -0
  277. package/src/browser/index.ts +1 -0
  278. package/src/browser/module.ts +4 -0
  279. package/src/browser/user/index.ts +2 -0
  280. package/src/browser/user/user-manager.ts +103 -0
  281. package/src/browser/user/user-protocol.ts +11 -0
  282. package/src/common/index.ts +1 -0
  283. package/src/common/security-protocol.ts +65 -0
  284. package/src/node/access/access-decision-manager.ts +43 -0
  285. package/src/node/access/access-decision-utils.ts +18 -0
  286. package/src/node/access/access-decision-voter.ts +73 -0
  287. package/src/node/access/access-protocol.ts +93 -0
  288. package/src/node/access/action-name-resolver.ts +19 -0
  289. package/src/node/access/index.ts +11 -0
  290. package/src/node/access/jexl-engine-provider.ts +18 -0
  291. package/src/node/access/method-advice.ts +64 -0
  292. package/src/node/access/policy-provider.ts +64 -0
  293. package/src/node/access/policy-resolver.ts +79 -0
  294. package/src/node/access/resource-name-resolver.ts +41 -0
  295. package/src/node/access/security-metadata-source.ts +47 -0
  296. package/src/node/annotation/action.ts +8 -0
  297. package/src/node/annotation/anonymous.ts +6 -0
  298. package/src/node/annotation/authenticated.ts +6 -0
  299. package/src/node/annotation/authorize.ts +39 -0
  300. package/src/node/annotation/index.ts +7 -0
  301. package/src/node/annotation/post-authorize.ts +6 -0
  302. package/src/node/annotation/pre-authorize.ts +6 -0
  303. package/src/node/annotation/resource.ts +12 -0
  304. package/src/node/authentication/authentication-manager.ts +61 -0
  305. package/src/node/authentication/authentication-middleware.ts +21 -0
  306. package/src/node/authentication/authentication-protocol.ts +37 -0
  307. package/src/node/authentication/authentication-provider.ts +155 -0
  308. package/src/node/authentication/authentication-success-handler.ts +83 -0
  309. package/src/node/authentication/index.ts +5 -0
  310. package/src/node/cache/cache-protocol.ts +17 -0
  311. package/src/node/cache/index.ts +2 -0
  312. package/src/node/cache/request-cache.ts +38 -0
  313. package/src/node/constants.ts +5 -0
  314. package/src/node/context/context-protocol.ts +43 -0
  315. package/src/node/context/index.ts +4 -0
  316. package/src/node/context/security-context-middleware.ts +28 -0
  317. package/src/node/context/security-context-store.ts +25 -0
  318. package/src/node/context/security-context-strategy.ts +24 -0
  319. package/src/node/crypto/index.ts +2 -0
  320. package/src/node/crypto/keygen/base64-string-key-generator.ts +15 -0
  321. package/src/node/crypto/keygen/index.ts +2 -0
  322. package/src/node/crypto/keygen/keygen-protocol.ts +7 -0
  323. package/src/node/crypto/password/index.ts +2 -0
  324. package/src/node/crypto/password/password-encoder.ts +39 -0
  325. package/src/node/crypto/password/password-protocol.ts +6 -0
  326. package/src/node/error/error-handler.ts +58 -0
  327. package/src/node/error/error-protocol.ts +5 -0
  328. package/src/node/error/error.ts +41 -0
  329. package/src/node/error/index.ts +3 -0
  330. package/src/node/index.ts +10 -0
  331. package/src/node/logout/index.ts +4 -0
  332. package/src/node/logout/logout-handler.ts +12 -0
  333. package/src/node/logout/logout-middleware.ts +49 -0
  334. package/src/node/logout/logout-protocol.ts +17 -0
  335. package/src/node/logout/logout-success-handler-provider.ts +21 -0
  336. package/src/node/logout/logout-success-handler.ts +22 -0
  337. package/src/node/module.ts +5 -0
  338. package/src/node/user/index.ts +6 -0
  339. package/src/node/user/user-checker.ts +37 -0
  340. package/src/node/user/user-mapper.ts +26 -0
  341. package/src/node/user/user-middleware.ts +40 -0
  342. package/src/node/user/user-protocol.ts +20 -0
  343. package/src/node/user/user-service.ts +28 -0
  344. package/src/package.spec.ts +4 -0
@@ -0,0 +1,58 @@
1
+ import { AUTHENTICATION_ERROR_HANDLER_PRIORITY, ACCESS_DENIED_ERROR_HANDLER_PRIORITY } from './error-protocol';
2
+ import { AuthenticationError, AccessDeniedError } from './error';
3
+ import { ErrorHandler, Context, RedirectStrategy } from '@celljs/web/lib/node';
4
+ import { Component, Value, Autowired } from '@celljs/core';
5
+ import { HttpStatus, HttpHeaders, XML_HTTP_REQUEST } from '@celljs/http';
6
+ import { RequestCache } from '../cache';
7
+
8
+ @Component(ErrorHandler)
9
+ export class AuthenticationErrorHandler implements ErrorHandler {
10
+ readonly priority: number = AUTHENTICATION_ERROR_HANDLER_PRIORITY;
11
+
12
+ @Value('cell.security.basic.realm')
13
+ protected realm: string;
14
+
15
+ @Value('cell.security.basic.enabled')
16
+ protected readonly baseEnabled: boolean;
17
+
18
+ @Value('cell.security.loginPage')
19
+ protected loginPage: string;
20
+
21
+ @Autowired(RedirectStrategy)
22
+ protected readonly redirectStrategy: RedirectStrategy;
23
+
24
+ @Autowired(RequestCache)
25
+ protected readonly requestCache: RequestCache;
26
+
27
+ canHandle(ctx: Context, err: Error): Promise<boolean> {
28
+ return Promise.resolve(err instanceof AuthenticationError);
29
+ }
30
+
31
+ async handle(ctx: Context, err: AuthenticationError): Promise<void> {
32
+ if (ctx.request.get(HttpHeaders.X_REQUESTED_WITH) !== XML_HTTP_REQUEST && !this.baseEnabled) {
33
+ await this.requestCache.save();
34
+ await this.redirectStrategy.send(this.loginPage);
35
+ ctx.response.end(err.message);
36
+ } else {
37
+ if (this.baseEnabled) {
38
+ ctx.response.setHeader(HttpHeaders.WWW_AUTHENTICATE, `Basic realm="${this.realm}"`);
39
+ }
40
+ ctx.response.statusCode = HttpStatus.UNAUTHORIZED;
41
+ ctx.response.end(err.message);
42
+ }
43
+ }
44
+ }
45
+
46
+ @Component(ErrorHandler)
47
+ export class AccessDeniedErrorHandler implements ErrorHandler {
48
+ readonly priority: number = ACCESS_DENIED_ERROR_HANDLER_PRIORITY;
49
+
50
+ canHandle(ctx: Context, err: Error): Promise<boolean> {
51
+ return Promise.resolve(err instanceof AccessDeniedError);
52
+ }
53
+
54
+ async handle(ctx: Context, err: AccessDeniedError): Promise<void> {
55
+ ctx.response.statusCode = HttpStatus.FORBIDDEN;
56
+ ctx.response.end(err.message);
57
+ }
58
+ }
@@ -0,0 +1,5 @@
1
+ import { HTTP_ERROR_HANDLER_PRIORITY } from '@celljs/web/lib/node';
2
+
3
+ export const ACCESS_DENIED_ERROR_HANDLER_PRIORITY = HTTP_ERROR_HANDLER_PRIORITY + 100;
4
+
5
+ export const AUTHENTICATION_ERROR_HANDLER_PRIORITY = ACCESS_DENIED_ERROR_HANDLER_PRIORITY + 100;
@@ -0,0 +1,41 @@
1
+ import { CustomError } from '@celljs/core';
2
+
3
+ export class AuthenticationError extends CustomError {
4
+
5
+ constructor(message?: string) {
6
+ super(message);
7
+ }
8
+
9
+ }
10
+
11
+ export class AccountStatusError extends AuthenticationError {
12
+
13
+ }
14
+
15
+ export class UsernameNotFoundError extends AuthenticationError {
16
+
17
+ }
18
+
19
+ export class BadCredentialsError extends AuthenticationError {
20
+
21
+ }
22
+
23
+ export class LockedError extends AccountStatusError {
24
+
25
+ }
26
+
27
+ export class DisabledError extends AccountStatusError {
28
+
29
+ }
30
+
31
+ export class AccountExpiredError extends AccountStatusError {
32
+
33
+ }
34
+
35
+ export class CredentialsExpiredError extends AccountStatusError {
36
+
37
+ }
38
+
39
+ export class AccessDeniedError extends CustomError {
40
+
41
+ }
@@ -0,0 +1,3 @@
1
+ export * from './error';
2
+ export * from './error-handler';
3
+ export * from './error-protocol';
@@ -0,0 +1,10 @@
1
+ export * from './authentication';
2
+ export * from './context';
3
+ export * from './crypto';
4
+ export * from './error';
5
+ export * from './user';
6
+ export * from './access';
7
+ export * from './annotation';
8
+ export * from './constants';
9
+ export * from './logout';
10
+ export * from './cache';
@@ -0,0 +1,4 @@
1
+ export * from './logout-protocol';
2
+ export * from './logout-middleware';
3
+ export * from './logout-handler';
4
+ export * from './logout-success-handler';
@@ -0,0 +1,12 @@
1
+ import { LogoutHandler } from './logout-protocol';
2
+ import { Component } from '@celljs/core';
3
+ import { SecurityContext } from '../context';
4
+
5
+ @Component(LogoutHandler)
6
+ export class SecurityContextLogoutHandler implements LogoutHandler {
7
+
8
+ async logout(): Promise<void> {
9
+ SecurityContext.setCurrent(undefined);
10
+ }
11
+
12
+ }
@@ -0,0 +1,49 @@
1
+ import { RequestMatcher, Middleware, Context } from '@celljs/web/lib/node';
2
+ import { Component, Autowired, Value } from '@celljs/core';
3
+ import { LogoutHandler, LOGOUT_MIDDLEWARE_PRIORITY } from './logout-protocol';
4
+ import { LogoutSuccessHandlerProvider } from './logout-success-handler-provider';
5
+ import { PathResolver } from '@celljs/web';
6
+
7
+ @Component(Middleware)
8
+ export class LogoutMiddleware implements Middleware {
9
+
10
+ @Autowired(LogoutHandler)
11
+ protected readonly logoutHandlers: LogoutHandler[];
12
+
13
+ @Autowired(LogoutSuccessHandlerProvider)
14
+ protected readonly logoutSuccessHandlerProvider: LogoutSuccessHandlerProvider;
15
+
16
+ @Value('cell.security.logoutUrl')
17
+ protected readonly logoutUrl: string;
18
+
19
+ @Value('cell.security.logoutMethod')
20
+ protected readonly logoutMethod: string;
21
+
22
+ @Autowired(PathResolver)
23
+ protected readonly pathResolver: PathResolver;
24
+
25
+ @Autowired(RequestMatcher)
26
+ protected readonly requestMatcher: RequestMatcher;
27
+
28
+ async handle(ctx: Context, next: () => Promise<void>): Promise<void> {
29
+ if (await this.canHandle()) {
30
+ for (const logoutHandler of this.logoutHandlers) {
31
+ await logoutHandler.logout();
32
+ }
33
+
34
+ for (const logoutSuccessHandler of this.logoutSuccessHandlerProvider.provide()) {
35
+ await logoutSuccessHandler.onLogoutSuccess();
36
+ }
37
+ return;
38
+ }
39
+ await next();
40
+
41
+ }
42
+
43
+ async canHandle(): Promise<boolean> {
44
+ return !!await this.requestMatcher.match(await this.pathResolver.resolve(this.logoutUrl), this.logoutMethod);
45
+ }
46
+
47
+ readonly priority: number = LOGOUT_MIDDLEWARE_PRIORITY;
48
+
49
+ }
@@ -0,0 +1,17 @@
1
+ import { SECURITY_CONTEXT_MIDDLEWARE_PRIORITY } from '../context';
2
+
3
+ export const LogoutHandler = Symbol('LogoutHandler');
4
+ export const LogoutSuccessHandler = Symbol('LogoutSuccessHandler');
5
+
6
+ export const LOGOUT_MIDDLEWARE_PRIORITY = SECURITY_CONTEXT_MIDDLEWARE_PRIORITY - 100;
7
+
8
+ export const LOGOUT_SUCCESS_HANDLER_PRIORITY = 2000;
9
+
10
+ export interface LogoutHandler {
11
+ logout(): Promise<void>;
12
+ }
13
+
14
+ export interface LogoutSuccessHandler {
15
+ readonly priority: number;
16
+ onLogoutSuccess(): Promise<void>;
17
+ }
@@ -0,0 +1,21 @@
1
+ import { Component, Autowired, Prioritizeable } from '@celljs/core';
2
+ import { LogoutSuccessHandler } from './logout-protocol';
3
+
4
+ @Component()
5
+ export class LogoutSuccessHandlerProvider {
6
+
7
+ protected prioritized: LogoutSuccessHandler[];
8
+
9
+ constructor(
10
+ @Autowired(LogoutSuccessHandler)
11
+ protected readonly logoutSuccessHandler: LogoutSuccessHandler[]
12
+ ) { }
13
+
14
+ provide(): LogoutSuccessHandler[] {
15
+ if (!this.prioritized) {
16
+ this.prioritized = Prioritizeable.prioritizeAllSync(this.logoutSuccessHandler).map(c => c.value);
17
+ }
18
+ return this.prioritized;
19
+ }
20
+
21
+ }
@@ -0,0 +1,22 @@
1
+ import { LogoutSuccessHandler, LOGOUT_SUCCESS_HANDLER_PRIORITY } from './logout-protocol';
2
+ import { Component, Value, Autowired } from '@celljs/core';
3
+ import { Context, RedirectStrategy } from '@celljs/web/lib/node';
4
+ import { XML_HTTP_REQUEST, HttpHeaders } from '@celljs/http';
5
+
6
+ @Component(LogoutSuccessHandler)
7
+ export class SimpleUrlLogoutSuccessHandler implements LogoutSuccessHandler {
8
+
9
+ @Value('cell.security.logoutSuccessUrl')
10
+ protected readonly logoutSuccessUrl: string;
11
+
12
+ @Autowired(RedirectStrategy)
13
+ protected readonly redirectStrategy: RedirectStrategy;
14
+
15
+ readonly priority = LOGOUT_SUCCESS_HANDLER_PRIORITY;
16
+
17
+ async onLogoutSuccess(): Promise<void> {
18
+ if (Context.getRequest().get(HttpHeaders.X_REQUESTED_WITH) !== XML_HTTP_REQUEST) {
19
+ await this.redirectStrategy.send(this.logoutSuccessUrl);
20
+ }
21
+ }
22
+ }
@@ -0,0 +1,5 @@
1
+ import 'reflect-metadata';
2
+ import { autoBind } from '@celljs/core';
3
+ import './index';
4
+
5
+ export default autoBind();
@@ -0,0 +1,6 @@
1
+ export * from './user-protocol';
2
+ export * from './user-service';
3
+ export * from './user-checker';
4
+ export * from './user-mapper';
5
+ export * from './user-middleware';
6
+
@@ -0,0 +1,37 @@
1
+ import { UserChecker } from './user-protocol';
2
+ import { Component, Autowired, Logger } from '@celljs/core';
3
+ import { LockedError, AccountExpiredError, DisabledError, CredentialsExpiredError } from '../error';
4
+ import { User } from '../../common';
5
+
6
+ @Component(UserChecker)
7
+ export class UserCheckerImpl implements UserChecker {
8
+ @Autowired(Logger)
9
+ protected readonly logger: Logger;
10
+
11
+ async check(user: User): Promise<void> {
12
+ if (!user.accountNonLocked) {
13
+ this.logger.info('User account is locked');
14
+
15
+ throw new LockedError('User account is locked');
16
+ }
17
+
18
+ if (!user.enabled) {
19
+ this.logger.info('User account is disabled');
20
+
21
+ throw new DisabledError('User is disabled');
22
+ }
23
+
24
+ if (!user.accountNonExpired) {
25
+ this.logger.info('User account is expired');
26
+
27
+ throw new AccountExpiredError('User account has expired');
28
+ }
29
+
30
+ if (!user.credentialsNonExpired) {
31
+ this.logger.info('User account is expired');
32
+
33
+ throw new CredentialsExpiredError('User credentials have expired');
34
+ }
35
+ }
36
+
37
+ }
@@ -0,0 +1,26 @@
1
+ import { UserMapper } from './user-protocol';
2
+ import { Component, Value } from '@celljs/core';
3
+ import { User, UserMapperRule } from '../../common';
4
+
5
+ @Component(UserMapper)
6
+ export class UserMapperImpl implements UserMapper {
7
+
8
+ @Value('cell.security.userMapperRule')
9
+ protected rules: { [id: string]: UserMapperRule };
10
+
11
+ async map(user: User): Promise<void> {
12
+ if (user.type && user.claims) {
13
+ const rule = this.rules[user.type];
14
+ if (rule) {
15
+ for (const prop in rule) {
16
+ if (Object.prototype.hasOwnProperty.call(rule, prop)) {
17
+ const value = rule[prop];
18
+ if (value) {
19
+ user[prop] = user.claims[value];
20
+ }
21
+ }
22
+ }
23
+ }
24
+ }
25
+ }
26
+ }
@@ -0,0 +1,40 @@
1
+ import { RequestMatcher, Middleware, Context } from '@celljs/web/lib/node';
2
+ import { Component, Autowired, Value } from '@celljs/core';
3
+ import { HttpHeaders, MediaType } from '@celljs/http';
4
+ import { PathResolver } from '@celljs/web';
5
+ import { USER_MIDDLEWARE_PRIORITY } from './user-protocol';
6
+ import { SecurityContext } from '../context/context-protocol';
7
+
8
+ @Component(Middleware)
9
+ export class UserMiddleware implements Middleware {
10
+
11
+ @Value('cell.security.userInfoEndpoint')
12
+ protected readonly userInfoEndpoint: any;
13
+
14
+ @Autowired(PathResolver)
15
+ protected readonly pathResolver: PathResolver;
16
+
17
+ @Autowired(RequestMatcher)
18
+ protected readonly requestMatcher: RequestMatcher;
19
+
20
+ async handle(ctx: Context, next: () => Promise<void>): Promise<void> {
21
+ if (await this.canHandle()) {
22
+ const auth = SecurityContext.getAuthentication();
23
+ if (auth.authenticated) {
24
+ ctx.response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8);
25
+ ctx.response.body = JSON.stringify(auth.principal);
26
+ }
27
+ return;
28
+ }
29
+ await next();
30
+
31
+ }
32
+
33
+ async canHandle(): Promise<boolean> {
34
+ const { url, method } = this.userInfoEndpoint;
35
+ return !!await this.requestMatcher.match(await this.pathResolver.resolve(url), method);
36
+ }
37
+
38
+ readonly priority: number = USER_MIDDLEWARE_PRIORITY;
39
+
40
+ }
@@ -0,0 +1,20 @@
1
+ import { User } from '../../common';
2
+ import { SECURITY_CONTEXT_MIDDLEWARE_PRIORITY } from '../context/context-protocol';
3
+
4
+ export const UserService = Symbol('UserService');
5
+ export const UserChecker = Symbol('UserChecker');
6
+ export const UserMapper = Symbol('UserMapper');
7
+
8
+ export const USER_MIDDLEWARE_PRIORITY = SECURITY_CONTEXT_MIDDLEWARE_PRIORITY - 100;
9
+
10
+ export interface UserService<R, U extends User> {
11
+ load(userRequest: R): Promise<U>;
12
+ }
13
+
14
+ export interface UserChecker {
15
+ check(user: User): Promise<void>;
16
+ }
17
+
18
+ export interface UserMapper {
19
+ map(user: User): Promise<void>;
20
+ }
@@ -0,0 +1,28 @@
1
+ import { UserService } from './user-protocol';
2
+ import { Value, Component } from '@celljs/core';
3
+ import { UsernameNotFoundError } from '../error';
4
+ import { User, UserType } from '../../common';
5
+
6
+ @Component(UserService)
7
+ export class UserServiceImpl implements UserService<string, User> {
8
+
9
+ @Value('cell.security')
10
+ protected readonly options: any;
11
+
12
+ async load(username: string): Promise<User> {
13
+ if (this.options.username === username) {
14
+ return {
15
+ type: UserType.Memery,
16
+ username,
17
+ password: this.options.password,
18
+ accountNonExpired: true,
19
+ accountNonLocked: true,
20
+ credentialsNonExpired: true,
21
+ enabled: true,
22
+ policies: []
23
+ };
24
+ }
25
+
26
+ throw new UsernameNotFoundError(`Could not find: ${username}`);
27
+ }
28
+ }
@@ -0,0 +1,4 @@
1
+ describe('security package', () => {
2
+
3
+ it('support code coverage statistics', () => {});
4
+ });