@amedia/user 0.10.1 → 0.12.0
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/.eik/integrity.json +36 -0
- package/amedia-user-js.iml +9 -0
- package/dist/actions/authorization.d.ts +3 -1
- package/dist/actions/storage.d.ts +3 -0
- package/dist/autologin/index.d.ts +1 -0
- package/dist/autologin/thirdPartyCookie.d.ts +3 -0
- package/{esm/cache/engines/global.spec.d.ts → dist/autologin/thirdPartyCookie.spec.d.ts} +1 -1
- package/dist/eventHandlers/userAttributes.d.ts +4 -0
- package/{esm/cache/index.spec.d.ts → dist/index-storage.spec.d.ts} +4 -4
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -1
- package/dist/janitor/index.d.ts +10 -0
- package/dist/janitor/jobs/accessCookieRefresher.d.ts +1 -0
- package/{esm/clients/mercury.spec.d.ts → dist/janitor/jobs/accessCookieRefresher.spec.d.ts} +4 -4
- package/dist/types.d.ts +1 -0
- package/dist/utils/url.d.ts +3 -0
- package/eik.json +9 -0
- package/package.json +12 -2
- package/rollup.config.js +14 -0
- package/src/actions/authentication.ts +4 -9
- package/src/actions/authorization.ts +18 -5
- package/src/actions/storage.ts +6 -1
- package/src/actions/userData.ts +2 -1
- package/src/autologin/index.ts +7 -0
- package/src/autologin/thirdPartyCookie.spec.ts +108 -0
- package/src/autologin/thirdPartyCookie.ts +69 -0
- package/src/clients/mercury.ts +7 -3
- package/src/clients/pluto.spec.ts +1 -0
- package/src/clients/pluto.ts +2 -0
- package/src/eventHandlers/userAttributes.ts +8 -1
- package/src/index-storage.spec.ts +96 -0
- package/src/index-subscription.spec.ts +0 -54
- package/src/index.ts +11 -1
- package/src/janitor/index.ts +39 -0
- package/src/janitor/jobs/accessCookieRefresher.spec.ts +230 -0
- package/src/janitor/jobs/accessCookieRefresher.ts +96 -0
- package/src/types.ts +1 -0
- package/src/utils/url.ts +14 -1
- package/test.html +12 -0
- package/esm/actions/authentication.d.ts +0 -5
- package/esm/actions/authorization.d.ts +0 -7
- package/esm/actions/preferences.d.ts +0 -9
- package/esm/actions/preferences.spec.d.ts +0 -1
- package/esm/actions/storage.d.ts +0 -1
- package/esm/actions/userData.d.ts +0 -2
- package/esm/cache/engines/global.d.ts +0 -21
- package/esm/cache/engines/indexeddb.d.ts +0 -7
- package/esm/cache/index.d.ts +0 -11
- package/esm/cache/types.d.ts +0 -11
- package/esm/clients/europa.d.ts +0 -28
- package/esm/clients/europa.spec.d.ts +0 -1
- package/esm/clients/jupiter.d.ts +0 -30
- package/esm/clients/jupiter.spec.d.ts +0 -1
- package/esm/clients/mercury.d.ts +0 -18
- package/esm/clients/nebula.d.ts +0 -2
- package/esm/clients/nebula.spec.d.ts +0 -1
- package/esm/clients/pluto.d.ts +0 -3
- package/esm/clients/pluto.spec.d.ts +0 -1
- package/esm/eventHandlers/activation.d.ts +0 -15
- package/esm/eventHandlers/authentication.d.ts +0 -17
- package/esm/eventHandlers/userAttributes.d.ts +0 -20
- package/esm/exceptions/PaywallError.d.ts +0 -7
- package/esm/index-authentcation.spec.d.ts +0 -4
- package/esm/index-subscription.spec.d.ts +0 -4
- package/esm/index.d.ts +0 -23
- package/esm/lib/PaywallUnlocker.d.ts +0 -16
- package/esm/lib/PaywallUnlocker.spec.d.ts +0 -4
- package/esm/lib/User.d.ts +0 -21
- package/esm/lib/cookie.d.ts +0 -5
- package/esm/lib/rpc.d.ts +0 -6
- package/esm/store/index.d.ts +0 -22
- package/esm/store/index.spec.d.ts +0 -1
- package/esm/types.d.ts +0 -21
- package/esm/user.js +0 -2
- package/esm/user.js.map +0 -1
- package/esm/utils/customEventWrapper.d.ts +0 -17
- package/esm/utils/deepEquals.d.ts +0 -4
- package/esm/utils/getOrUpdate.d.ts +0 -6
- package/esm/utils/getOrUpdate.spec.d.ts +0 -1
- package/esm/utils/location.d.ts +0 -2
- package/esm/utils/log.d.ts +0 -26
- package/esm/utils/queue.d.ts +0 -12
- package/esm/utils/url.d.ts +0 -3
- package/esm/utils/url.spec.d.ts +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amedia/user",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Client lib for working with aID user and associated data",
|
|
5
5
|
"repository": "https://github.com/amedia/amedia-user-js",
|
|
6
6
|
"author": "Amedia Utvikling (ID og data)",
|
|
@@ -8,13 +8,15 @@
|
|
|
8
8
|
"scripts": {
|
|
9
9
|
"test": "jest --coverage",
|
|
10
10
|
"lint": "eslint ./src --ext .ts",
|
|
11
|
-
"build": "webpack --mode=production"
|
|
11
|
+
"build": "webpack --mode=production",
|
|
12
|
+
"build:rollup": "rollup -c"
|
|
12
13
|
},
|
|
13
14
|
"dependencies": {
|
|
14
15
|
"@amedia/browserid": "0.1.6",
|
|
15
16
|
"@amedia/frontend-rpc": "0.0.12",
|
|
16
17
|
"dexie": "3.0.3",
|
|
17
18
|
"regenerator-runtime": "0.13.7",
|
|
19
|
+
"rollup-plugin-uglify": "^6.0.4",
|
|
18
20
|
"runtypes": "6.2.1"
|
|
19
21
|
},
|
|
20
22
|
"devDependencies": {
|
|
@@ -23,6 +25,10 @@
|
|
|
23
25
|
"@babel/core": "7.14.0",
|
|
24
26
|
"@babel/preset-env": "7.14.1",
|
|
25
27
|
"@babel/preset-typescript": "7.13.0",
|
|
28
|
+
"@eik/cli": "1.8.30",
|
|
29
|
+
"@rollup/plugin-commonjs": "19.0.0",
|
|
30
|
+
"@rollup/plugin-node-resolve": "13.0.0",
|
|
31
|
+
"@rollup/plugin-typescript": "8.2.1",
|
|
26
32
|
"@types/convict": "6.0.1",
|
|
27
33
|
"@types/dexie": "1.3.1",
|
|
28
34
|
"@types/jest": "^26.0.23",
|
|
@@ -38,9 +44,13 @@
|
|
|
38
44
|
"mockdate": "3.0.5",
|
|
39
45
|
"npm-run-all": "4.1.5",
|
|
40
46
|
"prettier": "2.2.1",
|
|
47
|
+
"rollup": "2.51.0",
|
|
48
|
+
"rollup-plugin-peer-deps-external": "2.2.4",
|
|
49
|
+
"rollup-plugin-typescript2": "0.30.0",
|
|
41
50
|
"source-map-loader": "2.0.1",
|
|
42
51
|
"ts-jest": "^26.5.5",
|
|
43
52
|
"ts-loader": "9.1.0",
|
|
53
|
+
"tslib": "2.2.0",
|
|
44
54
|
"typescript": "4.2.4",
|
|
45
55
|
"webpack": "5.36.2",
|
|
46
56
|
"webpack-cli": "4.6.0"
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import resolve from '@rollup/plugin-node-resolve';
|
|
2
|
+
import commonjs from '@rollup/plugin-commonjs';
|
|
3
|
+
import typescript from 'rollup-plugin-typescript2';
|
|
4
|
+
import { uglify } from 'rollup-plugin-uglify';
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
input: 'src/index.ts',
|
|
8
|
+
output: {
|
|
9
|
+
file: 'esm/user.js',
|
|
10
|
+
format: 'esm',
|
|
11
|
+
sourcemap: true,
|
|
12
|
+
},
|
|
13
|
+
plugins: [resolve(), commonjs(), typescript(), uglify()],
|
|
14
|
+
};
|
|
@@ -3,8 +3,9 @@ import cache from '../cache';
|
|
|
3
3
|
import { getRPCClient } from '../lib/rpc';
|
|
4
4
|
import { removeCookie } from '../lib/cookie';
|
|
5
5
|
import {
|
|
6
|
-
|
|
6
|
+
getAidDomain,
|
|
7
7
|
getCurrentPublicationDomain,
|
|
8
|
+
getCurrentUrl,
|
|
8
9
|
} from '../utils/url';
|
|
9
10
|
import log from '../utils/log';
|
|
10
11
|
import { UserAttributes } from '../types';
|
|
@@ -12,18 +13,12 @@ import store from '../store';
|
|
|
12
13
|
import { navigateTo } from '../utils/location';
|
|
13
14
|
|
|
14
15
|
export const login = (requestedUrl: URL | string) => {
|
|
15
|
-
|
|
16
|
-
const environment = getCurrentEnvironment();
|
|
17
|
-
if (environment === 'localhost') {
|
|
18
|
-
url = new URL(`https://www.aid.no.localhost.api.no/aid/logg_inn/`);
|
|
19
|
-
} else if (environment) {
|
|
20
|
-
url = new URL(`https://www-${environment}.aid.no/aid/logg_inn/`);
|
|
21
|
-
}
|
|
16
|
+
const url = new URL(`https://${getAidDomain()}/aid/logg_inn/`);
|
|
22
17
|
url.searchParams.set(
|
|
23
18
|
'requestedUrl',
|
|
24
19
|
requestedUrl instanceof URL
|
|
25
20
|
? requestedUrl.href
|
|
26
|
-
: requestedUrl ||
|
|
21
|
+
: requestedUrl || getCurrentUrl()
|
|
27
22
|
);
|
|
28
23
|
navigateTo(url.href);
|
|
29
24
|
};
|
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
getNonUserAccess,
|
|
4
4
|
getVstoken,
|
|
5
5
|
} from '../clients/jupiter';
|
|
6
|
-
import { getCurrentPublicationDomain } from '../utils/url';
|
|
6
|
+
import { getCurrentPublicationDomain, getCurrentUrl } from '../utils/url';
|
|
7
7
|
import { UserAttributes } from '../types';
|
|
8
8
|
import { activateSubscription, UserDeactivatedError } from '../clients/mercury';
|
|
9
9
|
import { logout } from './authentication';
|
|
@@ -34,7 +34,9 @@ export const hasAccess = (
|
|
|
34
34
|
currentAccessFeatures.includes(requestedAccessFeature)
|
|
35
35
|
).length > 0;
|
|
36
36
|
|
|
37
|
-
export const
|
|
37
|
+
export const setUserAccessCookie = async (
|
|
38
|
+
publicationDomain
|
|
39
|
+
): Promise<boolean> => {
|
|
38
40
|
const vstoken = await getVstoken(publicationDomain);
|
|
39
41
|
if (vstoken.status === 'success') {
|
|
40
42
|
/**
|
|
@@ -45,6 +47,12 @@ export const setAccessCookies = async (publicationDomain): Promise<boolean> => {
|
|
|
45
47
|
setCookie('VSTOKEN', vstoken.cookie, 365, '/');
|
|
46
48
|
return true;
|
|
47
49
|
}
|
|
50
|
+
return false;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export const setNonUserAccessCookie = async (
|
|
54
|
+
publicationDomain
|
|
55
|
+
): Promise<boolean> => {
|
|
48
56
|
const vstoken2 = await getNonUserAccess(publicationDomain);
|
|
49
57
|
if (vstoken2.status === 'success') {
|
|
50
58
|
setCookie('VSTOKEN2', vstoken2.vstoken_cookie, 1, '/');
|
|
@@ -58,14 +66,19 @@ export const hasRecentlyTriedToUnlockPaywall = (): boolean => {
|
|
|
58
66
|
if (!cookie) {
|
|
59
67
|
return false;
|
|
60
68
|
}
|
|
69
|
+
const currentUrl = getCurrentUrl();
|
|
61
70
|
if (Array.isArray(cookie)) {
|
|
62
|
-
return cookie.filter((value) => value ===
|
|
71
|
+
return cookie.filter((value) => value === currentUrl).length > 0;
|
|
63
72
|
}
|
|
64
|
-
return cookie ===
|
|
73
|
+
return cookie === currentUrl;
|
|
65
74
|
};
|
|
66
75
|
|
|
76
|
+
export const setAccessCookies = async (publicationDomain): Promise<boolean> =>
|
|
77
|
+
setUserAccessCookie(publicationDomain) &&
|
|
78
|
+
setNonUserAccessCookie(publicationDomain);
|
|
79
|
+
|
|
67
80
|
export const setHasTriedToUnlockPaywall = (): void => {
|
|
68
|
-
const url = new URL(
|
|
81
|
+
const url = new URL(getCurrentUrl());
|
|
69
82
|
url.searchParams.set('access', 'granted');
|
|
70
83
|
|
|
71
84
|
const expires = new Date();
|
package/src/actions/storage.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getNamespaces } from '../clients/europa';
|
|
1
|
+
import { getNamespaces, updateNamespace } from '../clients/europa';
|
|
2
2
|
import store from '../store';
|
|
3
3
|
|
|
4
4
|
export const requestStorage = (namespaces: string[]) => {
|
|
@@ -6,3 +6,8 @@ export const requestStorage = (namespaces: string[]) => {
|
|
|
6
6
|
store.set('storage', storage);
|
|
7
7
|
});
|
|
8
8
|
};
|
|
9
|
+
|
|
10
|
+
export const updateStorage = (storage: { [namespace: string]: object }) =>
|
|
11
|
+
Object.keys(storage).forEach((namespace) =>
|
|
12
|
+
updateNamespace(namespace, storage[namespace])
|
|
13
|
+
);
|
package/src/actions/userData.ts
CHANGED
|
@@ -8,7 +8,7 @@ import store from '../store';
|
|
|
8
8
|
import { getInfo } from '../clients/nebula';
|
|
9
9
|
|
|
10
10
|
const getExtraData = () =>
|
|
11
|
-
getBrowserId().then((browserId) =>
|
|
11
|
+
getBrowserId().then((browserId: string) =>
|
|
12
12
|
getInfo(
|
|
13
13
|
window.location.hostname,
|
|
14
14
|
browserId,
|
|
@@ -19,6 +19,7 @@ const getExtraData = () =>
|
|
|
19
19
|
const dataServiceMap: {
|
|
20
20
|
[key in keyof UserAttributes]?: () => Promise<Partial<UserAttributes>>;
|
|
21
21
|
} = {
|
|
22
|
+
uuid: getSession,
|
|
22
23
|
name: getSession,
|
|
23
24
|
trackingKey: getSession,
|
|
24
25
|
access: getAccess,
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import fetch from 'jest-fetch-mock';
|
|
2
|
+
import cache from '../cache';
|
|
3
|
+
import thirdPartyCookie, { CACHE_KEY, CACHE_TTL } from './thirdPartyCookie';
|
|
4
|
+
import * as location from '../utils/location';
|
|
5
|
+
import * as url from '../utils/url';
|
|
6
|
+
import * as pluto from '../clients/pluto';
|
|
7
|
+
import store from '../store';
|
|
8
|
+
import restoreAllMocks = jest.restoreAllMocks;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
restoreAllMocks();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
function itShouldSetAlreadyTriedCache() {
|
|
15
|
+
it('should set already tried cache', () => {
|
|
16
|
+
expect(cache.set).toHaveBeenCalledWith(CACHE_KEY, true, CACHE_TTL);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function itShouldNotNavigateAway() {
|
|
21
|
+
it('should not navigate away', () => {
|
|
22
|
+
expect(location.navigateTo).not.toHaveBeenCalled();
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
describe('Autologin: Third Party Cookie', () => {
|
|
27
|
+
let cacheGetSpy: jasmine.Spy;
|
|
28
|
+
let storeGetSpy: jasmine.Spy;
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
cacheGetSpy = spyOn(cache, 'get');
|
|
31
|
+
spyOn(cache, 'set');
|
|
32
|
+
spyOn(location, 'navigateTo');
|
|
33
|
+
storeGetSpy = spyOn(store, 'get');
|
|
34
|
+
spyOn(url, 'getAidDomain').and.returnValue('www.aid.no');
|
|
35
|
+
spyOn(url, 'getCurrentUrl').and.returnValue(
|
|
36
|
+
'https://avisnavn.no/abo/?query=param'
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('when already logged in', () => {
|
|
41
|
+
beforeEach(async () => {
|
|
42
|
+
spyOn(pluto, 'getCurrentSession').and.returnValue(Promise.resolve());
|
|
43
|
+
storeGetSpy.and.returnValue({ isLoggedIn: true });
|
|
44
|
+
await thirdPartyCookie();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should not try to get already tried cache', () => {
|
|
48
|
+
expect(cacheGetSpy).not.toHaveBeenCalledWith(CACHE_KEY);
|
|
49
|
+
});
|
|
50
|
+
itShouldNotNavigateAway();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('when not logged in first party', () => {
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
spyOn(pluto, 'getCurrentSession').and.returnValue(Promise.resolve());
|
|
56
|
+
storeGetSpy.and.returnValue({ isLoggedIn: false });
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('when already checked third party option', () => {
|
|
60
|
+
beforeEach(async () => {
|
|
61
|
+
cacheGetSpy.and.returnValue(true);
|
|
62
|
+
await thirdPartyCookie();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should not set already tried cache', () => {
|
|
66
|
+
expect(cache.set).not.toHaveBeenCalled();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
itShouldNotNavigateAway();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe('when not logged in via third party', () => {
|
|
73
|
+
beforeEach(async () => {
|
|
74
|
+
fetch.doMockIf(
|
|
75
|
+
`https://www.aid.no/api/pluto/v1/sessions/current`,
|
|
76
|
+
JSON.stringify({}),
|
|
77
|
+
{
|
|
78
|
+
status: 403,
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
await thirdPartyCookie();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
itShouldSetAlreadyTriedCache();
|
|
85
|
+
itShouldNotNavigateAway();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe('when logged in via third party', () => {
|
|
89
|
+
beforeEach(async () => {
|
|
90
|
+
fetch.doMockIf(
|
|
91
|
+
`https://www.aid.no/api/pluto/v1/sessions/current`,
|
|
92
|
+
JSON.stringify({
|
|
93
|
+
user_name: 'Donald Duck',
|
|
94
|
+
tracking_key: 'key',
|
|
95
|
+
})
|
|
96
|
+
);
|
|
97
|
+
await thirdPartyCookie();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
itShouldSetAlreadyTriedCache();
|
|
101
|
+
it('should navigate to auto', async () => {
|
|
102
|
+
expect(location.navigateTo).toHaveBeenCalledWith(
|
|
103
|
+
'https://www.aid.no/logg_inn/auto/?context=third_party&requestedUrl=https%3A%2F%2Favisnavn.no%2Fabo%2F%3Fquery%3Dparam'
|
|
104
|
+
);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Boolean, Record, Static, String } from 'runtypes';
|
|
2
|
+
import cache from '../cache';
|
|
3
|
+
import log from '../utils/log';
|
|
4
|
+
import { getAidDomain, getCurrentUrl } from '../utils/url';
|
|
5
|
+
import { navigateTo } from '../utils/location';
|
|
6
|
+
import { getCurrentSession } from '../clients/pluto';
|
|
7
|
+
import store from '../store';
|
|
8
|
+
|
|
9
|
+
export const CACHE_KEY = 'haveTriedThirdPartyLoginCheck';
|
|
10
|
+
export const CACHE_TTL = 60 * 60 * 1000; // One hour
|
|
11
|
+
|
|
12
|
+
const ServiceSessionSchema = Record({
|
|
13
|
+
user_name: String,
|
|
14
|
+
tracking_key: String,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const getAidSession = () =>
|
|
18
|
+
<Promise<Static<typeof ServiceSessionSchema> | null>>fetch(
|
|
19
|
+
`https://${getAidDomain()}/api/pluto/v1/sessions/current`,
|
|
20
|
+
{
|
|
21
|
+
mode: 'cors',
|
|
22
|
+
credentials: 'include',
|
|
23
|
+
}
|
|
24
|
+
)
|
|
25
|
+
.then((response) => {
|
|
26
|
+
if (!response.ok) {
|
|
27
|
+
if (response.status !== 403) {
|
|
28
|
+
log.error(
|
|
29
|
+
`Unknown error (${response.status}: ${response.statusText}) when fetching current session`,
|
|
30
|
+
{ response }
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
return response.json();
|
|
36
|
+
})
|
|
37
|
+
.then((data) => {
|
|
38
|
+
if (!data) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
return ServiceSessionSchema.check(data);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
log.error('Invalid schema', error);
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
export default async function thirdPartyCookie() {
|
|
50
|
+
await getCurrentSession();
|
|
51
|
+
if (store.get('state')?.isLoggedIn) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// TODO: Should perhaps use cookie for this? Cache as it stands now only survives on the current page until vi replace it with something more persistent
|
|
56
|
+
if (await cache.get(CACHE_KEY, Boolean)) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
await cache.set(CACHE_KEY, true, CACHE_TTL);
|
|
60
|
+
|
|
61
|
+
const aidSession = await getAidSession();
|
|
62
|
+
if (aidSession) {
|
|
63
|
+
// Try to get newspaper cookie
|
|
64
|
+
const url = new URL(`https://${getAidDomain()}/logg_inn/auto/`);
|
|
65
|
+
url.searchParams.set('context', 'third_party');
|
|
66
|
+
url.searchParams.set('requestedUrl', getCurrentUrl());
|
|
67
|
+
navigateTo(url.href);
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/clients/mercury.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
getCurrentHostname,
|
|
3
|
+
getCurrentPublicationDomain,
|
|
4
|
+
getCurrentUrl,
|
|
5
|
+
} from '../utils/url';
|
|
2
6
|
|
|
3
7
|
function postLog(url: string, query: { [index: string]: any }) {
|
|
4
8
|
const body = new FormData();
|
|
@@ -26,8 +30,8 @@ const backendLog = (
|
|
|
26
30
|
const extendedContext = {
|
|
27
31
|
...context,
|
|
28
32
|
message,
|
|
29
|
-
url:
|
|
30
|
-
domain:
|
|
33
|
+
url: getCurrentUrl(),
|
|
34
|
+
domain: getCurrentHostname(),
|
|
31
35
|
};
|
|
32
36
|
postLog(`//www.aid.no/api/mercury/v1/log/${level}`, extendedContext);
|
|
33
37
|
};
|
package/src/clients/pluto.ts
CHANGED
|
@@ -5,6 +5,7 @@ import store from '../store';
|
|
|
5
5
|
import { UserAttributes } from '../types';
|
|
6
6
|
|
|
7
7
|
const ServiceSessionSchema = Record({
|
|
8
|
+
user_uuid: String,
|
|
8
9
|
user_name: String,
|
|
9
10
|
tracking_key: String,
|
|
10
11
|
});
|
|
@@ -12,6 +13,7 @@ const ServiceSessionSchema = Record({
|
|
|
12
13
|
const mapSessionToUserAttributes = (
|
|
13
14
|
session: Static<typeof ServiceSessionSchema>
|
|
14
15
|
): UserAttributes => ({
|
|
16
|
+
uuid: session.user_uuid,
|
|
15
17
|
name: session.user_name,
|
|
16
18
|
trackingKey: session.tracking_key,
|
|
17
19
|
});
|
|
@@ -2,11 +2,12 @@ import { requestAttributes } from '../actions/userData';
|
|
|
2
2
|
import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
|
|
3
3
|
import { State, UserAttributes } from '../types';
|
|
4
4
|
import store from '../store';
|
|
5
|
-
import { requestStorage } from '../actions/storage';
|
|
5
|
+
import { requestStorage, updateStorage } from '../actions/storage';
|
|
6
6
|
|
|
7
7
|
export enum AmediaUserEvents {
|
|
8
8
|
REQUEST_USER_ATTRIBUTES = 'amedia-user.request-user-attributes',
|
|
9
9
|
REQUEST_STORAGE_NAMESPACES = 'amedia-user.request-storage-namespaces',
|
|
10
|
+
UPDATE_STORAGE = 'amedua-user.update-storage-namespaces',
|
|
10
11
|
ON_ATTRIBUTES_UPDATED = 'amedia-user.on-user-attributes-updated',
|
|
11
12
|
ON_STATE_UPDATED = 'amedia-user.on-state-updated',
|
|
12
13
|
ON_STORAGE_UPDATED = 'amedia-user.on-storage-updated',
|
|
@@ -18,6 +19,9 @@ declare global {
|
|
|
18
19
|
Array<keyof UserAttributes>
|
|
19
20
|
>;
|
|
20
21
|
[AmediaUserEvents.REQUEST_STORAGE_NAMESPACES]: CustomEvent<Array<string>>;
|
|
22
|
+
[AmediaUserEvents.UPDATE_STORAGE]: CustomEvent<{
|
|
23
|
+
[namespace: string]: object;
|
|
24
|
+
}>;
|
|
21
25
|
[AmediaUserEvents.ON_ATTRIBUTES_UPDATED]: CustomEvent<UserAttributes>;
|
|
22
26
|
[AmediaUserEvents.ON_STATE_UPDATED]: CustomEvent<State>;
|
|
23
27
|
[AmediaUserEvents.ON_STORAGE_UPDATED]: CustomEvent<{
|
|
@@ -50,6 +54,9 @@ export const listenForUserAttributeRequests = () => {
|
|
|
50
54
|
listenWithExclusivity(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, (evt) => {
|
|
51
55
|
requestStorage(evt.detail);
|
|
52
56
|
});
|
|
57
|
+
listenWithExclusivity(AmediaUserEvents.UPDATE_STORAGE, (evt) => {
|
|
58
|
+
updateStorage(evt.detail);
|
|
59
|
+
});
|
|
53
60
|
};
|
|
54
61
|
|
|
55
62
|
listenForUserAttributeRequests();
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import * as europa from './clients/europa';
|
|
6
|
+
import { updateStorage } from './index';
|
|
7
|
+
import * as storage from './actions/storage';
|
|
8
|
+
import store from './store';
|
|
9
|
+
import User from './lib/User';
|
|
10
|
+
import { setRootElement } from './utils/customEventWrapper';
|
|
11
|
+
import { listenForUserAttributeRequests } from './eventHandlers/userAttributes';
|
|
12
|
+
import restoreAllMocks = jest.restoreAllMocks;
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
restoreAllMocks();
|
|
16
|
+
|
|
17
|
+
// We cannot use window as our event root during testing
|
|
18
|
+
// since tests are executed in parallel.
|
|
19
|
+
setRootElement(new Document());
|
|
20
|
+
|
|
21
|
+
// Store must be cleared before we re-listen for user attribute requests
|
|
22
|
+
store.clear();
|
|
23
|
+
|
|
24
|
+
// Also, since we keep creating new root elements, we also
|
|
25
|
+
// need to attach to these new roots.
|
|
26
|
+
listenForUserAttributeRequests();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('Storage', () => {
|
|
30
|
+
it('should update storage', () => {
|
|
31
|
+
spyOn(europa, 'updateNamespace');
|
|
32
|
+
|
|
33
|
+
updateStorage({
|
|
34
|
+
user_info: {
|
|
35
|
+
some: 'data',
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
expect(europa.updateNamespace).toHaveBeenCalledWith('user_info', {
|
|
40
|
+
some: 'data',
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should return storage', () => {
|
|
45
|
+
jest.spyOn(storage, 'requestStorage').mockImplementation(() => {
|
|
46
|
+
store.set('storage', {
|
|
47
|
+
namespace: {
|
|
48
|
+
key: 'value',
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const onUpdate = jest.fn();
|
|
54
|
+
new User().withStorage('namespace').subscribe(onUpdate);
|
|
55
|
+
|
|
56
|
+
expect(onUpdate).toHaveBeenCalledWith({
|
|
57
|
+
storage: {
|
|
58
|
+
namespace: {
|
|
59
|
+
key: 'value',
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
state: {},
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should return multiple namespaces if requested (but only requested)', () => {
|
|
67
|
+
jest.spyOn(storage, 'requestStorage').mockImplementation(() => {
|
|
68
|
+
store.set('storage', {
|
|
69
|
+
one: {
|
|
70
|
+
key1: 'value1',
|
|
71
|
+
},
|
|
72
|
+
two: {
|
|
73
|
+
key2: 'value2',
|
|
74
|
+
},
|
|
75
|
+
three: {
|
|
76
|
+
key3: 'value3',
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const onUpdate = jest.fn();
|
|
82
|
+
new User().withStorage('one', 'three').subscribe(onUpdate);
|
|
83
|
+
|
|
84
|
+
expect(onUpdate).toHaveBeenCalledWith({
|
|
85
|
+
storage: {
|
|
86
|
+
one: {
|
|
87
|
+
key1: 'value1',
|
|
88
|
+
},
|
|
89
|
+
three: {
|
|
90
|
+
key3: 'value3',
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
state: {},
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
});
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
|
|
5
5
|
import User from './lib/User';
|
|
6
6
|
import * as userData from './actions/userData';
|
|
7
|
-
import * as storage from './actions/storage';
|
|
8
7
|
import { setRootElement } from './utils/customEventWrapper';
|
|
9
8
|
import { listenForUserAttributeRequests } from './eventHandlers/userAttributes';
|
|
10
9
|
import store from './store';
|
|
@@ -192,57 +191,4 @@ describe('Public endpoint - userdata', () => {
|
|
|
192
191
|
state: { isLoggedIn: true },
|
|
193
192
|
});
|
|
194
193
|
});
|
|
195
|
-
|
|
196
|
-
it('should return storage', () => {
|
|
197
|
-
jest.spyOn(storage, 'requestStorage').mockImplementation(() => {
|
|
198
|
-
store.set('storage', {
|
|
199
|
-
namespace: {
|
|
200
|
-
key: 'value',
|
|
201
|
-
},
|
|
202
|
-
});
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
const onUpdate = jest.fn();
|
|
206
|
-
new User().withStorage('namespace').subscribe(onUpdate);
|
|
207
|
-
|
|
208
|
-
expect(onUpdate).toHaveBeenCalledWith({
|
|
209
|
-
storage: {
|
|
210
|
-
namespace: {
|
|
211
|
-
key: 'value',
|
|
212
|
-
},
|
|
213
|
-
},
|
|
214
|
-
state: {},
|
|
215
|
-
});
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
it('should return multiple namespaces if requested (but only requested)', () => {
|
|
219
|
-
jest.spyOn(storage, 'requestStorage').mockImplementation(() => {
|
|
220
|
-
store.set('storage', {
|
|
221
|
-
one: {
|
|
222
|
-
key1: 'value1',
|
|
223
|
-
},
|
|
224
|
-
two: {
|
|
225
|
-
key2: 'value2',
|
|
226
|
-
},
|
|
227
|
-
three: {
|
|
228
|
-
key3: 'value3',
|
|
229
|
-
},
|
|
230
|
-
});
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
const onUpdate = jest.fn();
|
|
234
|
-
new User().withStorage('one', 'three').subscribe(onUpdate);
|
|
235
|
-
|
|
236
|
-
expect(onUpdate).toHaveBeenCalledWith({
|
|
237
|
-
storage: {
|
|
238
|
-
one: {
|
|
239
|
-
key1: 'value1',
|
|
240
|
-
},
|
|
241
|
-
three: {
|
|
242
|
-
key3: 'value3',
|
|
243
|
-
},
|
|
244
|
-
},
|
|
245
|
-
state: {},
|
|
246
|
-
});
|
|
247
|
-
});
|
|
248
194
|
});
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,9 @@ import { dispatch, listenForAllEvents } from './utils/customEventWrapper';
|
|
|
4
4
|
import * as authentication from './actions/authentication';
|
|
5
5
|
import User from './lib/User';
|
|
6
6
|
import PaywallUnlocker from './lib/PaywallUnlocker';
|
|
7
|
+
import { AmediaUserEvents } from './eventHandlers/userAttributes';
|
|
8
|
+
import { getCurrentUrl } from './utils/url';
|
|
9
|
+
import { runJanitor } from './janitor';
|
|
7
10
|
|
|
8
11
|
/**
|
|
9
12
|
* Enables you to subscribe to the user attributes you care about and
|
|
@@ -17,7 +20,7 @@ export const user = () => new User();
|
|
|
17
20
|
* @param requestedUrl
|
|
18
21
|
*/
|
|
19
22
|
export const goToLoginPage = (requestedUrl?: string) =>
|
|
20
|
-
authentication.login(requestedUrl ||
|
|
23
|
+
authentication.login(requestedUrl || getCurrentUrl());
|
|
21
24
|
|
|
22
25
|
export const logout = () => authentication.logout();
|
|
23
26
|
|
|
@@ -36,3 +39,10 @@ export const tryActivateSubscriptionAccess = (
|
|
|
36
39
|
});
|
|
37
40
|
|
|
38
41
|
export const paywall = () => new PaywallUnlocker();
|
|
42
|
+
|
|
43
|
+
export const updateStorage = (storage: { [index: string]: Object }): void =>
|
|
44
|
+
dispatch(
|
|
45
|
+
new CustomEvent(AmediaUserEvents.UPDATE_STORAGE, { detail: storage })
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
runJanitor();
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import accessCookieRefresher from './jobs/accessCookieRefresher';
|
|
2
|
+
import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
|
|
3
|
+
import { attemptAutologin } from '../autologin';
|
|
4
|
+
|
|
5
|
+
const jobs = [attemptAutologin, accessCookieRefresher];
|
|
6
|
+
|
|
7
|
+
async function performJobs() {
|
|
8
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
9
|
+
for (const job of jobs) {
|
|
10
|
+
// eslint-disable-next-line no-await-in-loop
|
|
11
|
+
await job();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
enum JanitorEvents {
|
|
16
|
+
LOADED = 'amedia-user.janitor.loaded',
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare global {
|
|
20
|
+
interface WindowEventMap {
|
|
21
|
+
[JanitorEvents.LOADED]: CustomEvent<{}>;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// We want to ensure that the Janitor is started exactly once. The trick here is to
|
|
26
|
+
// listen to an event end if we receive it exclusively, we know that we are the first
|
|
27
|
+
// to load this script and therefore responsible for running the Janitor once.
|
|
28
|
+
export const runJanitor = async () => {
|
|
29
|
+
let janitorStarted = false;
|
|
30
|
+
listenWithExclusivity(JanitorEvents.LOADED, () => {
|
|
31
|
+
if (janitorStarted) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
janitorStarted = true;
|
|
36
|
+
performJobs();
|
|
37
|
+
});
|
|
38
|
+
dispatch(new CustomEvent(JanitorEvents.LOADED));
|
|
39
|
+
};
|