kinvey-js-sdk
Advanced tools
Comparing version 6.0.3 to 7.0.0
@@ -11,3 +11,3 @@ # Change Log | ||
* behaviour when refreshing KCS tokens ([a49b215](https://github.com/Kinvey/js-sdk/commit/a49b2151b0730ac0705f2c7a7ae666181935465b)) | ||
* fixed behaviour when refreshing KCS tokens ([a49b215](https://github.com/Kinvey/js-sdk/commit/a49b2151b0730ac0705f2c7a7ae666181935465b)) | ||
* do not save refresh_token returned from _me endpoint ([3201bfa](https://github.com/Kinvey/js-sdk/commit/3201bfae329588f2c59180490defad1a89d7e804)) | ||
@@ -14,0 +14,0 @@ |
@@ -43,3 +43,4 @@ import { HttpHeaders, KinveyHttpHeaders, KinveyHttpAuth } from './headers'; | ||
constructor(config: KinveyHttpRequestConfig); | ||
_refreshMICSession(micSession: any): Promise<boolean>; | ||
execute(retry?: boolean): Promise<HttpResponse>; | ||
} |
@@ -63,2 +63,3 @@ "use strict"; | ||
var session_1 = require("./session"); | ||
var cache_1 = require("../datastore/cache"); | ||
var utils_1 = require("./utils"); | ||
@@ -143,2 +144,57 @@ var REQUEST_QUEUE = new p_queue_1.default(); | ||
} | ||
function cleanUp() { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var error_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 5, , 6]); | ||
// TODO: Unregister from live service | ||
// Remove the session | ||
return [4 /*yield*/, session_1.removeSession()]; | ||
case 1: | ||
// TODO: Unregister from live service | ||
// Remove the session | ||
_a.sent(); | ||
// Clear cache's | ||
return [4 /*yield*/, cache_1.QueryCache.clear()]; | ||
case 2: | ||
// Clear cache's | ||
_a.sent(); | ||
return [4 /*yield*/, cache_1.SyncCache.clear()]; | ||
case 3: | ||
_a.sent(); | ||
return [4 /*yield*/, cache_1.DataStoreCache.clear()]; | ||
case 4: | ||
_a.sent(); | ||
return [3 /*break*/, 6]; | ||
case 5: | ||
error_1 = _a.sent(); | ||
log_1.logger.error(error_1.message); | ||
return [3 /*break*/, 6]; | ||
case 6: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
} | ||
function sendSessionRefreshRequest(req) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var response; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, http_1.send(req.toPlainObject())]; | ||
case 1: | ||
response = _a.sent(); | ||
if (!(response.statusCode > 399)) return [3 /*break*/, 4]; | ||
if (!(response.statusCode < 500)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, cleanUp()]; | ||
case 2: | ||
_a.sent(); | ||
_a.label = 3; | ||
case 3: return [2 /*return*/, null]; | ||
case 4: return [2 /*return*/, response.data]; | ||
} | ||
}); | ||
}); | ||
} | ||
var KinveyHttpRequest = /** @class */ (function (_super) { | ||
@@ -154,43 +210,13 @@ __extends(KinveyHttpRequest, _super); | ||
} | ||
KinveyHttpRequest.prototype.execute = function (retry) { | ||
if (retry === void 0) { retry = true; } | ||
KinveyHttpRequest.prototype._refreshMICSession = function (micSession) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var error_1, activeSession, socialIdentity_1, micIdentityKey, micSession_1, refreshRequest, refreshResponse, newMICSession, loginRequest, loginResponse, newSession, request, response, error_2; | ||
var kinveyMICIdentityKey, refreshRequest, refreshData, newMICSession, loginRequest, newSession, error_2; | ||
var _a; | ||
var _this = this; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (!this.auth) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, this.headers.setAuthorization(this.auth)]; | ||
kinveyMICIdentityKey = 'kinveyAuth'; | ||
_b.label = 1; | ||
case 1: | ||
_b.sent(); | ||
_b.label = 2; | ||
case 2: | ||
_b.trys.push([2, 4, , 15]); | ||
return [4 /*yield*/, _super.prototype.execute.call(this)]; | ||
case 3: return [2 /*return*/, _b.sent()]; | ||
case 4: | ||
error_1 = _b.sent(); | ||
if (!retry) return [3 /*break*/, 14]; | ||
if (!(error_1 instanceof invalidCredentials_1.InvalidCredentialsError)) return [3 /*break*/, 13]; | ||
if (isRefreshTokenRequestInProgress()) { | ||
return [2 /*return*/, REQUEST_QUEUE.add(function () { | ||
var request = new KinveyHttpRequest(_this); | ||
return request.execute(false).catch(function () { return Promise.reject(error_1); }); | ||
})]; | ||
} | ||
// Mark refresh token request in progress | ||
markRefreshTokenRequestInProgress(); | ||
return [4 /*yield*/, session_1.getSession()]; | ||
case 5: | ||
activeSession = _b.sent(); | ||
socialIdentity_1 = (activeSession && activeSession._socialIdentity) || {}; | ||
micIdentityKey = Object.keys(socialIdentity_1).find(function (sessionKey) { return socialIdentity_1[sessionKey].identity === 'kinveyAuth'; }); | ||
if (!micIdentityKey) return [3 /*break*/, 13]; | ||
micSession_1 = socialIdentity_1[micIdentityKey]; | ||
if (!micSession_1) return [3 /*break*/, 13]; | ||
_b.label = 6; | ||
case 6: | ||
_b.trys.push([6, 12, , 13]); | ||
_b.trys.push([1, 7, , 8]); | ||
refreshRequest = new KinveyHttpRequest({ | ||
@@ -201,3 +227,3 @@ method: HttpRequestMethod.POST, | ||
Authorization: function () { | ||
var credentials = js_base64_1.Base64.encode(micSession_1.client_id + ":" + kinvey_1.getAppSecret()); | ||
var credentials = js_base64_1.Base64.encode(micSession.client_id + ":" + kinvey_1.getAppSecret()); | ||
return "Basic " + credentials; | ||
@@ -209,41 +235,103 @@ } | ||
grant_type: 'refresh_token', | ||
client_id: micSession_1.client_id, | ||
redirect_uri: micSession_1.redirect_uri, | ||
refresh_token: micSession_1.refresh_token | ||
client_id: micSession.client_id, | ||
redirect_uri: micSession.redirect_uri, | ||
refresh_token: micSession.refresh_token | ||
} | ||
}); | ||
return [4 /*yield*/, refreshRequest.execute(false)]; | ||
case 7: | ||
refreshResponse = _b.sent(); | ||
newMICSession = Object.assign({}, micSession_1, refreshResponse.data); | ||
activeSession._socialIdentity[micIdentityKey] = Object.assign({}, activeSession._socialIdentity[micIdentityKey], newMICSession); | ||
return [4 /*yield*/, sendSessionRefreshRequest(refreshRequest)]; | ||
case 2: | ||
refreshData = _b.sent(); | ||
if (!refreshData) { | ||
return [2 /*return*/, false]; | ||
} | ||
// Persist the new access and refresh tokens | ||
return [4 /*yield*/, session_1.setSession(activeSession)]; | ||
case 8: | ||
return [4 /*yield*/, session_1.setKinveyMICSession(refreshData)]; | ||
case 3: | ||
// Persist the new access and refresh tokens | ||
_b.sent(); | ||
return [4 /*yield*/, session_1.getKinveyMICSession()]; | ||
case 4: | ||
newMICSession = _b.sent(); | ||
loginRequest = new KinveyHttpRequest({ | ||
method: HttpRequestMethod.POST, | ||
auth: headers_1.KinveyHttpAuth.App, | ||
headers: new headers_1.KinveyHttpHeaders({ | ||
'Content-Type': function () { return 'application/json'; }, | ||
Authorization: function () { | ||
var credentials = js_base64_1.Base64.encode(kinvey_1.getAppKey() + ":" + kinvey_1.getAppSecret()); | ||
return "Basic " + credentials; | ||
} | ||
}), | ||
url: utils_1.formatKinveyBaasUrl(utils_1.KinveyBaasNamespace.User, '/login'), | ||
body: { | ||
_socialIdentity: (_a = {}, | ||
_a[micIdentityKey] = newMICSession, | ||
_a[kinveyMICIdentityKey] = newMICSession, | ||
_a) | ||
} | ||
}); | ||
return [4 /*yield*/, loginRequest.execute(false)]; | ||
case 9: | ||
loginResponse = _b.sent(); | ||
newSession = loginResponse.data; | ||
newSession._socialIdentity[micIdentityKey] = Object.assign({}, newSession._socialIdentity[micIdentityKey], newMICSession); | ||
return [4 /*yield*/, sendSessionRefreshRequest(loginRequest)]; | ||
case 5: | ||
newSession = _b.sent(); | ||
if (!newSession) { | ||
return [2 /*return*/, false]; | ||
} | ||
newSession._socialIdentity[kinveyMICIdentityKey] = Object.assign({}, newSession._socialIdentity[kinveyMICIdentityKey], newMICSession); | ||
// Set the new session | ||
return [4 /*yield*/, session_1.setSession(newSession)]; | ||
case 10: | ||
case 6: | ||
// Set the new session | ||
_b.sent(); | ||
return [2 /*return*/, true]; | ||
case 7: | ||
error_2 = _b.sent(); | ||
log_1.logger.error(error_2.message); | ||
return [2 /*return*/, false]; | ||
case 8: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
KinveyHttpRequest.prototype.execute = function (retry) { | ||
if (retry === void 0) { retry = true; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var error_3, micSession, isSuccess, request, response, error_4; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!this.auth) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, this.headers.setAuthorization(this.auth)]; | ||
case 1: | ||
_a.sent(); | ||
_a.label = 2; | ||
case 2: | ||
_a.trys.push([2, 4, , 17]); | ||
return [4 /*yield*/, _super.prototype.execute.call(this)]; | ||
case 3: return [2 /*return*/, _a.sent()]; | ||
case 4: | ||
error_3 = _a.sent(); | ||
if (!retry) return [3 /*break*/, 16]; | ||
if (!(error_3 instanceof invalidCredentials_1.InvalidCredentialsError)) return [3 /*break*/, 15]; | ||
if (isRefreshTokenRequestInProgress()) { | ||
return [2 /*return*/, REQUEST_QUEUE.add(function () { | ||
var request = new KinveyHttpRequest(_this); | ||
return request.execute(false).catch(function () { return Promise.reject(error_3); }); | ||
})]; | ||
} | ||
// Mark refresh token request in progress | ||
markRefreshTokenRequestInProgress(); | ||
return [4 /*yield*/, session_1.getKinveyMICSession()]; | ||
case 5: | ||
micSession = _a.sent(); | ||
if (!micSession) return [3 /*break*/, 12]; | ||
_a.label = 6; | ||
case 6: | ||
_a.trys.push([6, 10, , 11]); | ||
return [4 /*yield*/, this._refreshMICSession(micSession)]; | ||
case 7: | ||
isSuccess = _a.sent(); | ||
if (!isSuccess) return [3 /*break*/, 9]; | ||
request = new KinveyHttpRequest(this); | ||
return [4 /*yield*/, request.execute(false)]; | ||
case 11: | ||
response = _b.sent(); | ||
case 8: | ||
response = _a.sent(); | ||
// Mark the refresh token as complete | ||
@@ -253,14 +341,21 @@ markRefreshTokenRequestComplete(); | ||
return [2 /*return*/, response]; | ||
case 12: | ||
error_2 = _b.sent(); | ||
log_1.logger.error(error_2.message); | ||
return [3 /*break*/, 13]; | ||
case 9: return [3 /*break*/, 11]; | ||
case 10: | ||
error_4 = _a.sent(); | ||
log_1.logger.error(error_4.message); | ||
return [3 /*break*/, 11]; | ||
case 11: return [3 /*break*/, 14]; | ||
case 12: return [4 /*yield*/, cleanUp()]; | ||
case 13: | ||
_a.sent(); | ||
_a.label = 14; | ||
case 14: | ||
// Mark the refresh token as complete | ||
markRefreshTokenRequestComplete(); | ||
_b.label = 14; | ||
case 14: | ||
_a.label = 15; | ||
case 15: | ||
// Throw the error | ||
throw error_1; | ||
case 15: return [2 /*return*/]; | ||
throw error_3; | ||
case 16: return [3 /*break*/, 17]; | ||
case 17: return [2 /*return*/]; | ||
} | ||
@@ -267,0 +362,0 @@ }); |
@@ -26,1 +26,3 @@ import { Entity } from '../storage'; | ||
export declare function removeDeviceToken(username: string): Promise<boolean>; | ||
export declare function getKinveyMICSession(): Promise<any>; | ||
export declare function setKinveyMICSession(newKinveyMICSession: any): Promise<boolean>; |
@@ -37,3 +37,7 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var get_1 = __importDefault(require("lodash/get")); | ||
var config_1 = require("../config"); | ||
@@ -180,2 +184,38 @@ var kinvey_1 = require("../kinvey"); | ||
exports.removeDeviceToken = removeDeviceToken; | ||
function getKinveyMICSession() { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var session; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, getSession()]; | ||
case 1: | ||
session = _a.sent(); | ||
return [2 /*return*/, get_1.default(session, '_socialIdentity.kinveyAuth', null)]; | ||
} | ||
}); | ||
}); | ||
} | ||
exports.getKinveyMICSession = getKinveyMICSession; | ||
function setKinveyMICSession(newKinveyMICSession) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var existingKinveyMICSession, existingSession, mergedMICSession; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, getKinveyMICSession()]; | ||
case 1: | ||
existingKinveyMICSession = _a.sent(); | ||
if (!existingKinveyMICSession) { | ||
return [2 /*return*/, false]; | ||
} | ||
return [4 /*yield*/, getSession()]; | ||
case 2: | ||
existingSession = _a.sent(); | ||
mergedMICSession = Object.assign({}, existingSession._socialIdentity.kinveyAuth, newKinveyMICSession); | ||
existingSession._socialIdentity.kinveyAuth = mergedMICSession; | ||
return [2 /*return*/, setSession(existingSession)]; | ||
} | ||
}); | ||
}); | ||
} | ||
exports.setKinveyMICSession = setKinveyMICSession; | ||
//# sourceMappingURL=session.js.map |
{ | ||
"name": "kinvey-js-sdk", | ||
"description": "Kinvey JavaScript SDK for JavaScript applications.", | ||
"version": "6.0.3", | ||
"version": "7.0.0", | ||
"license": "Apache-2.0", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
762761
11880