@opencampus/ocid-connect-js 1.2.1 → 1.2.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.
@@ -29,51 +29,36 @@ var LoginCallBack = function LoginCallBack(_ref) {
29
29
  var _useOCAuth = (0, _OCContext.useOCAuth)(),
30
30
  ocAuth = _useOCAuth.ocAuth,
31
31
  authState = _useOCAuth.authState,
32
- updateAuthState = _useOCAuth.updateAuthState;
32
+ setAuthError = _useOCAuth.setAuthError;
33
33
  (0, _react.useEffect)(function () {
34
34
  var handleLogin = /*#__PURE__*/function () {
35
35
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
36
- var _authState;
37
36
  return _regeneratorRuntime().wrap(function _callee$(_context) {
38
37
  while (1) switch (_context.prev = _context.next) {
39
38
  case 0:
40
39
  if (!ocAuth) {
41
- _context.next = 12;
40
+ _context.next = 11;
42
41
  break;
43
42
  }
44
43
  _context.prev = 1;
45
44
  _context.next = 4;
46
45
  return ocAuth.handleLoginRedirect();
47
46
  case 4:
48
- _authState = _context.sent;
49
- if (_authState.idToken) {
50
- updateAuthState(_authState);
51
- handledRedirect = true;
52
- successCallback();
53
- } else {
54
- updateAuthState({
55
- error: 'missing idToken from redirect callback'
56
- });
57
- if (errorCallback) {
58
- errorCallback();
59
- }
60
- }
61
- _context.next = 12;
47
+ successCallback();
48
+ _context.next = 11;
62
49
  break;
63
- case 8:
64
- _context.prev = 8;
50
+ case 7:
51
+ _context.prev = 7;
65
52
  _context.t0 = _context["catch"](1);
66
- updateAuthState({
67
- error: _context.t0
68
- });
53
+ setAuthError(_context.t0);
69
54
  if (errorCallback) {
70
55
  errorCallback();
71
56
  }
72
- case 12:
57
+ case 11:
73
58
  case "end":
74
59
  return _context.stop();
75
60
  }
76
- }, _callee, null, [[1, 8]]);
61
+ }, _callee, null, [[1, 7]]);
77
62
  }));
78
63
  return function handleLogin() {
79
64
  return _ref2.apply(this, arguments);
@@ -32,34 +32,53 @@ var OCConnect = function OCConnect(_ref) {
32
32
  _useState2 = _slicedToArray(_useState, 2),
33
33
  ocAuth = _useState2[0],
34
34
  setOcAuth = _useState2[1];
35
- var _useState3 = (0, _react.useState)({
35
+ var _useState3 = (0, _react.useState)(),
36
+ _useState4 = _slicedToArray(_useState3, 2),
37
+ OCId = _useState4[0],
38
+ setOCId = _useState4[1];
39
+ var _useState5 = (0, _react.useState)(),
40
+ _useState6 = _slicedToArray(_useState5, 2),
41
+ ethAddress = _useState6[0],
42
+ setEthAddress = _useState6[1];
43
+ var _useState7 = (0, _react.useState)({
36
44
  isAuthenticated: false
37
45
  }),
38
- _useState4 = _slicedToArray(_useState3, 2),
39
- authState = _useState4[0],
40
- setAuthState = _useState4[1];
46
+ _useState8 = _slicedToArray(_useState7, 2),
47
+ authState = _useState8[0],
48
+ setAuthState = _useState8[1];
49
+ var _useState9 = (0, _react.useState)(null),
50
+ _useState10 = _slicedToArray(_useState9, 2),
51
+ authError = _useState10[0],
52
+ setAuthError = _useState10[1];
53
+ var updateAuthState = function updateAuthState(authState) {
54
+ setAuthState(authState);
55
+ setOCId(authState.OCId);
56
+ setEthAddress(authState.ethAddress);
57
+ };
58
+
59
+ // init sdk
41
60
  (0, _react.useEffect)(function () {
42
61
  var authSdk = sandboxMode ? new _auth.OCAuthSandbox(opts) : new _auth.OCAuthLive(opts);
43
- setOcAuth(authSdk);
44
62
  updateAuthState(authSdk.getAuthState());
63
+ setOcAuth(authSdk);
45
64
  }, []);
46
- var updateAuthState = function updateAuthState(authState) {
47
- var accessToken = authState.accessToken,
48
- idToken = authState.idToken,
49
- isAuthenticated = authState.isAuthenticated,
50
- error = authState.error;
51
- setAuthState({
52
- accessToken: accessToken,
53
- idToken: idToken,
54
- isAuthenticated: isAuthenticated,
55
- error: error
56
- });
57
- };
65
+ (0, _react.useEffect)(function () {
66
+ if (ocAuth.authInfoManager) {
67
+ // reactively recieve update on the authstate change
68
+ ocAuth.authInfoManager.subscribe(updateAuthState);
69
+ return function () {
70
+ ocAuth.authInfoManager.unsubscribe(updateAuthState);
71
+ };
72
+ }
73
+ }, [ocAuth]);
58
74
  return /*#__PURE__*/_react["default"].createElement(_OCContext.OCContext.Provider, {
59
75
  value: {
76
+ OCId: OCId,
77
+ ethAddress: ethAddress,
60
78
  ocAuth: ocAuth,
61
79
  authState: authState,
62
- updateAuthState: updateAuthState
80
+ authError: authError,
81
+ setAuthError: setAuthError
63
82
  }
64
83
  }, children);
65
84
  };
package/lib/sdk/auth.js CHANGED
@@ -65,21 +65,24 @@ var OCAuthCore = exports.OCAuthCore = /*#__PURE__*/function () {
65
65
  }, {
66
66
  key: "logout",
67
67
  value: function () {
68
- var _logout = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
69
- var requestUrl;
68
+ var _logout = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(logoutReturnTo) {
69
+ var url;
70
70
  return _regeneratorRuntime().wrap(function _callee$(_context) {
71
71
  while (1) switch (_context.prev = _context.next) {
72
72
  case 0:
73
73
  this.clearStorage();
74
- requestUrl = this.logoutEndPoint;
75
- window.location.assign(requestUrl);
76
- case 3:
74
+ url = new URL(this.logoutEndPoint);
75
+ if (logoutReturnTo) {
76
+ url.searchParams.append('returnTo', logoutReturnTo);
77
+ }
78
+ window.location.assign(url.toString());
79
+ case 4:
77
80
  case "end":
78
81
  return _context.stop();
79
82
  }
80
83
  }, _callee, this);
81
84
  }));
82
- function logout() {
85
+ function logout(_x) {
83
86
  return _logout.apply(this, arguments);
84
87
  }
85
88
  return logout;
@@ -111,7 +114,7 @@ var OCAuthCore = exports.OCAuthCore = /*#__PURE__*/function () {
111
114
  }
112
115
  }, _callee2, this);
113
116
  }));
