@amedia/user 0.13.35 → 0.13.36

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 (179) hide show
  1. package/dist/user.cjs +1 -1
  2. package/dist/user.cjs.map +3 -3
  3. package/dist/user.d.ts +12 -0
  4. package/dist/user.js +1 -1
  5. package/dist/user.js.map +3 -3
  6. package/package.json +5 -2
  7. package/.codeclimate.yml +0 -8
  8. package/.eslintignore +0 -2
  9. package/.eslintrc +0 -11
  10. package/.prettierrc.json +0 -1
  11. package/@types/amedia-frontend-rpc.d.ts +0 -11
  12. package/@types/browserid.d.ts +0 -4
  13. package/build.js +0 -28
  14. package/checkVersion.cjs +0 -12
  15. package/cloudbuild.json +0 -29
  16. package/cop.json +0 -8
  17. package/dist/actions/authentication.d.ts +0 -4
  18. package/dist/actions/authentication.spec.d.ts +0 -1
  19. package/dist/actions/authorization.d.ts +0 -14
  20. package/dist/actions/authorization.spec.d.ts +0 -4
  21. package/dist/actions/preferences.d.ts +0 -9
  22. package/dist/actions/preferences.spec.d.ts +0 -1
  23. package/dist/actions/storage.d.ts +0 -7
  24. package/dist/actions/userData.d.ts +0 -2
  25. package/dist/actions/userData.spec.d.ts +0 -1
  26. package/dist/autologin/index.d.ts +0 -7
  27. package/dist/autologin/index.spec.d.ts +0 -1
  28. package/dist/autologin/thirdPartyCookie.d.ts +0 -3
  29. package/dist/autologin/thirdPartyCookie.spec.d.ts +0 -1
  30. package/dist/cache/engines/global.d.ts +0 -24
  31. package/dist/cache/engines/global.spec.d.ts +0 -1
  32. package/dist/cache/index.d.ts +0 -11
  33. package/dist/cache/index.spec.d.ts +0 -1
  34. package/dist/cache/types.d.ts +0 -11
  35. package/dist/clients/europa.d.ts +0 -24
  36. package/dist/clients/europa.spec.d.ts +0 -1
  37. package/dist/clients/jupiter.d.ts +0 -30
  38. package/dist/clients/jupiter.spec.d.ts +0 -1
  39. package/dist/clients/mercury.d.ts +0 -3
  40. package/dist/clients/mercury.spec.d.ts +0 -1
  41. package/dist/clients/mercuryLog.d.ts +0 -8
  42. package/dist/clients/mercuryLog.spec.d.ts +0 -1
  43. package/dist/clients/nebula.d.ts +0 -2
  44. package/dist/clients/nebula.spec.d.ts +0 -1
  45. package/dist/clients/pluto.d.ts +0 -4
  46. package/dist/clients/pluto.spec.d.ts +0 -1
  47. package/dist/eventHandlers/activation.d.ts +0 -15
  48. package/dist/eventHandlers/authentication.d.ts +0 -17
  49. package/dist/eventHandlers/eventMap.d.ts +0 -29
  50. package/dist/eventHandlers/state.d.ts +0 -1
  51. package/dist/eventHandlers/state.spec.d.ts +0 -1
  52. package/dist/eventHandlers/storage.d.ts +0 -1
  53. package/dist/eventHandlers/storage.spec.d.ts +0 -1
  54. package/dist/eventHandlers/userAttributes.d.ts +0 -1
  55. package/dist/eventHandlers/userAttributes.spec.d.ts +0 -1
  56. package/dist/exceptions/PaywallError.d.ts +0 -8
  57. package/dist/index-authentcation.spec.d.ts +0 -1
  58. package/dist/index-storage.spec.d.ts +0 -1
  59. package/dist/index-subscription.spec.d.ts +0 -1
  60. package/dist/index.d.ts +0 -20
  61. package/dist/janitor/index.d.ts +0 -10
  62. package/dist/janitor/index.spec.d.ts +0 -1
  63. package/dist/janitor/jobs/accessCookieRefresher.d.ts +0 -1
  64. package/dist/janitor/jobs/accessCookieRefresher.spec.d.ts +0 -1
  65. package/dist/janitor/jobs/daxsubCookie.d.ts +0 -3
  66. package/dist/janitor/jobs/daxsubCookie.spec.d.ts +0 -1
  67. package/dist/janitor/jobs/removeUserStorage.d.ts +0 -1
  68. package/dist/janitor/jobs/removeUserStorage.spec.d.ts +0 -1
  69. package/dist/lib/PaywallUnlockRequest.d.ts +0 -17
  70. package/dist/lib/PaywallUnlockRequest.spec.d.ts +0 -1
  71. package/dist/lib/UserDataRequest.d.ts +0 -22
  72. package/dist/lib/UserDataRequest.spec.d.ts +0 -1
  73. package/dist/lib/cookie.d.ts +0 -9
  74. package/dist/lib/rpc.d.ts +0 -6
  75. package/dist/store/index.d.ts +0 -23
  76. package/dist/store/index.spec.d.ts +0 -1
  77. package/dist/types.d.ts +0 -22
  78. package/dist/utils/customEventWrapper.d.ts +0 -19
  79. package/dist/utils/debouncer.d.ts +0 -26
  80. package/dist/utils/debouncer.spec.d.ts +0 -1
  81. package/dist/utils/deepEquals.d.ts +0 -1
  82. package/dist/utils/expectBootLogs.d.ts +0 -2
  83. package/dist/utils/getOrUpdate.d.ts +0 -6
  84. package/dist/utils/getOrUpdate.spec.d.ts +0 -1
  85. package/dist/utils/localhostCheck.d.ts +0 -1
  86. package/dist/utils/location.d.ts +0 -3
  87. package/dist/utils/log.d.ts +0 -9
  88. package/dist/utils/log.spec.d.ts +0 -1
  89. package/dist/utils/loginState.d.ts +0 -2
  90. package/dist/utils/networkError.d.ts +0 -1
  91. package/dist/utils/queue.d.ts +0 -12
  92. package/dist/utils/type.d.ts +0 -6
  93. package/dist/utils/url.d.ts +0 -6
  94. package/dist/utils/url.spec.d.ts +0 -1
  95. package/eik.json +0 -10
  96. package/global.d.ts +0 -1
  97. package/jest.config.js +0 -7
  98. package/setupJest.js +0 -1
  99. package/src/actions/authentication.spec.ts +0 -51
  100. package/src/actions/authentication.ts +0 -43
  101. package/src/actions/authorization.spec.ts +0 -369
  102. package/src/actions/authorization.ts +0 -117
  103. package/src/actions/preferences.spec.ts +0 -41
  104. package/src/actions/preferences.ts +0 -41
  105. package/src/actions/storage.ts +0 -22
  106. package/src/actions/userData.spec.ts +0 -133
  107. package/src/actions/userData.ts +0 -68
  108. package/src/autologin/index.spec.ts +0 -239
  109. package/src/autologin/index.ts +0 -92
  110. package/src/autologin/thirdPartyCookie.spec.ts +0 -95
  111. package/src/autologin/thirdPartyCookie.ts +0 -55
  112. package/src/cache/engines/global.spec.ts +0 -62
  113. package/src/cache/engines/global.ts +0 -48
  114. package/src/cache/index.spec.ts +0 -130
  115. package/src/cache/index.ts +0 -48
  116. package/src/cache/types.ts +0 -12
  117. package/src/clients/europa.spec.ts +0 -209
  118. package/src/clients/europa.ts +0 -183
  119. package/src/clients/jupiter.spec.ts +0 -179
  120. package/src/clients/jupiter.ts +0 -119
  121. package/src/clients/mercury.spec.ts +0 -59
  122. package/src/clients/mercury.ts +0 -48
  123. package/src/clients/mercuryLog.spec.ts +0 -20
  124. package/src/clients/mercuryLog.ts +0 -67
  125. package/src/clients/nebula.spec.ts +0 -70
  126. package/src/clients/nebula.ts +0 -83
  127. package/src/clients/pluto.spec.ts +0 -165
  128. package/src/clients/pluto.ts +0 -118
  129. package/src/eventHandlers/activation.ts +0 -35
  130. package/src/eventHandlers/authentication.ts +0 -34
  131. package/src/eventHandlers/eventMap.ts +0 -34
  132. package/src/eventHandlers/state.spec.ts +0 -48
  133. package/src/eventHandlers/state.ts +0 -23
  134. package/src/eventHandlers/storage.spec.ts +0 -114
  135. package/src/eventHandlers/storage.ts +0 -45
  136. package/src/eventHandlers/userAttributes.spec.ts +0 -93
  137. package/src/eventHandlers/userAttributes.ts +0 -31
  138. package/src/exceptions/PaywallError.ts +0 -16
  139. package/src/index-authentcation.spec.ts +0 -26
  140. package/src/index-storage.spec.ts +0 -144
  141. package/src/index-subscription.spec.ts +0 -213
  142. package/src/index.ts +0 -59
  143. package/src/janitor/index.spec.ts +0 -18
  144. package/src/janitor/index.ts +0 -59
  145. package/src/janitor/jobs/accessCookieRefresher.spec.ts +0 -213
  146. package/src/janitor/jobs/accessCookieRefresher.ts +0 -98
  147. package/src/janitor/jobs/daxsubCookie.spec.ts +0 -109
  148. package/src/janitor/jobs/daxsubCookie.ts +0 -38
  149. package/src/janitor/jobs/removeUserStorage.spec.ts +0 -34
  150. package/src/janitor/jobs/removeUserStorage.ts +0 -12
  151. package/src/lib/PaywallUnlockRequest.spec.ts +0 -363
  152. package/src/lib/PaywallUnlockRequest.ts +0 -143
  153. package/src/lib/UserDataRequest.spec.ts +0 -77
  154. package/src/lib/UserDataRequest.ts +0 -213
  155. package/src/lib/cookie.ts +0 -121
  156. package/src/lib/rpc.ts +0 -19
  157. package/src/store/index.spec.ts +0 -49
  158. package/src/store/index.ts +0 -64
  159. package/src/types.ts +0 -22
  160. package/src/utils/customEventWrapper.ts +0 -58
  161. package/src/utils/debouncer.spec.ts +0 -252
  162. package/src/utils/debouncer.ts +0 -123
  163. package/src/utils/deepEquals.ts +0 -42
  164. package/src/utils/expectBootLogs.ts +0 -9
  165. package/src/utils/getOrUpdate.spec.ts +0 -166
  166. package/src/utils/getOrUpdate.ts +0 -66
  167. package/src/utils/localhostCheck.ts +0 -9
  168. package/src/utils/location.ts +0 -31
  169. package/src/utils/log.spec.ts +0 -148
  170. package/src/utils/log.ts +0 -124
  171. package/src/utils/loginState.ts +0 -24
  172. package/src/utils/networkError.ts +0 -11
  173. package/src/utils/queue.ts +0 -32
  174. package/src/utils/type.ts +0 -19
  175. package/src/utils/url.spec.ts +0 -33
  176. package/src/utils/url.ts +0 -35
  177. package/testSetup.js +0 -19
  178. package/tsconfig.eslint.json +0 -24
  179. package/tsconfig.json +0 -18
