@opencampus/ocid-connect-js 1.2.1 → 1.2.2

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 CHANGED
@@ -110,7 +110,7 @@ Use useOCAuth hook to read credentials info
110
110
  import { useOCAuth } from '@opencampus/ocid-connect-js';
111
111
 
112
112
  const UserTokenPage = (props) => {
113
- const { authState, ocAuth } = useOCAuth();
113
+ const { authState, ocAuth, OCId, ethAddress } = useOCAuth();
114
114
 
115
115
  if (authState.error !== undefined) {
116
116
  return <div>Error: {authState.error.message}</div>;
@@ -119,8 +119,10 @@ const UserTokenPage = (props) => {
119
119
  <div>
120
120
  <h4>User Info</h4>
121
121
  <pre>
122
- { JSON.stringify(ocAuth.getAuthInfo(), null, 2) }
122
+ { JSON.stringify(ocAuth.getAuthState(), null, 2) }
123
123
  </pre>
124
+ <pre>{OCId}</pre>
125
+ <pre>{ethAddress}</pre>
124
126
  </div>
125
127
  );
126
128
  }
@@ -298,7 +300,7 @@ export default function Home() {
298
300
  <div>
299
301
  <h1>Welcome to My App</h1>
300
302
  {authState.isAuthenticated ? (
301
- <p>You are logged in! {JSON.stringify(ocAuth.getAuthInfo())}</p>
303
+ <p>You are logged in! {JSON.stringify(ocAuth.getAuthState())}</p>
302
304
 
303
305
  ) : (
304
306
  <LoginButton />
@@ -328,8 +330,8 @@ Main Methods of Auth SDK
328
330
  | --- | --- |
329
331
  | signInWithRedirect | Initialize login process. Accept "state" as an input |
330
332
  | handleLoginRedirect | Return the auth state of the login process |
331
- | getAuthInfo | Return auth object { edu_username, eth_address } |
332
- | getAuthState | Return auth state data { accessToken, idToken, isAuthenticated } |
333
+ | getAuthState | Return auth state data { accessToken, idToken, OCId, ethAddress, isAuthenticated } |
334
+ | getStateParameter() | Return the state that was initialized in signin process |
333
335
 
334
336
  Sample usage
335
337
 
@@ -369,5 +371,12 @@ try {
369
371
  }
370
372
  ```
371
373
 
374
+ Access OCId info of Auth SDK
375
+
376
+ | property | Description |
377
+ | --- | --- |
378
+ | OCId | return OC ID |
379
+ | ethAddress | Return eth Wallet Address that connect to the ID |
380
+
372
381
  ### License
373
382
  ocid-connect-js is released under the MIT license.
@@ -360,51 +360,36 @@ var LoginCallBack = function LoginCallBack(_ref) {
360
360
  var _useOCAuth = Object(_OCContext__WEBPACK_IMPORTED_MODULE_1__["useOCAuth"])(),
361
361
  ocAuth = _useOCAuth.ocAuth,
362
362
  authState = _useOCAuth.authState,
363
- updateAuthState = _useOCAuth.updateAuthState;
363
+ setAuthError = _useOCAuth.setAuthError;
364
364
  Object(react__WEBPACK_IMPORTED_MODULE_0__["useEffect"])(function () {
365
365
  var handleLogin = /*#__PURE__*/function () {
366
366
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
367
- var _authState;
368
367
  return _regeneratorRuntime().wrap(function _callee$(_context) {
369
368
  while (1) switch (_context.prev = _context.next) {
370
369
  case 0:
371
370
  if (!ocAuth) {
372
- _context.next = 12;
371
+ _context.next = 11;
373
372
  break;
374
373
  }
375
374
  _context.prev = 1;
376
375
  _context.next = 4;
377
376
  return ocAuth.handleLoginRedirect();
378
377
  case 4:
379
- _authState = _context.sent;
380
- if (_authState.idToken) {
381
- updateAuthState(_authState);
382
- handledRedirect = true;
383
- successCallback();
384
- } else {
385
- updateAuthState({
386
- error: 'missing idToken from redirect callback'
387
- });
388
- if (errorCallback) {
389
- errorCallback();
390
- }
391
- }
392
- _context.next = 12;
378
+ successCallback();
379
+ _context.next = 11;
393
380
  break;
394
- case 8:
395
- _context.prev = 8;
381
+ case 7:
382
+ _context.prev = 7;
396
383
  _context.t0 = _context["catch"](1);
397
- updateAuthState({
398
- error: _context.t0
399
- });
384
+ setAuthError(_context.t0);
400
385
  if (errorCallback) {
401
386
  errorCallback();
402
387
  }
403
- case 12:
388
+ case 11:
404
389
  case "end":
405
390
  return _context.stop();
406
391
  }
407
- }, _callee, null, [[1, 8]]);
392
+ }, _callee, null, [[1, 7]]);
408
393
  }));
409
394
  return function handleLogin() {
410
395
  return _ref2.apply(this, arguments);
@@ -458,34 +443,53 @@ var OCConnect = function OCConnect(_ref) {
458
443
  _useState2 = _slicedToArray(_useState, 2),
459
444
  ocAuth = _useState2[0],
460
445
  setOcAuth = _useState2[1];
461
- var _useState3 = Object(react__WEBPACK_IMPORTED_MODULE_0__["useState"])({
446
+ var _useState3 = Object(react__WEBPACK_IMPORTED_MODULE_0__["useState"])(),
447
+ _useState4 = _slicedToArray(_useState3, 2),
448
+ OCId = _useState4[0],
449
+ setOCId = _useState4[1];
450
+ var _useState5 = Object(react__WEBPACK_IMPORTED_MODULE_0__["useState"])(),
451
+ _useState6 = _slicedToArray(_useState5, 2),
452
+ ethAddress = _useState6[0],
453
+ setEthAddress = _useState6[1];
454
+ var _useState7 = Object(react__WEBPACK_IMPORTED_MODULE_0__["useState"])({
462
455
  isAuthenticated: false
463
456
  }),
464
- _useState4 = _slicedToArray(_useState3, 2),
465
- authState = _useState4[0],
466
- setAuthState = _useState4[1];
457
+ _useState8 = _slicedToArray(_useState7, 2),
458
+ authState = _useState8[0],
459
+ setAuthState = _useState8[1];
460
+ var _useState9 = Object(react__WEBPACK_IMPORTED_MODULE_0__["useState"])(null),
461
+ _useState10 = _slicedToArray(_useState9, 2),
462
+ authError = _useState10[0],
463
+ setAuthError = _useState10[1];
464
+ var updateAuthState = function updateAuthState(authState) {
465
+ setAuthState(authState);
466
+ setOCId(authState.OCId);
467
+ setEthAddress(authState.ethAddress);
468
+ };
469
+
470
+ // init sdk
467
471
  Object(react__WEBPACK_IMPORTED_MODULE_0__["useEffect"])(function () {
468
472
  var authSdk = sandboxMode ? new _sdk_auth__WEBPACK_IMPORTED_MODULE_2__["OCAuthSandbox"](opts) : new _sdk_auth__WEBPACK_IMPORTED_MODULE_2__["OCAuthLive"](opts);
469
- setOcAuth(authSdk);
470
473
  updateAuthState(authSdk.getAuthState());
474
+ setOcAuth(authSdk);
471
475
  }, []);
472
- var updateAuthState = function updateAuthState(authState) {
473
- var accessToken = authState.accessToken,
474
- idToken = authState.idToken,
475
- isAuthenticated = authState.isAuthenticated,
476
- error = authState.error;
477
- setAuthState({
478
- accessToken: accessToken,
479
- idToken: idToken,
480
- isAuthenticated: isAuthenticated,
481
- error: error
482
- });
483
- };
476
+ Object(react__WEBPACK_IMPORTED_MODULE_0__["useEffect"])(function () {
477
+ if (ocAuth.authInfoManager) {
478
+ // reactively recieve update on the authstate change
479
+ ocAuth.authInfoManager.subscribe(updateAuthState);
480
+ return function () {
481
+ ocAuth.authInfoManager.unsubscribe(updateAuthState);
482
+ };
483
+ }
484
+ }, [ocAuth]);
484
485
  return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default.a.createElement(_OCContext__WEBPACK_IMPORTED_MODULE_1__["OCContext"].Provider, {
485
486
  value: {
487
+ OCId: OCId,
488
+ ethAddress: ethAddress,
486
489
  ocAuth: ocAuth,
487
490
  authState: authState,
488
- updateAuthState: updateAuthState
491
+ authError: authError,
492
+ setAuthError: setAuthError
489
493
  }
490
494
  }, children);
491
495
  };
@@ -639,7 +643,7 @@ var OCAuthCore = /*#__PURE__*/function () {
639
643
  break;
640
644
  }
641
645
  _context3.next = 7;
642
- return this.tokenManager.exchangeTokenFromCode(urlParams.code, codeVerifier);
646
+ return this.tokenManager.exchangeTokenFromCode(urlParams.code, codeVerifier, urlParams.state);
643
647
  case 7:
644
648
  // clear transaction meta, coz it's completed
645
649
  this.transactionManager.clear();
@@ -675,26 +679,18 @@ var OCAuthCore = /*#__PURE__*/function () {
675
679
  var _this$getParsedIdToke = this.getParsedIdToken(),
676
680
  edu_username = _this$getParsedIdToke.edu_username,
677
681
  eth_address = _this$getParsedIdToke.eth_address;
678
- this.authInfoManager.setIdInfo({
679
- edu_username: edu_username,
680
- eth_address: eth_address
681
- });
682
+ this.authInfoManager.setAuthState(this.getAccessToken(), this.getIdToken(), edu_username, eth_address, this.isAuthenticated());
682
683
  }
683
684
  }
684
685
  }, {
685
686
  key: "getAuthState",
686
687
  value: function getAuthState() {
687
- var authState = {
688
- accessToken: this.getAccessToken(),
689
- idToken: this.getIdToken(),
690
- isAuthenticated: this.isAuthenticated()
691
- };
692
- return authState;
688
+ return this.authInfoManager.getAuthState();
693
689
  }
694
690
  }, {
695
- key: "getAuthInfo",
696
- value: function getAuthInfo() {
697
- return this.authInfoManager.getAuthInfo();
691
+ key: "getStateParameter",
692
+ value: function getStateParameter() {
693
+ return this.tokenManager.getStateParameter();
698
694
  }
699
695
  }, {
700
696
  key: "getIdToken",
@@ -726,6 +722,20 @@ var OCAuthCore = /*#__PURE__*/function () {
726
722
  }
727
723
  return {};
728
724
  }
725
+ }, {
726
+ key: "OCId",
727
+ get: function get() {
728
+ var _info$OCId;
729
+ var info = this.authInfoManager.getAuthState();
730
+ return (_info$OCId = info.OCId) !== null && _info$OCId !== void 0 ? _info$OCId : null;
731
+ }
732
+ }, {
733
+ key: "ethAddress",
734
+ get: function get() {
735
+ var _info$ethAddress;
736
+ var info = this.authInfoManager.getAuthState();
737
+ return (_info$ethAddress = info.ethAddress) !== null && _info$ethAddress !== void 0 ? _info$ethAddress : null;
738
+ }
729
739
  }]);
730
740
  }();
731
741
  var LIVE_PUBLIC_KEY = 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEBIDHtLbgVM76SXZ4iuIjuO+ERQPnVpJzagOsZdYxFG3ZJmvfdpr/Z29SLUbdZWafrOlAVlKe1Ovf/tcH671tTw==';
@@ -781,6 +791,7 @@ var OCAuthSandbox = /*#__PURE__*/function (_OCAuthCore3) {
781
791
 
782
792
  "use strict";
783
793
  __webpack_require__.r(__webpack_exports__);
794
+ !(function webpackMissingModule() { var e = new Error("Cannot find module 'tiny-emitter'"); e.code = 'MODULE_NOT_FOUND'; throw e; }());
784
795
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
785
796
  function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
786
797
  function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
@@ -797,35 +808,48 @@ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e
797
808
  *
798
809
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
799
810
  */
811
+
812
+
813
+ var EVENT_AUTH_STATE_CHANGE = 'authStateChange';
800
814
  var AuthInfoManager = /*#__PURE__*/function () {
801
815
  function AuthInfoManager() {
802
816
  _classCallCheck(this, AuthInfoManager);
803
- _defineProperty(this, "_idInfo", void 0);
817
+ _defineProperty(this, "_emmitter", void 0);
818
+ _defineProperty(this, "_authState", void 0);
804
819
  this._authState = null;
805
- this._idInfo = null;
820
+ this._emitter = new !(function webpackMissingModule() { var e = new Error("Cannot find module 'tiny-emitter'"); e.code = 'MODULE_NOT_FOUND'; throw e; }())();
806
821
  }
807
822
  return _createClass(AuthInfoManager, [{
808
- key: "setIdInfo",
809
- value: function setIdInfo(IdInfo) {
810
- this._idInfo = Object.assign({}, IdInfo);
811
- }
812
- }, {
813
- key: "getIdInfoAttribute",
814
- value: function getIdInfoAttribute(attribute) {
815
- if (this._idInfo === null) {
816
- return undefined;
817
- }
818
- return this._idInfo[attribute];
823
+ key: "setAuthState",
824
+ value: function setAuthState(accessToken, idToken, OCId, ethAddress, isAuthenticated) {
825
+ this._authState = {
826
+ accessToken: accessToken,
827
+ idToken: idToken,
828
+ OCId: OCId,
829
+ ethAddress: ethAddress,
830
+ isAuthenticated: isAuthenticated
831
+ };
832
+ this._emitter.emit(EVENT_AUTH_STATE_CHANGE, this._authState);
819
833
  }
820
834
  }, {
821
- key: "getAuthInfo",
822
- value: function getAuthInfo() {
823
- return Object.assign({}, this._idInfo);
835
+ key: "getAuthState",
836
+ value: function getAuthState() {
837
+ return Object.assign({}, this._authState);
824
838
  }
825
839
  }, {
826
840
  key: "clear",
827
841
  value: function clear() {
828
- this._idInfo = null;
842
+ this._authState = null;
843
+ }
844
+ }, {
845
+ key: "subscribe",
846
+ value: function subscribe(handler) {
847
+ this._emitter.on(EVENT_AUTH_STATE_CHANGE, handler);
848
+ }
849
+ }, {
850
+ key: "unsubscribe",
851
+ value: function unsubscribe(handler) {
852
+ this._emitter.off(EVENT_AUTH_STATE_CHANGE, handler);
829
853
  }
830
854
  }]);
831
855
  }();
@@ -884,16 +908,27 @@ var TokenManager = /*#__PURE__*/function () {
884
908
  key: "exchangeTokenFromCode",
885
909
  value: function () {
886
910
  var _exchangeTokenFromCode = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(accessCode, codeVerifier) {
887
- var body, response, _yield$response$json, access_token, id_token, pubKey, tokenVerified, parsedAccessToken;
911
+ var state,
912
+ body,
913
+ response,
914
+ _yield$response$json,
915
+ access_token,
916
+ id_token,
917
+ pubKey,
918
+ tokenVerified,
919
+ parsedAccessToken,
920
+ storageData,
921
+ _args = arguments;
888
922
  return _regeneratorRuntime().wrap(function _callee$(_context) {
889
923
  while (1) switch (_context.prev = _context.next) {
890
924
  case 0:
925
+ state = _args.length > 2 && _args[2] !== undefined ? _args[2] : null;
891
926
  body = {
892
927
  accessCode: accessCode,
893
928
  codeVerifier: codeVerifier
894
929
  };
895
- _context.prev = 1;
896
- _context.next = 4;
930
+ _context.prev = 2;
931
+ _context.next = 5;
897
932
  return fetch(this.tokenEndPoint, {
898
933
  method: 'POST',
899
934
  headers: {
@@ -901,58 +936,66 @@ var TokenManager = /*#__PURE__*/function () {
901
936
  },
902
937
  body: JSON.stringify(body)
903
938
  });
904
- case 4:
939
+ case 5:
905
940
  response = _context.sent;
906
- _context.next = 7;
941
+ _context.next = 8;
907
942
  return response.json();
908
- case 7:
943
+ case 8:
909
944
  _yield$response$json = _context.sent;
910
945
  access_token = _yield$response$json.access_token;
911
946
  id_token = _yield$response$json.id_token;
912
947
  if (!(!access_token || !id_token)) {
913
- _context.next = 12;
948
+ _context.next = 13;
914
949
  break;
915
950
  }
916
951
  throw new _utils_errors__WEBPACK_IMPORTED_MODULE_2__["AuthError"]('Fail to exchange token');
917
- case 12:
918
- _context.next = 14;
952
+ case 13:
953
+ _context.next = 15;
919
954
  return this.getPublicKey();
920
- case 14:
955
+ case 15:
921
956
  pubKey = _context.sent;
922
- _context.next = 17;
957
+ _context.next = 18;
923
958
  return Object(_crypto__WEBPACK_IMPORTED_MODULE_1__["verifyToken"])(id_token, pubKey);
924
- case 17:
959
+ case 18:
925
960
  tokenVerified = _context.sent;
926
961
  if (tokenVerified) {
927
- _context.next = 20;
962
+ _context.next = 21;
928
963
  break;
929
964
  }
930
965
  throw new _utils_errors__WEBPACK_IMPORTED_MODULE_2__["AuthError"]('Unable to verify token');
931
- case 20:
966
+ case 21:
932
967
  parsedAccessToken = Object(_utils__WEBPACK_IMPORTED_MODULE_0__["parseJwt"])(access_token);
933
- this.storageManager.getStorageObject().setStorage({
968
+ storageData = Object.assign({
934
969
  access_token: access_token,
935
970
  id_token: id_token,
936
971
  expired: parsedAccessToken.exp
972
+ }, state && {
973
+ state: state
937
974
  });
938
- _context.next = 28;
975
+ this.storageManager.getStorageObject().setStorage(storageData);
976
+ _context.next = 30;
939
977
  break;
940
- case 24:
941
- _context.prev = 24;
942
- _context.t0 = _context["catch"](1);
978
+ case 26:
979
+ _context.prev = 26;
980
+ _context.t0 = _context["catch"](2);
943
981
  console.log(_context.t0);
944
982
  throw new _utils_errors__WEBPACK_IMPORTED_MODULE_2__["AuthError"]('Token acquisition failed');
945
- case 28:
983
+ case 30:
946
984
  case "end":
947
985
  return _context.stop();
948
986
  }
949
- }, _callee, this, [[1, 24]]);
987
+ }, _callee, this, [[2, 26]]);
950
988
  }));
951
989
  function exchangeTokenFromCode(_x, _x2) {
952
990
  return _exchangeTokenFromCode.apply(this, arguments);
953
991
  }
954
992
  return exchangeTokenFromCode;
955
993
  }()
994
+ }, {
995
+ key: "getStateParameter",
996
+ value: function getStateParameter() {
997
+ return this.storageManager.getStorageObject().getItem('state');
998
+ }
956
999
  }, {
957
1000
  key: "getIdToken",
958
1001
  value: function getIdToken() {