feat(console): grpc web interceptors, cleanup services (#631)

* managementservice, authservice, change all refs

* refactor grpc auth

* fix references

* interceptor logic

* new interceptor

* appinitializers, environment loader, interceptors

* remove circular dep

* init authconfig in grpc service

* authconfig

* lint

* update mgmt service

* fix references

* update clientid, fix interceptor

* rm unused authconfig

* todo auth interceptor
This commit is contained in:
Max Peintner
2020-08-29 10:43:35 +02:00
committed by GitHub
parent 40b8faaddd
commit eeea1f1c4b
81 changed files with 1877 additions and 2639 deletions

View File

@@ -0,0 +1,40 @@
import { Injectable } from '@angular/core';
import { Request, UnaryInterceptor, UnaryResponse } from 'grpc-web';
import { filter, first } from 'rxjs/operators';
import { AuthenticationService } from '../authentication.service';
import { StorageService } from '../storage.service';
const authorizationKey = 'Authorization';
const bearerPrefix = 'Bearer';
const accessTokenStorageKey = 'access_token';
@Injectable({ providedIn: 'root' })
export class AuthInterceptor<TReq = unknown, TResp = unknown> implements UnaryInterceptor<TReq, TResp> {
constructor(private authenticationService: AuthenticationService, private storageService: StorageService) { }
public async intercept(request: Request<TReq, TResp>, invoker: any): Promise<UnaryResponse<TReq, TResp>> {
await this.authenticationService.authenticationChanged.pipe(
filter((authed) => !!authed),
first(),
).toPromise();
const metadata = request.getMetadata();
const accessToken = this.storageService.getItem(accessTokenStorageKey);
metadata[authorizationKey] = `${bearerPrefix} ${accessToken}`;
return invoker(request).then((response: any) => {
// const message = response.getResponseMessage();
const respMetadata = response.getMetadata();
// TODO: intercept unauthenticated an authenticate
// const status = respMetadata['grpc-status'];
// console.log(respMetadata, status);
// if (status?.code === 16) {
// this.authenticationService.authenticate();
// }
return response;
});
}
}