Skip to content

Commit

Permalink
Merge pull request googleapis#719 from ajkannan/add-no-auth
Browse files Browse the repository at this point in the history
Add NoAuthCredentials
  • Loading branch information
ajkannan committed Mar 7, 2016
2 parents 7eb957d + 0c4af89 commit d3224b3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ public RestorableState<AuthCredentials> capture() {
}
}

/**
* Represents service account credentials.
*
* @see <a href="https://cloud.google.com/docs/authentication#user_accounts_and_service_accounts">
* User accounts and service accounts</a>
*/
public static class ServiceAccountAuthCredentials extends AuthCredentials {

private final String account;
Expand Down Expand Up @@ -195,6 +201,14 @@ public RestorableState<AuthCredentials> capture() {
}
}

/**
* Represents Application Default Credentials, which are credentials that are inferred from the
* runtime environment.
*
* @see <a
* href="https://developers.google.com/identity/protocols/application-default-credentials">
* Google Application Default Credentials</a>
*/
public static class ApplicationDefaultAuthCredentials extends AuthCredentials {

private GoogleCredentials googleCredentials;
Expand Down Expand Up @@ -243,6 +257,38 @@ public RestorableState<AuthCredentials> capture() {
}
}

/**
* A placeholder for credentials to signify that requests sent to the server should not be
* authenticated. This is typically useful when using the local service emulators, such as
* {@code LocalGcdHelper} and {@code LocalResourceManagerHelper}.
*/
public static class NoAuthCredentials extends AuthCredentials {

private static final AuthCredentials INSTANCE = new NoAuthCredentials();
private static final NoAuthCredentialsState STATE = new NoAuthCredentialsState();

private static class NoAuthCredentialsState
implements RestorableState<AuthCredentials>, Serializable {

private static final long serialVersionUID = -4022100563954640465L;

@Override
public AuthCredentials restore() {
return INSTANCE;
}
}

@Override
public GoogleCredentials credentials() {
return null;
}

@Override
public RestorableState<AuthCredentials> capture() {
return STATE;
}
}

public abstract GoogleCredentials credentials();

public static AuthCredentials createForAppEngine() {
Expand Down Expand Up @@ -281,6 +327,15 @@ public static ServiceAccountAuthCredentials createFor(String account, PrivateKey
return new ServiceAccountAuthCredentials(account, privateKey);
}

/**
* Creates a placeholder denoting that no credentials should be used. This is typically useful
* when using the local service emulators, such as {@code LocalGcdHelper} and
* {@code LocalResourceManagerHelper}.
*/
public static AuthCredentials noAuth() {
return NoAuthCredentials.INSTANCE;
}

/**
* Creates Service Account Credentials given a stream for credentials in JSON format.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,10 @@ public RetryParams retryParams() {
* options.
*/
public HttpRequestInitializer httpRequestInitializer() {
final HttpRequestInitializer delegate = authCredentials() != null
? new HttpCredentialsAdapter(authCredentials().credentials().createScoped(scopes()))
: null;
final HttpRequestInitializer delegate =
authCredentials() != null && authCredentials.credentials() != null
? new HttpCredentialsAdapter(authCredentials().credentials().createScoped(scopes()))
: null;
return new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.api.services.datastore.DatastoreV1.RunQueryResponse;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.gcloud.AuthCredentials;
import com.google.gcloud.RetryParams;
import com.google.gcloud.datastore.Query.ResultType;
import com.google.gcloud.datastore.StructuredQuery.OrderBy;
Expand Down Expand Up @@ -128,6 +129,7 @@ public void setUp() {
options = DatastoreOptions.builder()
.projectId(PROJECT_ID)
.host("http://localhost:" + PORT)
.authCredentials(AuthCredentials.noAuth())
.retryParams(RetryParams.noRetries())
.build();
datastore = options.service();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.ByteStreams;
import com.google.gcloud.AuthCredentials;
import com.google.gcloud.resourcemanager.ResourceManagerOptions;

import com.sun.net.httpserver.Headers;
Expand Down Expand Up @@ -550,17 +551,21 @@ private LocalResourceManagerHelper() {
}

/**
* Creates a LocalResourceManagerHelper object that listens to requests on the local machine.
* Creates a {@code LocalResourceManagerHelper} object that listens to requests on the local
* machine.
*/
public static LocalResourceManagerHelper create() {
return new LocalResourceManagerHelper();
}

/**
* Returns a ResourceManagerOptions instance that sets the host to use the mock server.
* Returns a {@link ResourceManagerOptions} instance that sets the host to use the mock server.
*/
public ResourceManagerOptions options() {
return ResourceManagerOptions.builder().host("http://localhost:" + port).build();
return ResourceManagerOptions.builder()
.host("http://localhost:" + port)
.authCredentials(AuthCredentials.noAuth())
.build();
}

/**
Expand Down

0 comments on commit d3224b3

Please sign in to comment.