@daffodil/auth 0.0.0-PLACEHOLDER

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 (240) hide show
  1. package/.eslintrc.js +51 -0
  2. package/README.md +14 -0
  3. package/driver/in-memory/ng-package.json +8 -0
  4. package/driver/in-memory/package.json +3 -0
  5. package/driver/in-memory/src/backend/auth.service.integration.spec.ts +125 -0
  6. package/driver/in-memory/src/backend/auth.service.ts +131 -0
  7. package/driver/in-memory/src/backend/auth.service.unit.spec.ts +119 -0
  8. package/driver/in-memory/src/drivers/auth/auth.service.spec.ts +57 -0
  9. package/driver/in-memory/src/drivers/auth/auth.service.ts +24 -0
  10. package/driver/in-memory/src/drivers/auth-driver.module.ts +48 -0
  11. package/driver/in-memory/src/drivers/login/login.service.spec.ts +93 -0
  12. package/driver/in-memory/src/drivers/login/login.service.ts +36 -0
  13. package/driver/in-memory/src/drivers/public_api.ts +4 -0
  14. package/driver/in-memory/src/drivers/register/register.service.spec.ts +113 -0
  15. package/driver/in-memory/src/drivers/register/register.service.ts +38 -0
  16. package/driver/in-memory/src/drivers/reset-password/service.spec.ts +149 -0
  17. package/driver/in-memory/src/drivers/reset-password/service.ts +42 -0
  18. package/driver/in-memory/src/index.ts +1 -0
  19. package/driver/in-memory/src/models/db-customer.type.ts +8 -0
  20. package/driver/in-memory/src/public_api.ts +3 -0
  21. package/driver/magento/ng-package.json +8 -0
  22. package/driver/magento/package.json +3 -0
  23. package/driver/magento/src/apollo-bearer-token-link-generator.spec.ts +107 -0
  24. package/driver/magento/src/apollo-bearer-token-link-generator.ts +39 -0
  25. package/driver/magento/src/auth-driver.module.ts +48 -0
  26. package/driver/magento/src/auth.service.spec.ts +154 -0
  27. package/driver/magento/src/auth.service.ts +40 -0
  28. package/driver/magento/src/errors/codes.ts +5 -0
  29. package/driver/magento/src/errors/map.ts +14 -0
  30. package/driver/magento/src/errors/transform.spec.ts +41 -0
  31. package/driver/magento/src/errors/transform.ts +6 -0
  32. package/driver/magento/src/index.ts +1 -0
  33. package/driver/magento/src/login.service.spec.ts +261 -0
  34. package/driver/magento/src/login.service.ts +65 -0
  35. package/driver/magento/src/models/public_api.ts +4 -0
  36. package/driver/magento/src/models/response/auth.ts +3 -0
  37. package/driver/magento/src/models/response/register.type.ts +7 -0
  38. package/driver/magento/src/models/response/reset-password.type.ts +3 -0
  39. package/driver/magento/src/models/response/send-reset-email.type.ts +3 -0
  40. package/driver/magento/src/public_api.ts +12 -0
  41. package/driver/magento/src/queries/check-token.ts +10 -0
  42. package/driver/magento/src/queries/create-customer.ts +24 -0
  43. package/driver/magento/src/queries/generate-token.ts +13 -0
  44. package/driver/magento/src/queries/public_api.ts +8 -0
  45. package/driver/magento/src/queries/reset-password.ts +12 -0
  46. package/driver/magento/src/queries/responses/check-token-response.ts +5 -0
  47. package/driver/magento/src/queries/responses/generate-token-response.ts +5 -0
  48. package/driver/magento/src/queries/responses/public_api.ts +3 -0
  49. package/driver/magento/src/queries/responses/revoke-customer-token-response.ts +5 -0
  50. package/driver/magento/src/queries/revoke-customer-token.ts +10 -0
  51. package/driver/magento/src/queries/send-reset-email.ts +10 -0
  52. package/driver/magento/src/register.service.spec.ts +189 -0
  53. package/driver/magento/src/register.service.ts +65 -0
  54. package/driver/magento/src/reset-password.service.spec.ts +342 -0
  55. package/driver/magento/src/reset-password.service.ts +78 -0
  56. package/driver/magento/src/transforms/auth-transformer.service.spec.ts +42 -0
  57. package/driver/magento/src/transforms/auth-transformer.service.ts +25 -0
  58. package/driver/magento/src/validators/check-token.spec.ts +40 -0
  59. package/driver/magento/src/validators/check-token.ts +13 -0
  60. package/driver/magento/src/validators/generate-token.spec.ts +40 -0
  61. package/driver/magento/src/validators/generate-token.ts +13 -0
  62. package/driver/magento/src/validators/public_api.ts +5 -0
  63. package/driver/magento/src/validators/reset-password.spec.ts +38 -0
  64. package/driver/magento/src/validators/reset-password.ts +13 -0
  65. package/driver/magento/src/validators/revoke-token.spec.ts +40 -0
  66. package/driver/magento/src/validators/revoke-token.ts +13 -0
  67. package/driver/magento/src/validators/send-reset-email.spec.ts +38 -0
  68. package/driver/magento/src/validators/send-reset-email.ts +13 -0
  69. package/driver/ng-package.json +8 -0
  70. package/driver/package.json +3 -0
  71. package/driver/src/errors/authentication-failed.ts +14 -0
  72. package/driver/src/errors/codes.enum.ts +6 -0
  73. package/driver/src/errors/invalid-api-response.ts +14 -0
  74. package/driver/src/errors/map.ts +15 -0
  75. package/driver/src/errors/public_api.ts +6 -0
  76. package/driver/src/errors/registration-failed.ts +17 -0
  77. package/driver/src/errors/unauthorized.ts +14 -0
  78. package/driver/src/index.ts +1 -0
  79. package/driver/src/interfaces/auth-service.interface.ts +11 -0
  80. package/driver/src/interfaces/login-service.interface.ts +26 -0
  81. package/driver/src/interfaces/public_api.ts +4 -0
  82. package/driver/src/interfaces/register-service.interface.ts +26 -0
  83. package/driver/src/interfaces/reset-password-service.interface.ts +26 -0
  84. package/driver/src/public_api.ts +3 -0
  85. package/driver/src/services/auth-check.service.spec.ts +81 -0
  86. package/driver/src/services/auth-check.service.ts +50 -0
  87. package/driver/src/services/public_api.ts +1 -0
  88. package/driver/testing/ng-package.json +8 -0
  89. package/driver/testing/package.json +3 -0
  90. package/driver/testing/src/drivers/auth/auth.service.spec.ts +40 -0
  91. package/driver/testing/src/drivers/auth/auth.service.ts +19 -0
  92. package/driver/testing/src/drivers/auth-driver.module.ts +48 -0
  93. package/driver/testing/src/drivers/login/login.service.spec.ts +59 -0
  94. package/driver/testing/src/drivers/login/login.service.ts +30 -0
  95. package/driver/testing/src/drivers/public_api.ts +4 -0
  96. package/driver/testing/src/drivers/register/register.service.spec.ts +49 -0
  97. package/driver/testing/src/drivers/register/register.service.ts +32 -0
  98. package/driver/testing/src/drivers/reset-password/service.spec.ts +48 -0
  99. package/driver/testing/src/drivers/reset-password/service.ts +33 -0
  100. package/driver/testing/src/index.ts +1 -0
  101. package/driver/testing/src/public_api.ts +1 -0
  102. package/karma.conf.js +10 -0
  103. package/ng-package.json +8 -0
  104. package/ng-package.prod.json +7 -0
  105. package/package.json +39 -0
  106. package/routing/ng-package.json +8 -0
  107. package/routing/package.json +3 -0
  108. package/routing/src/config/default.ts +9 -0
  109. package/routing/src/config/interface.ts +33 -0
  110. package/routing/src/config/public_api.ts +6 -0
  111. package/routing/src/config/token.ts +24 -0
  112. package/routing/src/effects/redirect.effects.spec.ts +236 -0
  113. package/routing/src/effects/redirect.effects.ts +85 -0
  114. package/routing/src/guards/guest-only-guard-redirect.token.ts +3 -0
  115. package/routing/src/guards/guest-only.guard.spec.ts +127 -0
  116. package/routing/src/guards/guest-only.guard.ts +69 -0
  117. package/routing/src/guards/member-only-guard-redirect.token.ts +3 -0
  118. package/routing/src/guards/member-only.guard.spec.ts +127 -0
  119. package/routing/src/guards/member-only.guard.ts +70 -0
  120. package/routing/src/guards/public_api.ts +6 -0
  121. package/routing/src/guards/reset-password.guard.spec.ts +113 -0
  122. package/routing/src/guards/reset-password.guard.ts +44 -0
  123. package/routing/src/index.ts +1 -0
  124. package/routing/src/module.ts +34 -0
  125. package/routing/src/public_api.ts +5 -0
  126. package/routing/src/redirect.module.ts +13 -0
  127. package/src/errors/codes.enum.ts +3 -0
  128. package/src/errors/map.ts +9 -0
  129. package/src/errors/missing-token.ts +18 -0
  130. package/src/errors/public_api.ts +3 -0
  131. package/src/index.ts +1 -0
  132. package/src/injection-tokens/error-matcher.token.ts +12 -0
  133. package/src/injection-tokens/public_api.ts +1 -0
  134. package/src/models/account-registration.ts +18 -0
  135. package/src/models/auth-token.ts +3 -0
  136. package/src/models/login-info.ts +4 -0
  137. package/src/models/public_api.ts +4 -0
  138. package/src/models/reset-password-info.type.ts +14 -0
  139. package/src/public_api.ts +5 -0
  140. package/src/storage/auth-storage.service.spec.ts +43 -0
  141. package/src/storage/auth-storage.service.ts +30 -0
  142. package/state/ng-package.json +8 -0
  143. package/state/package.json +3 -0
  144. package/state/src/actions/auth.actions.ts +75 -0
  145. package/state/src/actions/login.actions.ts +85 -0
  146. package/state/src/actions/public_api.ts +4 -0
  147. package/state/src/actions/register.actions.ts +50 -0
  148. package/state/src/actions/reset-password.actions.ts +92 -0
  149. package/state/src/auth-state.module.ts +45 -0
  150. package/state/src/config/default.ts +7 -0
  151. package/state/src/config/public_api.ts +6 -0
  152. package/state/src/config/token.ts +14 -0
  153. package/state/src/config/type.ts +7 -0
  154. package/state/src/effects/auth.effects.spec.ts +232 -0
  155. package/state/src/effects/auth.effects.ts +125 -0
  156. package/state/src/effects/login.effects.spec.ts +258 -0
  157. package/state/src/effects/login.effects.ts +126 -0
  158. package/state/src/effects/register.effects.spec.ts +206 -0
  159. package/state/src/effects/register.effects.ts +93 -0
  160. package/state/src/effects/reset-password.effects.spec.ts +238 -0
  161. package/state/src/effects/reset-password.effects.ts +110 -0
  162. package/state/src/facades/auth/facade.interface.ts +18 -0
  163. package/state/src/facades/auth/facade.spec.ts +135 -0
  164. package/state/src/facades/auth/facade.ts +42 -0
  165. package/state/src/facades/auth/public_api.ts +2 -0
  166. package/state/src/facades/login/facade.interface.ts +14 -0
  167. package/state/src/facades/login/facade.spec.ts +111 -0
  168. package/state/src/facades/login/facade.ts +38 -0
  169. package/state/src/facades/login/public_api.ts +2 -0
  170. package/state/src/facades/public_api.ts +4 -0
  171. package/state/src/facades/register/facade.interface.ts +14 -0
  172. package/state/src/facades/register/facade.spec.ts +103 -0
  173. package/state/src/facades/register/facade.ts +38 -0
  174. package/state/src/facades/register/public_api.ts +2 -0
  175. package/state/src/facades/reset-password/facade.interface.ts +17 -0
  176. package/state/src/facades/reset-password/facade.spec.ts +112 -0
  177. package/state/src/facades/reset-password/facade.ts +44 -0
  178. package/state/src/facades/reset-password/public_api.ts +2 -0
  179. package/state/src/index.ts +1 -0
  180. package/state/src/public_api.ts +8 -0
  181. package/state/src/reducers/auth/auth-initial-state.ts +7 -0
  182. package/state/src/reducers/auth/auth-reducer-state.interface.ts +7 -0
  183. package/state/src/reducers/auth/auth.reducer.spec.ts +250 -0
  184. package/state/src/reducers/auth/auth.reducer.ts +72 -0
  185. package/state/src/reducers/auth/public_api.ts +3 -0
  186. package/state/src/reducers/auth-feature-state.interface.ts +16 -0
  187. package/state/src/reducers/auth-reducers.ts +14 -0
  188. package/state/src/reducers/auth-store-feature-key.ts +1 -0
  189. package/state/src/reducers/login/login-initial-state.ts +6 -0
  190. package/state/src/reducers/login/login-reducer-state.interface.ts +6 -0
  191. package/state/src/reducers/login/login.reducer.spec.ts +190 -0
  192. package/state/src/reducers/login/login.reducer.ts +47 -0
  193. package/state/src/reducers/login/public_api.ts +3 -0
  194. package/state/src/reducers/public_api.ts +11 -0
  195. package/state/src/reducers/register/public_api.ts +3 -0
  196. package/state/src/reducers/register/register-initial-state.ts +6 -0
  197. package/state/src/reducers/register/register-reducer-state.interface.ts +6 -0
  198. package/state/src/reducers/register/register.reducer.spec.ts +111 -0
  199. package/state/src/reducers/register/register.reducer.ts +40 -0
  200. package/state/src/reducers/reset-password/initial-state.ts +7 -0
  201. package/state/src/reducers/reset-password/public_api.ts +3 -0
  202. package/state/src/reducers/reset-password/reducer.spec.ts +196 -0
  203. package/state/src/reducers/reset-password/reducer.ts +49 -0
  204. package/state/src/reducers/reset-password/state.interface.ts +8 -0
  205. package/state/src/selectors/auth/auth.selector.spec.ts +87 -0
  206. package/state/src/selectors/auth/auth.selector.ts +51 -0
  207. package/state/src/selectors/auth-all.selector.ts +40 -0
  208. package/state/src/selectors/auth-feature.selector.ts +18 -0
  209. package/state/src/selectors/login/login.selector.spec.ts +88 -0
  210. package/state/src/selectors/login/login.selector.ts +44 -0
  211. package/state/src/selectors/public_api.ts +1 -0
  212. package/state/src/selectors/register/register.selector.spec.ts +75 -0
  213. package/state/src/selectors/register/register.selector.ts +45 -0
  214. package/state/src/selectors/reset-password/selector.spec.ts +90 -0
  215. package/state/src/selectors/reset-password/selector.ts +52 -0
  216. package/state/testing/ng-package.json +8 -0
  217. package/state/testing/package.json +3 -0
  218. package/state/testing/src/auth-testing.module.ts +23 -0
  219. package/state/testing/src/index.ts +1 -0
  220. package/state/testing/src/mock-auth-facade.ts +18 -0
  221. package/state/testing/src/mock-login-facade.ts +17 -0
  222. package/state/testing/src/mock-register-facade.ts +17 -0
  223. package/state/testing/src/mock-reset-password-facade.ts +18 -0
  224. package/state/testing/src/public_api.ts +5 -0
  225. package/test.ts +24 -0
  226. package/testing/ng-package.json +8 -0
  227. package/testing/package.json +3 -0
  228. package/testing/src/factories/account-registration.factory.spec.ts +37 -0
  229. package/testing/src/factories/account-registration.factory.ts +22 -0
  230. package/testing/src/factories/auth-token.factory.ts +18 -0
  231. package/testing/src/factories/auth.factory.spec.ts +45 -0
  232. package/testing/src/factories/public_api.ts +3 -0
  233. package/testing/src/factories/reset-password-info.factory.spec.ts +35 -0
  234. package/testing/src/factories/reset-password-info.factory.ts +20 -0
  235. package/testing/src/index.ts +1 -0
  236. package/testing/src/public_api.ts +1 -0
  237. package/tsconfig.json +38 -0
  238. package/tsconfig.lib.json +28 -0
  239. package/tsconfig.lib.prod.json +9 -0
  240. package/tsconfig.spec.json +17 -0
