mirror of https://github.com/grpc/grpc-node.git
fix: use getRequestHeaders if available
This commit is contained in:
parent
a10c534b01
commit
b9130b239d
|
|
@ -37,6 +37,7 @@ export interface OAuth2Client {
|
||||||
getRequestMetadata: (url: string, callback: (err: Error|null, headers?: {
|
getRequestMetadata: (url: string, callback: (err: Error|null, headers?: {
|
||||||
Authorization: string
|
Authorization: string
|
||||||
}) => void) => void;
|
}) => void) => void;
|
||||||
|
getRequestHeaders: (url?: string) => Promise<{Authorization: string}>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**** Client Credentials ****/
|
/**** Client Credentials ****/
|
||||||
|
|
@ -49,19 +50,36 @@ export const credentials = mixin(
|
||||||
* @param googleCredentials The authentication client to use.
|
* @param googleCredentials The authentication client to use.
|
||||||
* @return The resulting CallCredentials object.
|
* @return The resulting CallCredentials object.
|
||||||
*/
|
*/
|
||||||
createFromGoogleCredential: (googleCredentials: OAuth2Client):
|
createFromGoogleCredential: (
|
||||||
CallCredentials => {
|
googleCredentials: OAuth2Client): CallCredentials => {
|
||||||
return CallCredentials.createFromMetadataGenerator(
|
return CallCredentials.createFromMetadataGenerator(
|
||||||
(options, callback) => {
|
(options, callback) => {
|
||||||
|
// google-auth-library pre-v2.0.0 does not have getRequestHeaders
|
||||||
|
// but has getRequestMetadata, which is deprecated in v2.0.0
|
||||||
|
let getHeaders: Promise<{Authorization: string}>;
|
||||||
|
if (typeof googleCredentials.getRequestHeaders === 'function') {
|
||||||
|
getHeaders =
|
||||||
|
googleCredentials.getRequestHeaders(options.service_url);
|
||||||
|
} else {
|
||||||
|
getHeaders = new Promise((resolve, reject) => {
|
||||||
googleCredentials.getRequestMetadata(
|
googleCredentials.getRequestMetadata(
|
||||||
options.service_url, (err, headers) => {
|
options.service_url, (err, headers) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
reject(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
resolve(headers);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
getHeaders.then(
|
||||||
|
headers => {
|
||||||
const metadata = new Metadata();
|
const metadata = new Metadata();
|
||||||
metadata.add('authorization', headers!.Authorization);
|
metadata.add('authorization', headers.Authorization);
|
||||||
callback(null, metadata);
|
callback(null, metadata);
|
||||||
|
},
|
||||||
|
err => {
|
||||||
|
callback(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue