From dbe87d6592a8384ce8c4104ca032db15cb6e0a08 Mon Sep 17 00:00:00 2001 From: Tair Sabirgaliev Date: Fri, 10 Feb 2017 05:41:49 +0600 Subject: [PATCH] angular2-product-app: simplify KeycloakHttp --- .../src/main/webapp/app/keycloak.http.ts | 107 +++--------------- 1 file changed, 15 insertions(+), 92 deletions(-) diff --git a/examples/demo-template/angular2-product-app/src/main/webapp/app/keycloak.http.ts b/examples/demo-template/angular2-product-app/src/main/webapp/app/keycloak.http.ts index 028f256a90..8c0958cff8 100644 --- a/examples/demo-template/angular2-product-app/src/main/webapp/app/keycloak.http.ts +++ b/examples/demo-template/angular2-product-app/src/main/webapp/app/keycloak.http.ts @@ -9,52 +9,10 @@ import {Observable} from 'rxjs/Rx'; */ @Injectable() export class KeycloakHttp extends Http { - constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions, private _keycloakService:KeycloakService) { + constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions, private _keycloakService: KeycloakService) { super(_backend, _defaultOptions); } - private setToken(options: RequestOptionsArgs) { - - if (options == null || KeycloakService.auth == null || KeycloakService.auth.authz == null || KeycloakService.auth.authz.token == null) { - console.log("Need a token, but no token is available, not setting bearer token."); - return; - } - - options.headers.set('Authorization', 'Bearer ' + KeycloakService.auth.authz.token); - } - - private configureRequest(f:Function, url:string | Request, options:RequestOptionsArgs, body?: any):Observable { - let tokenPromise:Promise = this._keycloakService.getToken(); - let tokenObservable:Observable = Observable.fromPromise(tokenPromise); - let tokenUpdateObservable:Observable = Observable.create((observer) => { - if (options == null) { - let headers = new Headers(); - options = new RequestOptions({ headers: headers }); - } - - this.setToken(options); - observer.next(); - observer.complete(); - }); - let requestObservable:Observable = Observable.create((observer) => { - let result; - if (body) { - result = f.apply(this, [url, body, options]); - } else { - result = f.apply(this, [url, options]); - } - - result.subscribe((response) => { - observer.next(response); - observer.complete(); - }, (err) => observer.error(err)); - }); - - return >Observable - .merge(tokenObservable, tokenUpdateObservable, requestObservable, 1) // Insure no concurrency in the merged Observables - .filter((response) => response instanceof Response); - } - /** * Performs any type of http request. First argument is required, and can either be a url or * a {@link Request} instance. If the first argument is a url, an optional {@link RequestOptions} @@ -62,55 +20,20 @@ export class KeycloakHttp extends Http { * of {@link BaseRequestOptions} before performing the request. */ request(url: string | Request, options?: RequestOptionsArgs): Observable { - return this.configureRequest(super.request, url, options); + const tokenPromise: Promise = this._keycloakService.getToken(); + const tokenObservable: Observable = Observable.fromPromise(tokenPromise); + + if (typeof url === 'string') { + return tokenObservable.map(token => { + const authOptions = new RequestOptions({headers: new Headers({'Authorization': 'Bearer ' + token})}); + return new RequestOptions().merge(options).merge(authOptions); + }).concatMap(opts => super.request(url, opts)); + } else if (url instanceof Request) { + return tokenObservable.map(token => { + url.headers.set('Authorization', 'Bearer ' + token); + return url; + }).concatMap(request => super.request(request)); + } } - /** - * Performs a request with `get` http method. - */ - get(url: string, options?: RequestOptionsArgs): Observable { - return this.configureRequest(super.get, url, options); - } - - /** - * Performs a request with `post` http method. - */ - post(url: string, body: any, options?: RequestOptionsArgs): Observable { - return this.configureRequest(super.post, url, options, body); - } - - /** - * Performs a request with `put` http method. - */ - put(url: string, body: any, options?: RequestOptionsArgs): Observable { - return this.configureRequest(super.put, url, options, body); - } - - /** - * Performs a request with `delete` http method. - */ - delete(url: string, options?: RequestOptionsArgs): Observable { - return this.configureRequest(super.delete, url, options); - } - - /** - * Performs a request with `patch` http method. - */ - patch(url: string, body: any, options?: RequestOptionsArgs): Observable { - return this.configureRequest(super.patch, url, options, body); - } - - /** - * Performs a request with `head` http method. - */ - head(url: string, options?: RequestOptionsArgs): Observable { - return this.configureRequest(super.head, url, options); - } - - /** - * Performs a request with `options` http method. - */ - options(url: string, options?: RequestOptionsArgs): Observable { - return this.configureRequest(super.options, url, options); - } }