@amedia/user 0.13.6 → 0.13.8
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.
- package/amedia-user-js.iml +9 -0
- package/dist/actions/authorization.d.ts +2 -2
- package/dist/autologin/index.d.ts +2 -1
- package/dist/index.js +1 -1
- package/dist/lib/cookie.d.ts +4 -0
- package/global.d.ts +1 -0
- package/jest.config.js +2 -3
- package/package.json +2 -1
- package/src/actions/authorization.spec.ts +87 -0
- package/src/actions/authorization.ts +10 -17
- package/src/autologin/index.spec.ts +128 -34
- package/src/autologin/index.ts +23 -14
- package/src/eventHandlers/storage.ts +3 -1
- package/src/index-storage.spec.ts +82 -31
- package/src/janitor/index.ts +2 -2
- package/src/lib/PaywallUnlockRequest.spec.ts +34 -22
- package/src/lib/PaywallUnlockRequest.ts +22 -19
- package/src/lib/cookie.ts +12 -0
- package/testSetup.js +1 -0
- package/dist/lib/UserDataRequest.spec.d.ts +0 -1
- package/src/lib/UserDataRequest.spec.ts +0 -71
|
@@ -68,18 +68,6 @@ export const setNonUserAccessCookie = async (
|
|
|
68
68
|
};
|
|
69
69
|
};
|
|
70
70
|
|
|
71
|
-
export const hasRecentlyTriedToUnlockPaywall = (): boolean => {
|
|
72
|
-
const cookie = getCookie('aid-paywall-check-performed');
|
|
73
|
-
if (!cookie) {
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
const currentUrl = getCurrentUrl();
|
|
77
|
-
if (Array.isArray(cookie)) {
|
|
78
|
-
return cookie.filter((value) => value === currentUrl).length > 0;
|
|
79
|
-
}
|
|
80
|
-
return cookie === currentUrl;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
71
|
export const setAccessCookies = async (
|
|
84
72
|
publicationDomain
|
|
85
73
|
): Promise<SetCookieResponse> => {
|
|
@@ -111,12 +99,17 @@ export const setAccessCookies = async (
|
|
|
111
99
|
);
|
|
112
100
|
};
|
|
113
101
|
|
|
114
|
-
export const
|
|
115
|
-
const url = new URL(getCurrentUrl());
|
|
116
|
-
url.searchParams.set('access', 'granted');
|
|
117
|
-
|
|
102
|
+
export const setHasPerformedPaywallReload = (requestedUrl?): void => {
|
|
118
103
|
const expires = new Date();
|
|
119
104
|
expires.setTime(expires.getTime() + 2 * 60 * 1000); // Add two minutes
|
|
120
105
|
|
|
121
|
-
setCookie(
|
|
106
|
+
setCookie(
|
|
107
|
+
'aid-paywall-reload-performed',
|
|
108
|
+
requestedUrl || getCurrentUrl(),
|
|
109
|
+
expires
|
|
110
|
+
);
|
|
122
111
|
};
|
|
112
|
+
|
|
113
|
+
export const hasRecentlyPerformedPaywallReload = (requestedUrl?): boolean =>
|
|
114
|
+
getCookie('aid-paywall-reload-performed') ===
|
|
115
|
+
(requestedUrl || getCurrentUrl());
|
|
@@ -5,7 +5,11 @@
|
|
|
5
5
|
import store from '../store';
|
|
6
6
|
|
|
7
7
|
import * as thirdPartyCookie from './thirdPartyCookie';
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
attemptAutoLoginJob,
|
|
10
|
+
attemptPaywallAutoLogin,
|
|
11
|
+
AUTOLOGIN_COOKIE_TTL,
|
|
12
|
+
} from './index';
|
|
9
13
|
import * as location from '../utils/location';
|
|
10
14
|
import * as url from '../utils/url';
|
|
11
15
|
import * as cookie from '../lib/cookie';
|
|
@@ -21,36 +25,42 @@ function itShouldNotNavigateAway() {
|
|
|
21
25
|
});
|
|
22
26
|
}
|
|
23
27
|
|
|
24
|
-
function
|
|
25
|
-
it('should
|
|
26
|
-
expect(thirdPartyCookie.default).toHaveBeenCalledTimes(
|
|
28
|
+
function itShouldCallThirdPartyCookieStrategy() {
|
|
29
|
+
it('should try third party auto login', () => {
|
|
30
|
+
expect(thirdPartyCookie.default).toHaveBeenCalledTimes(1);
|
|
27
31
|
});
|
|
28
32
|
}
|
|
29
33
|
|
|
30
|
-
function
|
|
31
|
-
it(
|
|
32
|
-
expect(
|
|
34
|
+
function itShouldNavigateToAutoLogin(context) {
|
|
35
|
+
it(`should navigate to auto with context ${context}`, async () => {
|
|
36
|
+
expect(location.navigateTo).toHaveBeenCalledWith(
|
|
37
|
+
`https://www.aid.no/aid/logg_inn/auto?context=auto_login_${context}&requestedUrl=https%3A%2F%2Fwww.avisnavn.no%2Fabo%2F%3Fquery%3Dparam`
|
|
38
|
+
);
|
|
33
39
|
});
|
|
34
40
|
}
|
|
35
41
|
|
|
36
|
-
|
|
42
|
+
let getCookieSpy: jasmine.Spy;
|
|
43
|
+
|
|
44
|
+
beforeEach(() => {
|
|
45
|
+
restoreAllMocks();
|
|
46
|
+
resetAllMocks();
|
|
47
|
+
spyOn(location, 'navigateTo');
|
|
48
|
+
spyOn(url, 'getAidDomain').and.returnValue('www.aid.no');
|
|
49
|
+
spyOn(url, 'getCurrentUrl').and.returnValue(
|
|
50
|
+
'https://www.avisnavn.no/abo/?query=param'
|
|
51
|
+
);
|
|
52
|
+
getCookieSpy = spyOn(cookie, 'getCookie').and.returnValue('0');
|
|
53
|
+
spyOn(cookie, 'setCookie');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('attemptAutoLoginJob', () => {
|
|
37
57
|
let shouldPerformAutoLogin = false;
|
|
38
|
-
let getCookieSpy: jasmine.Spy;
|
|
39
58
|
|
|
40
59
|
beforeEach(() => {
|
|
41
|
-
restoreAllMocks();
|
|
42
|
-
resetAllMocks();
|
|
43
60
|
shouldPerformAutoLogin = false;
|
|
44
|
-
spyOn(location, 'navigateTo');
|
|
45
|
-
spyOn(url, 'getAidDomain').and.returnValue('www.aid.no');
|
|
46
|
-
spyOn(url, 'getCurrentUrl').and.returnValue(
|
|
47
|
-
'https://www.avisnavn.no/abo/?query=param'
|
|
48
|
-
);
|
|
49
61
|
jest
|
|
50
62
|
.spyOn(thirdPartyCookie, 'default')
|
|
51
63
|
.mockImplementation(() => Promise.resolve(shouldPerformAutoLogin));
|
|
52
|
-
getCookieSpy = spyOn(cookie, 'getCookie').and.returnValue('0');
|
|
53
|
-
spyOn(cookie, 'setCookie');
|
|
54
64
|
});
|
|
55
65
|
|
|
56
66
|
describe('when already logged in', () => {
|
|
@@ -68,7 +78,7 @@ describe('autologin', () => {
|
|
|
68
78
|
});
|
|
69
79
|
});
|
|
70
80
|
store.set('state', { isLoggedIn: true });
|
|
71
|
-
await
|
|
81
|
+
await attemptAutoLoginJob();
|
|
72
82
|
});
|
|
73
83
|
|
|
74
84
|
it('should not try third party auto login', () => {
|
|
@@ -99,36 +109,31 @@ describe('autologin', () => {
|
|
|
99
109
|
delete global.top; // Set window.top to something other than window.self
|
|
100
110
|
// @ts-ignore
|
|
101
111
|
global.top = {};
|
|
102
|
-
await
|
|
112
|
+
await attemptAutoLoginJob();
|
|
103
113
|
});
|
|
104
114
|
afterEach(() => {
|
|
105
115
|
// @ts-ignore
|
|
106
116
|
global.top = originalTop;
|
|
107
117
|
});
|
|
108
118
|
|
|
109
|
-
itShouldNotAttemptAutoLogin();
|
|
110
119
|
itShouldNotNavigateAway();
|
|
111
120
|
});
|
|
112
121
|
|
|
113
122
|
describe('when a strategy triggers auto login', () => {
|
|
114
123
|
beforeEach(async () => {
|
|
115
124
|
shouldPerformAutoLogin = true;
|
|
116
|
-
await
|
|
125
|
+
await attemptAutoLoginJob();
|
|
117
126
|
});
|
|
118
127
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
expect(location.navigateTo).toHaveBeenCalledWith(
|
|
122
|
-
'https://www.aid.no/aid/logg_inn/auto?context=auto_login_third_party&requestedUrl=https%3A%2F%2Fwww.avisnavn.no%2Fabo%2F%3Fquery%3Dparam'
|
|
123
|
-
);
|
|
124
|
-
});
|
|
128
|
+
itShouldCallThirdPartyCookieStrategy();
|
|
129
|
+
itShouldNavigateToAutoLogin('third_party');
|
|
125
130
|
});
|
|
126
131
|
describe('when no strategy triggers auto login', () => {
|
|
127
132
|
beforeEach(async () => {
|
|
128
|
-
await
|
|
133
|
+
await attemptAutoLoginJob();
|
|
129
134
|
});
|
|
130
135
|
|
|
131
|
-
|
|
136
|
+
itShouldCallThirdPartyCookieStrategy();
|
|
132
137
|
itShouldNotNavigateAway();
|
|
133
138
|
});
|
|
134
139
|
|
|
@@ -136,9 +141,10 @@ describe('autologin', () => {
|
|
|
136
141
|
describe('when tried within the last minute', () => {
|
|
137
142
|
beforeEach(async () => {
|
|
138
143
|
getCookieSpy.and.returnValue(Date.now());
|
|
139
|
-
await
|
|
144
|
+
await attemptAutoLoginJob();
|
|
140
145
|
});
|
|
141
|
-
|
|
146
|
+
itShouldCallThirdPartyCookieStrategy();
|
|
147
|
+
itShouldNotNavigateAway();
|
|
142
148
|
});
|
|
143
149
|
|
|
144
150
|
describe('when last attempt is old', () => {
|
|
@@ -146,9 +152,97 @@ describe('autologin', () => {
|
|
|
146
152
|
getCookieSpy.and.returnValue(
|
|
147
153
|
Date.now() - AUTOLOGIN_COOKIE_TTL - 1000
|
|
148
154
|
);
|
|
149
|
-
await
|
|
155
|
+
await attemptAutoLoginJob();
|
|
156
|
+
});
|
|
157
|
+
itShouldCallThirdPartyCookieStrategy();
|
|
158
|
+
itShouldNotNavigateAway();
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
describe('attemptPaywallAutoLogin', () => {
|
|
165
|
+
beforeEach(() => {});
|
|
166
|
+
|
|
167
|
+
describe('when already logged in', () => {
|
|
168
|
+
beforeEach(async () => {
|
|
169
|
+
jest
|
|
170
|
+
.spyOn(UserDataRequest.prototype, 'subscribe')
|
|
171
|
+
.mockImplementation((callback) => {
|
|
172
|
+
callback({
|
|
173
|
+
attributes: {
|
|
174
|
+
name: 'Some name',
|
|
175
|
+
},
|
|
176
|
+
state: {
|
|
177
|
+
isLoggedIn: true,
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
store.set('state', { isLoggedIn: true });
|
|
182
|
+
await attemptPaywallAutoLogin();
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe('when not already logged in', () => {
|
|
187
|
+
beforeEach(async () => {
|
|
188
|
+
jest
|
|
189
|
+
.spyOn(UserDataRequest.prototype, 'subscribe')
|
|
190
|
+
.mockImplementation((callback) => {
|
|
191
|
+
callback({
|
|
192
|
+
attributes: {},
|
|
193
|
+
state: {
|
|
194
|
+
isLoggedIn: false,
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
store.set('state', { isLoggedIn: false });
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe('when in iframe (Dr. Edition)', () => {
|
|
202
|
+
let originalTop;
|
|
203
|
+
beforeEach(async () => {
|
|
204
|
+
originalTop = global.top;
|
|
205
|
+
// @ts-ignore
|
|
206
|
+
delete global.top; // Set window.top to something other than window.self
|
|
207
|
+
// @ts-ignore
|
|
208
|
+
global.top = {};
|
|
209
|
+
await attemptPaywallAutoLogin();
|
|
210
|
+
});
|
|
211
|
+
afterEach(() => {
|
|
212
|
+
// @ts-ignore
|
|
213
|
+
global.top = originalTop;
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
itShouldNotNavigateAway();
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
describe('when not in iframe', () => {
|
|
220
|
+
describe('when already tried cookie is not found', () => {
|
|
221
|
+
beforeEach(async () => {
|
|
222
|
+
getCookieSpy.and.returnValue(undefined);
|
|
223
|
+
await attemptPaywallAutoLogin();
|
|
224
|
+
});
|
|
225
|
+
itShouldNavigateToAutoLogin('paywall');
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
describe('when already tried cookie was found', () => {
|
|
229
|
+
describe('when tried within the last minute', () => {
|
|
230
|
+
beforeEach(async () => {
|
|
231
|
+
getCookieSpy.and.returnValue(Date.now());
|
|
232
|
+
await attemptPaywallAutoLogin();
|
|
233
|
+
});
|
|
234
|
+
itShouldNotNavigateAway();
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
describe('when last attempt is old', () => {
|
|
238
|
+
beforeEach(async () => {
|
|
239
|
+
getCookieSpy.and.returnValue(
|
|
240
|
+
Date.now() - AUTOLOGIN_COOKIE_TTL - 1000
|
|
241
|
+
);
|
|
242
|
+
await attemptPaywallAutoLogin();
|
|
243
|
+
});
|
|
244
|
+
itShouldNavigateToAutoLogin('paywall');
|
|
150
245
|
});
|
|
151
|
-
itShouldAttemptAutoLogin();
|
|
152
246
|
});
|
|
153
247
|
});
|
|
154
248
|
});
|
package/src/autologin/index.ts
CHANGED
|
@@ -4,16 +4,9 @@ import { navigateTo } from '../utils/location';
|
|
|
4
4
|
import { getCookie, removeCookie, setCookie } from '../lib/cookie';
|
|
5
5
|
import UserDataRequest from '../lib/UserDataRequest';
|
|
6
6
|
|
|
7
|
-
const AUTOLOGIN_COOKIE = '
|
|
7
|
+
const AUTOLOGIN_COOKIE = 'has-tried-autologin';
|
|
8
8
|
export const AUTOLOGIN_COOKIE_TTL = 60000; // One minute
|
|
9
9
|
|
|
10
|
-
const strategies = [
|
|
11
|
-
{
|
|
12
|
-
name: 'third_party',
|
|
13
|
-
handler: thirdPartyCookie,
|
|
14
|
-
},
|
|
15
|
-
];
|
|
16
|
-
|
|
17
10
|
function inIframe() {
|
|
18
11
|
try {
|
|
19
12
|
return window.self !== window.top;
|
|
@@ -22,7 +15,7 @@ function inIframe() {
|
|
|
22
15
|
}
|
|
23
16
|
}
|
|
24
17
|
|
|
25
|
-
function
|
|
18
|
+
function hasAlreadyAttemptedAutoLogin() {
|
|
26
19
|
const cookie = getCookie(AUTOLOGIN_COOKIE);
|
|
27
20
|
if (cookie && typeof cookie !== 'object') {
|
|
28
21
|
try {
|
|
@@ -37,7 +30,7 @@ function haveAlreadyAttemptedAutoLogin() {
|
|
|
37
30
|
return false;
|
|
38
31
|
}
|
|
39
32
|
|
|
40
|
-
|
|
33
|
+
const attemptAutologin = async (strategies) => {
|
|
41
34
|
if (inIframe()) {
|
|
42
35
|
// Avoid auto-login on preview for dr edition.
|
|
43
36
|
return;
|
|
@@ -50,10 +43,6 @@ export const attemptAutologin = async () => {
|
|
|
50
43
|
return;
|
|
51
44
|
}
|
|
52
45
|
|
|
53
|
-
if (haveAlreadyAttemptedAutoLogin()) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
46
|
// eslint-disable-next-line no-restricted-syntax
|
|
58
47
|
for (const strategy of strategies) {
|
|
59
48
|
// eslint-disable-next-line no-await-in-loop
|
|
@@ -61,6 +50,10 @@ export const attemptAutologin = async () => {
|
|
|
61
50
|
.handler()
|
|
62
51
|
.then((shouldPerformAutoLogin) => {
|
|
63
52
|
if (shouldPerformAutoLogin) {
|
|
53
|
+
if (hasAlreadyAttemptedAutoLogin()) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
64
57
|
// Try to get newspaper cookie
|
|
65
58
|
const url = new URL(`https://${getAidDomain()}/aid/logg_inn/auto`);
|
|
66
59
|
url.searchParams.set('context', `auto_login_${strategy.name}`);
|
|
@@ -76,3 +69,19 @@ export const attemptAutologin = async () => {
|
|
|
76
69
|
}
|
|
77
70
|
}
|
|
78
71
|
};
|
|
72
|
+
|
|
73
|
+
export const attemptAutoLoginJob = async () =>
|
|
74
|
+
attemptAutologin([
|
|
75
|
+
{
|
|
76
|
+
name: 'third_party',
|
|
77
|
+
handler: thirdPartyCookie,
|
|
78
|
+
},
|
|
79
|
+
]);
|
|
80
|
+
|
|
81
|
+
export const attemptPaywallAutoLogin = async () =>
|
|
82
|
+
attemptAutologin([
|
|
83
|
+
{
|
|
84
|
+
name: 'paywall',
|
|
85
|
+
handler: () => Promise.resolve(true),
|
|
86
|
+
},
|
|
87
|
+
]);
|
|
@@ -29,49 +29,100 @@ beforeEach(() => {
|
|
|
29
29
|
|
|
30
30
|
describe('Storage', () => {
|
|
31
31
|
beforeEach(() => {
|
|
32
|
-
|
|
32
|
+
spyOn(europa, 'updateNamespace');
|
|
33
33
|
});
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
describe('when is logged in', () => {
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
store.set('state', { isLoggedIn: true });
|
|
38
|
+
});
|
|
37
39
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
describe('when updating storage', () => {
|
|
41
|
+
beforeEach(() => {
|
|
42
|
+
updateStorage({
|
|
43
|
+
user_info: {
|
|
44
|
+
some: 'data',
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
it('should update storage', () => {
|
|
49
|
+
expect(europa.updateNamespace).toHaveBeenCalledWith('user_info', {
|
|
50
|
+
some: 'data',
|
|
51
|
+
});
|
|
52
|
+
});
|
|
42
53
|
});
|
|
43
54
|
|
|
44
|
-
|
|
45
|
-
|
|
55
|
+
describe('when getting storage namespaces', () => {
|
|
56
|
+
let onUpdate;
|
|
57
|
+
beforeEach(() => {
|
|
58
|
+
store.set('storage', {
|
|
59
|
+
one: { key1: 'value1' },
|
|
60
|
+
two: { key2: 'value2' },
|
|
61
|
+
three: { key3: 'value3' },
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
onUpdate = jest.fn();
|
|
65
|
+
new UserDataRequest().withStorage(['one', 'three']).subscribe(onUpdate);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('should only return requested', () => {
|
|
69
|
+
expect(onUpdate).toHaveBeenCalledWith({
|
|
70
|
+
storage: {
|
|
71
|
+
one: {
|
|
72
|
+
key1: 'value1',
|
|
73
|
+
},
|
|
74
|
+
three: {
|
|
75
|
+
key3: 'value3',
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
state: {
|
|
79
|
+
isLoggedIn: true,
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
});
|
|
46
83
|
});
|
|
47
84
|
});
|
|
48
85
|
|
|
49
|
-
describe('
|
|
50
|
-
let onUpdate;
|
|
86
|
+
describe('when not logged in', () => {
|
|
51
87
|
beforeEach(() => {
|
|
52
|
-
store.set('
|
|
53
|
-
one: { key1: 'value1' },
|
|
54
|
-
two: { key2: 'value2' },
|
|
55
|
-
three: { key3: 'value3' },
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
onUpdate = jest.fn();
|
|
59
|
-
new UserDataRequest().withStorage(['one', 'three']).subscribe(onUpdate);
|
|
88
|
+
store.set('state', { isLoggedIn: false });
|
|
60
89
|
});
|
|
61
90
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
},
|
|
68
|
-
three: {
|
|
69
|
-
key3: 'value3',
|
|
91
|
+
describe('when updating storage', () => {
|
|
92
|
+
beforeEach(() => {
|
|
93
|
+
updateStorage({
|
|
94
|
+
user_info: {
|
|
95
|
+
some: 'data',
|
|
70
96
|
},
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
it('should not attempt storage update', () => {
|
|
100
|
+
expect(europa.updateNamespace).not.toHaveBeenCalled();
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe('when getting storage namespaces', () => {
|
|
105
|
+
let onUpdate;
|
|
106
|
+
beforeEach(() => {
|
|
107
|
+
store.set('storage', {
|
|
108
|
+
one: { key1: 'value1' },
|
|
109
|
+
two: { key2: 'value2' },
|
|
110
|
+
three: { key3: 'value3' },
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
onUpdate = jest.fn();
|
|
114
|
+
new UserDataRequest().withStorage(['one', 'three']).subscribe(onUpdate);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('should have been called once', () => {
|
|
118
|
+
expect(onUpdate).toHaveBeenCalledTimes(1);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('should never return namespaces', () => {
|
|
122
|
+
expect(onUpdate).toHaveBeenCalledWith({
|
|
123
|
+
state: { isLoggedIn: false },
|
|
124
|
+
storage: {},
|
|
125
|
+
});
|
|
75
126
|
});
|
|
76
127
|
});
|
|
77
128
|
});
|
package/src/janitor/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import accessCookieRefresher from './jobs/accessCookieRefresher';
|
|
2
2
|
import { removeUserStorage } from './jobs/removeUserStorage';
|
|
3
3
|
import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
|
|
4
|
-
import {
|
|
4
|
+
import { attemptAutoLoginJob } from '../autologin';
|
|
5
5
|
|
|
6
|
-
const jobs = [
|
|
6
|
+
const jobs = [attemptAutoLoginJob, accessCookieRefresher, removeUserStorage];
|
|
7
7
|
|
|
8
8
|
async function performJobs() {
|
|
9
9
|
// eslint-disable-next-line no-restricted-syntax
|
|
@@ -12,6 +12,7 @@ import PaywallError from '../exceptions/PaywallError';
|
|
|
12
12
|
import { setRootElement } from '../utils/customEventWrapper';
|
|
13
13
|
import { listenForUserAttributeRequests } from '../eventHandlers/userAttributes';
|
|
14
14
|
import { listenForStateRequests } from '../eventHandlers/state';
|
|
15
|
+
import { removeAllCookies } from './cookie';
|
|
15
16
|
import restoreAllMocks = jest.restoreAllMocks;
|
|
16
17
|
import spyOn = jest.spyOn;
|
|
17
18
|
|
|
@@ -23,6 +24,7 @@ beforeEach(() => {
|
|
|
23
24
|
// We cannot use window as our event root during testing
|
|
24
25
|
// since tests are executed in parallel.
|
|
25
26
|
setRootElement(new Document());
|
|
27
|
+
removeAllCookies();
|
|
26
28
|
|
|
27
29
|
// Also, since we keep creating new root elements, we also
|
|
28
30
|
// need to attach to these new roots.
|
|
@@ -56,29 +58,13 @@ function itShouldAttemptActivationWithAccessFeatures(accessFeatures: string[]) {
|
|
|
56
58
|
|
|
57
59
|
describe('PaywallUnlocker', () => {
|
|
58
60
|
beforeEach(() => {
|
|
59
|
-
spyOn(autologin, '
|
|
60
|
-
spyOn(authorization, '
|
|
61
|
+
spyOn(autologin, 'attemptPaywallAutoLogin').mockResolvedValue();
|
|
62
|
+
spyOn(authorization, 'setHasPerformedPaywallReload');
|
|
61
63
|
spyOn(authorization, 'activate');
|
|
62
64
|
spyOn(location, 'reload').mockReturnValue();
|
|
63
65
|
spyOn(location, 'replace').mockReturnValue();
|
|
64
66
|
});
|
|
65
67
|
|
|
66
|
-
describe('When already tried to unlock', () => {
|
|
67
|
-
beforeEach(() => {
|
|
68
|
-
spyOn(authorization, 'hasRecentlyTriedToUnlockPaywall').mockReturnValue(
|
|
69
|
-
true
|
|
70
|
-
);
|
|
71
|
-
});
|
|
72
|
-
it('should throw exception', () => {
|
|
73
|
-
expect(new PaywallUnlockRequest().tryUnlock()).rejects.toThrow(
|
|
74
|
-
new PaywallError(
|
|
75
|
-
'Unlock check already performed.',
|
|
76
|
-
PaywallError.ERROR_RELOADED_NO_ACCESS
|
|
77
|
-
)
|
|
78
|
-
);
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
|
|
82
68
|
describe('When not logged in', () => {
|
|
83
69
|
let unlockResponse;
|
|
84
70
|
beforeEach(async () => {
|
|
@@ -88,7 +74,7 @@ describe('PaywallUnlocker', () => {
|
|
|
88
74
|
});
|
|
89
75
|
|
|
90
76
|
it('should attempt autologin', async () => {
|
|
91
|
-
expect(autologin.
|
|
77
|
+
expect(autologin.attemptPaywallAutoLogin).toHaveBeenCalledTimes(1);
|
|
92
78
|
});
|
|
93
79
|
|
|
94
80
|
it('should return no access', () => {
|
|
@@ -110,7 +96,7 @@ describe('PaywallUnlocker', () => {
|
|
|
110
96
|
});
|
|
111
97
|
|
|
112
98
|
it('should not attempt autologin', () => {
|
|
113
|
-
expect(autologin.
|
|
99
|
+
expect(autologin.attemptPaywallAutoLogin).not.toHaveBeenCalled();
|
|
114
100
|
});
|
|
115
101
|
|
|
116
102
|
itShouldNotRedirect();
|
|
@@ -128,8 +114,34 @@ describe('PaywallUnlocker', () => {
|
|
|
128
114
|
unlockResponse = await new PaywallUnlockRequest().tryUnlock();
|
|
129
115
|
});
|
|
130
116
|
|
|
131
|
-
|
|
132
|
-
|
|
117
|
+
describe('When called with reload and already tried to reload', () => {
|
|
118
|
+
let error;
|
|
119
|
+
beforeEach(async () => {
|
|
120
|
+
spyOn(
|
|
121
|
+
authorization,
|
|
122
|
+
'hasRecentlyPerformedPaywallReload'
|
|
123
|
+
).mockReturnValue(true);
|
|
124
|
+
error = undefined;
|
|
125
|
+
try {
|
|
126
|
+
await new PaywallUnlockRequest().reloadOnAccess().tryUnlock();
|
|
127
|
+
} catch (e) {
|
|
128
|
+
error = e;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
it('should throw exception', () => {
|
|
132
|
+
expect(error).toEqual(
|
|
133
|
+
new PaywallError(
|
|
134
|
+
'Unlock check already performed.',
|
|
135
|
+
PaywallError.ERROR_RELOADED_NO_ACCESS
|
|
136
|
+
)
|
|
137
|
+
);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('should not store that we tried, since we perform no reload', () => {
|
|
142
|
+
expect(
|
|
143
|
+
authorization.setHasPerformedPaywallReload
|
|
144
|
+
).not.toHaveBeenCalled();
|
|
133
145
|
});
|
|
134
146
|
|
|
135
147
|
it('should give access with access features', () => {
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
2
|
activate,
|
|
3
3
|
hasAccess,
|
|
4
|
-
|
|
4
|
+
hasRecentlyPerformedPaywallReload,
|
|
5
5
|
setAccessCookies,
|
|
6
|
-
|
|
6
|
+
setHasPerformedPaywallReload,
|
|
7
7
|
} from '../actions/authorization';
|
|
8
8
|
import { getCurrentPublicationDomain } from '../utils/url';
|
|
9
9
|
import * as location from '../utils/location';
|
|
10
10
|
import PaywallError from '../exceptions/PaywallError';
|
|
11
11
|
import UserDataRequest from './UserDataRequest';
|
|
12
|
-
import {
|
|
12
|
+
import { attemptPaywallAutoLogin } from '../autologin';
|
|
13
13
|
|
|
14
14
|
type UnlockResponse = {
|
|
15
15
|
isLoggedIn: boolean;
|
|
@@ -40,21 +40,13 @@ class PaywallUnlockRequest {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
public async tryUnlock(): Promise<UnlockResponse> {
|
|
43
|
-
// Ensures we don't get stuck in a reload/redirect loop
|
|
44
|
-
if (hasRecentlyTriedToUnlockPaywall()) {
|
|
45
|
-
throw new PaywallError(
|
|
46
|
-
'Unlock check already performed.',
|
|
47
|
-
PaywallError.ERROR_RELOADED_NO_ACCESS
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
43
|
const {
|
|
52
44
|
state: { isLoggedIn },
|
|
53
|
-
} = await new UserDataRequest().fetch();
|
|
45
|
+
} = await new UserDataRequest().withAttributes(['name']).fetch();
|
|
54
46
|
if (!isLoggedIn) {
|
|
55
47
|
// Will attempt one or more strategies for creating a first party session
|
|
56
48
|
// Redirects/reloads might occur
|
|
57
|
-
await
|
|
49
|
+
await attemptPaywallAutoLogin();
|
|
58
50
|
return {
|
|
59
51
|
isLoggedIn: false,
|
|
60
52
|
hasAccess: false,
|
|
@@ -85,12 +77,23 @@ class PaywallUnlockRequest {
|
|
|
85
77
|
);
|
|
86
78
|
}
|
|
87
79
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
80
|
+
if (this.reload || this.requestedUrl) {
|
|
81
|
+
// Ensures we don't get stuck in a reload/redirect loop
|
|
82
|
+
if (hasRecentlyPerformedPaywallReload(this.requestedUrl)) {
|
|
83
|
+
throw new PaywallError(
|
|
84
|
+
'Unlock check already performed.',
|
|
85
|
+
PaywallError.ERROR_RELOADED_NO_ACCESS
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
setHasPerformedPaywallReload(this.requestedUrl);
|
|
90
|
+
if (this.reload) {
|
|
91
|
+
location.reload();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (this.requestedUrl) {
|
|
95
|
+
location.replace(this.requestedUrl);
|
|
96
|
+
}
|
|
94
97
|
}
|
|
95
98
|
return {
|
|
96
99
|
isLoggedIn: true,
|