114
- function signInWithRedirect(_x) {
117
+ function signInWithRedirect(_x2) {
115
118
  return _signInWithRedirect.apply(this, arguments);
116
119
  }
117
120
  return signInWithRedirect;
@@ -136,7 +139,7 @@ var OCAuthCore = exports.OCAuthCore = /*#__PURE__*/function () {
136
139
  break;
137
140
  }
138
141
  _context3.next = 7;
139
- return this.tokenManager.exchangeTokenFromCode(urlParams.code, codeVerifier);
142
+ return this.tokenManager.exchangeTokenFromCode(urlParams.code, codeVerifier, urlParams.state);
140
143
  case 7:
141
144
  // clear transaction meta, coz it's completed
142
145
  this.transactionManager.clear();
@@ -172,26 +175,18 @@ var OCAuthCore = exports.OCAuthCore = /*#__PURE__*/function () {
172
175
  var _this$getParsedIdToke = this.getParsedIdToken(),
173
176
  edu_username = _this$getParsedIdToke.edu_username,
174
177
  eth_address = _this$getParsedIdToke.eth_address;
175
- this.authInfoManager.setIdInfo({
176
- edu_username: edu_username,
177
- eth_address: eth_address
178
- });
178
+ this.authInfoManager.setAuthState(this.getAccessToken(), this.getIdToken(), edu_username, eth_address, this.isAuthenticated());
179
179
  }
180
180
  }
181
181
  }, {
182
182
  key: "getAuthState",
183
183
  value: function getAuthState() {
184
- var authState = {
185
- accessToken: this.getAccessToken(),
186
- idToken: this.getIdToken(),
187
- isAuthenticated: this.isAuthenticated()
188
- };
189
- return authState;
184
+ return this.authInfoManager.getAuthState();
190
185
  }
191
186
  }, {
192
- key: "getAuthInfo",
193
- value: function getAuthInfo() {
194
- return this.authInfoManager.getAuthInfo();
187
+ key: "getStateParameter",
188
+ value: function getStateParameter() {
189
+ return this.tokenManager.getStateParameter();
195
190
  }
196
191
  }, {
197
192
  key: "getIdToken",
@@ -223,6 +218,20 @@ var OCAuthCore = exports.OCAuthCore = /*#__PURE__*/function () {
223
218
  }
224
219
  return {};
225
220
  }
221
+ }, {
222
+ key: "OCId",
223
+ get: function get() {
224
+ var _info$OCId;
225
+ var info = this.authInfoManager.getAuthState();
226
+ return (_info$OCId = info.OCId) !== null && _info$OCId !== void 0 ? _info$OCId : null;
227
+ }
228
+ }, {
229
+ key: "ethAddress",
230
+ get: function get() {
231
+ var _info$ethAddress;
232
+ var info = this.authInfoManager.getAuthState();
233
+ return (_info$ethAddress = info.ethAddress) !== null && _info$ethAddress !== void 0 ? _info$ethAddress : null;
234
+ }
226
235
  }]);
