Skip to content

Commit

Permalink
feat: enable self signed JWT for http (#850)
Browse files Browse the repository at this point in the history
  • Loading branch information
arithmetic1728 committed Oct 6, 2021
1 parent 3f0bf5a commit aba0ec0
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 49 deletions.
Expand Up @@ -215,6 +215,16 @@ protected MethodDefinition createDefaultCredentialsProviderBuilderMethod() {
.setArguments(DEFAULT_SERVICE_SCOPES_VAR_EXPR)
.setReturnType(returnType)
.build();
// enable self signed JWT.
credsProviderBuilderExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(credsProviderBuilderExpr)
.setMethodName("setUseJwtAccessWithScope")
.setArguments(
ValueExpr.withValue(
PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build()))
.setReturnType(returnType)
.build();
return MethodDefinition.builder()
.setHeaderCommentStatements(
SettingsCommentComposer.DEFAULT_CREDENTIALS_PROVIDER_BUILDER_METHOD_COMMENT)
Expand Down
Expand Up @@ -14,21 +14,16 @@

package com.google.api.generator.gapic.composer.grpc;

import com.google.api.gax.core.GoogleCredentialsProvider;
import com.google.api.gax.grpc.GaxGrpcProperties;
import com.google.api.gax.grpc.GrpcTransportChannel;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.MethodDefinition;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.PrimitiveValue;
import com.google.api.generator.engine.ast.ScopeNode;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.ValueExpr;
import com.google.api.generator.engine.ast.Variable;
import com.google.api.generator.engine.ast.VariableExpr;
import com.google.api.generator.gapic.composer.comment.SettingsCommentComposer;
import com.google.api.generator.gapic.composer.common.AbstractServiceStubSettingsClassComposer;
import com.google.api.generator.gapic.composer.store.TypeStore;
import com.google.api.generator.gapic.model.Service;
Expand Down Expand Up @@ -74,47 +69,6 @@ protected Expr initializeTransportProviderBuilder(
.build();
}

@Override
protected MethodDefinition createDefaultCredentialsProviderBuilderMethod() {
TypeNode returnType =
TypeNode.withReference(
ConcreteReference.withClazz(GoogleCredentialsProvider.Builder.class));
MethodInvocationExpr credsProviderBuilderExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(FIXED_TYPESTORE.get("GoogleCredentialsProvider"))
.setMethodName("newBuilder")
.build();
credsProviderBuilderExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(credsProviderBuilderExpr)
.setMethodName("setScopesToApply")
.setArguments(DEFAULT_SERVICE_SCOPES_VAR_EXPR)
.setReturnType(returnType)
.build();

// This section is specific to GAPIC clients. It sets UseJwtAccessWithScope value to true to
// enable self signed JWT feature.
credsProviderBuilderExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(credsProviderBuilderExpr)
.setMethodName("setUseJwtAccessWithScope")
.setArguments(
ValueExpr.withValue(
PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build()))
.setReturnType(returnType)
.build();

return MethodDefinition.builder()
.setHeaderCommentStatements(
SettingsCommentComposer.DEFAULT_CREDENTIALS_PROVIDER_BUILDER_METHOD_COMMENT)
.setScope(ScopeNode.PUBLIC)
.setIsStatic(true)
.setReturnType(returnType)
.setName("defaultCredentialsProviderBuilder")
.setReturnExpr(credsProviderBuilderExpr)
.build();
}

@Override
protected List<MethodDefinition> createApiClientHeaderProviderBuilderMethods(
Service service, TypeStore typeStore) {
Expand Down
Expand Up @@ -135,7 +135,9 @@ public class ComplianceStubSettings extends StubSettings<ComplianceStubSettings>

/** Returns a builder for the default credentials for this service. */
public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilder() {
return GoogleCredentialsProvider.newBuilder().setScopesToApply(DEFAULT_SERVICE_SCOPES);
return GoogleCredentialsProvider.newBuilder()
.setScopesToApply(DEFAULT_SERVICE_SCOPES)
.setUseJwtAccessWithScope(true);
}

/** Returns a builder for the default ChannelProvider for this service. */
Expand Down
Expand Up @@ -287,7 +287,9 @@ public static List<String> getDefaultServiceScopes() {

/** Returns a builder for the default credentials for this service. */
public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilder() {
return GoogleCredentialsProvider.newBuilder().setScopesToApply(DEFAULT_SERVICE_SCOPES);
return GoogleCredentialsProvider.newBuilder()
.setScopesToApply(DEFAULT_SERVICE_SCOPES)
.setUseJwtAccessWithScope(true);
}

/** Returns a builder for the default ChannelProvider for this service. */
Expand Down
Expand Up @@ -125,7 +125,9 @@ public static List<String> getDefaultServiceScopes() {

/** Returns a builder for the default credentials for this service. */
public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilder() {
return GoogleCredentialsProvider.newBuilder().setScopesToApply(DEFAULT_SERVICE_SCOPES);
return GoogleCredentialsProvider.newBuilder()
.setScopesToApply(DEFAULT_SERVICE_SCOPES)
.setUseJwtAccessWithScope(true);
}

/** Returns a builder for the default ChannelProvider for this service. */
Expand Down

0 comments on commit aba0ec0

Please sign in to comment.