keycloak-angular
Version:
Easy Keycloak setup for Angular applications
518 lines (510 loc) • 21.2 kB
JavaScript
import { __awaiter, __generator, __values, __spread } from 'tslib';
import { Injectable, NgModule } from '@angular/core';
import { HttpHeaders, HTTP_INTERCEPTORS } from '@angular/common/http';
import { Subject, Observable, from } from 'rxjs';
import * as Keycloak_ from 'keycloak-js';
import { mergeMap } from 'rxjs/operators';
import { CommonModule } from '@angular/common';
var KeycloakEventType = {
OnAuthError: 0,
OnAuthLogout: 1,
OnAuthRefreshError: 2,
OnAuthRefreshSuccess: 3,
OnAuthSuccess: 4,
OnReady: 5,
OnTokenExpired: 6,
};
KeycloakEventType[KeycloakEventType.OnAuthError] = 'OnAuthError';
KeycloakEventType[KeycloakEventType.OnAuthLogout] = 'OnAuthLogout';
KeycloakEventType[KeycloakEventType.OnAuthRefreshError] = 'OnAuthRefreshError';
KeycloakEventType[KeycloakEventType.OnAuthRefreshSuccess] = 'OnAuthRefreshSuccess';
KeycloakEventType[KeycloakEventType.OnAuthSuccess] = 'OnAuthSuccess';
KeycloakEventType[KeycloakEventType.OnReady] = 'OnReady';
KeycloakEventType[KeycloakEventType.OnTokenExpired] = 'OnTokenExpired';
function KeycloakEvent() { }
if (false) {
KeycloakEvent.prototype.type;
KeycloakEvent.prototype.args;
}
var KeycloakAuthGuard = (function () {
function KeycloakAuthGuard(router, keycloakAngular) {
this.router = router;
this.keycloakAngular = keycloakAngular;
}
KeycloakAuthGuard.prototype.canActivate = function (route, state) {
var _this = this;
return new Promise((function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var _a, _b, result, error_1;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_c.trys.push([0, 4, , 5]);
_a = this;
return [4, this.keycloakAngular.isLoggedIn()];
case 1:
_a.authenticated = _c.sent();
_b = this;
return [4, this.keycloakAngular.getUserRoles(true)];
case 2:
_b.roles = _c.sent();
return [4, this.isAccessAllowed(route, state)];
case 3:
result = _c.sent();
resolve(result);
return [3, 5];
case 4:
error_1 = _c.sent();
reject('An error happened during access validation. Details:' + error_1);
return [3, 5];
case 5: return [2];
}
});
}); }));
};
return KeycloakAuthGuard;
}());
if (false) {
KeycloakAuthGuard.prototype.authenticated;
KeycloakAuthGuard.prototype.roles;
KeycloakAuthGuard.prototype.router;
KeycloakAuthGuard.prototype.keycloakAngular;
KeycloakAuthGuard.prototype.isAccessAllowed = function (route, state) { };
}
var Keycloak = Keycloak_;
var KeycloakService = (function () {
function KeycloakService() {
this._keycloakEvents$ = new Subject();
}
KeycloakService.prototype.bindsKeycloakEvents = function () {
var _this = this;
this._instance.onAuthError = (function (errorData) {
_this._keycloakEvents$.next({
args: errorData,
type: KeycloakEventType.OnAuthError
});
});
this._instance.onAuthLogout = (function () {
_this._keycloakEvents$.next({ type: KeycloakEventType.OnAuthLogout });
});
this._instance.onAuthRefreshSuccess = (function () {
_this._keycloakEvents$.next({
type: KeycloakEventType.OnAuthRefreshSuccess
});
});
this._instance.onAuthRefreshError = (function () {
_this._keycloakEvents$.next({
type: KeycloakEventType.OnAuthRefreshError
});
});
this._instance.onAuthSuccess = (function () {
_this._keycloakEvents$.next({ type: KeycloakEventType.OnAuthSuccess });
});
this._instance.onTokenExpired = (function () {
_this._keycloakEvents$.next({
type: KeycloakEventType.OnTokenExpired
});
});
this._instance.onReady = (function (authenticated) {
_this._keycloakEvents$.next({
args: authenticated,
type: KeycloakEventType.OnReady
});
});
};
KeycloakService.prototype.loadExcludedUrls = function (bearerExcludedUrls) {
var e_1, _a;
var excludedUrls = [];
try {
for (var bearerExcludedUrls_1 = __values(bearerExcludedUrls), bearerExcludedUrls_1_1 = bearerExcludedUrls_1.next(); !bearerExcludedUrls_1_1.done; bearerExcludedUrls_1_1 = bearerExcludedUrls_1.next()) {
var item = bearerExcludedUrls_1_1.value;
var excludedUrl = void 0;
if (typeof item === 'string') {
excludedUrl = { urlPattern: new RegExp(item, 'i'), httpMethods: [] };
}
else {
excludedUrl = {
urlPattern: new RegExp(item.url, 'i'),
httpMethods: item.httpMethods
};
}
excludedUrls.push(excludedUrl);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (bearerExcludedUrls_1_1 && !bearerExcludedUrls_1_1.done && (_a = bearerExcludedUrls_1.return)) _a.call(bearerExcludedUrls_1);
}
finally { if (e_1) throw e_1.error; }
}
return excludedUrls;
};
KeycloakService.prototype.initServiceValues = function (_a) {
var _b = _a.enableBearerInterceptor, enableBearerInterceptor = _b === void 0 ? true : _b, _c = _a.loadUserProfileAtStartUp, loadUserProfileAtStartUp = _c === void 0 ? true : _c, _d = _a.bearerExcludedUrls, bearerExcludedUrls = _d === void 0 ? [] : _d, _e = _a.authorizationHeaderName, authorizationHeaderName = _e === void 0 ? 'Authorization' : _e, _f = _a.bearerPrefix, bearerPrefix = _f === void 0 ? 'bearer' : _f, initOptions = _a.initOptions;
this._enableBearerInterceptor = enableBearerInterceptor;
this._loadUserProfileAtStartUp = loadUserProfileAtStartUp;
this._authorizationHeaderName = authorizationHeaderName;
this._bearerPrefix = bearerPrefix.trim().concat(' ');
this._excludedUrls = this.loadExcludedUrls(bearerExcludedUrls);
this._silentRefresh = initOptions ? initOptions.flow === 'implicit' : false;
};
KeycloakService.prototype.init = function (options) {
var _this = this;
if (options === void 0) { options = {}; }
return new Promise((function (resolve, reject) {
_this.initServiceValues(options);
var config = options.config, initOptions = options.initOptions;
_this._instance = Keycloak(config);
_this.bindsKeycloakEvents();
_this._instance
.init(initOptions)
.success((function (authenticated) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(authenticated && this._loadUserProfileAtStartUp)) return [3, 2];
return [4, this.loadUserProfile()];
case 1:
_a.sent();
_a.label = 2;
case 2:
resolve(authenticated);
return [2];
}
});
}); }))
.error((function (kcError) {
var msg = 'An error happened during Keycloak initialization.';
if (kcError) {
var error = kcError.error, error_description = kcError.error_description;
msg = msg.concat("\nAdapter error details:\nError: " + error + "\nDescription: " + error_description);
}
reject(msg);
}));
}));
};
KeycloakService.prototype.login = function (options) {
var _this = this;
if (options === void 0) { options = {}; }
return new Promise((function (resolve, reject) {
_this._instance
.login(options)
.success((function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this._loadUserProfileAtStartUp) return [3, 2];
return [4, this.loadUserProfile()];
case 1:
_a.sent();
_a.label = 2;
case 2:
resolve();
return [2];
}
});
}); }))
.error((function () { return reject("An error happened during the login."); }));
}));
};
KeycloakService.prototype.logout = function (redirectUri) {
var _this = this;
return new Promise((function (resolve, reject) {
var options = {
redirectUri: redirectUri
};
_this._instance
.logout(options)
.success((function () {
_this._userProfile = undefined;
resolve();
}))
.error((function () { return reject('An error happened during logout.'); }));
}));
};
KeycloakService.prototype.register = function (options) {
var _this = this;
if (options === void 0) { options = { action: 'register' }; }
return new Promise((function (resolve, reject) {
_this._instance
.register(options)
.success((function () {
resolve();
}))
.error((function () { return reject('An error happened during the register execution.'); }));
}));
};
KeycloakService.prototype.isUserInRole = function (role, resource) {
var hasRole;
hasRole = this._instance.hasResourceRole(role, resource);
if (!hasRole) {
hasRole = this._instance.hasRealmRole(role);
}
return hasRole;
};
KeycloakService.prototype.getUserRoles = function (allRoles) {
if (allRoles === void 0) { allRoles = true; }
var roles = [];
if (this._instance.resourceAccess) {
for (var key in this._instance.resourceAccess) {
if (this._instance.resourceAccess.hasOwnProperty(key)) {
var resourceAccess = this._instance.resourceAccess[key];
var clientRoles = resourceAccess['roles'] || [];
roles = roles.concat(clientRoles);
}
}
}
if (allRoles && this._instance.realmAccess) {
var realmRoles = this._instance.realmAccess['roles'] || [];
roles.push.apply(roles, __spread(realmRoles));
}
return roles;
};
KeycloakService.prototype.isLoggedIn = function () {
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, 2, , 3]);
if (!this._instance.authenticated) {
return [2, false];
}
return [4, this.updateToken(20)];
case 1:
_a.sent();
return [2, true];
case 2:
error_1 = _a.sent();
return [2, false];
case 3: return [2];
}
});
});
};
KeycloakService.prototype.isTokenExpired = function (minValidity) {
if (minValidity === void 0) { minValidity = 0; }
return this._instance.isTokenExpired(minValidity);
};
KeycloakService.prototype.updateToken = function (minValidity) {
var _this = this;
if (minValidity === void 0) { minValidity = 5; }
return new Promise((function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
if (this._silentRefresh) {
if (this.isTokenExpired()) {
reject('Failed to refresh the token, or the session is expired');
}
else {
resolve(true);
}
return [2];
}
if (!this._instance) {
reject('Keycloak Angular library is not initialized.');
return [2];
}
this._instance
.updateToken(minValidity)
.success((function (refreshed) {
resolve(refreshed);
}))
.error((function () { return reject('Failed to refresh the token, or the session is expired'); }));
return [2];
});
}); }));
};
KeycloakService.prototype.loadUserProfile = function (forceReload) {
var _this = this;
if (forceReload === void 0) { forceReload = false; }
return new Promise((function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
if (this._userProfile && !forceReload) {
resolve(this._userProfile);
return [2];
}
if (!this._instance.authenticated) {
reject('The user profile was not loaded as the user is not logged in.');
return [2];
}
this._instance
.loadUserProfile()
.success((function (result) {
_this._userProfile = ((result));
resolve(_this._userProfile);
}))
.error((function () { return reject('The user profile could not be loaded.'); }));
return [2];
});
}); }));
};
KeycloakService.prototype.getToken = function () {
var _this = this;
return new Promise((function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4, this.updateToken(10)];
case 1:
_a.sent();
resolve(this._instance.token);
return [3, 3];
case 2:
error_2 = _a.sent();
this.login();
return [3, 3];
case 3: return [2];
}
});
}); }));
};
KeycloakService.prototype.getUsername = function () {
if (!this._userProfile) {
throw new Error('User not logged in or user profile was not loaded.');
}
return ((this._userProfile.username));
};
KeycloakService.prototype.clearToken = function () {
this._instance.clearToken();
};
KeycloakService.prototype.addTokenToHeader = function (headers) {
var _this = this;
if (headers === void 0) { headers = new HttpHeaders(); }
return Observable.create((function (observer) { return __awaiter(_this, void 0, void 0, function () {
var token, error_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4, this.getToken()];
case 1:
token = _a.sent();
headers = headers.set(this._authorizationHeaderName, this._bearerPrefix + token);
observer.next(headers);
observer.complete();
return [3, 3];
case 2:
error_3 = _a.sent();
observer.error(error_3);
return [3, 3];
case 3: return [2];
}
});
}); }));
};
KeycloakService.prototype.getKeycloakInstance = function () {
return this._instance;
};
Object.defineProperty(KeycloakService.prototype, "excludedUrls", {
get: function () {
return this._excludedUrls;
},
enumerable: true,
configurable: true
});
Object.defineProperty(KeycloakService.prototype, "enableBearerInterceptor", {
get: function () {
return this._enableBearerInterceptor;
},
enumerable: true,
configurable: true
});
Object.defineProperty(KeycloakService.prototype, "keycloakEvents$", {
get: function () {
return this._keycloakEvents$;
},
enumerable: true,
configurable: true
});
KeycloakService.decorators = [
{ type: Injectable }
];
return KeycloakService;
}());
if (false) {
KeycloakService.prototype._instance;
KeycloakService.prototype._userProfile;
KeycloakService.prototype._enableBearerInterceptor;
KeycloakService.prototype._silentRefresh;
KeycloakService.prototype._loadUserProfileAtStartUp;
KeycloakService.prototype._bearerPrefix;
KeycloakService.prototype._authorizationHeaderName;
KeycloakService.prototype._excludedUrls;
KeycloakService.prototype._keycloakEvents$;
}
var KeycloakBearerInterceptor = (function () {
function KeycloakBearerInterceptor(keycloak) {
this.keycloak = keycloak;
}
KeycloakBearerInterceptor.prototype.isUrlExcluded = function (_a, _b) {
var method = _a.method, url = _a.url;
var urlPattern = _b.urlPattern, httpMethods = _b.httpMethods;
var httpTest = httpMethods.length === 0 ||
httpMethods.join().indexOf(method.toUpperCase()) > -1;
var urlTest = urlPattern.test(url);
return httpTest && urlTest;
};
KeycloakBearerInterceptor.prototype.intercept = function (req, next) {
var _this = this;
var _a = this.keycloak, enableBearerInterceptor = _a.enableBearerInterceptor, excludedUrls = _a.excludedUrls;
if (!enableBearerInterceptor) {
return next.handle(req);
}
var shallPass = excludedUrls.findIndex((function (item) { return _this.isUrlExcluded(req, item); })) > -1;
if (shallPass) {
return next.handle(req);
}
return from(this.keycloak.isLoggedIn()).pipe(mergeMap((function (loggedIn) { return loggedIn
? _this.handleRequestWithTokenHeader(req, next)
: next.handle(req); })));
};
KeycloakBearerInterceptor.prototype.handleRequestWithTokenHeader = function (req, next) {
return this.keycloak.addTokenToHeader(req.headers).pipe(mergeMap((function (headersWithBearer) {
var kcReq = req.clone({ headers: headersWithBearer });
return next.handle(kcReq);
})));
};
KeycloakBearerInterceptor.decorators = [
{ type: Injectable }
];
KeycloakBearerInterceptor.ctorParameters = function () { return [
{ type: KeycloakService }
]; };
return KeycloakBearerInterceptor;
}());
if (false) {
KeycloakBearerInterceptor.prototype.keycloak;
}
var CoreModule = (function () {
function CoreModule() {
}
CoreModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule],
providers: [
KeycloakService,
{
provide: HTTP_INTERCEPTORS,
useClass: KeycloakBearerInterceptor,
multi: true
}
]
},] }
];
return CoreModule;
}());
var KeycloakAngularModule = (function () {
function KeycloakAngularModule() {
}
KeycloakAngularModule.decorators = [
{ type: NgModule, args: [{
imports: [CoreModule]
},] }
];
return KeycloakAngularModule;
}());
export { CoreModule, KeycloakAngularModule, KeycloakAuthGuard, KeycloakBearerInterceptor, KeycloakEventType, KeycloakService };
//# sourceMappingURL=keycloak-angular.js.map