Merge pull request #554 from cjihrig/creds

grpc-js-core: delay composing credentials
This commit is contained in:
Michael Lumish 2018-09-25 15:48:51 -07:00 committed by GitHub
commit f3ac739175
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 10 deletions

View File

@ -7,10 +7,11 @@ import {Metadata} from './metadata';
export class CallCredentialsFilter extends BaseFilter implements Filter { export class CallCredentialsFilter extends BaseFilter implements Filter {
private serviceUrl: string; private serviceUrl: string;
constructor( constructor(
private readonly credentials: CallCredentials, private readonly channel: Http2Channel, private readonly stream: Call) {
private readonly host: string, private readonly path: string) {
super(); super();
const splitPath: string[] = path.split('/'); this.channel = channel;
this.stream = stream;
const splitPath: string[] = stream.getMethod().split('/');
let serviceName = ''; let serviceName = '';
/* The standard path format is "/{serviceName}/{methodName}", so if we split /* The standard path format is "/{serviceName}/{methodName}", so if we split
* by '/', the first item should be empty and the second should be the * by '/', the first item should be empty and the second should be the
@ -20,12 +21,15 @@ export class CallCredentialsFilter extends BaseFilter implements Filter {
} }
/* Currently, call credentials are only allowed on HTTPS connections, so we /* Currently, call credentials are only allowed on HTTPS connections, so we
* can assume that the scheme is "https" */ * can assume that the scheme is "https" */
this.serviceUrl = `https://${host}/${serviceName}`; this.serviceUrl = `https://${stream.getHost()}/${serviceName}`;
} }
async sendMetadata(metadata: Promise<Metadata>): Promise<Metadata> { async sendMetadata(metadata: Promise<Metadata>): Promise<Metadata> {
const channelCredentials = this.channel.credentials._getCallCredentials();
const streamCredentials = this.stream.getCredentials();
const credentials = channelCredentials.compose(streamCredentials);
const credsMetadata = const credsMetadata =
this.credentials.generateMetadata({service_url: this.serviceUrl}); credentials.generateMetadata({service_url: this.serviceUrl});
const resultMetadata = await metadata; const resultMetadata = await metadata;
resultMetadata.merge(await credsMetadata); resultMetadata.merge(await credsMetadata);
return resultMetadata; return resultMetadata;
@ -34,14 +38,12 @@ export class CallCredentialsFilter extends BaseFilter implements Filter {
export class CallCredentialsFilterFactory implements export class CallCredentialsFilterFactory implements
FilterFactory<CallCredentialsFilter> { FilterFactory<CallCredentialsFilter> {
private readonly credentials: CallCredentials; private readonly channel: Http2Channel;
constructor(channel: Http2Channel) { constructor(channel: Http2Channel) {
this.credentials = channel.credentials._getCallCredentials(); this.channel = channel;
} }
createFilter(callStream: Call): CallCredentialsFilter { createFilter(callStream: Call): CallCredentialsFilter {
return new CallCredentialsFilter( return new CallCredentialsFilter(this.channel, callStream);
this.credentials.compose(callStream.getCredentials()),
callStream.getHost(), callStream.getMethod());
} }
} }