@amedia/user 0.14.17 → 0.14.19
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/dist/user.d.ts +1 -1
- package/package.json +2 -3
- package/src/actions/authentication.spec.ts +0 -51
- package/src/actions/authentication.ts +0 -49
- package/src/actions/authorization.spec.ts +0 -377
- package/src/actions/authorization.ts +0 -120
- package/src/actions/preferences.spec.ts +0 -41
- package/src/actions/preferences.ts +0 -41
- package/src/actions/storage.ts +0 -22
- package/src/actions/userData.spec.ts +0 -133
- package/src/actions/userData.ts +0 -68
- package/src/autologin/index.spec.ts +0 -264
- package/src/autologin/index.ts +0 -113
- package/src/autologin/thirdPartyCookie.spec.ts +0 -95
- package/src/autologin/thirdPartyCookie.ts +0 -55
- package/src/cache/engines/global.spec.ts +0 -62
- package/src/cache/engines/global.ts +0 -49
- package/src/cache/index.spec.ts +0 -132
- package/src/cache/index.ts +0 -48
- package/src/cache/types.ts +0 -12
- package/src/clients/europa.spec.ts +0 -209
- package/src/clients/europa.ts +0 -183
- package/src/clients/jupiter.spec.ts +0 -178
- package/src/clients/jupiter.ts +0 -119
- package/src/clients/mercury.spec.ts +0 -126
- package/src/clients/mercury.ts +0 -105
- package/src/clients/mercuryLog.spec.ts +0 -20
- package/src/clients/mercuryLog.ts +0 -67
- package/src/clients/nebula.spec.ts +0 -70
- package/src/clients/nebula.ts +0 -83
- package/src/clients/pluto.spec.ts +0 -262
- package/src/clients/pluto.ts +0 -126
- package/src/eventHandlers/activation.ts +0 -40
- package/src/eventHandlers/authentication.ts +0 -34
- package/src/eventHandlers/eventMap.ts +0 -40
- package/src/eventHandlers/navigation.spec.ts +0 -189
- package/src/eventHandlers/navigation.ts +0 -58
- package/src/eventHandlers/state.spec.ts +0 -48
- package/src/eventHandlers/state.ts +0 -23
- package/src/eventHandlers/storage.spec.ts +0 -114
- package/src/eventHandlers/storage.ts +0 -45
- package/src/eventHandlers/userAttributes.spec.ts +0 -93
- package/src/eventHandlers/userAttributes.ts +0 -31
- package/src/exceptions/FetchTimeoutError.ts +0 -8
- package/src/exceptions/PaywallError.ts +0 -16
- package/src/index-authentcation.spec.ts +0 -26
- package/src/index-storage.spec.ts +0 -144
- package/src/index-subscription.spec.ts +0 -213
- package/src/index.ts +0 -46
- package/src/janitor/index.spec.ts +0 -18
- package/src/janitor/index.ts +0 -60
- package/src/janitor/jobs/accessCookieRefresher.spec.ts +0 -213
- package/src/janitor/jobs/accessCookieRefresher.ts +0 -96
- package/src/janitor/jobs/daxsubCookie.spec.ts +0 -109
- package/src/janitor/jobs/daxsubCookie.ts +0 -38
- package/src/janitor/jobs/removeUserStorage.spec.ts +0 -34
- package/src/janitor/jobs/removeUserStorage.ts +0 -12
- package/src/lib/PaywallUnlockRequest.spec.ts +0 -446
- package/src/lib/PaywallUnlockRequest.ts +0 -161
- package/src/lib/UserDataRequest.spec.ts +0 -231
- package/src/lib/UserDataRequest.ts +0 -267
- package/src/lib/cookie.ts +0 -121
- package/src/lib/rpc.ts +0 -19
- package/src/store/index.spec.ts +0 -49
- package/src/store/index.ts +0 -66
- package/src/types.ts +0 -22
- package/src/utils/customEventWrapper.spec.ts +0 -237
- package/src/utils/customEventWrapper.ts +0 -77
- package/src/utils/debouncer.spec.ts +0 -252
- package/src/utils/debouncer.ts +0 -123
- package/src/utils/deepEquals.ts +0 -42
- package/src/utils/expectBootLogs.ts +0 -9
- package/src/utils/getGlobalThis.ts +0 -17
- package/src/utils/getOrUpdate.spec.ts +0 -166
- package/src/utils/getOrUpdate.ts +0 -66
- package/src/utils/localhostCheck.ts +0 -10
- package/src/utils/location.spec.ts +0 -61
- package/src/utils/location.ts +0 -16
- package/src/utils/log.spec.ts +0 -177
- package/src/utils/log.ts +0 -112
- package/src/utils/loginState.ts +0 -24
- package/src/utils/networkError.ts +0 -11
- package/src/utils/queue.ts +0 -32
- package/src/utils/type.ts +0 -19
- package/src/utils/url.spec.ts +0 -33
- package/src/utils/url.ts +0 -37
package/src/clients/europa.ts
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import { Unknown, Dictionary, String, Static } from 'runtypes';
|
|
2
|
-
|
|
3
|
-
import cache from '../cache';
|
|
4
|
-
import getOrUpdate from '../utils/getOrUpdate';
|
|
5
|
-
import log from '../utils/log';
|
|
6
|
-
import Debouncer from '../utils/debouncer';
|
|
7
|
-
|
|
8
|
-
const CACHE_DURATION = 5 * 60000; // 5 minutes
|
|
9
|
-
const DEBOUNCE_WAIT_TIME = 20; // 20ms wait before actually calling Europa
|
|
10
|
-
|
|
11
|
-
export const EuropaSchema = Dictionary(Unknown, String);
|
|
12
|
-
|
|
13
|
-
export const clearLocalCache = () => cache.remove('europa');
|
|
14
|
-
|
|
15
|
-
const doFetch = (url: string, method: string = 'GET', body?: Object) =>
|
|
16
|
-
fetch(url, {
|
|
17
|
-
method,
|
|
18
|
-
body: body && JSON.stringify(body),
|
|
19
|
-
headers: {
|
|
20
|
-
'Content-Type': 'application/json',
|
|
21
|
-
Accept: 'application/json',
|
|
22
|
-
},
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
function mergeCache(
|
|
26
|
-
cachedData: Static<typeof EuropaSchema> | void,
|
|
27
|
-
newData: Static<typeof EuropaSchema>
|
|
28
|
-
) {
|
|
29
|
-
return Object.entries(newData).reduce(
|
|
30
|
-
(mergedNamespaces, [namespace, data]) => ({
|
|
31
|
-
...mergedNamespaces,
|
|
32
|
-
[namespace]: data,
|
|
33
|
-
}),
|
|
34
|
-
cachedData || {}
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const updateNamespaceInCache = async (namespace: string, data: Object) => {
|
|
39
|
-
const cachedData = await cache.get('europa', EuropaSchema);
|
|
40
|
-
const updatedData = mergeCache(cachedData, { [namespace]: data });
|
|
41
|
-
return cache.set('europa', updatedData, Date.now() + CACHE_DURATION);
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export const updateNamespace = async (namespace: string, data: Object) => {
|
|
45
|
-
const res = await doFetch(`/api/europa/v1/me/${namespace}`, 'PATCH', data);
|
|
46
|
-
if (!res.ok) {
|
|
47
|
-
log.error(
|
|
48
|
-
`Could not get namespace: ${namespace}, status: ${res.status}, statusText: ${res.statusText}`,
|
|
49
|
-
res
|
|
50
|
-
);
|
|
51
|
-
return {};
|
|
52
|
-
}
|
|
53
|
-
return updateNamespaceInCache(namespace, data);
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const debouncer = new Debouncer<string[], typeof EuropaSchema>(
|
|
57
|
-
DEBOUNCE_WAIT_TIME,
|
|
58
|
-
[],
|
|
59
|
-
Debouncer.stringListParamsAccumulator,
|
|
60
|
-
(params) =>
|
|
61
|
-
doFetch(`/api/europa/v1/me/${params.join(',')}`).then(async (response) => {
|
|
62
|
-
const body = await response.text();
|
|
63
|
-
if (!response.ok) {
|
|
64
|
-
if (response.status === 500 || response.status === 503) {
|
|
65
|
-
log.error(
|
|
66
|
-
`Could not get namespaces for: ${params}, status: ${response.status}`,
|
|
67
|
-
{ response, body }
|
|
68
|
-
);
|
|
69
|
-
} else {
|
|
70
|
-
log.warn(
|
|
71
|
-
`Could not get namespaces for: ${params}, status: ${response.status}`,
|
|
72
|
-
{ response, body }
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
return {};
|
|
76
|
-
}
|
|
77
|
-
return JSON.parse(body).namespaces;
|
|
78
|
-
})
|
|
79
|
-
);
|
|
80
|
-
|
|
81
|
-
export const getNamespaces = async <
|
|
82
|
-
S extends { [namespace: string]: { [index: string]: unknown } },
|
|
83
|
-
Namespaces extends Array<keyof S>
|
|
84
|
-
>(
|
|
85
|
-
namespaces: Namespaces,
|
|
86
|
-
options: { ignoreCache: boolean } = { ignoreCache: false }
|
|
87
|
-
): Promise<Pick<S, Namespaces[number]>> => {
|
|
88
|
-
if (namespaces.length === 0) {
|
|
89
|
-
throw new Error('namespaces array cannot be of length 0');
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
let missingNamespaces = namespaces as string[];
|
|
93
|
-
const cachedData = (await cache.get('europa', EuropaSchema)) || {
|
|
94
|
-
namespaces: {},
|
|
95
|
-
};
|
|
96
|
-
if (!options.ignoreCache) {
|
|
97
|
-
missingNamespaces = (
|
|
98
|
-
cachedData
|
|
99
|
-
? namespaces.filter(
|
|
100
|
-
(namespace) =>
|
|
101
|
-
!Object.keys(cachedData).includes(namespace as string)
|
|
102
|
-
)
|
|
103
|
-
: namespaces
|
|
104
|
-
) as string[];
|
|
105
|
-
if (cachedData && missingNamespaces.length === 0) {
|
|
106
|
-
// Yay! Everything we need is in the cache!
|
|
107
|
-
return Promise.resolve(cachedData as Pick<S, Namespaces[number]>);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const fetchedNamespaces = await debouncer.perform(missingNamespaces);
|
|
112
|
-
Object.entries(fetchedNamespaces).forEach(([namespace, fetchedNamespace]) =>
|
|
113
|
-
updateNamespaceInCache(namespace, fetchedNamespace as Object)
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
return Promise.resolve(
|
|
117
|
-
// @ts-ignore
|
|
118
|
-
Object.entries(mergeCache(cachedData, fetchedNamespaces))
|
|
119
|
-
.filter(([namespace]) => namespaces.includes(namespace))
|
|
120
|
-
.reduce(
|
|
121
|
-
(returnedEntries, [namespace, data]) => ({
|
|
122
|
-
...returnedEntries,
|
|
123
|
-
[namespace]: data,
|
|
124
|
-
}),
|
|
125
|
-
{} as Pick<S, Namespaces[number]>
|
|
126
|
-
)
|
|
127
|
-
);
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
export const saveNamespace = (namespace: string, data: Object) =>
|
|
131
|
-
doFetch(`/api/europa/v1/me/${namespace}`, 'PUT', data)
|
|
132
|
-
.then((res) => {
|
|
133
|
-
if (!res.ok) {
|
|
134
|
-
log.error(
|
|
135
|
-
`Could not get save namespace, namespace:${namespace} status: ${res.status}, statusText: ${res.statusText}`,
|
|
136
|
-
res
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
return {};
|
|
140
|
-
})
|
|
141
|
-
.then(() => getNamespaces([namespace], { ignoreCache: true })); // updates cache
|
|
142
|
-
|
|
143
|
-
export const getAllNamespaces = () =>
|
|
144
|
-
getOrUpdate(
|
|
145
|
-
'europa',
|
|
146
|
-
EuropaSchema,
|
|
147
|
-
() =>
|
|
148
|
-
doFetch(`/api/europa/v1/me`)
|
|
149
|
-
.then((res) => {
|
|
150
|
-
if (!res.ok) {
|
|
151
|
-
log.error(
|
|
152
|
-
`Could not get all namespaces, status: ${res.status}, statusText: ${res.statusText}`,
|
|
153
|
-
res
|
|
154
|
-
);
|
|
155
|
-
return {};
|
|
156
|
-
}
|
|
157
|
-
return res.json();
|
|
158
|
-
})
|
|
159
|
-
.then((data) => {
|
|
160
|
-
const europaValidData = EuropaSchema.check(data);
|
|
161
|
-
try {
|
|
162
|
-
return {
|
|
163
|
-
ttl: CACHE_DURATION,
|
|
164
|
-
data: europaValidData,
|
|
165
|
-
};
|
|
166
|
-
} catch (error) {
|
|
167
|
-
log.error(error, { data });
|
|
168
|
-
throw error;
|
|
169
|
-
}
|
|
170
|
-
}),
|
|
171
|
-
() => false
|
|
172
|
-
);
|
|
173
|
-
export const deleteNamespaceKey = (namespace: string, key: string) =>
|
|
174
|
-
doFetch(`/api/europa/v1/me/${namespace}/${key}`, 'DELETE')
|
|
175
|
-
.then((res) => {
|
|
176
|
-
if (!res.ok) {
|
|
177
|
-
log.error(
|
|
178
|
-
`Could not get namespace: ${namespace}, status: ${res.status}, statusText: ${res.statusText}`,
|
|
179
|
-
res
|
|
180
|
-
);
|
|
181
|
-
}
|
|
182
|
-
})
|
|
183
|
-
.then(() => getNamespaces([namespace], { ignoreCache: true })); // updates cache
|
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
import fetch from 'jest-fetch-mock';
|
|
2
|
-
import cache from '../cache';
|
|
3
|
-
import log from '../utils/log';
|
|
4
|
-
import {
|
|
5
|
-
checkForAccess,
|
|
6
|
-
getUserAccess,
|
|
7
|
-
AccessFeatures,
|
|
8
|
-
CheckAccessForPaper,
|
|
9
|
-
GetVstoken,
|
|
10
|
-
GetNonUserAccess,
|
|
11
|
-
getNonUserAccess,
|
|
12
|
-
} from './jupiter';
|
|
13
|
-
import anything = jasmine.anything;
|
|
14
|
-
import spyOn = jest.spyOn;
|
|
15
|
-
|
|
16
|
-
beforeEach(async () => {
|
|
17
|
-
await cache.clear();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe('jupiter service', () => {
|
|
21
|
-
describe('GET /check_access_for_paper', () => {
|
|
22
|
-
const siteDomain = 'www.rb.no';
|
|
23
|
-
const accessFeatures: AccessFeatures = ['newspaper'];
|
|
24
|
-
|
|
25
|
-
it('should check access for paper', async () => {
|
|
26
|
-
const response: CheckAccessForPaper = {
|
|
27
|
-
status: 'success',
|
|
28
|
-
access: 'granted',
|
|
29
|
-
publication: {
|
|
30
|
-
domain: 'www.rb.no',
|
|
31
|
-
id: 95,
|
|
32
|
-
},
|
|
33
|
-
access_features: ['amedia_staff', 'newspaper'],
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
fetch.mockResponseOnce(JSON.stringify(response));
|
|
37
|
-
|
|
38
|
-
await expect(checkForAccess(siteDomain, accessFeatures)).resolves.toEqual(
|
|
39
|
-
{
|
|
40
|
-
access: ['amedia_staff', 'newspaper'],
|
|
41
|
-
}
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
45
|
-
`/api/jupiter/v1/check_access_for_paper/${siteDomain}/?require=any&access_features=${accessFeatures.join(
|
|
46
|
-
','
|
|
47
|
-
)}`
|
|
48
|
-
);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('should deny access for user not logged in', async () => {
|
|
52
|
-
fetch.mockResponseOnce(JSON.stringify({ status: 'no_session_found' }), {
|
|
53
|
-
status: 400,
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
await expect(
|
|
57
|
-
checkForAccess(siteDomain, accessFeatures)
|
|
58
|
-
).rejects.toBeUndefined();
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it('should fail on runtime validation', async () => {
|
|
62
|
-
fetch.mockResponseOnce(JSON.stringify({ access_features: 'foo' }));
|
|
63
|
-
log.error = jest.fn();
|
|
64
|
-
|
|
65
|
-
await expect(
|
|
66
|
-
checkForAccess(siteDomain, accessFeatures)
|
|
67
|
-
).rejects.toBeUndefined();
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('should not cache when access denied', (done) => {
|
|
71
|
-
fetch.mockResponseOnce(
|
|
72
|
-
JSON.stringify({
|
|
73
|
-
status: 'success',
|
|
74
|
-
access: 'denied',
|
|
75
|
-
publication: {
|
|
76
|
-
domain: 'www.rb.no',
|
|
77
|
-
id: 95,
|
|
78
|
-
},
|
|
79
|
-
access_features: [],
|
|
80
|
-
})
|
|
81
|
-
);
|
|
82
|
-
const cacheSetSpy = spyOn(cache, 'set');
|
|
83
|
-
expect(checkForAccess(siteDomain, accessFeatures))
|
|
84
|
-
.resolves.toEqual({ access: [] })
|
|
85
|
-
.then(() => {
|
|
86
|
-
expect(cacheSetSpy).toHaveBeenCalledWith(
|
|
87
|
-
'checkForAccess',
|
|
88
|
-
anything(),
|
|
89
|
-
0
|
|
90
|
-
);
|
|
91
|
-
})
|
|
92
|
-
.then(done);
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
describe('POST /get_vstoken', () => {
|
|
97
|
-
const siteDomain = 'www.rb.no';
|
|
98
|
-
|
|
99
|
-
it('should return vstoken', async () => {
|
|
100
|
-
const response: GetVstoken = {
|
|
101
|
-
status: 'success',
|
|
102
|
-
cookie: 'ofofb:16187:amedia_staff,newspaper,plussalt:cc2c2c:rb:jupiter',
|
|
103
|
-
access_features: ['newspaper', 'plussalt'],
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
fetch.mockResponseOnce(JSON.stringify(response));
|
|
107
|
-
|
|
108
|
-
await expect(getUserAccess(siteDomain)).resolves.toEqual(response);
|
|
109
|
-
|
|
110
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
111
|
-
`/api/jupiter/v1/get_vstoken/${siteDomain}`,
|
|
112
|
-
{
|
|
113
|
-
method: 'post',
|
|
114
|
-
}
|
|
115
|
-
);
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it('should deny access for user not logged in', async () => {
|
|
119
|
-
fetch.mockResponseOnce(JSON.stringify({ status: 'no_session_found' }), {
|
|
120
|
-
status: 400,
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
await expect(getUserAccess(siteDomain)).rejects.toThrow(
|
|
124
|
-
'Request not ok (400: Bad Request).'
|
|
125
|
-
);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it('should fail on runtime validation', async () => {
|
|
129
|
-
fetch.mockResponseOnce(JSON.stringify({ access_features: 'foo' }));
|
|
130
|
-
log.error = jest.fn();
|
|
131
|
-
|
|
132
|
-
await expect(getUserAccess(siteDomain)).rejects.toThrow(
|
|
133
|
-
'Expected { status: "success"; cookie: string; access_features: string[]; }, but was incompatible'
|
|
134
|
-
);
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
describe('POST /non_user_access', () => {
|
|
139
|
-
const siteDomain = 'www.rb.no';
|
|
140
|
-
|
|
141
|
-
it('should return none user access', async () => {
|
|
142
|
-
const response: GetNonUserAccess = {
|
|
143
|
-
status: 'success',
|
|
144
|
-
vstoken_cookie: 'd82ab3842a:2619693230:::rb:jupiter',
|
|
145
|
-
uuid: '',
|
|
146
|
-
access_features: [],
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
fetch.mockResponseOnce(JSON.stringify(response));
|
|
150
|
-
|
|
151
|
-
await expect(getNonUserAccess(siteDomain)).resolves.toEqual(response);
|
|
152
|
-
|
|
153
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
154
|
-
`/api/jupiter/v1/non_user_access/${siteDomain}`,
|
|
155
|
-
{ method: 'post' }
|
|
156
|
-
);
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
it('should deny access for user not logged in', async () => {
|
|
160
|
-
fetch.mockResponseOnce(JSON.stringify({ status: 'no_session_found' }), {
|
|
161
|
-
status: 400,
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
await expect(getNonUserAccess(siteDomain)).rejects.toThrow(
|
|
165
|
-
'Request not ok (400: Bad Request).'
|
|
166
|
-
);
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
it('should fail on runtime validation', async () => {
|
|
170
|
-
fetch.mockResponseOnce(JSON.stringify({ vstoken_cookie: 3 }));
|
|
171
|
-
log.error = jest.fn();
|
|
172
|
-
|
|
173
|
-
await expect(getNonUserAccess(siteDomain)).rejects.toThrow(
|
|
174
|
-
'Expected { status: "success"; vstoken_cookie: string; uuid: string; access_features: string[]; }, but was incompatible'
|
|
175
|
-
);
|
|
176
|
-
});
|
|
177
|
-
});
|
|
178
|
-
});
|
package/src/clients/jupiter.ts
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Array,
|
|
3
|
-
Literal,
|
|
4
|
-
Number,
|
|
5
|
-
Record,
|
|
6
|
-
Static,
|
|
7
|
-
String,
|
|
8
|
-
Union,
|
|
9
|
-
} from 'runtypes';
|
|
10
|
-
import getOrUpdate from '../utils/getOrUpdate';
|
|
11
|
-
import { UserAttributes } from '../types';
|
|
12
|
-
import log from '../utils/log';
|
|
13
|
-
|
|
14
|
-
export type AccessFeatures = string[];
|
|
15
|
-
|
|
16
|
-
const PublicationSchema = Record({
|
|
17
|
-
id: Number,
|
|
18
|
-
domain: String,
|
|
19
|
-
});
|
|
20
|
-
export const CheckAccessForPaperSchema = Record({
|
|
21
|
-
status: Literal('success'),
|
|
22
|
-
access: Union(Literal('granted'), Literal('denied')),
|
|
23
|
-
publication: PublicationSchema,
|
|
24
|
-
access_features: Array(String),
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
export type CheckAccessForPaper = Static<typeof CheckAccessForPaperSchema>;
|
|
28
|
-
|
|
29
|
-
const GetVstokenSchema = Record({
|
|
30
|
-
status: Literal('success'),
|
|
31
|
-
cookie: String,
|
|
32
|
-
access_features: Array(String),
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
export type GetVstoken = Static<typeof GetVstokenSchema>;
|
|
36
|
-
|
|
37
|
-
const GetNonUserAccessSchema = Record({
|
|
38
|
-
status: Literal('success'),
|
|
39
|
-
vstoken_cookie: String,
|
|
40
|
-
uuid: String,
|
|
41
|
-
access_features: Array(String),
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
export type GetNonUserAccess = Static<typeof GetNonUserAccessSchema>;
|
|
45
|
-
|
|
46
|
-
export const checkForAccess = (
|
|
47
|
-
siteDomain: string,
|
|
48
|
-
accessFeatures: AccessFeatures
|
|
49
|
-
): Promise<Pick<UserAttributes, 'access'>> =>
|
|
50
|
-
getOrUpdate('checkForAccess', CheckAccessForPaperSchema, () =>
|
|
51
|
-
fetch(
|
|
52
|
-
`/api/jupiter/v1/check_access_for_paper/${siteDomain}/?require=any&access_features=${accessFeatures.join(
|
|
53
|
-
','
|
|
54
|
-
)}`
|
|
55
|
-
)
|
|
56
|
-
.then((response) => {
|
|
57
|
-
if (!response.ok) {
|
|
58
|
-
// TODO Handle this.
|
|
59
|
-
throw new Error(
|
|
60
|
-
`Request not ok (${response.status}: ${response.statusText}).`
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
return response.json();
|
|
64
|
-
})
|
|
65
|
-
.then((json) => {
|
|
66
|
-
try {
|
|
67
|
-
const data = CheckAccessForPaperSchema.check(json);
|
|
68
|
-
return {
|
|
69
|
-
ttl: data.access === 'granted' ? 1000 * 10 : 0, // 10 seconds
|
|
70
|
-
data,
|
|
71
|
-
};
|
|
72
|
-
} catch (error) {
|
|
73
|
-
log.error(error, { json });
|
|
74
|
-
throw error;
|
|
75
|
-
}
|
|
76
|
-
})
|
|
77
|
-
).then((data) => ({ access: data.access_features }));
|
|
78
|
-
|
|
79
|
-
export const getUserAccess = (siteDomain: string): Promise<GetVstoken> =>
|
|
80
|
-
fetch(`/api/jupiter/v1/get_vstoken/${siteDomain}`, { method: 'post' })
|
|
81
|
-
.then((response) => {
|
|
82
|
-
if (!response.ok) {
|
|
83
|
-
throw new Error(
|
|
84
|
-
`Request not ok (${response.status}: ${response.statusText}).`
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
return response.json();
|
|
88
|
-
})
|
|
89
|
-
.then((json) => {
|
|
90
|
-
try {
|
|
91
|
-
return GetVstokenSchema.check(json);
|
|
92
|
-
} catch (error) {
|
|
93
|
-
log.error(error, { json });
|
|
94
|
-
throw error;
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
export const getNonUserAccess = (
|
|
99
|
-
siteDomain: string
|
|
100
|
-
): Promise<GetNonUserAccess> =>
|
|
101
|
-
fetch(`/api/jupiter/v1/non_user_access/${siteDomain}`, {
|
|
102
|
-
method: 'post',
|
|
103
|
-
})
|
|
104
|
-
.then((response) => {
|
|
105
|
-
if (!response.ok) {
|
|
106
|
-
throw new Error(
|
|
107
|
-
`Request not ok (${response.status}: ${response.statusText}).`
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
return response.json();
|
|
111
|
-
})
|
|
112
|
-
.then((json) => {
|
|
113
|
-
try {
|
|
114
|
-
return GetNonUserAccessSchema.check(json);
|
|
115
|
-
} catch (error) {
|
|
116
|
-
log.error(error, { json });
|
|
117
|
-
throw error;
|
|
118
|
-
}
|
|
119
|
-
});
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import fetch from 'jest-fetch-mock';
|
|
2
|
-
import { ValidationError } from 'runtypes';
|
|
3
|
-
import {
|
|
4
|
-
activateSubscription,
|
|
5
|
-
AffiliationType,
|
|
6
|
-
getPrimarySite,
|
|
7
|
-
UserDeactivatedError,
|
|
8
|
-
} from './mercury';
|
|
9
|
-
import log from '../utils/log';
|
|
10
|
-
|
|
11
|
-
describe('activateSubscription()', () => {
|
|
12
|
-
it('should throw error if user is deactivated', async () => {
|
|
13
|
-
fetch.mockResponse(JSON.stringify({ status: 'user_deactivated' }), {
|
|
14
|
-
status: 401,
|
|
15
|
-
});
|
|
16
|
-
await expect(
|
|
17
|
-
activateSubscription(['newspaper'], 'www.telen.no')
|
|
18
|
-
).rejects.toThrow(UserDeactivatedError);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it('should return empty list if user not found', async () => {
|
|
22
|
-
fetch.mockResponse(JSON.stringify({ status: 'no_session_found' }), {
|
|
23
|
-
status: 401,
|
|
24
|
-
});
|
|
25
|
-
await expect(
|
|
26
|
-
activateSubscription(['newspaper'], 'www.telen.no')
|
|
27
|
-
).resolves.toEqual([]);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should return list if access features even if requested access is not "granted"', async () => {
|
|
31
|
-
fetch.mockResponse(
|
|
32
|
-
JSON.stringify({
|
|
33
|
-
status: 'subscription_not_found',
|
|
34
|
-
access: 'denied',
|
|
35
|
-
access_features: ['newspaper'],
|
|
36
|
-
}),
|
|
37
|
-
{
|
|
38
|
-
status: 400,
|
|
39
|
-
}
|
|
40
|
-
);
|
|
41
|
-
await expect(
|
|
42
|
-
activateSubscription(['newspaper'], 'www.telen.no')
|
|
43
|
-
).resolves.toEqual(['newspaper']);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('should return list of access features even if status is not "success"', async () => {
|
|
47
|
-
fetch.mockResponse(
|
|
48
|
-
JSON.stringify({
|
|
49
|
-
status: 'unknown',
|
|
50
|
-
access: 'granted',
|
|
51
|
-
access_features: ['newspaper'],
|
|
52
|
-
})
|
|
53
|
-
);
|
|
54
|
-
await expect(
|
|
55
|
-
activateSubscription(['newspaper'], 'www.telen.no')
|
|
56
|
-
).resolves.toEqual(['newspaper']);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('should return ["newspaper"] if status is "success" and access is "granted"', async () => {
|
|
60
|
-
fetch.mockResponse(
|
|
61
|
-
JSON.stringify({
|
|
62
|
-
status: 'success',
|
|
63
|
-
access: 'granted',
|
|
64
|
-
access_features: ['newspaper'],
|
|
65
|
-
})
|
|
66
|
-
);
|
|
67
|
-
await expect(
|
|
68
|
-
activateSubscription(['newspaper'], 'www.telen.no')
|
|
69
|
-
).resolves.toEqual(['newspaper']);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
describe('GET /primary_site', () => {
|
|
74
|
-
describe('with bad response', () => {
|
|
75
|
-
beforeEach(() => {
|
|
76
|
-
fetch.mockResponseOnce(JSON.stringify({ status: 'no_session_found' }), {
|
|
77
|
-
status: 400,
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('should return null', async () => {
|
|
82
|
-
await expect(getPrimarySite(['newspaper'])).resolves.toEqual(null);
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
describe('with invalid schema', () => {
|
|
87
|
-
beforeEach(() => {
|
|
88
|
-
log.error = jest.fn();
|
|
89
|
-
fetch.mockResponseOnce(JSON.stringify([{ invalid: 'schema' }]));
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('should return null', async () => {
|
|
93
|
-
await expect(getPrimarySite(['newspaper'])).resolves.toBeNull();
|
|
94
|
-
expect(log.error).toHaveBeenCalledWith(jasmine.any(ValidationError), {
|
|
95
|
-
json: [{ invalid: 'schema' }],
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
describe('with valid data', () => {
|
|
101
|
-
beforeEach(() => {
|
|
102
|
-
log.error = jest.fn();
|
|
103
|
-
fetch.mockResponseOnce(
|
|
104
|
-
JSON.stringify({
|
|
105
|
-
name: 'Telen',
|
|
106
|
-
domain: 'www.telen.no',
|
|
107
|
-
affiliation: 'access',
|
|
108
|
-
affiliations: ['access', 'family', 'subscription'],
|
|
109
|
-
})
|
|
110
|
-
);
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it('should return expected', async () => {
|
|
114
|
-
await expect(getPrimarySite(['newspaper'])).resolves.toEqual({
|
|
115
|
-
name: 'Telen',
|
|
116
|
-
domain: 'www.telen.no',
|
|
117
|
-
affiliation: AffiliationType.ACCESS,
|
|
118
|
-
affiliations: [
|
|
119
|
-
AffiliationType.ACCESS,
|
|
120
|
-
AffiliationType.FAMILY,
|
|
121
|
-
AffiliationType.SUBSCRIPTION,
|
|
122
|
-
],
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
});
|
package/src/clients/mercury.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { Array, Literal, Record, Static, String, Union } from 'runtypes';
|
|
2
|
-
import log from '../utils/log';
|
|
3
|
-
|
|
4
|
-
export class UserDeactivatedError extends Error {}
|
|
5
|
-
|
|
6
|
-
const EXPECTED_ERRORS = [
|
|
7
|
-
'no_session_found', // Not logged in
|
|
8
|
-
'subscription_not_found', // No active subscription
|
|
9
|
-
'publication_not_subscribable', // Activation is not possible on current publication
|
|
10
|
-
];
|
|
11
|
-
|
|
12
|
-
export enum AffiliationType {
|
|
13
|
-
ACCESS = 'access',
|
|
14
|
-
FAMILY = 'family',
|
|
15
|
-
SUBSCRIPTION = 'subscription',
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const PrimarySiteSchema = Record({
|
|
19
|
-
domain: String,
|
|
20
|
-
name: String,
|
|
21
|
-
affiliation: Literal(AffiliationType.ACCESS),
|
|
22
|
-
affiliations: Array(
|
|
23
|
-
Union(
|
|
24
|
-
Literal(AffiliationType.ACCESS),
|
|
25
|
-
Literal(AffiliationType.FAMILY),
|
|
26
|
-
Literal(AffiliationType.SUBSCRIPTION)
|
|
27
|
-
)
|
|
28
|
-
),
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
export type PrimarySite = Static<typeof PrimarySiteSchema>;
|
|
32
|
-
|
|
33
|
-
export const activateSubscription = async (
|
|
34
|
-
accessFeatures: string[],
|
|
35
|
-
siteDomain: string
|
|
36
|
-
): Promise<string[]> =>
|
|
37
|
-
fetch(
|
|
38
|
-
`/api/mercury/v2/access/activate?publication_wwwdomain=${siteDomain}&access_features=${accessFeatures.join(
|
|
39
|
-
','
|
|
40
|
-
)}`,
|
|
41
|
-
{
|
|
42
|
-
method: 'post',
|
|
43
|
-
}
|
|
44
|
-
).then(async (response) => {
|
|
45
|
-
const body = await response.text();
|
|
46
|
-
let data;
|
|
47
|
-
try {
|
|
48
|
-
data = JSON.parse(body);
|
|
49
|
-
} catch (error) {
|
|
50
|
-
log.error(
|
|
51
|
-
`Activate subscription failed. Mercury (HTTP/${response.status}: ${body})`,
|
|
52
|
-
{ error, response, accessFeatures, body }
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
if (!response.ok) {
|
|
56
|
-
// User deactivated
|
|
57
|
-
if (data?.status === 'user_deactivated') {
|
|
58
|
-
throw new UserDeactivatedError();
|
|
59
|
-
}
|
|
60
|
-
if (EXPECTED_ERRORS.includes(data?.status)) {
|
|
61
|
-
return data?.access_features || [];
|
|
62
|
-
}
|
|
63
|
-
log.error(
|
|
64
|
-
`Activate subscription failed. Mercury (HTTP/${response.status}: ${body})`,
|
|
65
|
-
{ response, accessFeatures, body }
|
|
66
|
-
);
|
|
67
|
-
return [];
|
|
68
|
-
}
|
|
69
|
-
return data?.access_features;
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
export const getPrimarySite = (
|
|
73
|
-
accessFeatures: string[]
|
|
74
|
-
): Promise<PrimarySite | null> =>
|
|
75
|
-
fetch(
|
|
76
|
-
`/api/mercury/v2/access/primary_site?${new URLSearchParams({
|
|
77
|
-
access_features: accessFeatures.join(','),
|
|
78
|
-
})}`,
|
|
79
|
-
{
|
|
80
|
-
method: 'get',
|
|
81
|
-
}
|
|
82
|
-
)
|
|
83
|
-
.then((response) => {
|
|
84
|
-
if (!response.ok) {
|
|
85
|
-
log.error(
|
|
86
|
-
`Failed to get primary site for user: Request not ok (${response.status}: ${response.statusText}).`
|
|
87
|
-
);
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
if (response.status === 204) {
|
|
91
|
-
return null; // No primary site found
|
|
92
|
-
}
|
|
93
|
-
return response.json();
|
|
94
|
-
})
|
|
95
|
-
.then((json) => {
|
|
96
|
-
if (json === null) {
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
try {
|
|
100
|
-
return PrimarySiteSchema.check(json);
|
|
101
|
-
} catch (error) {
|
|
102
|
-
log.error(error, { json });
|
|
103
|
-
return null;
|
|
104
|
-
}
|
|
105
|
-
});
|