@google-cloud/common
Advanced tools
Comparing version 4.0.1 to 4.0.2
@@ -5,2 +5,3 @@ import { AuthClient, GoogleAuth, GoogleAuthOptions } from 'google-auth-library'; | ||
import { BodyResponseCallback, DecorateRequestOptions, MakeAuthenticatedRequest, PackageJson } from './util'; | ||
export declare const DEFAULT_PROJECT_ID_TOKEN = "{{projectId}}"; | ||
export interface StreamRequestOptions extends DecorateRequestOptions { | ||
@@ -7,0 +8,0 @@ shouldReturnStream: true; |
@@ -16,3 +16,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Service = void 0; | ||
exports.Service = exports.DEFAULT_PROJECT_ID_TOKEN = void 0; | ||
/*! | ||
@@ -24,3 +24,3 @@ * @module common/service | ||
const util_1 = require("./util"); | ||
const PROJECT_ID_TOKEN = '{{projectId}}'; | ||
exports.DEFAULT_PROJECT_ID_TOKEN = '{{projectId}}'; | ||
class Service { | ||
@@ -49,3 +49,3 @@ /** | ||
this.packageJson = config.packageJson; | ||
this.projectId = options.projectId || PROJECT_ID_TOKEN; | ||
this.projectId = options.projectId || exports.DEFAULT_PROJECT_ID_TOKEN; | ||
this.projectIdRequired = config.projectIdRequired !== false; | ||
@@ -95,3 +95,3 @@ this.providedUserAgent = options.userAgent; | ||
const projectId = await this.authClient.getProjectId(); | ||
if (this.projectId === PROJECT_ID_TOKEN && projectId) { | ||
if (this.projectId === exports.DEFAULT_PROJECT_ID_TOKEN && projectId) { | ||
this.projectId = projectId; | ||
@@ -98,0 +98,0 @@ } |
@@ -72,2 +72,6 @@ /// <reference types="node" /> | ||
authClient?: AuthClient | GoogleAuth; | ||
/** | ||
* Determines if a projectId is required for authenticated requests. Defaults to `true`. | ||
*/ | ||
projectIdRequired?: boolean; | ||
} | ||
@@ -74,0 +78,0 @@ export interface MakeAuthenticatedRequestOptions { |
@@ -27,2 +27,3 @@ "use strict"; | ||
const teeny_request_1 = require("teeny-request"); | ||
const service_1 = require("./service"); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
@@ -322,3 +323,3 @@ const duplexify = require('duplexify'); | ||
const googleAutoAuthConfig = extend({}, config); | ||
if (googleAutoAuthConfig.projectId === '{{projectId}}') { | ||
if (googleAutoAuthConfig.projectId === service_1.DEFAULT_PROJECT_ID_TOKEN) { | ||
delete googleAutoAuthConfig.projectId; | ||
@@ -350,3 +351,6 @@ } | ||
const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : undefined; | ||
const onAuthenticated = (err, authenticatedReqOpts) => { | ||
async function setProjectId() { | ||
projectId = await authClient.getProjectId(); | ||
} | ||
const onAuthenticated = async (err, authenticatedReqOpts) => { | ||
const authLibraryError = err; | ||
@@ -362,2 +366,3 @@ const autoAuthFailed = err && | ||
try { | ||
// Try with existing `projectId` value | ||
authenticatedReqOpts = util.decorateRequest(authenticatedReqOpts, projectId); | ||
@@ -367,6 +372,20 @@ err = null; | ||
catch (e) { | ||
// A projectId was required, but we don't have one. | ||
// Re-use the "Could not load the default credentials error" if | ||
// auto auth failed. | ||
err = err || e; | ||
if (e instanceof projectify_1.MissingProjectIdError) { | ||
// A `projectId` was required, but we don't have one. | ||
try { | ||
// Attempt to get the `projectId` | ||
await setProjectId(); | ||
authenticatedReqOpts = util.decorateRequest(authenticatedReqOpts, projectId); | ||
err = null; | ||
} | ||
catch (e) { | ||
// Re-use the "Could not load the default credentials error" if | ||
// auto auth failed. | ||
err = err || e; | ||
} | ||
} | ||
else { | ||
// Some other error unrelated to missing `projectId` | ||
err = err || e; | ||
} | ||
} | ||
@@ -402,19 +421,42 @@ } | ||
}; | ||
Promise.all([ | ||
config.projectId && config.projectId !== '{{projectId}}' | ||
? // The user provided a project ID. We don't need to check with the | ||
// auth client, it could be incorrect. | ||
new Promise(resolve => resolve(config.projectId)) | ||
: authClient.getProjectId(), | ||
reqConfig.customEndpoint && reqConfig.useAuthWithCustomEndpoint !== true | ||
? // Using a custom API override. Do not use `google-auth-library` for | ||
// authentication. (ex: connecting to a local Datastore server) | ||
new Promise(resolve => resolve(reqOpts)) | ||
: authClient.authorizeRequest(reqOpts), | ||
]) | ||
.then(([_projectId, authorizedReqOpts]) => { | ||
projectId = _projectId; | ||
onAuthenticated(null, authorizedReqOpts); | ||
}) | ||
.catch(onAuthenticated); | ||
const prepareRequest = async () => { | ||
try { | ||
const getProjectId = async () => { | ||
if (config.projectId && | ||
config.projectId !== service_1.DEFAULT_PROJECT_ID_TOKEN) { | ||
// The user provided a project ID. We don't need to check with the | ||
// auth client, it could be incorrect. | ||
return config.projectId; | ||
} | ||
if (config.projectIdRequired === false) { | ||
// A projectId is not required. Return the default. | ||
return service_1.DEFAULT_PROJECT_ID_TOKEN; | ||
} | ||
return setProjectId(); | ||
}; | ||
const authorizeRequest = async () => { | ||
if (reqConfig.customEndpoint && | ||
!reqConfig.useAuthWithCustomEndpoint) { | ||
// Using a custom API override. Do not use `google-auth-library` for | ||
// authentication. (ex: connecting to a local Datastore server) | ||
return reqOpts; | ||
} | ||
else { | ||
return authClient.authorizeRequest(reqOpts); | ||
} | ||
}; | ||
const [_projectId, authorizedReqOpts] = await Promise.all([ | ||
getProjectId(), | ||
authorizeRequest(), | ||
]); | ||
if (_projectId) { | ||
projectId = _projectId; | ||
} | ||
return onAuthenticated(null, authorizedReqOpts); | ||
} | ||
catch (e) { | ||
return onAuthenticated(e); | ||
} | ||
}; | ||
prepareRequest(); | ||
if (stream) { | ||
@@ -421,0 +463,0 @@ return stream; |
@@ -7,2 +7,9 @@ # Changelog | ||
## [4.0.2](https://github.com/googleapis/nodejs-common/compare/v4.0.1...v4.0.2) (2022-06-21) | ||
### Bug Fixes | ||
* Requests Respect `config.projectIdRequired` === `false` ([#753](https://github.com/googleapis/nodejs-common/issues/753)) ([41934ef](https://github.com/googleapis/nodejs-common/commit/41934efbbc3e2b62f97142d46ae594d243ed31b8)) | ||
## [4.0.1](https://github.com/googleapis/nodejs-common/compare/v4.0.0...v4.0.1) (2022-06-13) | ||
@@ -9,0 +16,0 @@ |
{ | ||
"name": "@google-cloud/common", | ||
"description": "Common components for Cloud APIs Node.js Client Libraries", | ||
"version": "4.0.1", | ||
"version": "4.0.2", | ||
"license": "Apache-2.0", | ||
@@ -6,0 +6,0 @@ "author": "Google Inc.", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
132199
1994