@signiant/media-shuttle-sdk
Version:
The SDK for supporting file transfer to and from Media Shuttle
174 lines (173 loc) • 9.25 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
import { OIDC_TOKEN_ENDPOINT, OIDC_CLIENT_ID, OIDC_GRANT_TYPE, OIDC_REFRESH_TOKEN_GRANT_TYPE, OIDC_REFRESH_TOKEN, TOKEN_EXPIRY_BUFFER_IN_MS, OIDC_REVOKE_TOKEN_ENDPOINT, OIDC_TOKEN, } from '../../internal/constants/Authentication';
import { signiant_private } from '@signiant/media-shuttle-sdk-base';
import RequestHeaders from '../../internal/services/RequestHeaders';
import SDK_VERSION from '../../internal/constants/version';
/**
* The refresh credentials is used to get new auth token to make REST call for Upload and Download requests.
* This class will use the refresh token generated in a previous interaction with {@link LoginCredentials} to get new auth tokens.
* Also will automatically get a new auth token when the current auth token expires using the refresh token.
*
* It also has a logout function which will revoke the refresh token.
*
* Most callers are recommended to use the {@link LoginCredentials} class to get the initial credentials which will return an instance of this class,
* instead of directly instantiating this class.
*/
var RefreshingCredentials = /** @class */ (function () {
/**
* @param {object} options Options for this constructor.
*/
function RefreshingCredentials(_a) {
var refreshToken = _a.refreshToken, clientId = _a.clientId, domain = _a.domain;
this._refreshToken = refreshToken;
this._clientId = clientId;
this._domain = domain;
}
RefreshingCredentials.prototype.getAuthToken = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this._token && !this.checkIfTokenNeedsRefresh()) {
return [2 /*return*/, this._token];
}
if (!this._tokenFetchPromise) {
//reuse the promise for subsequent getAuthToken calls
this._tokenFetchPromise = this.reFetchToken();
}
return [4 /*yield*/, this._tokenFetchPromise];
case 1:
_a.sent();
//Delete the promise for so that a new one will be used for next refresh
delete this._tokenFetchPromise;
return [2 /*return*/, this._token];
}
});
});
};
RefreshingCredentials.fromAuthenticationResponse = function (response) {
var credentials = new RefreshingCredentials({
refreshToken: response.refreshToken,
clientId: response.clientId,
domain: response.domain,
});
credentials._token = response.idToken;
credentials._currentTokenExpiryTime = RefreshingCredentials.getCurrentTokenExpiryTime(response.expiresInSeconds);
return credentials;
};
RefreshingCredentials.getCurrentTokenExpiryTime = function (expiresInSeconds) {
return new Date().getTime() + Math.max(expiresInSeconds * 1000 - TOKEN_EXPIRY_BUFFER_IN_MS, 0);
};
RefreshingCredentials.prototype.checkIfTokenNeedsRefresh = function () {
return this._currentTokenExpiryTime && this._currentTokenExpiryTime <= new Date().getTime();
};
RefreshingCredentials.prototype.getNewToken = function () {
return __awaiter(this, void 0, void 0, function () {
var options, _a, expires_in, id_token;
var _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
options = {
url: "".concat(this._domain).concat(OIDC_TOKEN_ENDPOINT),
body: new URLSearchParams((_b = {},
_b[OIDC_GRANT_TYPE] = OIDC_REFRESH_TOKEN_GRANT_TYPE,
_b[OIDC_REFRESH_TOKEN] = this._refreshToken,
_b[OIDC_CLIENT_ID] = this._clientId,
_b)),
headers: (_c = {},
_c[RequestHeaders.SIGNIANT_MEDIA_SHUTTLE_SDK_VERSION_HEADER] = "".concat(SDK_VERSION),
_c),
};
return [4 /*yield*/, signiant_private.request.post(options)];
case 1:
_a = _d.sent(), expires_in = _a.expires_in, id_token = _a.id_token;
this._currentTokenExpiryTime = RefreshingCredentials.getCurrentTokenExpiryTime(expires_in); //Buffer of 5 minutes
return [2 /*return*/, id_token];
}
});
});
};
RefreshingCredentials.prototype.reFetchToken = function () {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = this;
return [4 /*yield*/, this.getNewToken()];
case 1:
_a._token = _b.sent();
return [2 /*return*/];
}
});
});
};
RefreshingCredentials.prototype.logout = function () {
var _a, _b;
var options = {
url: "".concat(this._domain).concat(OIDC_REVOKE_TOKEN_ENDPOINT),
body: new URLSearchParams((_a = {},
_a[OIDC_TOKEN] = this._refreshToken,
_a[OIDC_CLIENT_ID] = this._clientId,
_a)),
headers: (_b = {
'Content-Type': 'application/x-www-form-urlencoded'
},
_b[RequestHeaders.SIGNIANT_MEDIA_SHUTTLE_SDK_VERSION_HEADER] = "".concat(SDK_VERSION),
_b),
};
return signiant_private.request.post(options);
};
/**
* Returns the credentials attributes.
* This can be used to persist the credentials for later use, for example in a database or a file,
* including session/localstorage in a browser environment and then use those persisted credentials
* to create a new instance of {@link RefreshingCredentials} without having to go through the login process again.
* @returns An object containing refreshToken, clientId, domain, and token.
*/
RefreshingCredentials.prototype.getCredentials = function () {
return {
refreshToken: this._refreshToken,
clientId: this._clientId,
domain: this._domain,
};
};
return RefreshingCredentials;
}());
export default RefreshingCredentials;