@opencampus/ocid-connect-js 1.2.0 → 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();
@@ -672,30 +676,21 @@ var OCAuthCore = /*#__PURE__*/function () {
672
676
  if (this.tokenManager.hasExpired()) {
673
677
  this.authInfoManager.clear();
674
678
  } else {
675
- var idToken = this.tokenManager.getIdToken();
676
- var _parseJwt = Object(_utils__WEBPACK_IMPORTED_MODULE_4__["parseJwt"])(idToken),
677
- edu_username = _parseJwt.edu_username,
678
- eth_address = _parseJwt.eth_address;
679
- this.authInfoManager.setIdInfo({
680
- edu_username: edu_username,
681
- eth_address: eth_address
682
- });
679
+ var _this$getParsedIdToke = this.getParsedIdToken(),
680
+ edu_username = _this$getParsedIdToke.edu_username,
681
+ eth_address = _this$getParsedIdToke.eth_address;
682
+ this.authInfoManager.setAuthState(this.getAccessToken(), this.getIdToken(), edu_username, eth_address, this.isAuthenticated());
683
683
  }
684
684
  }
685
685
  }, {
686
686
  key: "getAuthState",
687
687
  value: function getAuthState() {
688
- var authState = {
689
- accessToken: this.getAccessToken(),
690
- idToken: this.getIdToken(),
691
- isAuthenticated: this.isAuthenticated()
692
- };
693
- return authState;
688
+ return this.authInfoManager.getAuthState();
694
689
  }
695
690
  }, {
696
- key: "getAuthInfo",
697
- value: function getAuthInfo() {
698
- return this.authInfoManager.getAuthInfo();
691
+ key: "getStateParameter",
692
+ value: function getStateParameter() {
693
+ return this.tokenManager.getStateParameter();
699
694
  }
700
695
  }, {
701
696
  key: "getIdToken",
@@ -707,6 +702,40 @@ var OCAuthCore = /*#__PURE__*/function () {
707
702
  value: function getAccessToken() {
708
703
  return this.tokenManager.getAccessToken();
709
704
  }
705
+ }, {
706
+ key: "getParsedIdToken",
707
+ value: function getParsedIdToken() {
708
+ // return all info in id token
709
+ var idToken = this.tokenManager.getIdToken();
710
+ if (idToken) {
711
+ return Object(_utils__WEBPACK_IMPORTED_MODULE_4__["parseJwt"])(idToken);
712
+ }
713
+ return {};
714
+ }
715
+ }, {
716
+ key: "getParsedAccessToken",
717
+ value: function getParsedAccessToken() {
718
+ // return all info in access token
719
+ var accessToken = this.tokenManager.getAccessToken();
720
+ if (accessToken) {
721
+ return Object(_utils__WEBPACK_IMPORTED_MODULE_4__["parseJwt"])(accessToken);
722
+ }
723
+ return {};
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
+ }
710
739
  }]);
711
740
  }();
712
741
  var LIVE_PUBLIC_KEY = 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEBIDHtLbgVM76SXZ4iuIjuO+ERQPnVpJzagOsZdYxFG3ZJmvfdpr/Z29SLUbdZWafrOlAVlKe1Ovf/tcH671tTw==';
@@ -762,6 +791,7 @@ var OCAuthSandbox = /*#__PURE__*/function (_OCAuthCore3) {
762
791
 
763
792
  "use strict";
764
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; }());
765
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); }
766
796
  function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
767
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); } }
@@ -778,35 +808,48 @@ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e
778
808
  *
779
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.
780
810
  */
811
+
812
+
813
+ var EVENT_AUTH_STATE_CHANGE = 'authStateChange';
781
814
  var AuthInfoManager = /*#__PURE__*/function () {
782
815
  function AuthInfoManager() {
783
816
  _classCallCheck(this, AuthInfoManager);
784
- _defineProperty(this, "_idInfo", void 0);
817
+ _defineProperty(this, "_emmitter", void 0);
818
+ _defineProperty(this, "_authState", void 0);
785
819
  this._authState = null;
786
- 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; }())();
787
821
  }
788
822
  return _createClass(AuthInfoManager, [{
789
- key: "setIdInfo",
790
- value: function setIdInfo(IdInfo) {
791
- this._idInfo = Object.assign({}, IdInfo);
792
- }
793
- }, {
794
- key: "getIdInfoAttribute",
795
- value: function getIdInfoAttribute(attribute) {
796
- if (this._idInfo === null) {
797
- return undefined;
798
- }
799
- 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);
800
833
  }
801
834
  }, {
802
- key: "getAuthInfo",
803
- value: function getAuthInfo() {
804
- return Object.assign({}, this._idInfo);
835
+ key: "getAuthState",
836
+ value: function getAuthState() {
837
+ return Object.assign({}, this._authState);
805
838
  }
806
839
  }, {
807
840
  key: "clear",
808
841
  value: function clear() {
809
- 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);
810
853
  }
811
854
  }]);
