keycloak-typescript
Version:
A user friendly library to use keycloak in nodejs projects
117 lines • 5.45 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const ITokenManager_1 = require("./Interfaces/ITokenManager");
// Helpers
const request_builder_1 = require("../helpers/request-builder");
// External
const querystring = __importStar(require("querystring"));
class TokenManager extends ITokenManager_1.ITokenManager {
constructor(url, observers) {
super();
this.initializeManager = (keycloakLogin) => __awaiter(this, void 0, void 0, function* () {
this.clientSecret = keycloakLogin.clientSecret;
this.clientId = keycloakLogin.clientId;
const body = {
client_id: this.clientId,
username: keycloakLogin.username,
password: keycloakLogin.password,
client_secret: this.clientSecret,
grant_type: 'password'
};
const apiConfig = {
url: this.url,
method: 'POST',
headers: {},
body: querystring.stringify(body)
};
yield this.makeRefreshRequest(apiConfig);
//Create timed task to refresh token
const marginInSeconds = 60;
if (this.accessTokenExpireTime != undefined &&
this.refreshTokenExpireTime != undefined) {
const accessTokenInterval = this.accessTokenExpireTime - marginInSeconds;
const refreshTokenInterval = this.refreshTokenExpireTime - marginInSeconds;
if (accessTokenInterval > refreshTokenInterval) {
setInterval(() => {
this.refreshAccessToken();
}, refreshTokenInterval * 1000);
}
else {
setInterval(() => {
this.refreshAccessToken();
}, accessTokenInterval * 1000);
}
}
});
this.makeRefreshRequest = (apiConfig) => __awaiter(this, void 0, void 0, function* () {
const response = yield (0, request_builder_1.requestBuilder)(apiConfig);
this.accessToken = response === null || response === void 0 ? void 0 : response.data.access_token;
this.refreshToken = response === null || response === void 0 ? void 0 : response.data.refresh_token;
this.accessTokenExpireTime = response === null || response === void 0 ? void 0 : response.data.expires_in;
this.refreshTokenExpireTime = response === null || response === void 0 ? void 0 : response.data.refresh_expires_in;
//notify observers about new access token
this.notify();
});
this.refreshAccessToken = () => __awaiter(this, void 0, void 0, function* () {
const body = {
client_id: this.clientId,
client_secret: this.clientSecret,
refresh_token: this.refreshToken,
grant_type: 'refresh_token'
};
const apiConfig = {
url: this.url,
method: 'POST',
headers: {},
body: querystring.stringify(body)
};
yield this.makeRefreshRequest(apiConfig);
});
this.notify = () => {
this.observers.forEach((observer) => {
observer.update(this, [this.accessToken ? this.accessToken : '']);
});
};
this.url = url;
//Attaching initial observers if any
if (observers) {
observers.forEach((observer) => {
this.attach(observer);
});
}
}
}
exports.default = TokenManager;
//# sourceMappingURL=TokenManager.js.map