227
236
  }();
228
237
  var LIVE_PUBLIC_KEY = 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEBIDHtLbgVM76SXZ4iuIjuO+ERQPnVpJzagOsZdYxFG3ZJmvfdpr/Z29SLUbdZWafrOlAVlKe1Ovf/tcH671tTw==';
@@ -4,14 +4,15 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
+ var _tinyEmitter = _interopRequireDefault(require("tiny-emitter"));
8
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
7
9
  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); }
8
10
  function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
9
11
  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); } }
10
12
  function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
11
13
  function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
12
14
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
13
- function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
14
- /*!
15
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*!
15
16
  * Copyright 2024-Present Animoca Brands Corporation Ltd.
16
17
  *
17
18
  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
@@ -20,35 +21,46 @@ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e
20
21
  *
21
22
  * 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.
22
23
  */
24
+ var EVENT_AUTH_STATE_CHANGE = 'authStateChange';
23
25
  var AuthInfoManager = /*#__PURE__*/function () {
24
26
  function AuthInfoManager() {
25
27
  _classCallCheck(this, AuthInfoManager);
26
- _defineProperty(this, "_idInfo", void 0);
28
+ _defineProperty(this, "_emmitter", void 0);
29
+ _defineProperty(this, "_authState", void 0);
27
30
  this._authState = null;
28
- this._idInfo = null;
31
+ this._emitter = new _tinyEmitter["default"]();
29
32
  }
30
33
  return _createClass(AuthInfoManager, [{
31
- key: "setIdInfo",
32
- value: function setIdInfo(IdInfo) {
33
- this._idInfo = Object.assign({}, IdInfo);
34
+ key: "setAuthState",
35
+ value: function setAuthState(accessToken, idToken, OCId, ethAddress, isAuthenticated) {
36
+ this._authState = {
37
+ accessToken: accessToken,
38
+ idToken: idToken,
39
+ OCId: OCId,
40
+ ethAddress: ethAddress,
41
+ isAuthenticated: isAuthenticated
42
+ };
43
+ this._emitter.emit(EVENT_AUTH_STATE_CHANGE, this._authState);
34
44
  }
35
45
  }, {
36
- key: "getIdInfoAttribute",
37
- value: function getIdInfoAttribute(attribute) {
38
- if (this._idInfo === null) {
39
- return undefined;
40
- }
41
- return this._idInfo[attribute];
42
- }
43
- }, {
44
- key: "getAuthInfo",
45
- value: function getAuthInfo() {
46
- return Object.assign({}, this._idInfo);
46
+ key: "getAuthState",
47
+ value: function getAuthState() {
48
+ return Object.assign({}, this._authState);
47
49
  }
48
50
  }, {
49
51
  key: "clear",
50
52
  value: function clear() {
51
- this._idInfo = null;
53
+ this._authState = null;
54
+ }
55
+ }, {
56
+ key: "subscribe",
57
+ value: function subscribe(handler) {
58
+ this._emitter.on(EVENT_AUTH_STATE_CHANGE, handler);
59
+ }
60
+ }, {
61
+ key: "unsubscribe",
62
+ value: function unsubscribe(handler) {
63
+ this._emitter.off(EVENT_AUTH_STATE_CHANGE, handler);
52
64
  }
53
65
  }]);
54
66
  }();
@@ -46,16 +46,27 @@ var TokenManager = /*#__PURE__*/function () {
46
46
  key: "exchangeTokenFromCode",
47
47
  value: function () {
48
48
  var _exchangeTokenFromCode = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(accessCode, codeVerifier) {
49
- var body, response, _yield$response$json, access_token, id_token, pubKey, tokenVerified, parsedAccessToken;
49
+ var state,
50
+ body,
51
+ response,
52
+ _yield$response$json,
53
+ access_token,
54
+ id_token,
55
+ pubKey,
56
+ tokenVerified,
57
+ parsedAccessToken,
58
+ storageData,
59
+ _args = arguments;
50
60
  return _regeneratorRuntime().wrap(function _callee$(_context) {
51
61
  while (1) switch (_context.prev = _context.next) {
52
62
  case 0:
63
+ state = _args.length > 2 && _args[2] !== undefined ? _args[2] : null;
53
64
  body = {
54
65
  accessCode: accessCode,
55
66
  codeVerifier: codeVerifier
56
67
  };
57
- _context.prev = 1;
58
- _context.next = 4;
68
+ _context.prev = 2;
69
+ _context.next = 5;
59
70
  return fetch(this.tokenEndPoint, {
60
71
  method: 'POST',
61
72
  headers: {
@@ -63,58 +74,66 @@ var TokenManager = /*#__PURE__*/function () {
63
74
  },
64
75
  body: JSON.stringify(body)
65
76
  });
66
- case 4:
77
+ case 5:
67
78
  response = _context.sent;
68
- _context.next = 7;
79
+ _context.next = 8;
69
80
  return response.json();
70
- case 7:
81
+ case 8:
71
82
  _yield$response$json = _context.sent;
72
83
  access_token = _yield$response$json.access_token;
73
84
  id_token = _yield$response$json.id_token;
74
85
  if (!(!access_token || !id_token)) {
75
- _context.next = 12;
86
+ _context.next = 13;
76
87
  break;
77
88
  }
78
89
  throw new _errors.AuthError('Fail to exchange token');
79
- case 12:
80
- _context.next = 14;
90
+ case 13:
91
+ _context.next = 15;
81
92
  return this.getPublicKey();
82
- case 14:
93
+ case 15:
83
94
  pubKey = _context.sent;
84
- _context.next = 17;
95
+ _context.next = 18;
85
96
  return (0, _crypto.verifyToken)(id_token, pubKey);
86
- case 17:
97
+ case 18:
87
98
  tokenVerified = _context.sent;
88
99
  if (tokenVerified) {
89
- _context.next = 20;
100
+ _context.next = 21;
90
101
  break;
91
102
  }
92
103
  throw new _errors.AuthError('Unable to verify token');
93
- case 20:
104
+ case 21:
94
105
  parsedAccessToken = (0, _utils.parseJwt)(access_token);
95
- this.storageManager.getStorageObject().setStorage({
106
+ storageData = Object.assign({
96
107
  access_token: access_token,
97
108
  id_token: id_token,
98
109
  expired: parsedAccessToken.exp
110
+ }, state && {
111
+ state: state
99
112
  });
100
- _context.next = 28;
113
+ this.storageManager.getStorageObject().setStorage(storageData);
114
+ _context.next = 30;
101
115
  break;
102
- case 24:
103
- _context.prev = 24;
104
- _context.t0 = _context["catch"](1);
116
+ case 26:
117
+ _context.prev = 26;
118
+ _context.t0 = _context["catch"](2);
105
119
  console.log(_context.t0);
106
120
  throw new _errors.AuthError('Token acquisition failed');
107
- case 28:
121
+ case 30:
108
122
  case "end":
109
123
  return _context.stop();
110
124
  }
111
- }, _callee, this, [[1, 24]]);
125
+ }, _callee, this, [[2, 26]]);
112
126
  }));
113
127
  function exchangeTokenFromCode(_x, _x2) {
114
128
  return _exchangeTokenFromCode.apply(this, arguments);
115
129
  }
116
130
  return exchangeTokenFromCode;
117
131
  }()
132
+ }, {
133
+ key: "getStateParameter",
134
+ value: function getStateParameter() {
135
+ return this.storageManager.getStorageObject().getItem('state');
136
+ }
118
137
  }, {
119
138
  key: "getIdToken",
120
139
  value: function getIdToken() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencampus/ocid-connect-js",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "author": "Animoca Brands",
5
5
  "license": "MIT",
6
6
  "description": "OCID Connector Library",
@@ -39,7 +39,6 @@
39
39
  "eslint-plugin-import": "^2.25.3",
40
40
  "eslint-plugin-react": "^7.27.1",
41
41
  "gulp": "^4.0.0",
42
- "js-cookie": "^3.0.5",
43
42
  "js-beautify": "^1.7.5",
44
43
  "json-loader": "^0.5.4",
45
44
  "lint-staged": "^12.1.2",
@@ -56,7 +55,10 @@
56
55
  "webpack-cli": "^3.1.2",
57
56
  "regenerator-runtime": "^0.14.1"
58
57
  },
59
- "dependencies": {},
58
+ "dependencies": {
59
+ "tiny-emitter": "^2.1.0",
60
+ "js-cookie": "^3.0.5"
61
+ },
60
62
  "peerDependencies": {
61
63
  "react": "^0.14.0 || ^15.0.1 || ^16.0.0 || ^17.0.0 || ^18.0.0",
62
64
  "react-dom": "^0.14.0 || ^15.0.1 || ^16.0.0 || ^17.0.0 || ^18.0.0"