@schibsted/account-sdk-browser 4.8.1 → 4.8.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@schibsted/account-sdk-browser",
3
- "version": "4.8.1",
3
+ "version": "4.8.3",
4
4
  "description": "Schibsted account SDK for browsers",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -15,6 +15,7 @@
15
15
  "cover": "jest --coverage",
16
16
  "postcover": "codecov",
17
17
  "preversion": "npm test",
18
+ "version": "node ./scripts/genversion.js && git add src/version.js",
18
19
  "postversion": "git push && git push --tags"
19
20
  },
20
21
  "author": "",
@@ -24,7 +25,7 @@
24
25
  },
25
26
  "devDependencies": {
26
27
  "@babel/core": "^7.11.4",
27
- "@babel/preset-env": "^7.11.0",
28
+ "@babel/preset-env": "^7.23.2",
28
29
  "babel-loader": "^8.1.0",
29
30
  "codecov": "^3.6.5",
30
31
  "core-js": "^3.6.5",
@@ -5,6 +5,6 @@
5
5
  'use strict';
6
6
 
7
7
  window.regeneratorRuntime = require('regenerator-runtime');
8
- const { Identity } = require('../identity');
8
+ const { Identity } = require('../identity.js');
9
9
 
10
10
  module.exports = { Identity };
package/src/identity.js CHANGED
@@ -14,8 +14,7 @@ import * as popup from './popup.js';
14
14
  import RESTClient from './RESTClient.js';
15
15
  import SDKError from './SDKError.js';
16
16
  import * as spidTalk from './spidTalk.js';
17
- import packageJson from '../package.json';
18
- const { version } = packageJson;
17
+ import version from './version.js';
19
18
 
20
19
  /**
21
20
  * @typedef {object} LoginOptions
@@ -111,6 +110,8 @@ const { version } = packageJson;
111
110
  * @property {boolean} tracking - (Only for connected users)
112
111
  * @property {boolean} clientAgreementAccepted - (Only for connected users)
113
112
  * @property {boolean} defaultAgreementAccepted - (Only for connected users)
113
+ * @property {string} pairId
114
+ * @property {string} sdrn
114
115
  */
115
116
 
116
117
  /**
@@ -593,8 +594,10 @@ export class Identity extends EventEmitter {
593
594
 
594
595
  /**
595
596
  * @async
596
- * @summary In Schibsted account, there are two ways of identifying a user; the `userId` and the
597
- * `uuid`. There are reasons for them both existing. The `userId` is a numeric identifier, but
597
+ * @summary
598
+ * In Schibsted account, there are multiple ways of identifying a user; the `userId`,
599
+ * `uuid` and `externalId` used for identifying a user-merchant pair (see {@link Identity#getExternalId}).
600
+ * There are reasons for them all to exist. The `userId` is a numeric identifier, but
598
601
  * since Schibsted account is deployed separately in Norway and Sweden, there are a lot of
599
602
  * duplicates. The `userId` was introduced early, so many sites still need to use them for
600
603
  * legacy reasons. The `uuid` is universally unique, and so — if we could disregard a lot of
@@ -612,6 +615,76 @@ export class Identity extends EventEmitter {
612
615
  throw new SDKError('The user is not connected to this merchant');
613
616
  }
614
617
 
618
+ /**
619
+ * @async
620
+ * @function
621
+ * @summary
622
+ * Retrieves the external identifier (`externalId`) for the authenticated user.
623
+ *
624
+ * In Schibsted Account there are multiple ways of identifying users, however for integrations with
625
+ * third-parties it's recommended to use `externalId` as it does not disclose
626
+ * any critical data whilst allowing for user identification.
627
+ *
628
+ * `externalId` is merchant-scoped using a pairwise identifier (`pairId`),
629
+ * meaning the same user's ID will differ between merchants.
630
+ * Additionally, this identifier is bound to the external party provided as argument.
631
+ *
632
+ * @description This function calls {@link Identity#hasSession} internally and thus has the side
633
+ * effect that it might perform an auto-login on the user
634
+ * @throws {SDKError} If the `pairId` is missing in user session.
635
+ * @throws {SDKError} If the `externalParty` is not defined
636
+ * @return {Promise<string>} The merchant- and 3rd-party-specific `externalId`
637
+ */
638
+ async getExternalId(externalParty, optionalSuffix = "") {
639
+ const { pairId } = await this.hasSession();
640
+
641
+ if (!pairId)
642
+ throw new SDKError('pairId missing in user session!');
643
+
644
+ if(!externalParty || externalParty.length === 0) {
645
+ throw new SDKError('externalParty cannot be empty');
646
+ }
647
+ const _toHexDigest = (hashBuffer) =>{
648
+ // convert buffer to byte array
649
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
650
+ // convert bytes to hex string
651
+ return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
652
+ }
653
+
654
+ const _getSha256Digest = (data) => {
655
+ return crypto.subtle.digest('SHA-256', data);
656
+ }
657
+
658
+ const _hashMessage = async (message) => {
659
+ const msgUint8 = new TextEncoder().encode(message);
660
+ return _getSha256Digest(msgUint8).then( (it) => _toHexDigest(it));
661
+ }
662
+
663
+ const _constructMessage = (pairId, externalParty, optionalSuffix) => {
664
+ return optionalSuffix
665
+ ? `${pairId}:${externalParty}:${optionalSuffix}`
666
+ : `${pairId}:${externalParty}`;
667
+ }
668
+
669
+ return _hashMessage(_constructMessage(pairId, externalParty, optionalSuffix))
670
+ }
671
+
672
+ /**
673
+ * @async
674
+ * @summary Enables brands to programmatically get the current the SDRN based on the user's session.
675
+ * @description This function calls {@link Identity#hasSession} internally and thus has the side
676
+ * effect that it might perform an auto-login on the user
677
+ * @throws {SDKError} If the SDRN is missing in user session object.
678
+ * @returns {Promise<string>}
679
+ */
680
+ async getUserSDRN() {
681
+ const { sdrn } = await this.hasSession();
682
+ if (sdrn) {
683
+ return sdrn;
684
+ }
685
+ throw new SDKError('Failed to get SDRN from user session');
686
+ }
687
+
615
688
  /**
616
689
  * @async
617
690
  * @summary In Schibsted account, there are two ways of identifying a user; the `userId` and the
@@ -850,7 +923,9 @@ export class Identity extends EventEmitter {
850
923
  * @param {SimplifiedLoginWidgetLoginOptions} loginParams - the same as `options` param for login function. Login will be called on user
851
924
  * continue action. `state` might be string or async function.
852
925
  * @param {SimplifiedLoginWidgetOptions} [options] - additional configuration of Simplified Login Widget
853
- * @return {Promise<boolean|SDKError>} - will resolve to true if widget will be display. Otherwise will throw SDKError
926
+ * @fires Identity#simplifiedLoginOpened
927
+ * @fires Identity#simplifiedLoginCancelled
928
+ * @return {Promise<boolean|SDKError>} - will resolve to true if widget will be display. Otherwise, will throw SDKError
854
929
  */
855
930
  async showSimplifiedLoginWidget(loginParams, options) {
856
931
  // getUserContextData doesn't throw exception
@@ -901,8 +976,24 @@ export class Identity extends EventEmitter {
901
976
  this.login(Object.assign(await prepareLoginParams(loginParams), {loginHint: userData.identifier, prompt: 'login'}));
902
977
  };
903
978
 
979
+ const initHandler = () => {
980
+ /**
981
+ * Emitted when the simplified login widget is displayed on the screen
982
+ * @event Identity#simplifiedLoginOpened
983
+ */
984
+ this.emit('simplifiedLoginOpened');
985
+ }
986
+
987
+ const cancelLoginHandler = () => {
988
+ /**
989
+ * Emitted when the user closes the simplified login widget
990
+ * @event Identity#simplifiedLoginCancelled
991
+ */
992
+ this.emit('simplifiedLoginCancelled');
993
+ }
994
+
904
995
  if (window.openSimplifiedLoginWidget) {
905
- window.openSimplifiedLoginWidget(initialParams, loginHandler, loginNotYouHandler);
996
+ window.openSimplifiedLoginWidget(initialParams, loginHandler, loginNotYouHandler, initHandler, cancelLoginHandler);
906
997
  return resolve(true);
907
998
  }
908
999
 
@@ -910,7 +1001,7 @@ export class Identity extends EventEmitter {
910
1001
  simplifiedLoginWidget.type = "text/javascript";
911
1002
  simplifiedLoginWidget.src = widgetUrl;
912
1003
  simplifiedLoginWidget.onload = () => {
913
- window.openSimplifiedLoginWidget(initialParams, loginHandler, loginNotYouHandler);
1004
+ window.openSimplifiedLoginWidget(initialParams, loginHandler, loginNotYouHandler, initHandler, cancelLoginHandler);
914
1005
  resolve(true);
915
1006
  };
916
1007
  simplifiedLoginWidget.onerror = () => {
@@ -12,8 +12,7 @@ import RESTClient from './RESTClient.js';
12
12
  import Cache from './cache.js';
13
13
  import * as spidTalk from './spidTalk.js';
14
14
  import SDKError from './SDKError.js';
15
- import packageJson from '../package.json';
16
- const { version } = packageJson;
15
+ import version from './version.js';
17
16
 
18
17
  const globalWindow = () => window;
19
18
 
@@ -0,0 +1,2 @@
1
+ declare const version: string;
2
+ export default version;
package/src/version.js ADDED
@@ -0,0 +1,5 @@
1
+ // Automatically generated in 'npm version' by scripts/genversion.js
2
+
3
+ 'use strict'
4
+ const version = '4.8.3';
5
+ export default version;