Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keycloakBearerInterceptorFn example #541

Open
OZIOisgood opened this issue Feb 12, 2024 · 1 comment
Open

keycloakBearerInterceptorFn example #541

OZIOisgood opened this issue Feb 12, 2024 · 1 comment

Comments

@OZIOisgood
Copy link

I wrote BearerInterceptor as plain JavaScript function.

import { HttpEvent, HttpHandlerFn, HttpInterceptorFn, HttpRequest } from '@angular/common/http';
import { inject } from '@angular/core';
import { KeycloakService } from 'keycloak-angular';
import { ExcludedUrlRegex } from 'keycloak-angular/lib/core/interfaces/keycloak-options';
import { Observable, combineLatest, from, mergeMap, of } from 'rxjs';

const isUrlExcluded = ({ method, url }: HttpRequest<unknown>, { urlPattern, httpMethods }: ExcludedUrlRegex): boolean => {
  const httpTest = (httpMethods?.length === 0 || httpMethods?.join().includes(method.toUpperCase())) ?? true;

  const urlTest = urlPattern.test(url);

  return httpTest && urlTest;
};

const conditionallyUpdateToken = async (req: HttpRequest<unknown>): Promise<boolean> => {
  const keycloak = inject(KeycloakService);

  if (keycloak.shouldUpdateToken(req)) {
    return await keycloak.updateToken();
  }

  return true;
};

const handleRequestWithTokenHeader = (req: HttpRequest<unknown>, next: HttpHandlerFn): Observable<HttpEvent<unknown>> => {
  const keycloak = inject(KeycloakService);

  return keycloak.addTokenToHeader(req.headers).pipe(
    mergeMap(headersWithBearer => {
      const kcReq = req.clone({ headers: headersWithBearer });
      return next(kcReq);
    })
  );
};

export const keycloakBearerInterceptorFn: HttpInterceptorFn = (req: HttpRequest<unknown>, next: HttpHandlerFn): Observable<HttpEvent<unknown>> => {
  const keycloak = inject(KeycloakService);

  const { enableBearerInterceptor, excludedUrls } = keycloak;

  if (!enableBearerInterceptor) {
    return next(req);
  }

  const shallPass: boolean = !keycloak.shouldAddToken(req) || excludedUrls.findIndex(item => isUrlExcluded(req, item)) > -1;
  if (shallPass) {
    return next(req);
  }

  return combineLatest([from(conditionallyUpdateToken(req)), of(keycloak.isLoggedIn())]).pipe(
    mergeMap(([, isLoggedIn]) => (isLoggedIn ? handleRequestWithTokenHeader(req, next) : next(req)))
  );
};

But I have a problem with "KeyCloakService injection".

How to use BearerInterceptor properly in Angular 17? (Ignoring the "withInterceptorsFromDi" option)

@HonoluluHenk
Copy link

Here's a working example that lets you use KeycloakAngular without other hacks.

Just use this snippet in the providers section of your bootstrapApplication:

    importProvidersFrom(KeycloakAngularModule),
    {
      provide: KeycloakBearerInterceptor,
      useClass: KeycloakBearerInterceptor,
    },
    provideHttpClient(
      withInterceptors([
        (req, next) => {
          return inject(KeycloakBearerInterceptor)
            .intercept(req, {handle: r => next(r)});
        },
      ]),
    ),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants