@schibsted/account-sdk-browser 6.0.0-alpha.7 → 6.0.0-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -4
- package/dist/account.d.ts +19 -7
- package/dist/identity.d.ts +23 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +38 -23
- package/dist/index.js.map +1 -1
- package/dist/session-response.d.ts +24 -0
- package/dist/types/account.d.ts +2 -0
- package/dist/types/session.d.ts +2 -5
- package/dist/version.d.ts +1 -1
- package/package.json +1 -1
- package/src/account.ts +39 -8
- package/src/has-access-response.ts +1 -1
- package/src/identity.ts +61 -2
- package/src/index.ts +8 -0
- package/src/session-response.ts +25 -4
- package/src/types/account.ts +2 -0
- package/src/types/session.ts +2 -5
- package/src/version.ts +1 -1
|
@@ -1,8 +1,32 @@
|
|
|
1
1
|
import { ConnectedSessionResponse, ConnectedSessionWithUserIdResponse, NormalizedSessionPayload, RedirectSessionPayload, SessionFailureResponse, SessionResponse, SessionSuccessResponse } from './types/session.js';
|
|
2
|
+
/**
|
|
3
|
+
* Checks whether a value is a successful session response.
|
|
4
|
+
*
|
|
5
|
+
* @param value - The value to check.
|
|
6
|
+
* @group Session
|
|
7
|
+
*/
|
|
2
8
|
export declare function isSessionSuccessResponse(value: unknown): value is SessionSuccessResponse;
|
|
9
|
+
/**
|
|
10
|
+
* Checks whether a successful session response represents a user connected to the merchant.
|
|
11
|
+
*
|
|
12
|
+
* @param value - The value to check.
|
|
13
|
+
* @group Session
|
|
14
|
+
*/
|
|
3
15
|
export declare function isConnectedSessionResponse(value: unknown): value is ConnectedSessionResponse;
|
|
16
|
+
/**
|
|
17
|
+
* Checks whether a value is a session failure response.
|
|
18
|
+
*
|
|
19
|
+
* @param value - The value to check.
|
|
20
|
+
* @group Session
|
|
21
|
+
*/
|
|
4
22
|
export declare function isSessionFailureResponse(value: unknown): value is SessionFailureResponse;
|
|
5
23
|
export declare function isRedirectSessionPayload(value: unknown): value is RedirectSessionPayload;
|
|
24
|
+
/**
|
|
25
|
+
* Checks whether a connected session response contains a usable user id.
|
|
26
|
+
*
|
|
27
|
+
* @param value - The value to check.
|
|
28
|
+
* @group Session
|
|
29
|
+
*/
|
|
6
30
|
export declare function isConnectedSessionWithUserIdResponse(value: SessionResponse): value is ConnectedSessionWithUserIdResponse;
|
|
7
31
|
/**
|
|
8
32
|
* Converts an unknown Session Service `hasSession` payload into one of the shapes the SDK knows how
|
package/dist/types/account.d.ts
CHANGED
|
@@ -10,6 +10,8 @@ export type AccountConfig = AccountBaseConfig & {
|
|
|
10
10
|
log?: LogFunction;
|
|
11
11
|
/** Callback triggered before a session refresh redirect happens. */
|
|
12
12
|
callbackBeforeRedirect?: IdentityBeforeRedirectCallback;
|
|
13
|
+
/** Product or feature ids used by `hasAccess()` when no ids are passed to the method. */
|
|
14
|
+
defaultProductIds?: string[];
|
|
13
15
|
/** Optional Varnish cookie setup applied during Account initialization. */
|
|
14
16
|
varnish?: VarnishCookieOptions;
|
|
15
17
|
};
|
package/dist/types/session.d.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* Session Service historically returned numeric ids, while some integrations and tests use
|
|
5
|
-
* string ids. SDK convenience methods normalize this where they promise a string.
|
|
2
|
+
* Numeric Schibsted Account user id.
|
|
6
3
|
*
|
|
7
4
|
* @group Session
|
|
8
5
|
*/
|
|
9
|
-
export type SessionUserId = number
|
|
6
|
+
export type SessionUserId = number;
|
|
10
7
|
/**
|
|
11
8
|
* Successful response returned from {@link Account#hasSession}.
|
|
12
9
|
*
|
package/dist/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const version = "6.0.0-alpha.
|
|
1
|
+
declare const version = "6.0.0-alpha.8";
|
|
2
2
|
export default version;
|
package/package.json
CHANGED
package/src/account.ts
CHANGED
|
@@ -28,6 +28,7 @@ import type { ConnectedSessionResponse, SessionResponse, SessionUserId } from '.
|
|
|
28
28
|
export class Account {
|
|
29
29
|
#identity: Identity;
|
|
30
30
|
#monetization: Monetization;
|
|
31
|
+
#defaultProductIds?: string[];
|
|
31
32
|
|
|
32
33
|
constructor(config: AccountConfig) {
|
|
33
34
|
const browserWindow = resolveBrowserWindow(config.window);
|
|
@@ -40,7 +41,7 @@ export class Account {
|
|
|
40
41
|
throw new SDKError('Schibsted Account SDK has already been initialized');
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
const { log, callbackBeforeRedirect, varnish } = config;
|
|
44
|
+
const { log, callbackBeforeRedirect, defaultProductIds, varnish } = config;
|
|
44
45
|
const sharedConfig = {
|
|
45
46
|
clientId: config.clientId,
|
|
46
47
|
redirectUri: config.redirectUri,
|
|
@@ -55,6 +56,11 @@ export class Account {
|
|
|
55
56
|
callbackBeforeRedirect,
|
|
56
57
|
});
|
|
57
58
|
|
|
59
|
+
if (defaultProductIds !== undefined && !Array.isArray(defaultProductIds)) {
|
|
60
|
+
throw new SDKError('Account defaultProductIds config must be an array');
|
|
61
|
+
}
|
|
62
|
+
this.#defaultProductIds = defaultProductIds;
|
|
63
|
+
|
|
58
64
|
if (varnish) {
|
|
59
65
|
if (typeof varnish !== 'object' || Array.isArray(varnish)) {
|
|
60
66
|
throw new SDKError('Account varnish config must be an object');
|
|
@@ -111,16 +117,30 @@ export class Account {
|
|
|
111
117
|
}
|
|
112
118
|
|
|
113
119
|
/**
|
|
114
|
-
* Returns the realm-specific Schibsted Account user id as a
|
|
120
|
+
* Returns the realm-specific Schibsted Account user id as a number.
|
|
115
121
|
*
|
|
116
122
|
* Use `getUserUuid()` when you need the globally unique user id.
|
|
117
123
|
*
|
|
118
124
|
* @throws {SDKError} If the user is not connected to the configured client.
|
|
119
125
|
*/
|
|
120
|
-
getUserId(): Promise<
|
|
126
|
+
getUserId(): Promise<number> {
|
|
121
127
|
return this.#identity.getUserId();
|
|
122
128
|
}
|
|
123
129
|
|
|
130
|
+
/**
|
|
131
|
+
* Returns an external identifier for the authenticated user, scoped to the configured client
|
|
132
|
+
* and external party.
|
|
133
|
+
*
|
|
134
|
+
* @param externalParty - External party for which the identifier is generated.
|
|
135
|
+
* @param optionalSuffix - Optional suffix included when generating the identifier.
|
|
136
|
+
* @throws {SDKError} If the session lookup fails, the session does not contain a `pairId`, or
|
|
137
|
+
* `externalParty` is empty.
|
|
138
|
+
* @deprecated Retained for compatibility with existing integrations. Avoid new usage.
|
|
139
|
+
*/
|
|
140
|
+
getExternalId(externalParty: string, optionalSuffix: string = ''): Promise<string> {
|
|
141
|
+
return this.#identity.getExternalId(externalParty, optionalSuffix);
|
|
142
|
+
}
|
|
143
|
+
|
|
124
144
|
/**
|
|
125
145
|
* Returns the globally unique user id from the current session.
|
|
126
146
|
*
|
|
@@ -238,16 +258,27 @@ export class Account {
|
|
|
238
258
|
}
|
|
239
259
|
|
|
240
260
|
/**
|
|
241
|
-
* Checks whether
|
|
261
|
+
* Checks whether the current user has access to any of the requested products or features.
|
|
242
262
|
*
|
|
263
|
+
* When `productIds` is omitted, the ids configured as `defaultProductIds` are used.
|
|
243
264
|
* Results are cached according to the TTL returned by Session Service.
|
|
244
265
|
*
|
|
245
|
-
* @param productIds - Product or feature ids to check.
|
|
246
|
-
* @param userId - Current user id, usually from `getUserId()`.
|
|
266
|
+
* @param productIds - Product or feature ids to check instead of the configured defaults.
|
|
247
267
|
* @returns Access data, or `null` when the user is not entitled.
|
|
248
|
-
* @throws {SDKError} If
|
|
268
|
+
* @throws {SDKError} If product ids are not provided or configured, `sessionDomain` is missing,
|
|
269
|
+
* the current user cannot be resolved, input is invalid, or the request fails.
|
|
249
270
|
*/
|
|
250
|
-
hasAccess(productIds
|
|
271
|
+
async hasAccess(productIds = this.#defaultProductIds): Promise<HasAccessResult | null> {
|
|
272
|
+
if (!productIds) {
|
|
273
|
+
throw new SDKError(
|
|
274
|
+
'hasAccess requires productIds or defaultProductIds in the Account config',
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
if (productIds.length === 0) {
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const userId = await this.#identity.getUserId();
|
|
251
282
|
return this.#monetization.hasAccess(productIds, userId);
|
|
252
283
|
}
|
|
253
284
|
|
|
@@ -17,7 +17,7 @@ export function parseHasAccessResult(value: unknown): HasAccessResult {
|
|
|
17
17
|
typeof entitled !== 'boolean' ||
|
|
18
18
|
typeof ttl !== 'number' ||
|
|
19
19
|
!Array.isArray(allowedFeatures) ||
|
|
20
|
-
|
|
20
|
+
typeof userId !== 'number' ||
|
|
21
21
|
!isStr(uuid) ||
|
|
22
22
|
!isStr(sig)
|
|
23
23
|
) {
|
package/src/identity.ts
CHANGED
|
@@ -535,14 +535,73 @@ export class Identity {
|
|
|
535
535
|
* @throws {SDKError} If the user isn't connected to the merchant
|
|
536
536
|
* @return The `userId` field (not to be confused with the `uuid`)
|
|
537
537
|
*/
|
|
538
|
-
async getUserId(): Promise<
|
|
538
|
+
async getUserId(): Promise<number> {
|
|
539
539
|
const user = await this.hasSession();
|
|
540
540
|
if (isConnectedSessionWithUserIdResponse(user)) {
|
|
541
|
-
return
|
|
541
|
+
return user.userId;
|
|
542
542
|
}
|
|
543
543
|
throw new SDKError('The user is not connected to this merchant');
|
|
544
544
|
}
|
|
545
545
|
|
|
546
|
+
/**
|
|
547
|
+
* @function
|
|
548
|
+
* @summary
|
|
549
|
+
* Retrieves the external identifier (`externalId`) for the authenticated user.
|
|
550
|
+
*
|
|
551
|
+
* In Schibsted Account there are multiple ways of identifying users, however for integrations with
|
|
552
|
+
* third-parties it's recommended to use `externalId` as it does not disclose
|
|
553
|
+
* any critical data whilst allowing for user identification.
|
|
554
|
+
*
|
|
555
|
+
* `externalId` is merchant-scoped using a pairwise identifier (`pairId`),
|
|
556
|
+
* meaning the same user's ID will differ between merchants.
|
|
557
|
+
* Additionally, this identifier is bound to the external party provided as argument.
|
|
558
|
+
*
|
|
559
|
+
* @param externalParty
|
|
560
|
+
* @param optionalSuffix
|
|
561
|
+
* @remarks This function calls {@link Identity#hasSession} internally and thus has the side
|
|
562
|
+
* effect that it might perform an auto-login on the user
|
|
563
|
+
* @throws {SDKError} If the `pairId` is missing in user session.
|
|
564
|
+
* @throws {SDKError} If the `externalParty` is not defined
|
|
565
|
+
* @return The merchant- and 3rd-party-specific `externalId`
|
|
566
|
+
*/
|
|
567
|
+
async getExternalId(externalParty: string, optionalSuffix: string = ''): Promise<string> {
|
|
568
|
+
const user = await this.hasSession();
|
|
569
|
+
const pairId = isSessionSuccessResponse(user) ? user.pairId : undefined;
|
|
570
|
+
|
|
571
|
+
if (!pairId) throw new SDKError('pairId missing in user session!');
|
|
572
|
+
|
|
573
|
+
if (!externalParty || externalParty.length === 0) {
|
|
574
|
+
throw new SDKError('externalParty cannot be empty');
|
|
575
|
+
}
|
|
576
|
+
const _toHexDigest = (hashBuffer: ArrayBuffer) => {
|
|
577
|
+
// convert buffer to byte array
|
|
578
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
579
|
+
// convert bytes to hex string
|
|
580
|
+
return hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
const _getSha256Digest = (data: BufferSource) => {
|
|
584
|
+
return crypto.subtle.digest('SHA-256', data);
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
const _hashMessage = async (message: string) => {
|
|
588
|
+
const msgUint8 = new TextEncoder().encode(message);
|
|
589
|
+
return _getSha256Digest(msgUint8).then((it) => _toHexDigest(it));
|
|
590
|
+
};
|
|
591
|
+
|
|
592
|
+
const _constructMessage = (
|
|
593
|
+
pairId: string,
|
|
594
|
+
externalParty: string,
|
|
595
|
+
optionalSuffix: string,
|
|
596
|
+
) => {
|
|
597
|
+
return optionalSuffix
|
|
598
|
+
? `${pairId}:${externalParty}:${optionalSuffix}`
|
|
599
|
+
: `${pairId}:${externalParty}`;
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
return _hashMessage(_constructMessage(pairId, externalParty, optionalSuffix));
|
|
603
|
+
}
|
|
604
|
+
|
|
546
605
|
/**
|
|
547
606
|
* @summary Enables brands to programmatically get the current the SDRN based on the user's session.
|
|
548
607
|
* @remarks This function calls {@link Identity#hasSession} internally and thus has the side
|
package/src/index.ts
CHANGED
|
@@ -7,6 +7,14 @@ export { createAccountSdk, getAccountSdk } from './account-sdk.js';
|
|
|
7
7
|
export * from './globals.js';
|
|
8
8
|
export { default as SDKError } from './sdk-error.js';
|
|
9
9
|
|
|
10
|
+
/** session response type guards */
|
|
11
|
+
export {
|
|
12
|
+
isConnectedSessionResponse,
|
|
13
|
+
isConnectedSessionWithUserIdResponse,
|
|
14
|
+
isSessionFailureResponse,
|
|
15
|
+
isSessionSuccessResponse,
|
|
16
|
+
} from './session-response.js';
|
|
17
|
+
|
|
10
18
|
/* public available types */
|
|
11
19
|
export type {
|
|
12
20
|
AccountConfig,
|
package/src/session-response.ts
CHANGED
|
@@ -13,14 +13,32 @@ import type {
|
|
|
13
13
|
} from './types/session.js';
|
|
14
14
|
import { isObject, isStr } from './validate.js';
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Checks whether a value is a successful session response.
|
|
18
|
+
*
|
|
19
|
+
* @param value - The value to check.
|
|
20
|
+
* @group Session
|
|
21
|
+
*/
|
|
16
22
|
export function isSessionSuccessResponse(value: unknown): value is SessionSuccessResponse {
|
|
17
23
|
return isObject(value) && typeof value.result === 'boolean';
|
|
18
24
|
}
|
|
19
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Checks whether a successful session response represents a user connected to the merchant.
|
|
28
|
+
*
|
|
29
|
+
* @param value - The value to check.
|
|
30
|
+
* @group Session
|
|
31
|
+
*/
|
|
20
32
|
export function isConnectedSessionResponse(value: unknown): value is ConnectedSessionResponse {
|
|
21
33
|
return isSessionSuccessResponse(value) && value.result === true;
|
|
22
34
|
}
|
|
23
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Checks whether a value is a session failure response.
|
|
38
|
+
*
|
|
39
|
+
* @param value - The value to check.
|
|
40
|
+
* @group Session
|
|
41
|
+
*/
|
|
24
42
|
export function isSessionFailureResponse(value: unknown): value is SessionFailureResponse {
|
|
25
43
|
return isObject(value) && Boolean(value.error);
|
|
26
44
|
}
|
|
@@ -29,13 +47,16 @@ export function isRedirectSessionPayload(value: unknown): value is RedirectSessi
|
|
|
29
47
|
return isObject(value) && Object.keys(value).length === 1 && isStr(value.redirectURL);
|
|
30
48
|
}
|
|
31
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Checks whether a connected session response contains a usable user id.
|
|
52
|
+
*
|
|
53
|
+
* @param value - The value to check.
|
|
54
|
+
* @group Session
|
|
55
|
+
*/
|
|
32
56
|
export function isConnectedSessionWithUserIdResponse(
|
|
33
57
|
value: SessionResponse,
|
|
34
58
|
): value is ConnectedSessionWithUserIdResponse {
|
|
35
|
-
return (
|
|
36
|
-
isConnectedSessionResponse(value) &&
|
|
37
|
-
(typeof value.userId === 'number' || typeof value.userId === 'string')
|
|
38
|
-
);
|
|
59
|
+
return isConnectedSessionResponse(value) && typeof value.userId === 'number';
|
|
39
60
|
}
|
|
40
61
|
|
|
41
62
|
/**
|
package/src/types/account.ts
CHANGED
|
@@ -11,6 +11,8 @@ export type AccountConfig = AccountBaseConfig & {
|
|
|
11
11
|
log?: LogFunction;
|
|
12
12
|
/** Callback triggered before a session refresh redirect happens. */
|
|
13
13
|
callbackBeforeRedirect?: IdentityBeforeRedirectCallback;
|
|
14
|
+
/** Product or feature ids used by `hasAccess()` when no ids are passed to the method. */
|
|
15
|
+
defaultProductIds?: string[];
|
|
14
16
|
/** Optional Varnish cookie setup applied during Account initialization. */
|
|
15
17
|
varnish?: VarnishCookieOptions;
|
|
16
18
|
};
|
package/src/types/session.ts
CHANGED
|
@@ -3,14 +3,11 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* Session Service historically returned numeric ids, while some integrations and tests use
|
|
9
|
-
* string ids. SDK convenience methods normalize this where they promise a string.
|
|
6
|
+
* Numeric Schibsted Account user id.
|
|
10
7
|
*
|
|
11
8
|
* @group Session
|
|
12
9
|
*/
|
|
13
|
-
export type SessionUserId = number
|
|
10
|
+
export type SessionUserId = number;
|
|
14
11
|
|
|
15
12
|
// Session Service object payloads.
|
|
16
13
|
|
package/src/version.ts
CHANGED