@amedia/user 0.13.39 → 0.13.41

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 (86) hide show
  1. package/dist/user.cjs +1 -1
  2. package/dist/user.cjs.map +3 -3
  3. package/dist/user.d.ts +5 -5
  4. package/dist/user.js +1 -1
  5. package/dist/user.js.map +3 -3
  6. package/package.json +5 -3
  7. package/src/actions/authentication.spec.ts +51 -0
  8. package/src/actions/authentication.ts +43 -0
  9. package/src/actions/authorization.spec.ts +369 -0
  10. package/src/actions/authorization.ts +117 -0
  11. package/src/actions/preferences.spec.ts +41 -0
  12. package/src/actions/preferences.ts +41 -0
  13. package/src/actions/storage.ts +22 -0
  14. package/src/actions/userData.spec.ts +133 -0
  15. package/src/actions/userData.ts +68 -0
  16. package/src/autologin/index.spec.ts +239 -0
  17. package/src/autologin/index.ts +92 -0
  18. package/src/autologin/thirdPartyCookie.spec.ts +95 -0
  19. package/src/autologin/thirdPartyCookie.ts +55 -0
  20. package/src/cache/engines/global.spec.ts +62 -0
  21. package/src/cache/engines/global.ts +49 -0
  22. package/src/cache/index.spec.ts +130 -0
  23. package/src/cache/index.ts +48 -0
  24. package/src/cache/types.ts +12 -0
  25. package/src/clients/europa.spec.ts +209 -0
  26. package/src/clients/europa.ts +183 -0
  27. package/src/clients/jupiter.spec.ts +179 -0
  28. package/src/clients/jupiter.ts +119 -0
  29. package/src/clients/mercury.spec.ts +59 -0
  30. package/src/clients/mercury.ts +48 -0
  31. package/src/clients/mercuryLog.spec.ts +20 -0
  32. package/src/clients/mercuryLog.ts +67 -0
  33. package/src/clients/nebula.spec.ts +70 -0
  34. package/src/clients/nebula.ts +83 -0
  35. package/src/clients/pluto.spec.ts +165 -0
  36. package/src/clients/pluto.ts +118 -0
  37. package/src/eventHandlers/activation.ts +36 -0
  38. package/src/eventHandlers/authentication.ts +34 -0
  39. package/src/eventHandlers/eventMap.ts +34 -0
  40. package/src/eventHandlers/state.spec.ts +48 -0
  41. package/src/eventHandlers/state.ts +23 -0
  42. package/src/eventHandlers/storage.spec.ts +114 -0
  43. package/src/eventHandlers/storage.ts +45 -0
  44. package/src/eventHandlers/userAttributes.spec.ts +93 -0
  45. package/src/eventHandlers/userAttributes.ts +31 -0
  46. package/src/exceptions/FetchTimeoutError.ts +8 -0
  47. package/src/exceptions/PaywallError.ts +16 -0
  48. package/src/index-authentcation.spec.ts +26 -0
  49. package/src/index-storage.spec.ts +144 -0
  50. package/src/index-subscription.spec.ts +213 -0
  51. package/src/index.ts +40 -0
  52. package/src/janitor/index.spec.ts +18 -0
  53. package/src/janitor/index.ts +59 -0
  54. package/src/janitor/jobs/accessCookieRefresher.spec.ts +213 -0
  55. package/src/janitor/jobs/accessCookieRefresher.ts +98 -0
  56. package/src/janitor/jobs/daxsubCookie.spec.ts +109 -0
  57. package/src/janitor/jobs/daxsubCookie.ts +38 -0
  58. package/src/janitor/jobs/removeUserStorage.spec.ts +34 -0
  59. package/src/janitor/jobs/removeUserStorage.ts +12 -0
  60. package/src/lib/PaywallUnlockRequest.spec.ts +363 -0
  61. package/src/lib/PaywallUnlockRequest.ts +143 -0
  62. package/src/lib/UserDataRequest.spec.ts +231 -0
  63. package/src/lib/UserDataRequest.ts +267 -0
  64. package/src/lib/cookie.ts +121 -0
  65. package/src/lib/rpc.ts +19 -0
  66. package/src/store/index.spec.ts +49 -0
  67. package/src/store/index.ts +64 -0
  68. package/src/types.ts +22 -0
  69. package/src/utils/customEventWrapper.ts +58 -0
  70. package/src/utils/debouncer.spec.ts +252 -0
  71. package/src/utils/debouncer.ts +123 -0
  72. package/src/utils/deepEquals.ts +42 -0
  73. package/src/utils/expectBootLogs.ts +9 -0
  74. package/src/utils/getGlobalThis.ts +17 -0
  75. package/src/utils/getOrUpdate.spec.ts +166 -0
  76. package/src/utils/getOrUpdate.ts +66 -0
  77. package/src/utils/localhostCheck.ts +10 -0
  78. package/src/utils/location.ts +31 -0
  79. package/src/utils/log.spec.ts +148 -0
  80. package/src/utils/log.ts +124 -0
  81. package/src/utils/loginState.ts +24 -0
  82. package/src/utils/networkError.ts +11 -0
  83. package/src/utils/queue.ts +32 -0
  84. package/src/utils/type.ts +19 -0
  85. package/src/utils/url.spec.ts +33 -0
  86. package/src/utils/url.ts +37 -0