812
855
  }();
@@ -865,16 +908,27 @@ var TokenManager = /*#__PURE__*/function () {
865
908
  key: "exchangeTokenFromCode",
866
909
  value: function () {
867
910
  var _exchangeTokenFromCode = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(accessCode, codeVerifier) {
868
- 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;
869
922
  return _regeneratorRuntime().wrap(function _callee$(_context) {
870
923
  while (1) switch (_context.prev = _context.next) {
871
924
  case 0:
925
+ state = _args.length > 2 && _args[2] !== undefined ? _args[2] : null;
872
926
  body = {
873
927
  accessCode: accessCode,
874
928
  codeVerifier: codeVerifier
875
929
  };
876
- _context.prev = 1;
877
- _context.next = 4;
930
+ _context.prev = 2;
931
+ _context.next = 5;
878
932
  return fetch(this.tokenEndPoint, {
879
933
  method: 'POST',
880
934
  headers: {
@@ -882,58 +936,66 @@ var TokenManager = /*#__PURE__*/function () {
882
936
  },
883
937
  body: JSON.stringify(body)
884
938
  });
885
- case 4:
939
+ case 5:
886
940
  response = _context.sent;
887
- _context.next = 7;
941
+ _context.next = 8;
888
942
  return response.json();
889
- case 7:
943
+ case 8:
890
944
  _yield$response$json = _context.sent;
891
945
  access_token = _yield$response$json.access_token;
892
946
  id_token = _yield$response$json.id_token;
893
947
  if (!(!access_token || !id_token)) {
894
- _context.next = 12;
948
+ _context.next = 13;
895
949
  break;
896
950
  }
897
951
  throw new _utils_errors__WEBPACK_IMPORTED_MODULE_2__["AuthError"]('Fail to exchange token');
898
- case 12:
899
- _context.next = 14;
952
+ case 13:
953
+ _context.next = 15;
900
954
  return this.getPublicKey();
901
- case 14:
955
+ case 15:
902
956
  pubKey = _context.sent;
903
- _context.next = 17;
957
+ _context.next = 18;
904
958
  return Object(_crypto__WEBPACK_IMPORTED_MODULE_1__["verifyToken"])(id_token, pubKey);
905
- case 17:
959
+ case 18:
906
960
  tokenVerified = _context.sent;
907
961
  if (tokenVerified) {
908
- _context.next = 20;
962
+ _context.next = 21;
909
963
  break;
910
964
  }
911
965
  throw new _utils_errors__WEBPACK_IMPORTED_MODULE_2__["AuthError"]('Unable to verify token');
912
- case 20:
966
+ case 21:
913
967
  parsedAccessToken = Object(_utils__WEBPACK_IMPORTED_MODULE_0__["parseJwt"])(access_token);
914
- this.storageManager.getStorageObject().setStorage({
968
+ storageData = Object.assign({
915
969
  access_token: access_token,
916
970
  id_token: id_token,
917
971
  expired: parsedAccessToken.exp
972
+ }, state && {
973
+ state: state
918
974
  });
919
- _context.next = 28;
975
+ this.storageManager.getStorageObject().setStorage(storageData);
976
+ _context.next = 30;
920
977
  break;
921
- case 24:
922
- _context.prev = 24;
923
- _context.t0 = _context["catch"](1);
978
+ case 26:
979
+ _context.prev = 26;
980
+ _context.t0 = _context["catch"](2);
924
981
  console.log(_context.t0);
925
982
  throw new _utils_errors__WEBPACK_IMPORTED_MODULE_2__["AuthError"]('Token acquisition failed');
926
- case 28:
983
+ case 30:
927
984
  case "end":
928
985
  return _context.stop();
929
986
  }
930
- }, _callee, this, [[1, 24]]);
987
+ }, _callee, this, [[2, 26]]);
931
988
  }));
932
989
  function exchangeTokenFromCode(_x, _x2) {
933
990
  return _exchangeTokenFromCode.apply(this, arguments);
934
991
  }
935
992
  return exchangeTokenFromCode;
936
993
  }()
994
+ }, {
995
+ key: "getStateParameter",
996
+ value: function getStateParameter() {
997
+ return this.storageManager.getStorageObject().getItem('state');
998
+ }
937
999
  }, {
938
1000
  key: "getIdToken",
939
1001
  value: function getIdToken() {