-
Notifications
You must be signed in to change notification settings - Fork 55
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 for API Key client option #351
Conversation
e6bb5c8
to
b23a426
Compare
@bshaffer Is this still in works, else we can close this? |
@vishwarajanand looking at the changelog, guidance for the API Key variable was removed. Let's check with the auth team and see what the status is on supporting API keys @TimurSadykov what are your thoughts on this one? |
I think the latest is that we don't add a generic support in auth libraries |
we are not going to add a generic support for api key due to multiple push backs |
Closing for now |
…php into add-apikey-client-option
src/ClientOptionsTrait.php
Outdated
// If the user has explicitly set the apiKey option, use Api Key credentials | ||
if (isset($credentialsConfig['apiKey'])) { | ||
return ApiKeyHeaderCredentials::build($credentialsConfig); | ||
} |
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.
I want to call out these two conditions from the API Key Design Doc.
- If both credentials and api key are explicitly set, return an error to the user.
- If api key is set, do not attempt the ADC flow.
I think the first condition is missing in this code.
Is the second condition also missing?
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.
In the block you've highlighted, the logic goes like this:
- IF
$credentials
are null (e.g. credentials have not been explicitly set), THEN- IF the
apiKey
option has been set, THEN createApiKeyHeaderCredentials
- ELSE use
applicationDefaultCredentials
(this happens inCredentialsWrapper::build
)
- IF the
So no, the second condition is not missing.
I can add validation for the first at the top of this function.
src/ApiKeyHeaderCredentials.php
Outdated
class ApiKeyHeaderCredentials implements HeaderCredentialsInterface | ||
{ | ||
private $apiKey; | ||
private $quotaProject; |
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.
Do you foresee any issues to adding a universe domain?
I don't have any clear guidance now, but I think eventually it may be relevant
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.
We do have guidance on universe domain with API Key - because there is no way to tie the two together, the universe domain is decided based on the supplied client option... so, while there CAN be a universe domain, it is not possible for the API key to validate it.
@westarle PTAL! |
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.
API Key interface looks good to me!
/** | ||
* @return string|null The quota project associated with the credentials. | ||
*/ | ||
public function getQuotaProject(): ?string |
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.
Nit:
I think that it looks cleaner with a union type than with a ?
. But with the new PHP update, I don't anymore haha.
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.
I am definitely undecided on this. I think sometimes the ?
looks better (because it's more concise).
src/ApiKeyHeaderCredentials.php
Outdated
* | ||
* @return callable Callable function that returns the API key header. | ||
*/ | ||
public function getAuthorizationHeaderCallback($unusedAudience = null): ?callable |
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.
nit:
Type for $unusedAudience
public function getAuthorizationHeaderCallback(null|string $unusdeAudience = null): null|callable
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.
good idea! Done
@@ -261,6 +261,7 @@ private static function determineMtlsEndpoint(string $apiEndpoint) | |||
private function createCredentialsWrapper($credentials, array $credentialsConfig, string $universeDomain) | |||
{ | |||
if (is_null($credentials)) { |
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.
I know this was written already but all the cases return on each if
. What if we forego the elseif
for all of them?:
if (is_null($credentials) {
return CredentialsWrapper::buidl($credentialsConfig, universeDomain);
}
if (is_string($credentials) || is_array($credentials)) {
return CredentialsWrapper::build(['keyFile' => $credentials] + $credentialsConfig, $universeDomain);
}
if ($credentials instanceof FetchAuthTokenInterface) {
authHttpHandler = $credentialsConfig['authHttpHandler'] ?? null;
return new CredentialsWrapper($credentials, $authHttpHandler, $universeDomain);
}
if ($credentials instanceof CredentialsWrapper) {
return $credentials;
}
throw new ValidationException(
'Unexpected value in $auth option, got: ' .
print_r($credentials, true)
);
I find it easier to read and more pleasing.
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.
Good catch, I totally agree!
Done.
Adds support for API Keys in GAPIC clients via a new
apiKey
client option:AIP: https://google.aip.dev/auth/4110
apiKey
client option - this will be sent in using thex-goog-api-key
header for all transports.apiKey
andcredentials
orcredentialsConfig.keyFile
option are supplied, an exception is thrownapiKey
option AND thequotaProject
option are set, the$quotaProject
is still sent in