-
Notifications
You must be signed in to change notification settings - Fork 388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Support Not Requiring projectId
When Not Required
#1448
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
59b8ad3
feat: Support Not Requiring `projectId` When Not Required
d-goog b765af0
test: Add test for `null` projectId support
d-goog 1c278a7
Merge branch 'main' into allow-missing-project-id
d-goog d1a90ea
Merge branch 'main' of https://github.com/googleapis/google-auth-libr…
d-goog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -116,6 +116,13 @@ export interface GoogleAuthOptions<T extends AuthClient = JSONClient> { | |||||||||||||||||||||||||||||||||||||||||||||||||
export const CLOUD_SDK_CLIENT_ID = | ||||||||||||||||||||||||||||||||||||||||||||||||||
'764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const GoogleAuthExceptionMessages = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
NO_PROJECT_ID_FOUND: | ||||||||||||||||||||||||||||||||||||||||||||||||||
'Unable to detect a Project Id in the current environment. \n' + | ||||||||||||||||||||||||||||||||||||||||||||||||||
'To learn more about authentication and Google APIs, visit: \n' + | ||||||||||||||||||||||||||||||||||||||||||||||||||
'https://cloud.google.com/docs/authentication/getting-started', | ||||||||||||||||||||||||||||||||||||||||||||||||||
} as const; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
export class GoogleAuth<T extends AuthClient = JSONClient> { | ||||||||||||||||||||||||||||||||||||||||||||||||||
transporter?: Transporter; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -191,9 +198,32 @@ export class GoogleAuth<T extends AuthClient = JSONClient> { | |||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
private getProjectIdAsync(): Promise<string | null> { | ||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||
* A temporary method for internal `getProjectId` usages where `null` is | ||||||||||||||||||||||||||||||||||||||||||||||||||
* acceptable. In a future major release, `getProjectId` should return `null` | ||||||||||||||||||||||||||||||||||||||||||||||||||
* (as the `Promise<string | null>` base signature describes) and this private | ||||||||||||||||||||||||||||||||||||||||||||||||||
* method should be removed. | ||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||
* @returns Promise that resolves with project id (or `null`) | ||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||
async #getProjectIdOptional(): Promise<string | null> { | ||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||
return await this.getProjectId(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
e instanceof Error && | ||||||||||||||||||||||||||||||||||||||||||||||||||
e.message === GoogleAuthExceptionMessages.NO_PROJECT_ID_FOUND | ||||||||||||||||||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
throw e; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+209
to
+222
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leveraging the Promises API (similar to the previous PR) 😄 might simplify it a bit here again:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
private async getProjectIdAsync(): Promise<string | null> { | ||||||||||||||||||||||||||||||||||||||||||||||||||
if (this._cachedProjectId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
return Promise.resolve(this._cachedProjectId); | ||||||||||||||||||||||||||||||||||||||||||||||||||
return this._cachedProjectId; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
// In implicit case, supports three environments. In order of precedence, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -212,11 +242,7 @@ export class GoogleAuth<T extends AuthClient = JSONClient> { | |||||||||||||||||||||||||||||||||||||||||||||||||
(await this.getExternalAccountClientProjectId()); | ||||||||||||||||||||||||||||||||||||||||||||||||||
this._cachedProjectId = projectId; | ||||||||||||||||||||||||||||||||||||||||||||||||||
if (!projectId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
throw new Error( | ||||||||||||||||||||||||||||||||||||||||||||||||||
'Unable to detect a Project Id in the current environment. \n' + | ||||||||||||||||||||||||||||||||||||||||||||||||||
'To learn more about authentication and Google APIs, visit: \n' + | ||||||||||||||||||||||||||||||||||||||||||||||||||
'https://cloud.google.com/docs/authentication/getting-started' | ||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||
throw new Error(GoogleAuthExceptionMessages.NO_PROJECT_ID_FOUND); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
return projectId; | ||||||||||||||||||||||||||||||||||||||||||||||||||
})(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -269,7 +295,7 @@ export class GoogleAuth<T extends AuthClient = JSONClient> { | |||||||||||||||||||||||||||||||||||||||||||||||||
if (this.cachedCredential) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||||||||||||||||||||||||
credential: this.cachedCredential, | ||||||||||||||||||||||||||||||||||||||||||||||||||
projectId: await this.getProjectIdAsync(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
projectId: await this.#getProjectIdOptional(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -287,7 +313,8 @@ export class GoogleAuth<T extends AuthClient = JSONClient> { | |||||||||||||||||||||||||||||||||||||||||||||||||
credential.scopes = this.getAnyScopes(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
this.cachedCredential = credential; | ||||||||||||||||||||||||||||||||||||||||||||||||||
projectId = await this.getProjectId(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
projectId = await this.#getProjectIdOptional(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
return {credential, projectId}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -302,7 +329,7 @@ export class GoogleAuth<T extends AuthClient = JSONClient> { | |||||||||||||||||||||||||||||||||||||||||||||||||
credential.scopes = this.getAnyScopes(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
this.cachedCredential = credential; | ||||||||||||||||||||||||||||||||||||||||||||||||||
projectId = await this.getProjectId(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
projectId = await this.#getProjectIdOptional(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
return {credential, projectId}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -329,7 +356,7 @@ export class GoogleAuth<T extends AuthClient = JSONClient> { | |||||||||||||||||||||||||||||||||||||||||||||||||
// the rest. | ||||||||||||||||||||||||||||||||||||||||||||||||||
(options as ComputeOptions).scopes = this.getAnyScopes(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
this.cachedCredential = new Compute(options); | ||||||||||||||||||||||||||||||||||||||||||||||||||
projectId = await this.getProjectId(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
projectId = await this.#getProjectIdOptional(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
return {projectId, credential: this.cachedCredential}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checking for
instanceof Error
is quite redundant here, if it threw like an error and has amessage
property like an error, it's def going to be an error 😄There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understandable; the
instanceof Error
check is more-so a type assertion. By default, TS considers 'e'unknown