@amedia/user 0.14.8 → 0.14.10
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/README.md +4 -3
- package/dist/user.d.ts +16 -11
- package/package.json +1 -1
- package/src/actions/authentication.spec.ts +1 -1
- package/src/actions/authentication.ts +9 -4
- package/src/actions/authorization.spec.ts +21 -13
- package/src/actions/authorization.ts +14 -11
- package/src/actions/userData.ts +2 -2
- package/src/clients/jupiter.spec.ts +20 -22
- package/src/clients/jupiter.ts +6 -6
- package/src/clients/mercury.spec.ts +15 -13
- package/src/clients/mercury.ts +3 -3
- package/src/clients/nebula.spec.ts +9 -9
- package/src/clients/nebula.ts +4 -4
- package/src/eventHandlers/activation.ts +6 -1
- package/src/index.ts +3 -0
- package/src/janitor/jobs/accessCookieRefresher.spec.ts +1 -1
- package/src/janitor/jobs/accessCookieRefresher.ts +9 -9
- package/src/lib/PaywallUnlockRequest.spec.ts +31 -2
- package/src/lib/PaywallUnlockRequest.ts +13 -3
- package/src/utils/debouncer.ts +1 -1
- package/src/utils/url.spec.ts +3 -3
- package/src/utils/url.ts +1 -1
package/README.md
CHANGED
|
@@ -139,7 +139,7 @@ If you're running a background task and don't mind waiting longer than default (
|
|
|
139
139
|
```
|
|
140
140
|
|
|
141
141
|
## Unlocking the paywall
|
|
142
|
-
Amedia's
|
|
142
|
+
Amedia's sites use a Paywall that's based on a cookie called VSTOKEN that is read by Varnish and used by the content system to decide if the content should be shown to the user or not. If not, the content will load what we call an incentive, a dialogue above the content, that will allow the user to log in and buy subscriptions. The incentive will use a `PaywallUnlockRequest` to see if it can somehow grant access to the user. In some cases it makes sense to create a different user interface for a paywall. In these cases we recommend that you use this `PaywallUnlockRequest` in order to get the same functionality.
|
|
143
143
|
|
|
144
144
|
More information about the paywall can be found here: [How the Paywall works](https://amedia.slite.com/app/channels/hOlqx3Miez/notes/UFqOFRV3g1)
|
|
145
145
|
|
|
@@ -151,6 +151,7 @@ To unlock the paywall, you first need to create a new `PaywallUnlockRequest`. On
|
|
|
151
151
|
to `requestedUrl`. (Provides more flexibility than `reloadOnAccess()`)
|
|
152
152
|
- `withAccessFeatures(accessFeatures: string[])`: Specify what access features will give access to the current content.
|
|
153
153
|
The default is [`newspaper`].
|
|
154
|
+
- `withSiteDomain(domain: string)`: Specify which site we to attempt activation against. The default is the current domain. This is mostly useful for direktesport.no which often knows what domain the user came from.
|
|
154
155
|
|
|
155
156
|
More details about the access model can be found here: [Access model](https://amedia.slite.com/app/channels/hOlqx3Miez/notes/nhG2Ok39BN)
|
|
156
157
|
|
|
@@ -203,12 +204,12 @@ Note that this *might result in redirects/reloads* if the user is not already lo
|
|
|
203
204
|
The following methods are actions that the user needs to be able to perform in an environment using logged in users.
|
|
204
205
|
|
|
205
206
|
### `goToLoginPage(requestedUrl: string)`
|
|
206
|
-
This action will send the user to the aID login page. It will take care of building the correct URL, including your current
|
|
207
|
+
This action will send the user to the aID login page. It will take care of building the correct URL, including your current site domain, so the login page is skinned correctly.
|
|
207
208
|
|
|
208
209
|
You may include a requestedUrl if you need to control where the user should end up after logging in. The default is to send the user back to the current URL where this action was called.
|
|
209
210
|
|
|
210
211
|
### `logout()`
|
|
211
|
-
This action will log the user out of aID. The logout is global, meaning it will log the session out of all
|
|
212
|
+
This action will log the user out of aID. The logout is global, meaning it will log the session out of all sites and aid.no.
|
|
212
213
|
|
|
213
214
|
## Updating storage
|
|
214
215
|
If you need to store preferences, favourite teams etc for the user, you can choose between two methods. One will update only the attributes of a namespace that you specify, the other will update everything. The main difference is that if you opt for updating everything, attributes that are present on the backend, but not in the update, will be deleted.
|
package/dist/user.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
declare module '@amedia/user/actions/authentication' {
|
|
2
2
|
import { UserAttributes } from '@amedia/user/types';
|
|
3
|
+
export const loginUrl: (requestedUrl: URL | string) => URL;
|
|
3
4
|
export const login: (requestedUrl: URL | string) => void;
|
|
4
5
|
export const logout: () => Promise<void>;
|
|
5
6
|
export const getSession: () => Promise<UserAttributes>;
|
|
@@ -16,11 +17,11 @@ declare module '@amedia/user/actions/authorization' {
|
|
|
16
17
|
accessFeatures: string[];
|
|
17
18
|
};
|
|
18
19
|
export const getAccess: (accessFeatures?: string[]) => Promise<Pick<UserAttributes, 'access'>>;
|
|
19
|
-
export const activate: (accessFeatures: string[]) => Promise<string[]>;
|
|
20
|
+
export const activate: (accessFeatures: string[], siteDomain: string) => Promise<string[]>;
|
|
20
21
|
export const hasAccess: (currentAccessFeatures: string[], requestedAccessFeatures: string[]) => boolean;
|
|
21
|
-
export const setUserAccessCookie: (
|
|
22
|
-
export const setNonUserAccessCookie: (
|
|
23
|
-
export const setAccessCookies: (
|
|
22
|
+
export const setUserAccessCookie: (siteDomain: string) => Promise<SetCookieResponse>;
|
|
23
|
+
export const setNonUserAccessCookie: (siteDomain: string) => Promise<SetCookieResponse>;
|
|
24
|
+
export const setAccessCookies: (siteDomain: string) => Promise<SetCookieResponse>;
|
|
24
25
|
export const setHasPerformedPaywallReload: (requestedUrl?: string | undefined) => void;
|
|
25
26
|
export const hasRecentlyPerformedPaywallReload: (requestedUrl?: string | undefined) => boolean;
|
|
26
27
|
export {};
|
|
@@ -222,14 +223,14 @@ declare module '@amedia/user/clients/jupiter' {
|
|
|
222
223
|
access_features: Array<String, false>;
|
|
223
224
|
}, false>;
|
|
224
225
|
export type GetNonUserAccess = Static<typeof GetNonUserAccessSchema>;
|
|
225
|
-
export const checkForAccess: (
|
|
226
|
-
export const getUserAccess: (
|
|
226
|
+
export const checkForAccess: (siteDomain: string, accessFeatures: AccessFeatures) => Promise<Pick<UserAttributes, 'access'>>;
|
|
227
|
+
export const getUserAccess: (siteDomain: string) => Promise<GetVstoken>;
|
|
227
228
|
export const getSitesWithAccessFeatures: ({ accessFeatures, excludeBundles, sortBy, }: {
|
|
228
229
|
accessFeatures: string[];
|
|
229
230
|
excludeBundles?: boolean | undefined;
|
|
230
231
|
sortBy?: "domain" | "veiklasse" | undefined;
|
|
231
232
|
}) => Promise<Sites>;
|
|
232
|
-
export const getNonUserAccess: (
|
|
233
|
+
export const getNonUserAccess: (siteDomain: string) => Promise<GetNonUserAccess>;
|
|
233
234
|
export {};
|
|
234
235
|
|
|
235
236
|
}
|
|
@@ -240,7 +241,7 @@ declare module '@amedia/user/clients/jupiter.spec' {
|
|
|
240
241
|
declare module '@amedia/user/clients/mercury' {
|
|
241
242
|
export class UserDeactivatedError extends Error {
|
|
242
243
|
}
|
|
243
|
-
export const activateSubscription: (accessFeatures: string[]) => Promise<string[]>;
|
|
244
|
+
export const activateSubscription: (accessFeatures: string[], siteDomain: string) => Promise<string[]>;
|
|
244
245
|
|
|
245
246
|
}
|
|
246
247
|
declare module '@amedia/user/clients/mercury.spec' {
|
|
@@ -264,7 +265,7 @@ declare module '@amedia/user/clients/mercuryLog.spec' {
|
|
|
264
265
|
}
|
|
265
266
|
declare module '@amedia/user/clients/nebula' {
|
|
266
267
|
import { UserAttributes } from '@amedia/user/types';
|
|
267
|
-
export const getInfo: (
|
|
268
|
+
export const getInfo: (siteDomain: string, browserId: string, trackingKey?: string | undefined) => Promise<Pick<UserAttributes, 'extraData'>>;
|
|
268
269
|
|
|
269
270
|
}
|
|
270
271
|
declare module '@amedia/user/clients/nebula.spec' {
|
|
@@ -291,6 +292,7 @@ declare module '@amedia/user/eventHandlers/activation' {
|
|
|
291
292
|
interface WindowEventMap {
|
|
292
293
|
[ActivationEvents.REQUEST_ACTIVATION]: CustomEvent<{
|
|
293
294
|
accessFeatures: string[];
|
|
295
|
+
siteDomain?: string;
|
|
294
296
|
}>;
|
|
295
297
|
[ActivationEvents.ON_ACTIVATION_COMPLETE]: CustomEvent<{
|
|
296
298
|
accessFeatures: string[];
|
|
@@ -417,6 +419,7 @@ declare module '@amedia/user/index' {
|
|
|
417
419
|
* @param requestedUrl
|
|
418
420
|
*/
|
|
419
421
|
export const goToLoginPage: (requestedUrl?: string | undefined) => void;
|
|
422
|
+
export const getLoginUrl: (requestedUrl?: string | undefined) => URL;
|
|
420
423
|
export const logout: () => Promise<void>;
|
|
421
424
|
export const updateStorage: (storage: {
|
|
422
425
|
[index: string]: Object;
|
|
@@ -486,6 +489,8 @@ declare module '@amedia/user/lib/PaywallUnlockRequest' {
|
|
|
486
489
|
private requestedAccessFeatures;
|
|
487
490
|
private reload;
|
|
488
491
|
private requestedUrl;
|
|
492
|
+
private siteDomain;
|
|
493
|
+
withSiteDomain(domain: string): this;
|
|
489
494
|
withAccessFeatures(accessFeatures: string[]): this;
|
|
490
495
|
reloadOnAccess(): this;
|
|
491
496
|
redirectOnAccess(requestedUrl: string): this;
|
|
@@ -641,7 +646,7 @@ declare module '@amedia/user/utils/debouncer' {
|
|
|
641
646
|
* resolve my promise', but instead of the fetch being performed right away, we want to wait for more users to
|
|
642
647
|
* request other stuff. This will keep us from performing lots of individual fetches to the backend.
|
|
643
648
|
*
|
|
644
|
-
* Our main use case is a standard
|
|
649
|
+
* Our main use case is a standard site front page where one component on the page requests the euucp namespace
|
|
645
650
|
* from Europa, and then another component requests the privacy_preferences namespace. Instead of the Europa client
|
|
646
651
|
* calling two fetches to the server, one for euucp and another for privacy_preferences, we want the Debouncer to
|
|
647
652
|
* aggregate these calls into one call to the backend, that requests both euucp AND privacy_preferences, so the response
|
|
@@ -759,7 +764,7 @@ declare module '@amedia/user/utils/url' {
|
|
|
759
764
|
export const getCurrentHostname: () => string;
|
|
760
765
|
export const getCurrentUrl: () => string;
|
|
761
766
|
export const getCurrentEnvironment: () => string | null;
|
|
762
|
-
export const
|
|
767
|
+
export const getCurrentSiteDomain: () => string;
|
|
763
768
|
export const getAidDomain: () => string;
|
|
764
769
|
|
|
765
770
|
}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@ import { login } from './authentication';
|
|
|
4
4
|
|
|
5
5
|
beforeEach(() => {
|
|
6
6
|
spyOn(url, 'getCurrentUrl').and.returnValue('https://www.telen.no/artikkel/');
|
|
7
|
-
spyOn(url, '
|
|
7
|
+
spyOn(url, 'getCurrentSiteDomain').and.returnValue('www.telen.no');
|
|
8
8
|
spyOn(location, 'navigateTo');
|
|
9
9
|
});
|
|
10
10
|
|
|
@@ -2,19 +2,24 @@ import { getCurrentSession, removeSession } from '../clients/pluto';
|
|
|
2
2
|
import cache from '../cache';
|
|
3
3
|
import { getRPCClient } from '../lib/rpc';
|
|
4
4
|
import { removeCookie } from '../lib/cookie';
|
|
5
|
-
import { getAidDomain,
|
|
5
|
+
import { getAidDomain, getCurrentSiteDomain } from '../utils/url';
|
|
6
6
|
import log from '../utils/log';
|
|
7
7
|
import { UserAttributes } from '../types';
|
|
8
8
|
import { navigateTo } from '../utils/location';
|
|
9
9
|
|
|
10
|
-
export const
|
|
10
|
+
export const loginUrl = (requestedUrl: URL | string) => {
|
|
11
11
|
const url = new URL(
|
|
12
|
-
`https://${getAidDomain()}/aid/logg_inn/${
|
|
12
|
+
`https://${getAidDomain()}/aid/logg_inn/${getCurrentSiteDomain()}/`
|
|
13
13
|
);
|
|
14
14
|
url.searchParams.set(
|
|
15
15
|
'requestedUrl',
|
|
16
16
|
requestedUrl instanceof URL ? requestedUrl.href : requestedUrl
|
|
17
17
|
);
|
|
18
|
+
return url;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const login = (requestedUrl: URL | string) => {
|
|
22
|
+
const url = loginUrl(requestedUrl);
|
|
18
23
|
navigateTo(url.href);
|
|
19
24
|
};
|
|
20
25
|
|
|
@@ -29,7 +34,7 @@ function clearCookies() {
|
|
|
29
34
|
|
|
30
35
|
export const logout = async () =>
|
|
31
36
|
// TODO: It now clears the entire cache. Might be too aggressive.
|
|
32
|
-
Promise.all([removeSession(
|
|
37
|
+
Promise.all([removeSession(getCurrentSiteDomain()), cache.clear()])
|
|
33
38
|
.then(() => {
|
|
34
39
|
// TODO This should be separated from logout and instead be setup to listen on state changes on storage
|
|
35
40
|
getRPCClient().userAuthenticated(false);
|
|
@@ -20,7 +20,7 @@ import * as authentication from './authentication';
|
|
|
20
20
|
import { removeAllCookies } from '../lib/cookie';
|
|
21
21
|
import { AccessFeatures } from '../clients/jupiter';
|
|
22
22
|
|
|
23
|
-
const
|
|
23
|
+
const siteDomain = 'www.avisnavn.no';
|
|
24
24
|
|
|
25
25
|
beforeEach(() => {
|
|
26
26
|
removeAllCookies();
|
|
@@ -49,8 +49,13 @@ describe('activate', () => {
|
|
|
49
49
|
.mockResolvedValue(['whatever']);
|
|
50
50
|
});
|
|
51
51
|
it('Returns whatever mercury returns', async () => {
|
|
52
|
-
expect(await activate(['newspaper'])).toEqual([
|
|
53
|
-
|
|
52
|
+
expect(await activate(['newspaper'], 'www.telen.no')).toEqual([
|
|
53
|
+
'whatever',
|
|
54
|
+
]);
|
|
55
|
+
expect(mercury.activateSubscription).toHaveBeenCalledWith(
|
|
56
|
+
['newspaper'],
|
|
57
|
+
'www.telen.no'
|
|
58
|
+
);
|
|
54
59
|
});
|
|
55
60
|
});
|
|
56
61
|
|
|
@@ -62,8 +67,11 @@ describe('activate', () => {
|
|
|
62
67
|
jest.spyOn(authentication, 'logout').mockResolvedValue(undefined);
|
|
63
68
|
});
|
|
64
69
|
it('Returns whatever mercury returns', async () => {
|
|
65
|
-
expect(await activate([])).toEqual([]);
|
|
66
|
-
expect(mercury.activateSubscription).toHaveBeenCalledWith(
|
|
70
|
+
expect(await activate([], 'www.telen.no')).toEqual([]);
|
|
71
|
+
expect(mercury.activateSubscription).toHaveBeenCalledWith(
|
|
72
|
+
[],
|
|
73
|
+
'www.telen.no'
|
|
74
|
+
);
|
|
67
75
|
expect(authentication.logout).toHaveBeenCalled();
|
|
68
76
|
});
|
|
69
77
|
});
|
|
@@ -205,7 +213,7 @@ describe('setUserAccessCookie', () => {
|
|
|
205
213
|
describe('when getUserAccess is successful', () => {
|
|
206
214
|
getUserAccessSucceeds(['newspaper', 'plussalt']);
|
|
207
215
|
beforeEach(async () => {
|
|
208
|
-
cookieResponse = await setUserAccessCookie(
|
|
216
|
+
cookieResponse = await setUserAccessCookie(siteDomain);
|
|
209
217
|
});
|
|
210
218
|
itReturnsCookieResponse(true, ['newspaper', 'plussalt']);
|
|
211
219
|
itSetsVstokenCookie();
|
|
@@ -214,7 +222,7 @@ describe('setUserAccessCookie', () => {
|
|
|
214
222
|
describe('when getUserAccess is not successful', () => {
|
|
215
223
|
getUserAccessFails(['newspaper', 'plussalt']);
|
|
216
224
|
beforeEach(async () => {
|
|
217
|
-
cookieResponse = await setUserAccessCookie(
|
|
225
|
+
cookieResponse = await setUserAccessCookie(siteDomain);
|
|
218
226
|
});
|
|
219
227
|
itReturnsCookieResponse(false, ['newspaper', 'plussalt']);
|
|
220
228
|
itSetsNoCookies();
|
|
@@ -225,7 +233,7 @@ describe('setNonUserAccessCookie', () => {
|
|
|
225
233
|
describe('when getNonUserAccess is successful', () => {
|
|
226
234
|
getNonUserAccessSucceeds(['newspaper', 'plussalt']);
|
|
227
235
|
beforeEach(async () => {
|
|
228
|
-
cookieResponse = await setNonUserAccessCookie(
|
|
236
|
+
cookieResponse = await setNonUserAccessCookie(siteDomain);
|
|
229
237
|
});
|
|
230
238
|
itReturnsCookieResponse(true, ['newspaper', 'plussalt']);
|
|
231
239
|
itSetsVstoken2Cookie();
|
|
@@ -234,7 +242,7 @@ describe('setNonUserAccessCookie', () => {
|
|
|
234
242
|
describe('when getNonUserAccess is not successful', () => {
|
|
235
243
|
getNonUserAccessFails(['newspaper', 'plussalt']);
|
|
236
244
|
beforeEach(async () => {
|
|
237
|
-
cookieResponse = await setNonUserAccessCookie(
|
|
245
|
+
cookieResponse = await setNonUserAccessCookie(siteDomain);
|
|
238
246
|
});
|
|
239
247
|
itReturnsCookieResponse(false, ['newspaper', 'plussalt']);
|
|
240
248
|
itSetsNoCookies();
|
|
@@ -246,7 +254,7 @@ describe('setAccessCookies', () => {
|
|
|
246
254
|
getUserAccessSucceeds(['newspaper', 'plussalt']);
|
|
247
255
|
getNonUserAccessSucceeds(['sport', 'pluss', 'newspaper']);
|
|
248
256
|
beforeEach(async () => {
|
|
249
|
-
cookieResponse = await setAccessCookies(
|
|
257
|
+
cookieResponse = await setAccessCookies(siteDomain);
|
|
250
258
|
});
|
|
251
259
|
itReturnsCookieResponse(true, ['newspaper', 'pluss', 'plussalt', 'sport']);
|
|
252
260
|
itSetsVstokenCookie();
|
|
@@ -257,7 +265,7 @@ describe('setAccessCookies', () => {
|
|
|
257
265
|
getUserAccessFails(['newspaper']);
|
|
258
266
|
getNonUserAccessSucceeds(['sport', 'pluss', 'newspaper']);
|
|
259
267
|
beforeEach(async () => {
|
|
260
|
-
cookieResponse = await setAccessCookies(
|
|
268
|
+
cookieResponse = await setAccessCookies(siteDomain);
|
|
261
269
|
});
|
|
262
270
|
itReturnsCookieResponse(false, ['newspaper', 'pluss', 'sport']);
|
|
263
271
|
itDoesNotSetVstokenCookie();
|
|
@@ -268,7 +276,7 @@ describe('setAccessCookies', () => {
|
|
|
268
276
|
getUserAccessSucceeds(['newspaper', 'plussalt']);
|
|
269
277
|
getNonUserAccessFails(['newspaper']);
|
|
270
278
|
beforeEach(async () => {
|
|
271
|
-
cookieResponse = await setAccessCookies(
|
|
279
|
+
cookieResponse = await setAccessCookies(siteDomain);
|
|
272
280
|
});
|
|
273
281
|
itReturnsCookieResponse(false, ['newspaper', 'plussalt']);
|
|
274
282
|
itSetsVstokenCookie();
|
|
@@ -279,7 +287,7 @@ describe('setAccessCookies', () => {
|
|
|
279
287
|
getUserAccessFails(['newspaper', 'plussalt']);
|
|
280
288
|
getNonUserAccessFails(['newspaper']);
|
|
281
289
|
beforeEach(async () => {
|
|
282
|
-
cookieResponse = await setAccessCookies(
|
|
290
|
+
cookieResponse = await setAccessCookies(siteDomain);
|
|
283
291
|
});
|
|
284
292
|
itReturnsCookieResponse(false, ['newspaper', 'plussalt']);
|
|
285
293
|
itSetsNoCookies();
|
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
getNonUserAccess,
|
|
4
4
|
getUserAccess,
|
|
5
5
|
} from '../clients/jupiter';
|
|
6
|
-
import {
|
|
6
|
+
import { getCurrentSiteDomain, getCurrentUrl } from '../utils/url';
|
|
7
7
|
import { UserAttributes } from '../types';
|
|
8
8
|
import { activateSubscription, UserDeactivatedError } from '../clients/mercury';
|
|
9
9
|
import { logout } from './authentication';
|
|
@@ -14,11 +14,14 @@ type SetCookieResponse = { success: boolean; accessFeatures: string[] };
|
|
|
14
14
|
export const getAccess = (
|
|
15
15
|
accessFeatures: string[] = ['newspaper']
|
|
16
16
|
): Promise<Pick<UserAttributes, 'access'>> =>
|
|
17
|
-
checkForAccess(
|
|
17
|
+
checkForAccess(getCurrentSiteDomain(), accessFeatures);
|
|
18
18
|
|
|
19
|
-
export const activate = async (
|
|
19
|
+
export const activate = async (
|
|
20
|
+
accessFeatures: string[],
|
|
21
|
+
siteDomain: string
|
|
22
|
+
): Promise<string[]> => {
|
|
20
23
|
try {
|
|
21
|
-
return await activateSubscription(accessFeatures);
|
|
24
|
+
return await activateSubscription(accessFeatures, siteDomain);
|
|
22
25
|
} catch (error) {
|
|
23
26
|
if (error instanceof UserDeactivatedError) {
|
|
24
27
|
await logout();
|
|
@@ -38,9 +41,9 @@ export const hasAccess = (
|
|
|
38
41
|
).length > 0;
|
|
39
42
|
|
|
40
43
|
export const setUserAccessCookie = async (
|
|
41
|
-
|
|
44
|
+
siteDomain: string
|
|
42
45
|
): Promise<SetCookieResponse> => {
|
|
43
|
-
const vstoken = await getUserAccess(
|
|
46
|
+
const vstoken = await getUserAccess(siteDomain);
|
|
44
47
|
if (vstoken.status === 'success') {
|
|
45
48
|
/**
|
|
46
49
|
* The VSTOKEN cookie will live for 365 days, but a timestamp in the payload will ensure that it cannot
|
|
@@ -56,9 +59,9 @@ export const setUserAccessCookie = async (
|
|
|
56
59
|
};
|
|
57
60
|
|
|
58
61
|
export const setNonUserAccessCookie = async (
|
|
59
|
-
|
|
62
|
+
siteDomain: string
|
|
60
63
|
): Promise<SetCookieResponse> => {
|
|
61
|
-
const vstoken2 = await getNonUserAccess(
|
|
64
|
+
const vstoken2 = await getNonUserAccess(siteDomain);
|
|
62
65
|
if (vstoken2.status === 'success') {
|
|
63
66
|
setCookie('VSTOKEN2', vstoken2.vstoken_cookie, 1, '/');
|
|
64
67
|
}
|
|
@@ -69,16 +72,16 @@ export const setNonUserAccessCookie = async (
|
|
|
69
72
|
};
|
|
70
73
|
|
|
71
74
|
export const setAccessCookies = async (
|
|
72
|
-
|
|
75
|
+
siteDomain: string
|
|
73
76
|
): Promise<SetCookieResponse> => {
|
|
74
77
|
const setters: Array<Promise<SetCookieResponse>> = [];
|
|
75
78
|
setters.push(
|
|
76
|
-
setUserAccessCookie(
|
|
79
|
+
setUserAccessCookie(siteDomain).catch(() =>
|
|
77
80
|
Promise.resolve({ success: false, accessFeatures: [] })
|
|
78
81
|
)
|
|
79
82
|
);
|
|
80
83
|
setters.push(
|
|
81
|
-
setNonUserAccessCookie(
|
|
84
|
+
setNonUserAccessCookie(siteDomain).catch(() =>
|
|
82
85
|
Promise.resolve({ success: false, accessFeatures: [] })
|
|
83
86
|
)
|
|
84
87
|
);
|
package/src/actions/userData.ts
CHANGED
|
@@ -7,13 +7,13 @@ import { getPrivacyPreferences } from './preferences';
|
|
|
7
7
|
import store from '../store';
|
|
8
8
|
import { getInfo } from '../clients/nebula';
|
|
9
9
|
import { whenIsLoggedIn, whenLoginStateResolved } from '../utils/loginState';
|
|
10
|
-
import {
|
|
10
|
+
import { getCurrentSiteDomain } from '../utils/url';
|
|
11
11
|
|
|
12
12
|
const getExtraData = () =>
|
|
13
13
|
Promise.all([getBrowserId(), whenLoginStateResolved()]).then(
|
|
14
14
|
([browserId, isLoggedIn]) =>
|
|
15
15
|
getInfo(
|
|
16
|
-
|
|
16
|
+
getCurrentSiteDomain(),
|
|
17
17
|
browserId,
|
|
18
18
|
isLoggedIn
|
|
19
19
|
? store.get('userAttributes')?.trackingKey || undefined
|
|
@@ -21,7 +21,7 @@ beforeEach(async () => {
|
|
|
21
21
|
|
|
22
22
|
describe('jupiter service', () => {
|
|
23
23
|
describe('GET /check_access_for_paper', () => {
|
|
24
|
-
const
|
|
24
|
+
const siteDomain = 'www.rb.no';
|
|
25
25
|
const accessFeatures: AccessFeatures = ['newspaper'];
|
|
26
26
|
|
|
27
27
|
it('should check access for paper', async () => {
|
|
@@ -37,14 +37,14 @@ describe('jupiter service', () => {
|
|
|
37
37
|
|
|
38
38
|
fetch.mockResponseOnce(JSON.stringify(response));
|
|
39
39
|
|
|
40
|
-
await expect(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
await expect(checkForAccess(siteDomain, accessFeatures)).resolves.toEqual(
|
|
41
|
+
{
|
|
42
|
+
access: ['amedia_staff', 'newspaper'],
|
|
43
|
+
}
|
|
44
|
+
);
|
|
45
45
|
|
|
46
46
|
expect(fetch).toHaveBeenCalledWith(
|
|
47
|
-
`/api/jupiter/v1/check_access_for_paper/${
|
|
47
|
+
`/api/jupiter/v1/check_access_for_paper/${siteDomain}/?require=any&access_features=${accessFeatures.join(
|
|
48
48
|
','
|
|
49
49
|
)}`
|
|
50
50
|
);
|
|
@@ -56,7 +56,7 @@ describe('jupiter service', () => {
|
|
|
56
56
|
});
|
|
57
57
|
|
|
58
58
|
await expect(
|
|
59
|
-
checkForAccess(
|
|
59
|
+
checkForAccess(siteDomain, accessFeatures)
|
|
60
60
|
).rejects.toBeUndefined();
|
|
61
61
|
});
|
|
62
62
|
|
|
@@ -65,7 +65,7 @@ describe('jupiter service', () => {
|
|
|
65
65
|
log.error = jest.fn();
|
|
66
66
|
|
|
67
67
|
await expect(
|
|
68
|
-
checkForAccess(
|
|
68
|
+
checkForAccess(siteDomain, accessFeatures)
|
|
69
69
|
).rejects.toBeUndefined();
|
|
70
70
|
});
|
|
71
71
|
|
|
@@ -82,7 +82,7 @@ describe('jupiter service', () => {
|
|
|
82
82
|
})
|
|
83
83
|
);
|
|
84
84
|
const cacheSetSpy = spyOn(cache, 'set');
|
|
85
|
-
expect(checkForAccess(
|
|
85
|
+
expect(checkForAccess(siteDomain, accessFeatures))
|
|
86
86
|
.resolves.toEqual({ access: [] })
|
|
87
87
|
.then(() => {
|
|
88
88
|
expect(cacheSetSpy).toHaveBeenCalledWith(
|
|
@@ -96,7 +96,7 @@ describe('jupiter service', () => {
|
|
|
96
96
|
});
|
|
97
97
|
|
|
98
98
|
describe('POST /get_vstoken', () => {
|
|
99
|
-
const
|
|
99
|
+
const siteDomain = 'www.rb.no';
|
|
100
100
|
|
|
101
101
|
it('should return vstoken', async () => {
|
|
102
102
|
const response: GetVstoken = {
|
|
@@ -107,10 +107,10 @@ describe('jupiter service', () => {
|
|
|
107
107
|
|
|
108
108
|
fetch.mockResponseOnce(JSON.stringify(response));
|
|
109
109
|
|
|
110
|
-
await expect(getUserAccess(
|
|
110
|
+
await expect(getUserAccess(siteDomain)).resolves.toEqual(response);
|
|
111
111
|
|
|
112
112
|
expect(fetch).toHaveBeenCalledWith(
|
|
113
|
-
`/api/jupiter/v1/get_vstoken/${
|
|
113
|
+
`/api/jupiter/v1/get_vstoken/${siteDomain}`,
|
|
114
114
|
{
|
|
115
115
|
method: 'post',
|
|
116
116
|
}
|
|
@@ -122,7 +122,7 @@ describe('jupiter service', () => {
|
|
|
122
122
|
status: 400,
|
|
123
123
|
});
|
|
124
124
|
|
|
125
|
-
await expect(getUserAccess(
|
|
125
|
+
await expect(getUserAccess(siteDomain)).rejects.toThrow(
|
|
126
126
|
'Request not ok (400: Bad Request).'
|
|
127
127
|
);
|
|
128
128
|
});
|
|
@@ -131,14 +131,14 @@ describe('jupiter service', () => {
|
|
|
131
131
|
fetch.mockResponseOnce(JSON.stringify({ access_features: 'foo' }));
|
|
132
132
|
log.error = jest.fn();
|
|
133
133
|
|
|
134
|
-
await expect(getUserAccess(
|
|
134
|
+
await expect(getUserAccess(siteDomain)).rejects.toThrow(
|
|
135
135
|
'Expected { status: "success"; cookie: string; access_features: string[]; }, but was incompatible'
|
|
136
136
|
);
|
|
137
137
|
});
|
|
138
138
|
});
|
|
139
139
|
|
|
140
140
|
describe('POST /non_user_access', () => {
|
|
141
|
-
const
|
|
141
|
+
const siteDomain = 'www.rb.no';
|
|
142
142
|
|
|
143
143
|
it('should return none user access', async () => {
|
|
144
144
|
const response: GetNonUserAccess = {
|
|
@@ -150,12 +150,10 @@ describe('jupiter service', () => {
|
|
|
150
150
|
|
|
151
151
|
fetch.mockResponseOnce(JSON.stringify(response));
|
|
152
152
|
|
|
153
|
-
await expect(getNonUserAccess(
|
|
154
|
-
response
|
|
155
|
-
);
|
|
153
|
+
await expect(getNonUserAccess(siteDomain)).resolves.toEqual(response);
|
|
156
154
|
|
|
157
155
|
expect(fetch).toHaveBeenCalledWith(
|
|
158
|
-
`/api/jupiter/v1/non_user_access/${
|
|
156
|
+
`/api/jupiter/v1/non_user_access/${siteDomain}`,
|
|
159
157
|
{ method: 'post' }
|
|
160
158
|
);
|
|
161
159
|
});
|
|
@@ -165,7 +163,7 @@ describe('jupiter service', () => {
|
|
|
165
163
|
status: 400,
|
|
166
164
|
});
|
|
167
165
|
|
|
168
|
-
await expect(getNonUserAccess(
|
|
166
|
+
await expect(getNonUserAccess(siteDomain)).rejects.toThrow(
|
|
169
167
|
'Request not ok (400: Bad Request).'
|
|
170
168
|
);
|
|
171
169
|
});
|
|
@@ -174,7 +172,7 @@ describe('jupiter service', () => {
|
|
|
174
172
|
fetch.mockResponseOnce(JSON.stringify({ vstoken_cookie: 3 }));
|
|
175
173
|
log.error = jest.fn();
|
|
176
174
|
|
|
177
|
-
await expect(getNonUserAccess(
|
|
175
|
+
await expect(getNonUserAccess(siteDomain)).rejects.toThrow(
|
|
178
176
|
'Expected { status: "success"; vstoken_cookie: string; uuid: string; access_features: string[]; }, but was incompatible'
|
|
179
177
|
);
|
|
180
178
|
});
|
package/src/clients/jupiter.ts
CHANGED
|
@@ -57,12 +57,12 @@ const GetNonUserAccessSchema = Record({
|
|
|
57
57
|
export type GetNonUserAccess = Static<typeof GetNonUserAccessSchema>;
|
|
58
58
|
|
|
59
59
|
export const checkForAccess = (
|
|
60
|
-
|
|
60
|
+
siteDomain: string,
|
|
61
61
|
accessFeatures: AccessFeatures
|
|
62
62
|
): Promise<Pick<UserAttributes, 'access'>> =>
|
|
63
63
|
getOrUpdate('checkForAccess', CheckAccessForPaperSchema, () =>
|
|
64
64
|
fetch(
|
|
65
|
-
`/api/jupiter/v1/check_access_for_paper/${
|
|
65
|
+
`/api/jupiter/v1/check_access_for_paper/${siteDomain}/?require=any&access_features=${accessFeatures.join(
|
|
66
66
|
','
|
|
67
67
|
)}`
|
|
68
68
|
)
|
|
@@ -89,8 +89,8 @@ export const checkForAccess = (
|
|
|
89
89
|
})
|
|
90
90
|
).then((data) => ({ access: data.access_features }));
|
|
91
91
|
|
|
92
|
-
export const getUserAccess = (
|
|
93
|
-
fetch(`/api/jupiter/v1/get_vstoken/${
|
|
92
|
+
export const getUserAccess = (siteDomain: string): Promise<GetVstoken> =>
|
|
93
|
+
fetch(`/api/jupiter/v1/get_vstoken/${siteDomain}`, { method: 'post' })
|
|
94
94
|
.then((response) => {
|
|
95
95
|
if (!response.ok) {
|
|
96
96
|
throw new Error(
|
|
@@ -145,9 +145,9 @@ export const getSitesWithAccessFeatures = ({
|
|
|
145
145
|
});
|
|
146
146
|
|
|
147
147
|
export const getNonUserAccess = (
|
|
148
|
-
|
|
148
|
+
siteDomain: string
|
|
149
149
|
): Promise<GetNonUserAccess> =>
|
|
150
|
-
fetch(`/api/jupiter/v1/non_user_access/${
|
|
150
|
+
fetch(`/api/jupiter/v1/non_user_access/${siteDomain}`, {
|
|
151
151
|
method: 'post',
|
|
152
152
|
})
|
|
153
153
|
.then((response) => {
|
|
@@ -6,16 +6,18 @@ describe('activateSubscription()', () => {
|
|
|
6
6
|
fetch.mockResponse(JSON.stringify({ status: 'user_deactivated' }), {
|
|
7
7
|
status: 401,
|
|
8
8
|
});
|
|
9
|
-
await expect(
|
|
10
|
-
|
|
11
|
-
);
|
|
9
|
+
await expect(
|
|
10
|
+
activateSubscription(['newspaper'], 'www.telen.no')
|
|
11
|
+
).rejects.toThrow(UserDeactivatedError);
|
|
12
12
|
});
|
|
13
13
|
|
|
14
14
|
it('should return empty list if user not found', async () => {
|
|
15
15
|
fetch.mockResponse(JSON.stringify({ status: 'no_session_found' }), {
|
|
16
16
|
status: 401,
|
|
17
17
|
});
|
|
18
|
-
await expect(
|
|
18
|
+
await expect(
|
|
19
|
+
activateSubscription(['newspaper'], 'www.telen.no')
|
|
20
|
+
).resolves.toEqual([]);
|
|
19
21
|
});
|
|
20
22
|
|
|
21
23
|
it('should return list if access features even if requested access is not "granted"', async () => {
|
|
@@ -26,9 +28,9 @@ describe('activateSubscription()', () => {
|
|
|
26
28
|
access_features: ['newspaper'],
|
|
27
29
|
})
|
|
28
30
|
);
|
|
29
|
-
await expect(
|
|
30
|
-
'newspaper',
|
|
31
|
-
]);
|
|
31
|
+
await expect(
|
|
32
|
+
activateSubscription(['newspaper'], 'www.telen.no')
|
|
33
|
+
).resolves.toEqual(['newspaper']);
|
|
32
34
|
});
|
|
33
35
|
|
|
34
36
|
it('should return list of access features even if status is not "success"', async () => {
|
|
@@ -39,9 +41,9 @@ describe('activateSubscription()', () => {
|
|
|
39
41
|
access_features: ['newspaper'],
|
|
40
42
|
})
|
|
41
43
|
);
|
|
42
|
-
await expect(
|
|
43
|
-
'newspaper',
|
|
44
|
-
]);
|
|
44
|
+
await expect(
|
|
45
|
+
activateSubscription(['newspaper'], 'www.telen.no')
|
|
46
|
+
).resolves.toEqual(['newspaper']);
|
|
45
47
|
});
|
|
46
48
|
|
|
47
49
|
it('should return ["newspaper"] if status is "success" and access is "granted"', async () => {
|
|
@@ -52,8 +54,8 @@ describe('activateSubscription()', () => {
|
|
|
52
54
|
access_features: ['newspaper'],
|
|
53
55
|
})
|
|
54
56
|
);
|
|
55
|
-
await expect(
|
|
56
|
-
'newspaper',
|
|
57
|
-
]);
|
|
57
|
+
await expect(
|
|
58
|
+
activateSubscription(['newspaper'], 'www.telen.no')
|
|
59
|
+
).resolves.toEqual(['newspaper']);
|
|
58
60
|
});
|
|
59
61
|
});
|
package/src/clients/mercury.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import log from '../utils/log';
|
|
2
|
-
import { getCurrentPublicationDomain } from '../utils/url';
|
|
3
2
|
|
|
4
3
|
export class UserDeactivatedError extends Error {}
|
|
5
4
|
|
|
@@ -10,10 +9,11 @@ const EXPECTED_ERRORS = [
|
|
|
10
9
|
];
|
|
11
10
|
|
|
12
11
|
export const activateSubscription = async (
|
|
13
|
-
accessFeatures: string[]
|
|
12
|
+
accessFeatures: string[],
|
|
13
|
+
siteDomain: string
|
|
14
14
|
): Promise<string[]> =>
|
|
15
15
|
fetch(
|
|
16
|
-
`/api/mercury/v2/access/activate?publication_wwwdomain=${
|
|
16
|
+
`/api/mercury/v2/access/activate?publication_wwwdomain=${siteDomain}&access_features=${accessFeatures.join(
|
|
17
17
|
','
|
|
18
18
|
)}`,
|
|
19
19
|
{
|
|
@@ -10,7 +10,7 @@ describe('nebula service', () => {
|
|
|
10
10
|
|
|
11
11
|
describe('GET /info', () => {
|
|
12
12
|
const userKey = '9ea9680f-c810-4693-a981-ca8e732a8a8f';
|
|
13
|
-
const
|
|
13
|
+
const siteDomain = 'www.rb.no';
|
|
14
14
|
const browserId = 'fc0bce06-560b-48e4-92f2-840e67178516';
|
|
15
15
|
|
|
16
16
|
it('should return user information', async () => {
|
|
@@ -35,12 +35,12 @@ describe('nebula service', () => {
|
|
|
35
35
|
|
|
36
36
|
fetch.mockResponseOnce(JSON.stringify(response));
|
|
37
37
|
|
|
38
|
-
await expect(
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
await expect(getInfo(siteDomain, browserId, userKey)).resolves.toEqual({
|
|
39
|
+
extraData: response,
|
|
40
|
+
});
|
|
41
41
|
|
|
42
42
|
expect(fetch).toHaveBeenCalledWith(
|
|
43
|
-
`https://services.gcloud.api.no/api/nebula/v1/info?domain=${
|
|
43
|
+
`https://services.gcloud.api.no/api/nebula/v1/info?domain=${siteDomain}&browser_id=${browserId}&user_key=${userKey}`
|
|
44
44
|
);
|
|
45
45
|
});
|
|
46
46
|
|
|
@@ -49,7 +49,7 @@ describe('nebula service', () => {
|
|
|
49
49
|
log.error = jest.fn();
|
|
50
50
|
|
|
51
51
|
await expect(
|
|
52
|
-
getInfo(userKey,
|
|
52
|
+
getInfo(userKey, siteDomain, browserId)
|
|
53
53
|
).rejects.toBeUndefined();
|
|
54
54
|
});
|
|
55
55
|
|
|
@@ -62,9 +62,9 @@ describe('nebula service', () => {
|
|
|
62
62
|
|
|
63
63
|
fetch.mockResponseOnce(JSON.stringify(response));
|
|
64
64
|
|
|
65
|
-
await expect(
|
|
66
|
-
|
|
67
|
-
|
|
65
|
+
await expect(getInfo(userKey, siteDomain, browserId)).resolves.toEqual({
|
|
66
|
+
extraData: response,
|
|
67
|
+
});
|
|
68
68
|
});
|
|
69
69
|
});
|
|
70
70
|
});
|
package/src/clients/nebula.ts
CHANGED
|
@@ -35,12 +35,12 @@ const InfoSchema = Dictionary(Unknown, String).And(
|
|
|
35
35
|
);
|
|
36
36
|
|
|
37
37
|
const getNebulaData = (
|
|
38
|
-
|
|
38
|
+
siteDomain: string,
|
|
39
39
|
browserId: string,
|
|
40
40
|
trackingKey?: string
|
|
41
41
|
) => {
|
|
42
42
|
const url = new URL('https://services.gcloud.api.no/api/nebula/v1/info');
|
|
43
|
-
url.searchParams.set('domain',
|
|
43
|
+
url.searchParams.set('domain', siteDomain);
|
|
44
44
|
url.searchParams.set('browser_id', browserId);
|
|
45
45
|
if (trackingKey) {
|
|
46
46
|
/**
|
|
@@ -74,10 +74,10 @@ const mapNebulaToUserData = (
|
|
|
74
74
|
});
|
|
75
75
|
|
|
76
76
|
export const getInfo = (
|
|
77
|
-
|
|
77
|
+
siteDomain: string,
|
|
78
78
|
browserId: string,
|
|
79
79
|
trackingKey?: string
|
|
80
80
|
): Promise<Pick<UserAttributes, 'extraData'>> =>
|
|
81
81
|
getOrUpdate('getNebulaInfo', InfoSchema, () =>
|
|
82
|
-
getNebulaData(
|
|
82
|
+
getNebulaData(siteDomain, browserId, trackingKey)
|
|
83
83
|
).then(mapNebulaToUserData);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
|
|
2
2
|
import { activate } from '../actions/authorization';
|
|
3
3
|
import getGlobalThis from '../utils/getGlobalThis';
|
|
4
|
+
import { getCurrentSiteDomain } from '../utils/url';
|
|
4
5
|
|
|
5
6
|
export enum ActivationEvents {
|
|
6
7
|
REQUEST_ACTIVATION = 'amedia-user.activation.request-activation',
|
|
@@ -11,6 +12,7 @@ declare global {
|
|
|
11
12
|
interface WindowEventMap {
|
|
12
13
|
[ActivationEvents.REQUEST_ACTIVATION]: CustomEvent<{
|
|
13
14
|
accessFeatures: string[];
|
|
15
|
+
siteDomain?: string;
|
|
14
16
|
}>;
|
|
15
17
|
[ActivationEvents.ON_ACTIVATION_COMPLETE]: CustomEvent<{
|
|
16
18
|
accessFeatures: string[];
|
|
@@ -20,7 +22,10 @@ declare global {
|
|
|
20
22
|
|
|
21
23
|
export const listenForActivationRequests = () => {
|
|
22
24
|
listenWithExclusivity(ActivationEvents.REQUEST_ACTIVATION, (evt) => {
|
|
23
|
-
activate(
|
|
25
|
+
activate(
|
|
26
|
+
evt.detail.accessFeatures,
|
|
27
|
+
evt.detail.siteDomain || getCurrentSiteDomain()
|
|
28
|
+
).then((accessFeatures) => {
|
|
24
29
|
dispatch(
|
|
25
30
|
new CustomEvent(ActivationEvents.ON_ACTIVATION_COMPLETE, {
|
|
26
31
|
detail: { accessFeatures },
|
package/src/index.ts
CHANGED
|
@@ -21,6 +21,9 @@ export { default as PaywallError } from './exceptions/PaywallError';
|
|
|
21
21
|
export const goToLoginPage = (requestedUrl?: string) =>
|
|
22
22
|
authentication.login(requestedUrl || getCurrentUrl());
|
|
23
23
|
|
|
24
|
+
export const getLoginUrl = (requestedUrl?: string) =>
|
|
25
|
+
authentication.loginUrl(requestedUrl || getCurrentUrl());
|
|
26
|
+
|
|
24
27
|
export const logout = () => authentication.logout();
|
|
25
28
|
|
|
26
29
|
export const updateStorage = (storage: { [index: string]: Object }) => {
|
|
@@ -10,7 +10,7 @@ import { AccessFeatures } from '../../clients/jupiter';
|
|
|
10
10
|
const mockUuid = '3-1-33-7';
|
|
11
11
|
|
|
12
12
|
beforeEach(() => {
|
|
13
|
-
spyOn(url, '
|
|
13
|
+
spyOn(url, 'getCurrentSiteDomain').mockReturnValue('www.avisnavn.no');
|
|
14
14
|
url.setCurrentHostname('www.avisnavn.no');
|
|
15
15
|
jest
|
|
16
16
|
.spyOn(authorization, 'setUserAccessCookie')
|
|
@@ -4,13 +4,13 @@ import {
|
|
|
4
4
|
setNonUserAccessCookie,
|
|
5
5
|
setUserAccessCookie,
|
|
6
6
|
} from '../../actions/authorization';
|
|
7
|
-
import {
|
|
7
|
+
import { getCurrentSiteDomain } from '../../utils/url';
|
|
8
8
|
|
|
9
9
|
const validCookieGenerator = 'jupiter';
|
|
10
10
|
|
|
11
11
|
async function verifyAndSetUserAccessCookie() {
|
|
12
12
|
const cookieName = 'VSTOKEN';
|
|
13
|
-
const
|
|
13
|
+
const siteDomain = getCurrentSiteDomain();
|
|
14
14
|
const cookie = getCookie(cookieName);
|
|
15
15
|
|
|
16
16
|
new UserDataRequest()
|
|
@@ -27,7 +27,7 @@ async function verifyAndSetUserAccessCookie() {
|
|
|
27
27
|
|
|
28
28
|
if (typeof cookie !== 'string') {
|
|
29
29
|
// We want to set a VSTOKEN cookie for the user so it's ready upon next page load
|
|
30
|
-
await setUserAccessCookie(
|
|
30
|
+
await setUserAccessCookie(siteDomain);
|
|
31
31
|
return;
|
|
32
32
|
}
|
|
33
33
|
|
|
@@ -44,18 +44,18 @@ async function verifyAndSetUserAccessCookie() {
|
|
|
44
44
|
if (
|
|
45
45
|
validTo.getTime() > Date.now() &&
|
|
46
46
|
cookieParts[3] === attributes.uuid &&
|
|
47
|
-
|
|
47
|
+
siteDomain.includes(cookieParts[4]) &&
|
|
48
48
|
cookieParts[5] === validCookieGenerator
|
|
49
49
|
) {
|
|
50
50
|
return; // Cookie is valid for more than 24 hours, keep it as it is
|
|
51
51
|
}
|
|
52
|
-
await setUserAccessCookie(
|
|
52
|
+
await setUserAccessCookie(siteDomain);
|
|
53
53
|
});
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
function verifyAndSetNonUserAccessCookie() {
|
|
57
57
|
const cookieName = 'VSTOKEN2';
|
|
58
|
-
const
|
|
58
|
+
const siteDomain = getCurrentSiteDomain();
|
|
59
59
|
const cookie = getCookie(cookieName);
|
|
60
60
|
|
|
61
61
|
new UserDataRequest()
|
|
@@ -63,7 +63,7 @@ function verifyAndSetNonUserAccessCookie() {
|
|
|
63
63
|
.then(async ({ state: { isLoggedIn } }) => {
|
|
64
64
|
if (isLoggedIn) {
|
|
65
65
|
if (typeof cookie !== 'string') {
|
|
66
|
-
await setNonUserAccessCookie(
|
|
66
|
+
await setNonUserAccessCookie(siteDomain);
|
|
67
67
|
return;
|
|
68
68
|
}
|
|
69
69
|
/* The format of the cookie is:
|
|
@@ -77,12 +77,12 @@ function verifyAndSetNonUserAccessCookie() {
|
|
|
77
77
|
// Make sure that current cookie is not too old and is created for this domain by jupiter
|
|
78
78
|
if (
|
|
79
79
|
validTo.getTime() > Date.now() &&
|
|
80
|
-
|
|
80
|
+
siteDomain.includes(cookieParts[4]) &&
|
|
81
81
|
cookieParts[5] === validCookieGenerator
|
|
82
82
|
) {
|
|
83
83
|
return;
|
|
84
84
|
}
|
|
85
|
-
await setNonUserAccessCookie(
|
|
85
|
+
await setNonUserAccessCookie(siteDomain);
|
|
86
86
|
}
|
|
87
87
|
})
|
|
88
88
|
.then(() => {});
|
|
@@ -41,9 +41,15 @@ function itShouldNotAttemptActivation() {
|
|
|
41
41
|
});
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
function itShouldAttemptActivationWithAccessFeatures(
|
|
44
|
+
function itShouldAttemptActivationWithAccessFeatures(
|
|
45
|
+
accessFeatures: string[],
|
|
46
|
+
siteDomain: string = 'localhost'
|
|
47
|
+
) {
|
|
45
48
|
it('should attempt activation with access features', () => {
|
|
46
|
-
expect(authorization.activate).toHaveBeenCalledWith(
|
|
49
|
+
expect(authorization.activate).toHaveBeenCalledWith(
|
|
50
|
+
accessFeatures,
|
|
51
|
+
siteDomain
|
|
52
|
+
);
|
|
47
53
|
});
|
|
48
54
|
}
|
|
49
55
|
|
|
@@ -257,6 +263,29 @@ describe('PaywallUnlocker', () => {
|
|
|
257
263
|
itShouldAttemptActivationWithAccessFeatures(['newspaper']);
|
|
258
264
|
itShouldNotRedirect();
|
|
259
265
|
});
|
|
266
|
+
|
|
267
|
+
describe('with specific siteDomain', () => {
|
|
268
|
+
beforeEach(async () => {
|
|
269
|
+
unlockResponse = await new PaywallUnlockRequest()
|
|
270
|
+
.withSiteDomain('www.telen.no')
|
|
271
|
+
.tryUnlock();
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('should give access with access features', () => {
|
|
275
|
+
expect(unlockResponse).toEqual({
|
|
276
|
+
isLoggedIn: true,
|
|
277
|
+
hasAccess: true,
|
|
278
|
+
accessFeatures: ['newspaper'],
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
itShouldAttemptActivationWithAccessFeatures(
|
|
283
|
+
['newspaper'],
|
|
284
|
+
'www.telen.no'
|
|
285
|
+
);
|
|
286
|
+
itShouldNotReload();
|
|
287
|
+
itShouldNotRedirect();
|
|
288
|
+
});
|
|
260
289
|
});
|
|
261
290
|
|
|
262
291
|
describe('and activation does not give access', () => {
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
setAccessCookies,
|
|
6
6
|
setHasPerformedPaywallReload,
|
|
7
7
|
} from '../actions/authorization';
|
|
8
|
-
import {
|
|
8
|
+
import { getCurrentSiteDomain } from '../utils/url';
|
|
9
9
|
import * as location from '../utils/location';
|
|
10
10
|
import PaywallError from '../exceptions/PaywallError';
|
|
11
11
|
import UserDataRequest from './UserDataRequest';
|
|
@@ -50,6 +50,13 @@ class PaywallUnlockRequest {
|
|
|
50
50
|
|
|
51
51
|
private requestedUrl: string | undefined;
|
|
52
52
|
|
|
53
|
+
private siteDomain: string = getCurrentSiteDomain();
|
|
54
|
+
|
|
55
|
+
public withSiteDomain(domain: string): this {
|
|
56
|
+
this.siteDomain = domain;
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
|
|
53
60
|
public withAccessFeatures(accessFeatures: string[]): this {
|
|
54
61
|
this.requestedAccessFeatures = accessFeatures;
|
|
55
62
|
return this;
|
|
@@ -139,7 +146,7 @@ class PaywallUnlockRequest {
|
|
|
139
146
|
private setAccessCookiesAndGetAccessFeatures(
|
|
140
147
|
requestedAccessFeatures: AccessFeatures
|
|
141
148
|
) {
|
|
142
|
-
return setAccessCookies(
|
|
149
|
+
return setAccessCookies(getCurrentSiteDomain()).then((response) => {
|
|
143
150
|
const accessDenied = !hasAccess(
|
|
144
151
|
response.accessFeatures,
|
|
145
152
|
requestedAccessFeatures
|
|
@@ -158,7 +165,10 @@ class PaywallUnlockRequest {
|
|
|
158
165
|
}
|
|
159
166
|
|
|
160
167
|
private async activateAccess(requestedAccessFeatures: AccessFeatures) {
|
|
161
|
-
const accessFeatures = await activate(
|
|
168
|
+
const accessFeatures = await activate(
|
|
169
|
+
this.requestedAccessFeatures,
|
|
170
|
+
this.siteDomain
|
|
171
|
+
);
|
|
162
172
|
return {
|
|
163
173
|
denied: !hasAccess(accessFeatures, requestedAccessFeatures),
|
|
164
174
|
accessFeatures,
|
package/src/utils/debouncer.ts
CHANGED
|
@@ -26,7 +26,7 @@ function deepCopy<T>(obj: T): T {
|
|
|
26
26
|
* resolve my promise', but instead of the fetch being performed right away, we want to wait for more users to
|
|
27
27
|
* request other stuff. This will keep us from performing lots of individual fetches to the backend.
|
|
28
28
|
*
|
|
29
|
-
* Our main use case is a standard
|
|
29
|
+
* Our main use case is a standard site front page where one component on the page requests the euucp namespace
|
|
30
30
|
* from Europa, and then another component requests the privacy_preferences namespace. Instead of the Europa client
|
|
31
31
|
* calling two fetches to the server, one for euucp and another for privacy_preferences, we want the Debouncer to
|
|
32
32
|
* aggregate these calls into one call to the backend, that requests both euucp AND privacy_preferences, so the response
|
package/src/utils/url.spec.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getCurrentEnvironment,
|
|
3
|
-
|
|
3
|
+
getCurrentSiteDomain,
|
|
4
4
|
setCurrentHostname,
|
|
5
5
|
} from './url';
|
|
6
6
|
|
|
7
7
|
describe('URL utils', () => {
|
|
8
|
-
it('should get proper
|
|
8
|
+
it('should get proper site domain', () => {
|
|
9
9
|
const domains = [
|
|
10
10
|
'www.telen.no.localhost.api.no',
|
|
11
11
|
'www.telen.no.snap5.api.no',
|
|
@@ -14,7 +14,7 @@ describe('URL utils', () => {
|
|
|
14
14
|
expect.assertions(domains.length);
|
|
15
15
|
domains.forEach((domain) => {
|
|
16
16
|
setCurrentHostname(domain);
|
|
17
|
-
expect(
|
|
17
|
+
expect(getCurrentSiteDomain()).toEqual('www.telen.no');
|
|
18
18
|
});
|
|
19
19
|
});
|
|
20
20
|
|
package/src/utils/url.ts
CHANGED
|
@@ -22,7 +22,7 @@ export const getCurrentEnvironment = () => {
|
|
|
22
22
|
return null;
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
-
export const
|
|
25
|
+
export const getCurrentSiteDomain = () =>
|
|
26
26
|
getCurrentHostname().replace(/\.[^.]+\.api\.no/, '');
|
|
27
27
|
|
|
28
28
|
export const getAidDomain = (): string => {
|