oidc-client-rx
Version:
ReactiveX enhanced OIDC and OAuth2 protocol support for browser-based JavaScript applications
68 lines (67 loc) • 3.49 kB
JavaScript
import { Injectable, inject } from "injection-js";
import { AuthStateService } from "../auth-state/auth-state.service.js";
import { ConfigurationService } from "../config/config.service.js";
import { LoggerService } from "../logging/logger.service.js";
import { flattenArray } from "../utils/collections/collections.helper.js";
import { ClosestMatchingRouteService } from "./closest-matching-route.service.js";
function _ts_decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : null === desc ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(decorators, target, key, desc);
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
class AuthInterceptor {
intercept(req, next) {
return interceptRequest(req, next.handle, {
configurationService: this.configurationService,
authStateService: this.authStateService,
closestMatchingRouteService: this.closestMatchingRouteService,
loggerService: this.loggerService
});
}
constructor(){
this.authStateService = inject(AuthStateService);
this.configurationService = inject(ConfigurationService);
this.loggerService = inject(LoggerService);
this.closestMatchingRouteService = inject(ClosestMatchingRouteService);
}
}
AuthInterceptor = _ts_decorate([
Injectable()
], AuthInterceptor);
function authInterceptor() {
const deps = {
configurationService: inject(ConfigurationService),
authStateService: inject(AuthStateService),
closestMatchingRouteService: inject(ClosestMatchingRouteService),
loggerService: inject(LoggerService)
};
return (req, next)=>interceptRequest(req, next, deps);
}
function interceptRequest(req, next, deps) {
if (!deps.configurationService.hasAtLeastOneConfig()) return next(req);
const allConfigurations = deps.configurationService.getAllConfigurations();
const allRoutesConfigured = allConfigurations.map((x)=>x.secureRoutes || []);
const allRoutesConfiguredFlat = flattenArray(allRoutesConfigured);
if (0 === allRoutesConfiguredFlat.length) {
deps.loggerService.logDebug(allConfigurations[0], "No routes to check configured");
return next(req);
}
const { matchingConfig, matchingRoute } = deps.closestMatchingRouteService.getConfigIdForClosestMatchingRoute(req.url, allConfigurations);
if (!matchingConfig) {
deps.loggerService.logDebug(allConfigurations[0], `Did not find any configured route for route ${req.url}`);
return next(req);
}
deps.loggerService.logDebug(matchingConfig, `'${req.url}' matches configured route '${matchingRoute}'`);
const token = deps.authStateService.getAccessToken(matchingConfig);
if (!token) {
deps.loggerService.logDebug(matchingConfig, `Wanted to add token to ${req.url} but found no token: '${token}'`);
return next(req);
}
deps.loggerService.logDebug(matchingConfig, `'${req.url}' matches configured route '${matchingRoute}', adding token`);
req = req.clone({
headers: req.headers.set("Authorization", `Bearer ${token}`)
});
return next(req);
}
export { AuthInterceptor, authInterceptor };