kinvey-js-sdk
Advanced tools
Comparing version 5.1.4 to 5.1.5
@@ -43,3 +43,4 @@ import { HttpHeaders, KinveyHttpHeaders, KinveyHttpAuth } from './headers'; | ||
constructor(config: KinveyHttpRequestConfig); | ||
_refreshMICSession(micSession: any): Promise<boolean>; | ||
execute(retry?: boolean): Promise<HttpResponse>; | ||
} |
@@ -60,2 +60,3 @@ "use strict"; | ||
var log_1 = require("../log"); | ||
var cache_1 = require("../datastore/cache"); | ||
var headers_1 = require("./headers"); | ||
@@ -91,2 +92,57 @@ var http_1 = require("./http"); | ||
exports.serialize = serialize; | ||
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 HttpRequest = /** @class */ (function () { | ||
@@ -150,3 +206,2 @@ function HttpRequest(config) { | ||
if (config.auth) { | ||
_this.headers.setAuthorization(config.auth); | ||
_this.auth = config.auth; | ||
@@ -156,35 +211,13 @@ } | ||
} | ||
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: | ||
_b.trys.push([0, 2, , 10]); | ||
return [4 /*yield*/, _super.prototype.execute.call(this)]; | ||
case 1: return [2 /*return*/, _b.sent()]; | ||
case 2: | ||
error_1 = _b.sent(); | ||
if (!retry) return [3 /*break*/, 9]; | ||
if (!(error_1 instanceof invalidCredentials_1.InvalidCredentialsError)) return [3 /*break*/, 8]; | ||
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(); | ||
activeSession = session_1.getSession(); | ||
socialIdentity_1 = (activeSession && activeSession._socialIdentity) || {}; | ||
micIdentityKey = Object.keys(socialIdentity_1).find(function (sessionKey) { return socialIdentity_1[sessionKey].identity === 'kinveyAuth'; }); | ||
if (!micIdentityKey) return [3 /*break*/, 8]; | ||
micSession_1 = socialIdentity_1[micIdentityKey]; | ||
if (!micSession_1) return [3 /*break*/, 8]; | ||
_b.label = 3; | ||
case 3: | ||
_b.trys.push([3, 7, , 8]); | ||
kinveyMICIdentityKey = 'kinveyAuth'; | ||
_b.label = 1; | ||
case 1: | ||
_b.trys.push([1, 4, , 5]); | ||
refreshRequest = new KinveyHttpRequest({ | ||
@@ -195,3 +228,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; | ||
@@ -203,35 +236,95 @@ } | ||
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 4: | ||
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 | ||
session_1.setSession(activeSession); | ||
session_1.setKinveyMICSession(refreshData); | ||
newMICSession = session_1.getKinveyMICSession(); | ||
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 5: | ||
loginResponse = _b.sent(); | ||
newSession = loginResponse.data; | ||
newSession._socialIdentity[micIdentityKey] = Object.assign({}, newSession._socialIdentity[micIdentityKey], newMICSession); | ||
return [4 /*yield*/, sendSessionRefreshRequest(loginRequest)]; | ||
case 3: | ||
newSession = _b.sent(); | ||
if (!newSession) { | ||
return [2 /*return*/, false]; | ||
} | ||
newSession._socialIdentity[kinveyMICIdentityKey] = Object.assign({}, newSession._socialIdentity[kinveyMICIdentityKey], newMICSession); | ||
// Set the new session | ||
session_1.setSession(newSession); | ||
return [2 /*return*/, true]; | ||
case 4: | ||
error_2 = _b.sent(); | ||
log_1.logger.error(error_2.message); | ||
return [2 /*return*/, false]; | ||
case 5: 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 6: | ||
response = _b.sent(); | ||
case 8: | ||
response = _a.sent(); | ||
// Mark the refresh token as complete | ||
@@ -241,14 +334,21 @@ markRefreshTokenRequestComplete(); | ||
return [2 /*return*/, response]; | ||
case 7: | ||
error_2 = _b.sent(); | ||
log_1.logger.error(error_2.message); | ||
return [3 /*break*/, 8]; | ||
case 8: | ||
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 = 9; | ||
case 9: | ||
_a.label = 15; | ||
case 15: | ||
// Throw the error | ||
throw error_1; | ||
case 10: return [2 /*return*/]; | ||
throw error_3; | ||
case 16: return [3 /*break*/, 17]; | ||
case 17: return [2 /*return*/]; | ||
} | ||
@@ -255,0 +355,0 @@ }); |
@@ -14,1 +14,3 @@ import { Entity } from '../storage'; | ||
export declare function removeSession(): boolean; | ||
export declare function getKinveyMICSession(): any; | ||
export declare function setKinveyMICSession(newKinveyMICSession: any): boolean; |
"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"); | ||
@@ -28,2 +32,18 @@ var kinvey_1 = require("../kinvey"); | ||
exports.removeSession = removeSession; | ||
function getKinveyMICSession() { | ||
var session = getSession(); | ||
return get_1.default(session, '_socialIdentity.kinveyAuth', null); | ||
} | ||
exports.getKinveyMICSession = getKinveyMICSession; | ||
function setKinveyMICSession(newKinveyMICSession) { | ||
var existingKinveyMICSession = getKinveyMICSession(); | ||
if (!existingKinveyMICSession) { | ||
return false; | ||
} | ||
var existingSession = getSession(); | ||
var mergedMICSession = Object.assign({}, existingSession._socialIdentity.kinveyAuth, newKinveyMICSession); | ||
existingSession._socialIdentity.kinveyAuth = mergedMICSession; | ||
return setSession(existingSession); | ||
} | ||
exports.setKinveyMICSession = setKinveyMICSession; | ||
//# sourceMappingURL=session.js.map |
{ | ||
"name": "kinvey-js-sdk", | ||
"description": "Kinvey JavaScript SDK for JavaScript applications.", | ||
"version": "5.1.4", | ||
"version": "5.1.5", | ||
"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
677351
10585