@@ -1,213 +0,0 @@
1
- import UserDataRequest from './lib/UserDataRequest';
2
- import * as userData from './actions/userData';
3
- import { listenForUserAttributeRequests } from './eventHandlers/userAttributes';
4
- import store from './store';
5
- import { listenForStateRequests } from './eventHandlers/state';
6
-
7
- beforeEach(() => {
8
- // Store must be cleared before we listen for user attribute requests
9
- store.clear();
10
-
11
- listenForUserAttributeRequests();
12
- listenForStateRequests();
13
- });
14
-
15
- describe('Public endpoint - userdata', () => {
16
- it('should alert about user data', () => {
17
- jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
18
- store.merge('userAttributes', { name: 'Donald Duck' });
19
- return Promise.resolve();
20
- });
21
-
22
- const onUserUpdate = jest.fn();
23
- new UserDataRequest().withAttributes(['name']).subscribe(onUserUpdate);
24
-
25
- expect(onUserUpdate).toHaveBeenCalledWith({
26
- attributes: {
27
- name: 'Donald Duck',
28
- },
29
- state: {},
30
- });
31
- });
32
-
33
- it('should update with more data', () => {
34
- jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
35
- store.merge('userAttributes', { name: 'Donald Duck' });
36
- store.merge('userAttributes', { uuid: '1234-123-1234' });
37
- return Promise.resolve();
38
- });
39
-
40
- const onUserUpdate = jest.fn();
41
- new UserDataRequest()
42
- .withAttributes(['name', 'uuid'])
43
- .subscribe(onUserUpdate);
44
-
45
- expect(onUserUpdate).nthCalledWith(1, {
46
- attributes: {
47
- name: 'Donald Duck',
48
- },
49
- state: {},
50
- });
51
- expect(onUserUpdate).nthCalledWith(2, {
52
- attributes: {
53
- uuid: '1234-123-1234',
54
- name: 'Donald Duck',
55
- },
56
- state: {},
57
- });
58
- });
59
-
60
- it('should not alert on same data', () => {
61
- jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
62
- store.merge('userAttributes', { name: 'Donald Duck' });
63
- return Promise.resolve();
64
- });
65
-
66
- const onUserUpdate1 = jest.fn();
67
- new UserDataRequest().withAttributes(['name']).subscribe(onUserUpdate1);
68
-
69
- const onUserUpdate2 = jest.fn();
70
- new UserDataRequest().withAttributes(['name']).subscribe(onUserUpdate2);
71
-
72
- expect(onUserUpdate1).toHaveBeenCalledTimes(1);
73
- expect(onUserUpdate1).toHaveBeenCalledWith({
74
- attributes: {
75
- name: 'Donald Duck',
76
- },
77
- state: {},
78
- });
79
-
80
- expect(onUserUpdate2).toHaveBeenCalledTimes(1);
81
- expect(onUserUpdate2).toHaveBeenCalledWith({
82
- attributes: {
83
- name: 'Donald Duck',
84
- },
85
- state: {},
86
- });
87
- });
88
-
89
- it('should alert when data changes', () => {
90
- jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
91
- store.merge('userAttributes', { name: 'Mickey Mouse' });
92
- return Promise.resolve();
93
- });
94
-
95
- const firstOnUserUpdate = jest.fn();
96
- new UserDataRequest().withAttributes(['name']).subscribe(firstOnUserUpdate);
97
-
98
- expect(firstOnUserUpdate).toHaveBeenCalledTimes(1);
99
- expect(firstOnUserUpdate).nthCalledWith(1, {
100
- attributes: { name: 'Mickey Mouse' },
101
- state: {},
102
- });
103
-
104
- // Change the name, this shoudl trigger new call on firstOnUserUpdate
105
- store.merge('userAttributes', { name: 'Donald Duck' });
106
- expect(firstOnUserUpdate).toHaveBeenCalledTimes(2);
107
- expect(firstOnUserUpdate).nthCalledWith(2, {
108
- attributes: {
109
- name: 'Donald Duck',
110
- },
111
- state: {},
112
- });
113
- });
114
-
115
- it('should not return more data that requested', () => {
116
- jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
117
- store.merge('userAttributes', {
118
- name: 'Donald Duck',
119
- email: 'donald.duck@andeby.no',
120
- });
121
- return Promise.resolve();
122
- });
123
-
124
- const onUserUpdate = jest.fn();
125
- new UserDataRequest().withAttributes(['name']).subscribe(onUserUpdate);
126
-
127
- expect(onUserUpdate).toHaveBeenCalledWith({
128
- attributes: {
129
- name: 'Donald Duck',
130
- },
131
- state: {},
132
- });
133
- });
134
-
135
- it('should support returning multiple requested attributes', () => {
136
- jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
137
- store.merge('userAttributes', {
138
- name: 'Donald Duck',
139
- email: 'donald.duck@andeby.no',
140
- });
141
- return Promise.resolve();
142
- });
143
-
144
- const onUserUpdate = jest.fn();
145
- new UserDataRequest()
146
- .withAttributes(['name', 'email'])
147
- .subscribe(onUserUpdate);
148
-
149
- expect(onUserUpdate).toHaveBeenCalledWith({
150
- attributes: {
151
- name: 'Donald Duck',
152
- email: 'donald.duck@andeby.no',
153
- },
154
- state: {},
155
- });
156
- });
157
-
158
- it('should support requesting different attributes', () => {
159
- jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
160
- store.merge('userAttributes', {
161
- name: 'Donald Duck',
162
- email: 'donald.duck@andeby.no',
163
- });
164
- return Promise.resolve();
165
- });
166
-
167
- const firstOnUserUpdate = jest.fn();
168
- new UserDataRequest()
169
- .withAttributes(['name', 'email'])
170
- .subscribe(firstOnUserUpdate);
171
-
172
- const secondOnUserUpdate = jest.fn();
173
- new UserDataRequest()
174
- .withAttributes(['email'])
175
- .subscribe(secondOnUserUpdate);
176
-
177
- expect(firstOnUserUpdate).toHaveBeenCalledTimes(1);
178
- expect(firstOnUserUpdate).nthCalledWith(1, {
179
- attributes: {
180
- name: 'Donald Duck',
181
- email: 'donald.duck@andeby.no',
182
- },
183
- state: {},
184
- });
185
-
186
- expect(secondOnUserUpdate).toHaveBeenCalledTimes(1);
187
- expect(secondOnUserUpdate).nthCalledWith(1, {
188
- attributes: {
189
- email: 'donald.duck@andeby.no',
190
- },
191
- state: {},
192
- });
193
- });
194
-
195
- it('should also report state changes', () => {
196
- jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
197
- store.merge('userAttributes', {
198
- name: 'Donald Duck',
199
- });
200
- store.merge('state', { isLoggedIn: true });
201
- return Promise.resolve();
202
- });
203
-
204
- const onUpdate = jest.fn();
205
-
206
- new UserDataRequest().withAttributes(['name']).subscribe(onUpdate);
207
-
208
- expect(onUpdate).toHaveBeenCalledWith({
209
- attributes: { name: 'Donald Duck' },
210
- state: { isLoggedIn: true },
211
- });
212
- });
213
- });
package/src/index.ts DELETED
@@ -1,59 +0,0 @@
1
- import { ActivationEvents } from './eventHandlers/activation';
2
- import { dispatch, listenForAllEvents } from './utils/customEventWrapper';
3
- import * as authentication from './actions/authentication';
4
- import { AmediaUserEvents } from './eventHandlers/eventMap';
5
- import { getCurrentUrl } from './utils/url';
6
- import { runJanitor } from './janitor';
7
- import { listenForStateRequests } from './eventHandlers/state';
8
- import { listenForUserAttributeRequests } from './eventHandlers/userAttributes';
9
- import { listenForStorageRequests } from './eventHandlers/storage';
10
- import { hasAccess } from './actions/authorization';
11
-
12
- export { default as UserDataRequest } from './lib/UserDataRequest';
13
- export { default as PaywallUnlockRequest } from './lib/PaywallUnlockRequest';
14
-
15
- /**
16
- * This will, if necessary, trigger a redirect to www.aid.no and return
17
- * to the requestedUrl if supplied or the current url before redirecting away if not.
18
- * @param requestedUrl
19
- */
20
- export const goToLoginPage = (requestedUrl?: string) =>
21
- authentication.login(requestedUrl || getCurrentUrl());
22
-
23
- export const logout = () => authentication.logout();
24
-
25
- export const tryActivateSubscriptionAccess = (
26
- requestedAccessFeatures: string[]
27
- ): Promise<{ hasAccess: boolean; accessFeatures: string[] }> =>
28
- new Promise((resolve) => {
29
- listenForAllEvents(ActivationEvents.ON_ACTIVATION_COMPLETE, (evt) => {
30
- const activatedAccessFeatures = evt.detail.accessFeatures;
31
- resolve({
32
- hasAccess: hasAccess(activatedAccessFeatures, requestedAccessFeatures),
33
- accessFeatures: activatedAccessFeatures,
34
- });
35
- });
36
- dispatch(
37
- new CustomEvent(ActivationEvents.REQUEST_ACTIVATION, {
38
- detail: { accessFeatures: requestedAccessFeatures },
39
- })
40
- );
41
- });
42
-
43
- export const updateStorage = (storage: { [index: string]: Object }) => {
44
- dispatch(
45
- new CustomEvent(AmediaUserEvents.UPDATE_STORAGE, { detail: storage })
46
- );
47
- };
48
-
49
- export const saveStorage = (storage: { [index: string]: Object }) => {
50
- dispatch(new CustomEvent(AmediaUserEvents.SAVE_STORAGE, { detail: storage }));
51
- };
52
-
53
- // @ts-ignore
54
- if (!globalThis.amediaUserTestMode) {
55
- listenForStateRequests();
56
- listenForUserAttributeRequests();
57
- listenForStorageRequests();
58
- runJanitor();
59
- }
@@ -1,18 +0,0 @@
1
- import * as localhostCheck from '../utils/localhostCheck';
2
- import { runJanitor } from './index';
3
- import log from '../utils/log';
4
-
5
- describe('when janitor is started on localhost', () => {
6
- let logWarnSpy: jest.SpyInstance;
7
- beforeEach(() => {
8
- jest.spyOn(localhostCheck, 'isRunningOnLocalhost').mockReturnValue(true);
9
- logWarnSpy = jest.spyOn(log, 'warn').mockImplementation();
10
- runJanitor();
11
- });
12
-
13
- it('should log a warning', () => {
14
- expect(logWarnSpy).toBeCalledWith(
15
- '@amedia/user started on localhost. In this scenario we have no way to resolve any login-, access- or userdata. Expect errors and/or redirects if you try.'
16
- );
17
- });
18
- });
@@ -1,59 +0,0 @@
1
- import accessCookieRefresher from './jobs/accessCookieRefresher';
2
- import { removeUserStorage } from './jobs/removeUserStorage';
3
- import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
4
- import { attemptAutoLoginJob } from '../autologin';
5
- import daxSubCookieHandler from './jobs/daxsubCookie';
6
- import log from '../utils/log';
7
-
8
- const jobs = [
9
- attemptAutoLoginJob,
10
- accessCookieRefresher,
11
- daxSubCookieHandler,
12
- removeUserStorage,
13
- ];
14
-
15
- async function performJobs() {
16
- if (typeof window === 'undefined') {
17
- // None of our current jobs make any sense to run in a web worker or other settings without a window object.
18
- return;
19
- }
20
- if (globalThis?.location?.hostname === 'localhost') {
21
- // Neither if we're running on localhost as this will never be able to resolve any data
22
- // eslint-disable-next-line no-console
23
- log.warn(
24
- '@amedia/user started on localhost. In this scenario we have no way to resolve any login-, access- or userdata. Expect errors and/or redirects if you try.'
25
- );
26
- return;
27
- }
28
- // eslint-disable-next-line no-restricted-syntax
29
- for (const job of jobs) {
30
- // eslint-disable-next-line no-await-in-loop
31
- await job();
32
- }
33
- }
34
-
35
- enum JanitorEvents {
36
- LOADED = 'amedia-user.janitor.loaded',
37
- }
38
-
39
- declare global {
40
- interface WindowEventMap {
41
- [JanitorEvents.LOADED]: CustomEvent<{}>;
42
- }
43
- }
44
-
45
- // We want to ensure that the Janitor is started exactly once. The trick here is to
46
- // listen to an event end if we receive it exclusively, we know that we are the first
47
- // to load this script and therefore responsible for running the Janitor once.
48
- export const runJanitor = async () => {
49
- let janitorStarted = false;
50
- listenWithExclusivity(JanitorEvents.LOADED, () => {
51
- if (janitorStarted) {
52
- return;
53
- }
54
-
55
- janitorStarted = true;
56
- performJobs();
57
- });
58
- dispatch(new CustomEvent(JanitorEvents.LOADED));
59
- };
@@ -1,213 +0,0 @@
1
- import accessCookieRefresher from './accessCookieRefresher';
2
- import * as url from '../../utils/url';
3
- import UserDataRequest from '../../lib/UserDataRequest';
4
- import * as cookie from '../../lib/cookie';
5
- import * as authorization from '../../actions/authorization';
6
- import store from '../../store';
7
- import spyOn = jest.spyOn;
8
- import { AccessFeatures } from '../../clients/jupiter';
9
-
10
- const mockUuid = '3-1-33-7';
11
-
12
- beforeEach(() => {
13
- spyOn(url, 'getCurrentPublicationDomain').mockReturnValue('www.avisnavn.no');
14
- url.setCurrentHostname('www.avisnavn.no');
15
- jest
16
- .spyOn(authorization, 'setUserAccessCookie')
17
- .mockImplementation(() =>
18
- Promise.resolve({ success: true, accessFeatures: ['newspaper'] })
19
- );
20
- jest
21
- .spyOn(authorization, 'setNonUserAccessCookie')
22
- .mockImplementation(() =>
23
- Promise.resolve({ success: true, accessFeatures: ['newspaper'] })
24
- );
25
- });
26
-
27
- function mockAccessCookie(
28
- validHours: number,
29
- uuid: string,
30
- accessFeatures: AccessFeatures,
31
- domain: string
32
- ) {
33
- const validTo = new Date();
34
- validTo.setHours(validTo.getHours() + validHours);
35
- return `signature:${Math.round(
36
- validTo.getTime() / 1000
37
- )}:${accessFeatures.join(',')}:${uuid}:${domain}:jupiter`;
38
- }
39
-
40
- describe('accessCookieRefresher', () => {
41
- describe('User is logged in', () => {
42
- beforeEach(() => {
43
- jest
44
- .spyOn(UserDataRequest.prototype, 'subscribe')
45
- .mockImplementation((callback) => {
46
- callback({
47
- attributes: {
48
- uuid: mockUuid,
49
- },
50
- state: {
51
- isLoggedIn: true,
52
- },
53
- });
54
- });
55
- store.set('state', { isLoggedIn: true });
56
- });
57
-
58
- describe('without any cookies', () => {
59
- beforeEach(() => {
60
- jest.spyOn(cookie, 'getCookie').mockReturnValue(null);
61
- });
62
- it('Should set user access cookie and non user access cookie', async () => {
63
- expect(await accessCookieRefresher()).toEqual([undefined, undefined]);
64
- expect(authorization.setUserAccessCookie).toHaveBeenCalledTimes(1);
65
- expect(authorization.setNonUserAccessCookie).toHaveBeenCalledTimes(1);
66
- });
67
- });
68
-
69
- describe('with only user access cookie', () => {
70
- beforeEach(() => {
71
- jest.spyOn(cookie, 'getCookie').mockImplementation((name) => {
72
- if (name === 'VSTOKEN') {
73
- return mockAccessCookie(25, mockUuid, ['newspaper'], 'avisnavn.no');
74
- }
75
- return null;
76
- });
77
- });
78
- it('Should set user access cookie and non user access cookie', async () => {
79
- expect(await accessCookieRefresher()).toEqual([undefined, undefined]);
80
- expect(authorization.setUserAccessCookie).toHaveBeenCalledTimes(0);
81
- expect(authorization.setNonUserAccessCookie).toHaveBeenCalledTimes(1);
82
- });
83
- });
84
-
85
- describe('with only non user access cookie', () => {
86
- beforeEach(() => {
87
- jest.spyOn(cookie, 'getCookie').mockImplementation((name) => {
88
- if (name === 'VSTOKEN2') {
89
- return mockAccessCookie(1, mockUuid, ['newspaper'], 'avisnavn.no');
90
- }
91
- return null;
92
- });
93
- });
94
- it('Should set user access cookie and non user access cookie', async () => {
95
- expect(await accessCookieRefresher()).toEqual([undefined, undefined]);
96
- expect(authorization.setUserAccessCookie).toHaveBeenCalledTimes(1);
97
- expect(authorization.setNonUserAccessCookie).toHaveBeenCalledTimes(0);
98
- });
99
- });
100
-
101
- describe('with user access cookie set for another domain', () => {
102
- beforeEach(() => {
103
- jest.spyOn(cookie, 'getCookie').mockImplementation((name) => {
104
- if (name === 'VSTOKEN') {
105
- return mockAccessCookie(
106
- 25,
107
- mockUuid,
108
- ['newspaper'],
109
- 'evildomain.no'
110
- );
111
- }
112
- return null;
113
- });
114
- });
115
- it('Should set user access cookie and non user access cookie', async () => {
116
- expect(await accessCookieRefresher()).toEqual([undefined, undefined]);
117
- expect(authorization.setUserAccessCookie).toHaveBeenCalledTimes(1);
118
- });
119
- });
120
-
121
- describe('with user access cookie valid for less than 24 hours', () => {
122
- beforeEach(() => {
123
- jest.spyOn(cookie, 'getCookie').mockImplementation((name) => {
124
- if (name === 'VSTOKEN') {
125
- return mockAccessCookie(23, mockUuid, ['newspaper'], 'avisnavn.no');
126
- }
127
- return null;
128
- });
129
- });
130
- it('Should set user access cookie and non user access cookie', async () => {
131
- expect(await accessCookieRefresher()).toEqual([undefined, undefined]);
132
- expect(authorization.setUserAccessCookie).toHaveBeenCalledTimes(1);
133
- });
134
- });
135
-
136
- describe('with non user access cookie set for another domain', () => {
137
- beforeEach(() => {
138
- jest.spyOn(cookie, 'getCookie').mockImplementation((name) => {
139
- if (name === 'VSTOKEN2') {
140
- return mockAccessCookie(
141
- 25,
142
- mockUuid,
143
- ['newspaper'],
144
- 'evildomain.no'
145
- );
146
- }
147
- return null;
148
- });
149
- });
150
- it('Should set user access cookie and non user access cookie', async () => {
151
- expect(await accessCookieRefresher()).toEqual([undefined, undefined]);
152
- expect(authorization.setNonUserAccessCookie).toHaveBeenCalledTimes(1);
153
- });
154
- });
155
-
156
- describe('with non user access cookie valid for less than 30 minutes', () => {
157
- beforeEach(() => {
158
- jest.spyOn(cookie, 'getCookie').mockImplementation((name) => {
159
- if (name === 'VSTOKEN2') {
160
- return mockAccessCookie(0, mockUuid, ['newspaper'], 'avisnavn.no');
161
- }
162
- return null;
163
- });
164
- });
165
- it('Should set user access cookie and non user access cookie', async () => {
166
- expect(await accessCookieRefresher()).toEqual([undefined, undefined]);
167
- expect(authorization.setNonUserAccessCookie).toHaveBeenCalledTimes(1);
168
- });
169
- });
170
- });
171
- describe('User is not logged in', () => {
172
- beforeEach(() => {
173
- jest
174
- .spyOn(UserDataRequest.prototype, 'subscribe')
175
- .mockImplementation((callback) => {
176
- callback({
177
- attributes: {},
178
- state: {
179
- isLoggedIn: false,
180
- },
181
- });
182
- });
183
- store.set('state', { isLoggedIn: false });
184
- });
185
- describe('without any cookies', () => {
186
- beforeEach(() => {
187
- jest.spyOn(cookie, 'getCookie').mockReturnValue(null);
188
- });
189
- it('Should not set any access cookies', async () => {
190
- expect(await accessCookieRefresher()).toEqual([undefined, undefined]);
191
- expect(authorization.setUserAccessCookie).toHaveBeenCalledTimes(0);
192
- expect(authorization.setNonUserAccessCookie).toHaveBeenCalledTimes(0);
193
- });
194
- });
195
- describe('User has both access cookies', () => {
196
- beforeEach(() => {
197
- jest
198
- .spyOn(cookie, 'getCookie')
199
- .mockImplementation(() =>
200
- mockAccessCookie(25, mockUuid, ['newspaper'], 'avisnavn.no')
201
- );
202
- jest.spyOn(cookie, 'removeCookie').mockImplementation();
203
- });
204
- it('Should remove user access cookie', async () => {
205
- expect(await accessCookieRefresher()).toEqual([undefined, undefined]);
206
- expect(authorization.setUserAccessCookie).toHaveBeenCalledTimes(0);
207
- expect(authorization.setNonUserAccessCookie).toHaveBeenCalledTimes(0);
208
- expect(cookie.removeCookie).toHaveBeenNthCalledWith(1, 'VSTOKEN');
209
- expect(cookie.removeCookie).toHaveBeenNthCalledWith(2, 'VSTOKEN2');
210
- });
211
- });
212
- });
213
- });
@@ -1,98 +0,0 @@
1
- import { getCookie, removeCookie } from '../../lib/cookie';
2
- import UserDataRequest from '../../lib/UserDataRequest';
3
- import {
4
- setNonUserAccessCookie,
5
- setUserAccessCookie,
6
- } from '../../actions/authorization';
7
- import { getCurrentPublicationDomain } from '../../utils/url';
8
-
9
- const validCookieGenerator = 'jupiter';
10
-
11
- async function verifyAndSetUserAccessCookie() {
12
- const cookieName = 'VSTOKEN';
13
- const publicationDomain = getCurrentPublicationDomain();
14
- const cookie = getCookie(cookieName);
15
-
16
- new UserDataRequest()
17
- .withAttributes(['uuid'])
18
- .fetch()
19
- .then(async ({ attributes, state }) => {
20
- if (state.isLoggedIn === false) {
21
- if (typeof cookie === 'string') {
22
- // We don't want logged out users to have lingering VSTOKEN cookies
23
- removeCookie(cookieName);
24
- }
25
- return;
26
- }
27
-
28
- if (typeof cookie !== 'string') {
29
- // We want to set a VSTOKEN cookie for the user so it's ready upon next page load
30
- await setUserAccessCookie(publicationDomain);
31
- return;
32
- }
33
-
34
- /* The format of the cookie is:
35
- * <0 - signature>:<1 - timestamp when cookie stops being valid>:<2 - access_features>:<3 - user_id>:<4 - second level domain>:<5 - generator name (jupiter)> */
36
- const cookieParts = cookie.split(':');
37
-
38
- const validTo = new Date(parseInt(cookieParts[1], 10) * 1000);
39
- validTo.setDate(
40
- validTo.getDate() - 1
41
- ); /* Set the time one day back so we can update it if it's only valid for one more day */
42
-
43
- // Make sure that current cookie is not too old, is created for logged in user for this domain by jupiter
44
- if (
45
- validTo.getTime() > Date.now() &&
46
- cookieParts[3] === attributes.uuid &&
47
- publicationDomain.includes(cookieParts[4]) &&
48
- cookieParts[5] === validCookieGenerator
49
- ) {
50
- return; // Cookie is valid for more than 24 hours, keep it as it is
51
- }
52
- await setUserAccessCookie(publicationDomain);
53
- });
54
- }
55
-
56
- function verifyAndSetNonUserAccessCookie() {
57
- const cookieName = 'VSTOKEN2';
58
- const publicationDomain = getCurrentPublicationDomain();
59
- const cookie = getCookie(cookieName);
60
-
61
- new UserDataRequest()
62
- .fetch()
63
- .then(async ({ state: { isLoggedIn } }) => {
64
- if (isLoggedIn) {
65
- if (typeof cookie !== 'string') {
66
- await setNonUserAccessCookie(publicationDomain);
67
- return;
68
- }
69
- /* The format of the cookie is:
70
- * <0 - signature>:<1 - timestamp when cookie stops being valid>:<2 - access_features>:<3 - uuid>:<4 - second level domain>:<5 - generator name (jupiter)>
71
- */
72
- const cookieParts = cookie.split(':');
73
- const validTo = new Date(parseInt(cookieParts[1], 10) * 1000);
74
- // // Check that token is valid for the next 30 minutes otherwise we need to update it
75
- validTo.setMinutes(validTo.getMinutes() - 30);
76
-
77
- // Make sure that current cookie is not too old and is created for this domain by jupiter
78
- if (
79
- validTo.getTime() > Date.now() &&
80
- publicationDomain.includes(cookieParts[4]) &&
81
- cookieParts[5] === validCookieGenerator
82
- ) {
83
- return;
84
- }
85
- await setNonUserAccessCookie(publicationDomain);
86
- } else {
87
- removeCookie(cookieName);
88
- }
89
- })
90
- .then(() => {});
91
- }
92
-
93
- export default function accessCookieRefresher() {
94
- return Promise.all([
95
- verifyAndSetUserAccessCookie(),
96
- verifyAndSetNonUserAccessCookie(),
97
- ]);
98
- }