@@ -0,0 +1,50 @@
1
+ import { Action } from '@ngrx/store';
2
+
3
+ import { DaffAccountRegistration } from '@daffodil/auth';
4
+ import { DaffStateError } from '@daffodil/core/state';
5
+
6
+ export enum DaffAuthRegisterActionTypes {
7
+ RegisterAction = '[@daffodil/auth] Register Action',
8
+ RegisterSuccessAction = '[@daffodil/auth] Register Success Action',
9
+ RegisterFailureAction = '[@daffodil/auth] Register Failure Action',
10
+ }
11
+
12
+ /**
13
+ * An action triggered to initialize a auth register request.
14
+ *
15
+ * @param registration
16
+ */
17
+ export class DaffAuthRegister<T extends DaffAccountRegistration = DaffAccountRegistration> implements Action {
18
+ readonly type = DaffAuthRegisterActionTypes.RegisterAction;
19
+
20
+ constructor(public registration: T, public autoLogin = true) {}
21
+ }
22
+
23
+ /**
24
+ * An action triggered upon a successful auth register request.
25
+ *
26
+ * @param token - the customer access token
27
+ */
28
+ export class DaffAuthRegisterSuccess implements Action {
29
+ readonly type = DaffAuthRegisterActionTypes.RegisterSuccessAction;
30
+
31
+ constructor(public token?: string) {}
32
+ }
33
+
34
+ /**
35
+ * An action triggered upon a failed auth request.
36
+ *
37
+ * @param errorMessage - an error message
38
+ */
39
+ export class DaffAuthRegisterFailure implements Action {
40
+ readonly type = DaffAuthRegisterActionTypes.RegisterFailureAction;
41
+
42
+ constructor(public errorMessage: DaffStateError) {}
43
+ }
44
+
45
+ export type DaffAuthRegisterActions<
46
+ T extends DaffAccountRegistration = DaffAccountRegistration,
47
+ > =
48
+ | DaffAuthRegister<T>
49
+ | DaffAuthRegisterSuccess
50
+ | DaffAuthRegisterFailure;
@@ -0,0 +1,92 @@
1
+ import { Action } from '@ngrx/store';
2
+
3
+ import { DaffAuthResetPasswordInfo } from '@daffodil/auth';
4
+ import { DaffStateError } from '@daffodil/core/state';
5
+
6
+ export enum DaffAuthResetPasswordActionTypes {
7
+ SendResetEmailAction = '[@daffodil/auth] Send Reset Email Action',
8
+ SendResetEmailSuccessAction = '[@daffodil/auth] Send Reset Email Success Action',
9
+ SendResetEmailFailureAction = '[@daffodil/auth] Send Reset Email Failure Action',
10
+ ResetPasswordAction = '[@daffodil/auth] Reset Password Action',
11
+ ResetPasswordSuccessAction = '[@daffodil/auth] Reset Password Success Action',
12
+ ResetPasswordFailureAction = '[@daffodil/auth] Reset Password Failure Action',
13
+ ResetPasswordLandingAction = '[@daffodil/auth] Reset Password Landing Action',
14
+ }
15
+
16
+ /**
17
+ * An action triggered to send the specified customer an email to reset their password.
18
+ */
19
+ export class DaffSendResetEmail implements Action {
20
+ readonly type = DaffAuthResetPasswordActionTypes.SendResetEmailAction;
21
+
22
+ constructor(public email: string) {}
23
+ }
24
+
25
+ /**
26
+ * An action triggered upon a successfully sent reset password email.
27
+ */
28
+ export class DaffSendResetEmailSuccess implements Action {
29
+ readonly type = DaffAuthResetPasswordActionTypes.SendResetEmailSuccessAction;
30
+ }
31
+
32
+ /**
33
+ * An action triggered upon a failure to send the reset password email.
34
+ *
35
+ * @param errorMessage - an error message
36
+ */
37
+ export class DaffSendResetEmailFailure implements Action {
38
+ readonly type = DaffAuthResetPasswordActionTypes.SendResetEmailFailureAction;
39
+
40
+ constructor(public errorMessage: DaffStateError) {}
41
+ }
42
+
43
+ /**
44
+ * An action triggered to initialize a logout request.
45
+ */
46
+ export class DaffResetPassword<T extends DaffAuthResetPasswordInfo = DaffAuthResetPasswordInfo> implements Action {
47
+ readonly type = DaffAuthResetPasswordActionTypes.ResetPasswordAction;
48
+
49
+ constructor(public info: T, public autoLogin = true) {}
50
+ }
51
+
52
+ /**
53
+ * An action triggered upon a successful logout.
54
+ */
55
+ export class DaffResetPasswordSuccess implements Action {
56
+ readonly type = DaffAuthResetPasswordActionTypes.ResetPasswordSuccessAction;
57
+
58
+ constructor(public token?: string) {}
59
+ }
60
+
61
+ /**
62
+ * An action triggered upon a failed logout request.
63
+ *
64
+ * @param errorMessage - an error message
65
+ */
66
+ export class DaffResetPasswordFailure implements Action {
67
+ readonly type = DaffAuthResetPasswordActionTypes.ResetPasswordFailureAction;
68
+
69
+ constructor(public errorMessage: DaffStateError) {}
70
+ }
71
+
72
+ /**
73
+ * An action triggered upon the user navigating to the reset password page from the link in their email.
74
+ *
75
+ * @param token - The token with which the password can be reset
76
+ */
77
+ export class DaffResetPasswordLanding implements Action {
78
+ readonly type = DaffAuthResetPasswordActionTypes.ResetPasswordLandingAction;
79
+
80
+ constructor(public token: DaffAuthResetPasswordInfo['token']) {}
81
+ }
82
+
83
+ export type DaffAuthResetPasswordActions<
84
+ T extends DaffAuthResetPasswordInfo = DaffAuthResetPasswordInfo,
85
+ > =
86
+ | DaffSendResetEmail
87
+ | DaffSendResetEmailSuccess
88
+ | DaffSendResetEmailFailure
89
+ | DaffResetPassword<T>
90
+ | DaffResetPasswordSuccess
91
+ | DaffResetPasswordFailure
92
+ | DaffResetPasswordLanding;
@@ -0,0 +1,45 @@
1
+ import {
2
+ ModuleWithProviders,
3
+ NgModule,
4
+ } from '@angular/core';
5
+ import { EffectsModule } from '@ngrx/effects';
6
+ import { StoreModule } from '@ngrx/store';
7
+
8
+ import {
9
+ DaffAuthStateConfig,
10
+ daffAuthStateDefaultConfig,
11
+ provideDaffAuthStateConfig,
12
+ } from './config/public_api';
13
+ import { DaffAuthEffects } from './effects/auth.effects';
14
+ import { DaffAuthLoginEffects } from './effects/login.effects';
15
+ import { DaffAuthRegisterEffects } from './effects/register.effects';
16
+ import { DaffAuthResetPasswordEffects } from './effects/reset-password.effects';
17
+ import {
18
+ DAFF_AUTH_STORE_FEATURE_KEY,
19
+ daffAuthReducers,
20
+ } from './reducers/public_api';
21
+
22
+ @NgModule({
23
+ imports: [
24
+ StoreModule.forFeature(DAFF_AUTH_STORE_FEATURE_KEY, daffAuthReducers),
25
+ EffectsModule.forFeature([
26
+ DaffAuthEffects,
27
+ DaffAuthLoginEffects,
28
+ DaffAuthRegisterEffects,
29
+ DaffAuthResetPasswordEffects,
30
+ ]),
31
+ ],
32
+ })
33
+ export class DaffAuthStateModule {
34
+ static withConfig(config: Partial<DaffAuthStateConfig> = {}): ModuleWithProviders<DaffAuthStateModule> {
35
+ return {
36
+ ngModule: DaffAuthStateModule,
37
+ providers: [
38
+ provideDaffAuthStateConfig({
39
+ ...daffAuthStateDefaultConfig,
40
+ ...config,
41
+ }),
42
+ ],
43
+ };
44
+ }
45
+ }
@@ -0,0 +1,7 @@
1
+ import { DaffAuthStateConfig } from './type';
2
+
3
+ const TWENTY_MINUTES = 1.2e6;
4
+
5
+ export const daffAuthStateDefaultConfig: DaffAuthStateConfig = {
6
+ checkInterval: TWENTY_MINUTES,
7
+ };
@@ -0,0 +1,6 @@
1
+ export { daffAuthStateDefaultConfig } from './default';
2
+ export {
3
+ DAFF_AUTH_STATE_CONFIG,
4
+ provideDaffAuthStateConfig,
5
+ } from './token';
6
+ export { DaffAuthStateConfig } from './type';
@@ -0,0 +1,14 @@
1
+ import {
2
+ InjectionToken,
3
+ ValueProvider,
4
+ } from '@angular/core';
5
+
6
+ import { daffAuthStateDefaultConfig } from './default';
7
+ import { DaffAuthStateConfig } from './type';
8
+
9
+ export const DAFF_AUTH_STATE_CONFIG = new InjectionToken('DAFF_AUTH_STATE_CONFIG', { factory: () => daffAuthStateDefaultConfig });
10
+
11
+ export const provideDaffAuthStateConfig = (config: DaffAuthStateConfig): ValueProvider => ({
12
+ provide: DAFF_AUTH_STATE_CONFIG,
13
+ useValue: config,
14
+ });
@@ -0,0 +1,7 @@
1
+ export interface DaffAuthStateConfig {
2
+ /**
3
+ * How often Daffodil will run the auth check.
4
+ * Defaults to twenty minutes.
5
+ */
6
+ checkInterval: number;
7
+ }
@@ -0,0 +1,232 @@
1
+ import { TestBed } from '@angular/core/testing';
2
+ import { provideMockActions } from '@ngrx/effects/testing';
3
+ import {
4
+ hot,
5
+ cold,
6
+ } from 'jasmine-marbles';
7
+ import {
8
+ Observable ,
9
+ of,
10
+ } from 'rxjs';
11
+
12
+ import { DaffAuthStorageService } from '@daffodil/auth';
13
+ import {
14
+ DaffAuthDriver,
15
+ DaffAuthServiceInterface,
16
+ DaffAuthenticationFailedError,
17
+ } from '@daffodil/auth/driver';
18
+ import {
19
+ DaffAuthCheck,
20
+ DaffAuthCheckSuccess,
21
+ DaffAuthCheckFailure,
22
+ DAFF_AUTH_STATE_CONFIG,
23
+ DaffAuthStateConfig,
24
+ DaffAuthServerSide,
25
+ DaffAuthStorageFailure,
26
+ DaffAuthGuardLogout,
27
+ DaffAuthLogoutSuccess,
28
+ } from '@daffodil/auth/state';
29
+ import {
30
+ DaffServerSideStorageError,
31
+ DaffStorageServiceError,
32
+ } from '@daffodil/core';
33
+ import { daffTransformErrorToStateError } from '@daffodil/core/state';
34
+ import {
35
+ DAFF_DRIVER_HTTP_CLIENT_CACHE_SERVICE,
36
+ DaffDriverHttpClientCacheServiceInterface,
37
+ } from '@daffodil/driver';
38
+
39
+ import { DaffAuthEffects } from './auth.effects';
40
+
41
+ describe('@daffodil/auth/state | DaffAuthEffects', () => {
42
+ let actions$: Observable<any>;
43
+ let effects: DaffAuthEffects;
44
+
45
+ let daffAuthStorageService: DaffAuthStorageService;
46
+ let daffAuthDriver: jasmine.SpyObj<DaffAuthServiceInterface>;
47
+ let clientCacheSpy: jasmine.SpyObj<DaffDriverHttpClientCacheServiceInterface>;
48
+ let getTokenSpy: jasmine.Spy<DaffAuthStorageService['getAuthToken']>;
49
+ let removeTokenSpy: jasmine.Spy<DaffAuthStorageService['removeAuthToken']>;
50
+
51
+ const authStorageFailureAction = new DaffAuthStorageFailure(daffTransformErrorToStateError(
52
+ new DaffStorageServiceError('Storage of auth token has failed.')),
53
+ );
54
+ const throwStorageError = () => {
55
+ throw new DaffStorageServiceError('Storage of auth token has failed.');
56
+ };
57
+
58
+ beforeEach(() => {
59
+ clientCacheSpy = jasmine.createSpyObj('DaffDriverHttpClientCacheServiceInterface', ['reset']);
60
+
61
+ TestBed.configureTestingModule({
62
+ providers: [
63
+ DaffAuthEffects,
64
+ provideMockActions(() => actions$),
65
+ {
66
+ provide: DaffAuthDriver,
67
+ useValue: jasmine.createSpyObj('DaffAuthService', ['check']),
68
+ },
69
+ {
70
+ provide: DAFF_AUTH_STATE_CONFIG,
71
+ useValue: <DaffAuthStateConfig>{
72
+ checkInterval: 100,
73
+ },
74
+ },
75
+ {
76
+ provide: DAFF_DRIVER_HTTP_CLIENT_CACHE_SERVICE,
77
+ useValue: clientCacheSpy,
78
+ },
79
+ ],
80
+ });
81
+
82
+ effects = TestBed.inject(DaffAuthEffects);
83
+
84
+ daffAuthDriver = TestBed.inject<jasmine.SpyObj<DaffAuthServiceInterface>>(DaffAuthDriver);
85
+ daffAuthStorageService = TestBed.inject(DaffAuthStorageService);
86
+
87
+ removeTokenSpy = spyOn(daffAuthStorageService, 'removeAuthToken');
88
+ getTokenSpy = spyOn(daffAuthStorageService, 'getAuthToken');
89
+ });
90
+
91
+ it('should be created', () => {
92
+ expect(effects).toBeTruthy();
93
+ });
94
+
95
+ describe('authCheckInterval$', () => {
96
+ let expected;
97
+
98
+ describe('when there is a token in storage', () => {
99
+
100
+ beforeEach(() => {
101
+ const mockAuthCheckAction = new DaffAuthCheck();
102
+ getTokenSpy.and.returnValue('token');
103
+
104
+ expected = cold('b', { b: mockAuthCheckAction });
105
+ });
106
+
107
+ it('should dispatch DaffAuthCheck every 100 ms', () => {
108
+ // TODO: fixgure out how to pass scheduler
109
+ expect(effects.authCheckInterval$).toBeObservable(expected);
110
+ });
111
+ });
112
+ });
113
+
114
+ describe('check$ | when the user checks if their auth token is valid', () => {
115
+ let expected;
116
+
117
+ const mockAuthCheckAction = new DaffAuthCheck();
118
+
119
+ beforeEach(() => {
120
+ actions$ = hot('--a', { a: mockAuthCheckAction });
121
+ getTokenSpy.and.returnValue('token');
122
+ });
123
+
124
+ describe('and the check is successful', () => {
125
+ beforeEach(() => {
126
+ daffAuthDriver.check.and.returnValue(of(undefined));
127
+ const mockAuthCheckSuccessAction = new DaffAuthCheckSuccess();
128
+
129
+ expected = cold('--b', { b: mockAuthCheckSuccessAction });
130
+ });
131
+
132
+ it('should notify state that the check succeeded', () => {
133
+ expect(effects.check$).toBeObservable(expected);
134
+ });
135
+ });
136
+
137
+ describe('and the check fails', () => {
138
+ let error: DaffAuthenticationFailedError;
139
+
140
+ beforeEach(() => {
141
+ error = new DaffAuthenticationFailedError('Auth token is not valid');
142
+ const response = cold('#', {}, error);
143
+ daffAuthDriver.check.and.returnValue(response);
144
+ const mockAuthCheckFailureAction = jasmine.any(DaffAuthCheckFailure);
145
+
146
+ expected = cold('--b', { b: mockAuthCheckFailureAction });
147
+ });
148
+
149
+ it('should notify state that the check failed', () => {
150
+ expect(effects.check$).toBeObservable(expected);
151
+ });
152
+ });
153
+ });
154
+
155
+ describe('removeAuthToken$', () => {
156
+ let expected;
157
+
158
+ describe('when AuthCheckFailure is dispatched', () => {
159
+ let authLogoutSuccessAction: DaffAuthCheckFailure;
160
+
161
+ beforeEach(() => {
162
+ authLogoutSuccessAction = new DaffAuthCheckFailure({ code: 'code', message: 'message' });
163
+ actions$ = hot('--a', { a: authLogoutSuccessAction });
164
+ expected = cold('---');
165
+ });
166
+
167
+ it('should remove the auth token from storage', () => {
168
+ expect(effects.removeAuthToken$).toBeObservable(expected);
169
+ expect(removeTokenSpy).toHaveBeenCalledWith();
170
+ });
171
+
172
+ describe('and the storage service throws an error', () => {
173
+ beforeEach(() => {
174
+ removeTokenSpy.and.callFake(throwStorageError);
175
+
176
+ expected = cold('--(b|)', { b: authStorageFailureAction });
177
+ });
178
+
179
+ it('should return a DaffAuthStorageFailure', () => {
180
+ expect(effects.removeAuthToken$).toBeObservable(expected);
181
+ });
182
+ });
183
+
184
+ describe('and the storage service throws a server side error', () => {
185
+ beforeEach(() => {
186
+ const error = new DaffServerSideStorageError('Server side');
187
+ const serverSideAction = new DaffAuthServerSide(daffTransformErrorToStateError(error));
188
+ removeTokenSpy.and.throwError(error);
189
+ expected = cold('--(a|)', { a: serverSideAction });
190
+ });
191
+
192
+ it('should dispatch a server side action', () => {
193
+ expect(effects.removeAuthToken$).toBeObservable(expected);
194
+ });
195
+ });
196
+ });
197
+ });
198
+
199
+ describe('clearClientCache$', () => {
200
+ let expected;
201
+
202
+ describe('when DaffAuthCheckFailure is dispatched', () => {
203
+ let revokeAction: DaffAuthCheckFailure;
204
+
205
+ beforeEach(() => {
206
+ revokeAction = new DaffAuthCheckFailure(null);
207
+ actions$ = hot('--a', { a: revokeAction });
208
+ expected = cold('---');
209
+ });
210
+
211
+ it('should reset the client cache', () => {
212
+ expect(effects.clearClientCache$).toBeObservable(expected);
213
+ expect(clientCacheSpy.reset).toHaveBeenCalledWith();
214
+ });
215
+ });
216
+
217
+ describe('when DaffAuthGuardLogout is dispatched', () => {
218
+ let revokeAction: DaffAuthGuardLogout;
219
+
220
+ beforeEach(() => {
221
+ revokeAction = new DaffAuthGuardLogout(null);
222
+ actions$ = hot('--a', { a: revokeAction });
223
+ expected = cold('---');
224
+ });
225
+
226
+ it('should reset the client cache', () => {
227
+ expect(effects.clearClientCache$).toBeObservable(expected);
228
+ expect(clientCacheSpy.reset).toHaveBeenCalledWith();
229
+ });
230
+ });
231
+ });
232
+ });
@@ -0,0 +1,125 @@
1
+ import {
2
+ Injectable,
3
+ Inject,
4
+ } from '@angular/core';
5
+ import {
6
+ Actions,
7
+ createEffect,
8
+ ofType,
9
+ } from '@ngrx/effects';
10
+ import {
11
+ of,
12
+ EMPTY,
13
+ } from 'rxjs';
14
+ import {
15
+ switchMap,
16
+ catchError,
17
+ map,
18
+ repeat,
19
+ filter,
20
+ tap,
21
+ } from 'rxjs/operators';
22
+
23
+ import {
24
+ DaffAuthStorageService,
25
+ DAFF_AUTH_ERROR_MATCHER,
26
+ } from '@daffodil/auth';
27
+ import { DaffAuthDriverTokenCheck } from '@daffodil/auth/driver';
28
+ import {
29
+ DaffError,
30
+ DaffServerSideStorageError,
31
+ DaffStorageServiceError,
32
+ } from '@daffodil/core';
33
+ import { ErrorTransformer } from '@daffodil/core/state';
34
+ import {
35
+ DAFF_DRIVER_HTTP_CLIENT_CACHE_SERVICE,
36
+ DaffDriverHttpClientCacheServiceInterface,
37
+ } from '@daffodil/driver';
38
+
39
+ import {
40
+ DaffAuthActionTypes,
41
+ DaffAuthCheckSuccess,
42
+ DaffAuthCheckFailure,
43
+ DaffAuthCheck,
44
+ DaffAuthStorageFailure,
45
+ DaffAuthServerSide,
46
+ DaffAuthLoginActionTypes,
47
+ DaffAuthActions,
48
+ DaffAuthLoginActions,
49
+ } from '../actions/public_api';
50
+ import {
51
+ DaffAuthStateConfig,
52
+ DAFF_AUTH_STATE_CONFIG,
53
+ } from '../config/public_api';
54
+
55
+ @Injectable()
56
+ export class DaffAuthEffects {
57
+ constructor(
58
+ private actions$: Actions<DaffAuthActions | DaffAuthLoginActions>,
59
+ private tokenCheck: DaffAuthDriverTokenCheck,
60
+ @Inject(DAFF_AUTH_ERROR_MATCHER) private errorMatcher: ErrorTransformer,
61
+ private storage: DaffAuthStorageService,
62
+ @Inject(DAFF_AUTH_STATE_CONFIG) private config: DaffAuthStateConfig,
63
+ @Inject(DAFF_DRIVER_HTTP_CLIENT_CACHE_SERVICE) private clientCache: DaffDriverHttpClientCacheServiceInterface,
64
+ ) {}
65
+
66
+ check$ = createEffect(() => this.actions$.pipe(
67
+ ofType(DaffAuthActionTypes.AuthCheckAction),
68
+ switchMap((action) =>
69
+ this.tokenCheck.check().pipe(
70
+ map(() => new DaffAuthCheckSuccess()),
71
+ catchError((error: DaffError) => of(new DaffAuthCheckFailure(this.errorMatcher(error)))),
72
+ ),
73
+ ),
74
+ ));
75
+
76
+ removeAuthToken$ = createEffect(() => this.actions$.pipe(
77
+ ofType(
78
+ DaffAuthActionTypes.AuthCheckFailureAction,
79
+ ),
80
+ tap(() => {
81
+ this.storage.removeAuthToken();
82
+ }),
83
+ switchMap(() => EMPTY),
84
+ catchError((error: Error) => {
85
+ switch (true) {
86
+ case error instanceof DaffServerSideStorageError:
87
+ return of(new DaffAuthServerSide(this.errorMatcher(error)));
88
+
89
+ case error instanceof DaffStorageServiceError:
90
+ default:
91
+ return of(new DaffAuthStorageFailure(this.errorMatcher(error)));
92
+ }
93
+ }),
94
+ ), { dispatch: false });
95
+
96
+ // this needs to be defined after `check$` or else the driver call won't be run
97
+ authCheckInterval$ = createEffect(() => of(new DaffAuthCheck()).pipe(
98
+ repeat({ delay: this.config.checkInterval }),
99
+ filter(() => !!this.storage.getAuthToken()),
100
+ catchError((error: Error) => {
101
+ switch (true) {
102
+ case error instanceof DaffServerSideStorageError:
103
+ return of(new DaffAuthServerSide(this.errorMatcher(error)));
104
+
105
+ case error instanceof DaffStorageServiceError:
106
+ return of(new DaffAuthStorageFailure(this.errorMatcher(error)));
107
+
108
+ default:
109
+ return EMPTY;
110
+ }
111
+ }),
112
+ ));
113
+
114
+ clearClientCache$ = createEffect(() => this.actions$.pipe(
115
+ ofType(
116
+ DaffAuthActionTypes.AuthCheckFailureAction,
117
+ DaffAuthActionTypes.AuthGuardLogoutAction,
118
+ DaffAuthLoginActionTypes.LogoutSuccessAction,
119
+ ),
120
+ tap(() => {
121
+ this.clientCache.reset();
122
+ }),
123
+ switchMap(() => EMPTY),
124
+ ), { dispatch: false });
125
+ }