@schibsted/account-sdk-browser 4.8.2 → 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.2",
3
+ "version": "4.8.3",
4
4
  "description": "Schibsted account SDK for browsers",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -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
@@ -110,6 +110,8 @@ import version from './version.js';
110
110
  * @property {boolean} tracking - (Only for connected users)
111
111
  * @property {boolean} clientAgreementAccepted - (Only for connected users)
112
112
  * @property {boolean} defaultAgreementAccepted - (Only for connected users)
113
+ * @property {string} pairId
114
+ * @property {string} sdrn
113
115
  */
114
116
 
115
117
  /**
@@ -592,8 +594,10 @@ export class Identity extends EventEmitter {
592
594
 
593
595
  /**
594
596
  * @async
595
- * @summary In Schibsted account, there are two ways of identifying a user; the `userId` and the
596
- * `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
597
601
  * since Schibsted account is deployed separately in Norway and Sweden, there are a lot of
598
602
  * duplicates. The `userId` was introduced early, so many sites still need to use them for
599
603
  * legacy reasons. The `uuid` is universally unique, and so — if we could disregard a lot of
@@ -611,6 +615,76 @@ export class Identity extends EventEmitter {
611
615
  throw new SDKError('The user is not connected to this merchant');
612
616
  }
613
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
+
614
688
  /**
615
689
  * @async
616
690
  * @summary In Schibsted account, there are two ways of identifying a user; the `userId` and the
@@ -849,7 +923,9 @@ export class Identity extends EventEmitter {
849
923
  * @param {SimplifiedLoginWidgetLoginOptions} loginParams - the same as `options` param for login function. Login will be called on user
850
924
  * continue action. `state` might be string or async function.
851
925
  * @param {SimplifiedLoginWidgetOptions} [options] - additional configuration of Simplified Login Widget
852
- * @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
853
929
  */
854
930
  async showSimplifiedLoginWidget(loginParams, options) {
855
931
  // getUserContextData doesn't throw exception
@@ -900,8 +976,24 @@ export class Identity extends EventEmitter {
900
976
  this.login(Object.assign(await prepareLoginParams(loginParams), {loginHint: userData.identifier, prompt: 'login'}));
901
977
  };
902
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
+
903
995
  if (window.openSimplifiedLoginWidget) {
904
- window.openSimplifiedLoginWidget(initialParams, loginHandler, loginNotYouHandler);
996
+ window.openSimplifiedLoginWidget(initialParams, loginHandler, loginNotYouHandler, initHandler, cancelLoginHandler);
905
997
  return resolve(true);
906
998
  }
907
999
 
@@ -909,7 +1001,7 @@ export class Identity extends EventEmitter {
909
1001
  simplifiedLoginWidget.type = "text/javascript";
910
1002
  simplifiedLoginWidget.src = widgetUrl;
911
1003
  simplifiedLoginWidget.onload = () => {
912
- window.openSimplifiedLoginWidget(initialParams, loginHandler, loginNotYouHandler);
1004
+ window.openSimplifiedLoginWidget(initialParams, loginHandler, loginNotYouHandler, initHandler, cancelLoginHandler);
913
1005
  resolve(true);
914
1006
  };
915
1007
  simplifiedLoginWidget.onerror = () => {
package/src/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // Automatically generated in 'npm version' by scripts/genversion.js
2
2
 
3
3
  'use strict'
4
- const version = '4.8.2';
4
+ const version = '4.8.3';
5
5
  export default version;