@@ -0,0 +1,363 @@
1
+ import PaywallUnlockRequest, { UnlockResponse } from './PaywallUnlockRequest';
2
+ import * as authorization from '../actions/authorization';
3
+ import * as location from '../utils/location';
4
+ import * as autologin from '../autologin';
5
+ import * as localhostCheck from '../utils/localhostCheck';
6
+ import store from '../store';
7
+ import PaywallError from '../exceptions/PaywallError';
8
+ import { listenForUserAttributeRequests } from '../eventHandlers/userAttributes';
9
+ import { listenForStateRequests } from '../eventHandlers/state';
10
+ import { removeAllCookies } from './cookie';
11
+ import spyOn = jest.spyOn;
12
+
13
+ beforeEach(() => {
14
+ store.clear();
15
+
16
+ removeAllCookies();
17
+
18
+ listenForStateRequests();
19
+ listenForUserAttributeRequests();
20
+ });
21
+
22
+ function itShouldNotReload() {
23
+ it('should not reload', () => {
24
+ expect(location.reload).not.toHaveBeenCalled();
25
+ });
26
+ }
27
+
28
+ function itShouldNotRedirect() {
29
+ it('should not redirect', () => {
30
+ expect(location.replace).not.toHaveBeenCalled();
31
+ });
32
+ }
33
+
34
+ function itShouldNotAttemptActivation() {
35
+ it('should not attempt activation', () => {
36
+ expect(authorization.activate).not.toHaveBeenCalled();
37
+ });
38
+ }
39
+
40
+ function itShouldAttemptActivationWithAccessFeatures(accessFeatures: string[]) {
41
+ it('should attempt activation with access features', () => {
42
+ expect(authorization.activate).toHaveBeenCalledWith(accessFeatures);
43
+ });
44
+ }
45
+
46
+ describe('PaywallUnlocker', () => {
47
+ beforeEach(() => {
48
+ spyOn(autologin, 'attemptPaywallAutoLogin').mockResolvedValue();
49
+ spyOn(authorization, 'setHasPerformedPaywallReload');
50
+ spyOn(authorization, 'activate');
51
+ spyOn(location, 'reload').mockReturnValue();
52
+ spyOn(location, 'replace').mockReturnValue();
53
+ });
54
+
55
+ describe('When running on localhost', () => {
56
+ beforeEach(() => {
57
+ jest.spyOn(localhostCheck, 'isRunningOnLocalhost').mockReturnValue(true);
58
+ });
59
+
60
+ it('should fail with expected error', async () => {
61
+ try {
62
+ await new PaywallUnlockRequest().tryUnlock();
63
+ } catch (e) {
64
+ expect(e).toEqual(
65
+ new Error('@amedia/user cannot unlock paywall on localhost')
66
+ );
67
+ }
68
+ });
69
+ });
70
+
71
+ describe('When not logged in', () => {
72
+ let unlockResponse: UnlockResponse;
73
+ beforeEach(async () => {
74
+ store.set('state', { isLoggedIn: false });
75
+ store.set('userAttributes', { uuid: 'uuid', name: 'Donald Duck' });
76
+ unlockResponse = await new PaywallUnlockRequest().tryUnlock();
77
+ });
78
+
79
+ it('should attempt autologin', async () => {
80
+ expect(autologin.attemptPaywallAutoLogin).toHaveBeenCalledTimes(1);
81
+ });
82
+
83
+ it('should return no access', () => {
84
+ expect(unlockResponse).toEqual({
85
+ isLoggedIn: false,
86
+ hasAccess: false,
87
+ accessFeatures: [],
88
+ });
89
+ });
90
+
91
+ itShouldNotRedirect();
92
+ itShouldNotReload();
93
+ });
94
+
95
+ describe('When loggen in', () => {
96
+ beforeEach(() => {
97
+ store.set('state', { isLoggedIn: true });
98
+ store.set('userAttributes', { uuid: 'uuid', name: 'Donald Duck' });
99
+ });
100
+
101
+ it('should not attempt autologin', () => {
102
+ expect(autologin.attemptPaywallAutoLogin).not.toHaveBeenCalled();
103
+ });
104
+
105
+ itShouldNotRedirect();
106
+ itShouldNotReload();
107
+
108
+ describe('when has access', () => {
109
+ let unlockResponse: UnlockResponse;
110
+ beforeEach(async () => {
111
+ store.merge('userAttributes', { access: ['newspaper'] });
112
+ spyOn(authorization, 'setAccessCookies').mockResolvedValue({
113
+ success: true,
114
+ accessFeatures: ['newspaper'],
115
+ });
116
+
117
+ unlockResponse = await new PaywallUnlockRequest().tryUnlock();
118
+ });
119
+
120
+ describe('When called with reload and already tried to reload', () => {
121
+ let error: Error | undefined;
122
+ beforeEach(async () => {
123
+ spyOn(
124
+ authorization,
125
+ 'hasRecentlyPerformedPaywallReload'
126
+ ).mockReturnValue(true);
127
+ error = undefined;
128
+ try {
129
+ await new PaywallUnlockRequest().reloadOnAccess().tryUnlock();
130
+ } catch (e) {
131
+ error = e;
132
+ }
133
+ });
134
+ it('should throw exception', () => {
135
+ expect(error).toEqual(
136
+ new PaywallError(
137
+ 'Unlock check already performed.',
138
+ PaywallError.ERROR_RELOADED_NO_ACCESS
139
+ )
140
+ );
141
+ });
142
+ });
143
+
144
+ it('should not store that we tried, since we perform no reload', () => {
145
+ expect(
146
+ authorization.setHasPerformedPaywallReload
147
+ ).not.toHaveBeenCalled();
148
+ });
149
+
150
+ it('should give access with access features', () => {
151
+ expect(unlockResponse).toEqual({
152
+ isLoggedIn: true,
153
+ hasAccess: true,
154
+ accessFeatures: ['newspaper'],
155
+ });
156
+ });
157
+
158
+ itShouldNotAttemptActivation();
159
+ itShouldNotRedirect();
160
+ itShouldNotReload();
161
+ });
162
+
163
+ describe('when no access', () => {
164
+ describe('and activation give access', () => {
165
+ let unlockResponse: UnlockResponse;
166
+ beforeEach(() => {
167
+ spyOn(authorization, 'setAccessCookies')
168
+ .mockResolvedValueOnce({
169
+ success: true,
170
+ accessFeatures: [],
171
+ })
172
+ .mockResolvedValueOnce({
173
+ success: true,
174
+ accessFeatures: ['newspaper'],
175
+ });
176
+ spyOn(authorization, 'activate').mockResolvedValue(['newspaper']);
177
+ });
178
+
179
+ describe('with no post access actions', () => {
180
+ beforeEach(async () => {
181
+ unlockResponse = await new PaywallUnlockRequest().tryUnlock();
182
+ });
183
+
184
+ it('should give access with access features', () => {
185
+ expect(unlockResponse).toEqual({
186
+ isLoggedIn: true,
187
+ hasAccess: true,
188
+ accessFeatures: ['newspaper'],
189
+ });
190
+ });
191
+
192
+ itShouldAttemptActivationWithAccessFeatures(['newspaper']);
193
+ itShouldNotRedirect();
194
+ itShouldNotReload();
195
+ });
196
+
197
+ describe('with redirect requested', () => {
198
+ beforeEach(async () => {
199
+ unlockResponse = await new PaywallUnlockRequest()
200
+ .redirectOnAccess('https://redirect.pls')
201
+ .tryUnlock();
202
+ });
203
+
204
+ it('should give access with access features', () => {
205
+ expect(unlockResponse).toEqual({
206
+ isLoggedIn: true,
207
+ hasAccess: true,
208
+ accessFeatures: ['newspaper'],
209
+ });
210
+ });
211
+
212
+ it('should navigate to expected url', () => {
213
+ expect(location.replace).toHaveBeenCalledWith(
214
+ 'https://redirect.pls'
215
+ );
216
+ });
217
+
218
+ itShouldAttemptActivationWithAccessFeatures(['newspaper']);
219
+ itShouldNotReload();
220
+ });
221
+
222
+ describe('with reload requested', () => {
223
+ beforeEach(async () => {
224
+ unlockResponse = await new PaywallUnlockRequest()
225
+ .reloadOnAccess()
226
+ .tryUnlock();
227
+ });
228
+
229
+ it('should give access with access features', () => {
230
+ expect(unlockResponse).toEqual({
231
+ isLoggedIn: true,
232
+ hasAccess: true,
233
+ accessFeatures: ['newspaper'],
234
+ });
235
+ });
236
+
237
+ it('should reload on access', () => {
238
+ expect(location.reload).toHaveBeenCalled();
239
+ });
240
+
241
+ itShouldAttemptActivationWithAccessFeatures(['newspaper']);
242
+ itShouldNotRedirect();
243
+ });
244
+ });
245
+
246
+ describe('and activation does not give access', () => {
247
+ let unlockResponse: UnlockResponse;
248
+ beforeEach(async () => {
249
+ spyOn(authorization, 'setAccessCookies').mockResolvedValueOnce({
250
+ success: true,
251
+ accessFeatures: [],
252
+ });
253
+ spyOn(authorization, 'activate').mockResolvedValue([]);
254
+ unlockResponse = await new PaywallUnlockRequest().tryUnlock();
255
+ });
256
+
257
+ it('should not give access', () => {
258
+ expect(unlockResponse).toEqual({
259
+ isLoggedIn: true,
260
+ hasAccess: false,
261
+ accessFeatures: [],
262
+ });
263
+ });
264
+
265
+ itShouldAttemptActivationWithAccessFeatures(['newspaper']);
266
+ itShouldNotRedirect();
267
+ itShouldNotReload();
268
+ });
269
+
270
+ describe("and activation don't give enough access", () => {
271
+ let unlockResponse: UnlockResponse;
272
+ beforeEach(async () => {
273
+ spyOn(authorization, 'setAccessCookies').mockResolvedValueOnce({
274
+ success: true,
275
+ accessFeatures: ['newspaper'],
276
+ });
277
+ spyOn(authorization, 'activate').mockResolvedValue(['newspaper']);
278
+ unlockResponse = await new PaywallUnlockRequest()
279
+ .withAccessFeatures(['plussalt'])
280
+ .tryUnlock();
281
+ });
282
+
283
+ it('should not give access, but return users access features', () => {
284
+ expect(unlockResponse).toEqual({
285
+ isLoggedIn: true,
286
+ hasAccess: false,
287
+ accessFeatures: ['newspaper'],
288
+ });
289
+ });
290
+
291
+ itShouldAttemptActivationWithAccessFeatures(['plussalt']);
292
+ itShouldNotRedirect();
293
+ itShouldNotReload();
294
+ });
295
+ });
296
+
297
+ describe('when setting access cookies fails', () => {
298
+ let paywallError: any;
299
+ beforeEach(async () => {
300
+ spyOn(authorization, 'setAccessCookies').mockResolvedValue({
301
+ success: false,
302
+ accessFeatures: [],
303
+ });
304
+ try {
305
+ await new PaywallUnlockRequest().tryUnlock();
306
+ } catch (error) {
307
+ paywallError = error;
308
+ }
309
+ });
310
+
311
+ it('should throw error', () => {
312
+ expect(paywallError).toEqual(
313
+ new PaywallError(
314
+ 'Could not set access cookies',
315
+ PaywallError.ERROR_ENABLING_ACCESS_FAILED
316
+ )
317
+ );
318
+ });
319
+
320
+ itShouldNotAttemptActivation();
321
+ itShouldNotRedirect();
322
+ itShouldNotReload();
323
+ });
324
+
325
+ describe('when internal system disagree about users access', () => {
326
+ let paywallError: any;
327
+ beforeEach(async () => {
328
+ spyOn(authorization, 'setAccessCookies')
329
+ .mockResolvedValueOnce({
330
+ success: true,
331
+ accessFeatures: ['newspaper'],
332
+ })
333
+ .mockResolvedValueOnce({
334
+ success: true,
335
+ accessFeatures: ['newspaper'], // Notice not changed
336
+ });
337
+ spyOn(authorization, 'activate').mockResolvedValue([
338
+ 'newspaper',
339
+ 'plussalt',
340
+ ]);
341
+ try {
342
+ await new PaywallUnlockRequest()
343
+ .withAccessFeatures(['plussalt'])
344
+ .tryUnlock();
345
+ } catch (error) {
346
+ paywallError = error;
347
+ }
348
+ });
349
+
350
+ it('should throw error about bad access state', () => {
351
+ expect(paywallError).toEqual(
352
+ new PaywallError(
353
+ 'New subscription was attached and checked successfully, but internal systems disagree about current newspaper access.',
354
+ PaywallError.ERROR_NO_ACCESS_AFTER_SUCCESSFUL_ACTIVATION_WITH_REQUESTED_ACCESS_FEATURES
355
+ )
356
+ );
357
+ });
358
+
359
+ itShouldNotReload();
360
+ itShouldNotRedirect();
361
+ });
362
+ });
363
+ });
@@ -0,0 +1,143 @@
1
+ import {
2
+ activate,
3
+ hasAccess,
4
+ hasRecentlyPerformedPaywallReload,
5
+ setAccessCookies,
6
+ setHasPerformedPaywallReload,
7
+ } from '../actions/authorization';
8
+ import { getCurrentPublicationDomain } from '../utils/url';
9
+ import * as location from '../utils/location';
10
+ import PaywallError from '../exceptions/PaywallError';
11
+ import UserDataRequest from './UserDataRequest';
12
+ import { attemptPaywallAutoLogin } from '../autologin';
13
+ import { AccessFeatures } from '../clients/jupiter';
14
+ import { isRunningOnLocalhost } from '../utils/localhostCheck';
15
+
16
+ export type UnlockResponse = {
17
+ isLoggedIn: boolean;
18
+ hasAccess: boolean;
19
+ accessFeatures: string[];
20
+ };
21
+
22
+ class PaywallUnlockRequest {
23
+ private requestedAccessFeatures: string[] = ['newspaper'];
24
+
25
+ private reload: boolean = false;
26
+
27
+ private requestedUrl: string | undefined;
28
+
29
+ public withAccessFeatures(accessFeatures: string[]): this {
30
+ this.requestedAccessFeatures = accessFeatures;
31
+ return this;
32
+ }
33
+
34
+ public reloadOnAccess(): this {
35
+ this.reload = true;
36
+ return this;
37
+ }
38
+
39
+ public redirectOnAccess(requestedUrl: string): this {
40
+ this.requestedUrl = requestedUrl;
41
+ return this;
42
+ }
43
+
44
+ public async tryUnlock(): Promise<UnlockResponse> {
45
+ if (isRunningOnLocalhost()) {
46
+ return Promise.reject(
47
+ new Error('@amedia/user cannot unlock paywall on localhost')
48
+ );
49
+ }
50
+ const {
51
+ state: { isLoggedIn },
52
+ } = await new UserDataRequest().withAttributes(['name']).fetch();
53
+ if (!isLoggedIn) {
54
+ // Will attempt one or more strategies for creating a first party session
55
+ // Redirects/reloads might occur
56
+ await attemptPaywallAutoLogin();
57
+ return {
58
+ isLoggedIn: false,
59
+ hasAccess: false,
60
+ accessFeatures: [],
61
+ };
62
+ }
63
+
64
+ let currentAccess = await this.setAccessCookiesAndGetAccessFeatures(
65
+ this.requestedAccessFeatures
66
+ );
67
+ if (currentAccess.denied) {
68
+ // Check if we actually have a valid subscription that's not properly connected to the user yet
69
+ currentAccess = await this.activateAccess(this.requestedAccessFeatures);
70
+ if (currentAccess.denied) {
71
+ return {
72
+ isLoggedIn: true,
73
+ hasAccess: false,
74
+ accessFeatures: currentAccess.accessFeatures,
75
+ };
76
+ }
77
+ currentAccess = await this.setAccessCookiesAndGetAccessFeatures(
78
+ this.requestedAccessFeatures
79
+ );
80
+ if (currentAccess.denied) {
81
+ throw new PaywallError(
82
+ 'New subscription was attached and checked successfully, but internal systems disagree about current newspaper access.',
83
+ PaywallError.ERROR_NO_ACCESS_AFTER_SUCCESSFUL_ACTIVATION_WITH_REQUESTED_ACCESS_FEATURES
84
+ );
85
+ }
86
+ }
87
+ if (this.reload || this.requestedUrl) {
88
+ // Ensures we don't get stuck in a reload/redirect loop
89
+ if (hasRecentlyPerformedPaywallReload(this.requestedUrl)) {
90
+ throw new PaywallError(
91
+ 'Unlock check already performed.',
92
+ PaywallError.ERROR_RELOADED_NO_ACCESS
93
+ );
94
+ }
95
+
96
+ setHasPerformedPaywallReload(this.requestedUrl);
97
+ if (this.reload) {
98
+ location.reload();
99
+ }
100
+
101
+ if (this.requestedUrl) {
102
+ location.replace(this.requestedUrl);
103
+ }
104
+ }
105
+ return {
106
+ isLoggedIn: true,
107
+ hasAccess: true,
108
+ accessFeatures: currentAccess.accessFeatures,
109
+ };
110
+ }
111
+
112
+ // eslint-disable-next-line class-methods-use-this
113
+ private setAccessCookiesAndGetAccessFeatures(
114
+ requestedAccessFeatures: AccessFeatures
115
+ ) {
116
+ return setAccessCookies(getCurrentPublicationDomain()).then((response) => {
117
+ const accessDenied = !hasAccess(
118
+ response.accessFeatures,
119
+ requestedAccessFeatures
120
+ );
121
+ if (!response.success && accessDenied) {
122
+ throw new PaywallError(
123
+ 'Could not set access cookies',
124
+ PaywallError.ERROR_ENABLING_ACCESS_FAILED
125
+ );
126
+ }
127
+ return {
128
+ denied: accessDenied,
129
+ accessFeatures: response.accessFeatures,
130
+ };
131
+ });
132
+ }
133
+
134
+ private async activateAccess(requestedAccessFeatures: AccessFeatures) {
135
+ const accessFeatures = await activate(this.requestedAccessFeatures);
136
+ return {
137
+ denied: !hasAccess(accessFeatures, requestedAccessFeatures),
138
+ accessFeatures,
139
+ };
140
+ }
141
+ }
142
+
143
+ export default PaywallUnlockRequest;