Search in sources :

Example 81 with ProjectName

use of com.google.recaptchaenterprise.v1.ProjectName in project pomocua-ogloszenia by coi-gov-pl.

the class CaptchaValidator method validate.

public boolean validate(String recaptchaResponse) {
    if (!properties.isEnabled()) {
        log.debug("Skip captcha validation. To enable change 'app.captcha.enabled' property");
        return true;
    }
    if (!responseSanityCheck(recaptchaResponse)) {
        log.warn("Response contains invalid characters");
        return false;
    }
    ProjectName projectName = ProjectName.of(properties.getGoogleCloudProjectId());
    Event event = Event.newBuilder().setSiteKey(properties.getSiteKey()).setToken(recaptchaResponse).build();
    CreateAssessmentRequest createAssessmentRequest = CreateAssessmentRequest.newBuilder().setParent(projectName.toString()).setAssessment(Assessment.newBuilder().setEvent(event).build()).build();
    Assessment response = recaptchaClient.createAssessment(createAssessmentRequest);
    if (response == null) {
        log.warn("Empty response from reCaptcha");
        return false;
    }
    // Check if the token is valid.
    if (!response.getTokenProperties().getValid()) {
        String invalidTokenReason = response.getTokenProperties().getInvalidReason().name();
        log.debug("The CreateAssessment call failed because the token was: " + invalidTokenReason);
        return false;
    }
    float score = response.getScore();
    if (score < properties.getAcceptLevel()) {
        List<String> reasons = response.getReasonsList().stream().map(classificationReason -> classificationReason.getDescriptorForType().getFullName()).collect(Collectors.toList());
        log.debug("Validation failed. Score: " + score + ". Reasons: " + String.join(", ", reasons));
        return false;
    }
    log.debug("Validation OK - score: " + score);
    return true;
}
Also used : Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) CreateAssessmentRequest(com.google.recaptchaenterprise.v1beta1.CreateAssessmentRequest) Service(org.springframework.stereotype.Service) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Assessment(com.google.recaptchaenterprise.v1beta1.Assessment) RecaptchaEnterpriseServiceV1Beta1Client(com.google.cloud.recaptchaenterprise.v1beta1.RecaptchaEnterpriseServiceV1Beta1Client) ProjectName(com.google.recaptchaenterprise.v1beta1.ProjectName) Pattern(java.util.regex.Pattern) Collectors(java.util.stream.Collectors) Event(com.google.recaptchaenterprise.v1beta1.Event) StringUtils(org.springframework.util.StringUtils) ProjectName(com.google.recaptchaenterprise.v1beta1.ProjectName) Assessment(com.google.recaptchaenterprise.v1beta1.Assessment) CreateAssessmentRequest(com.google.recaptchaenterprise.v1beta1.CreateAssessmentRequest) Event(com.google.recaptchaenterprise.v1beta1.Event)

Example 82 with ProjectName

use of com.google.recaptchaenterprise.v1.ProjectName in project java-secretmanager by googleapis.

the class NativeImageSecretManagerSample method hasSecret.

static boolean hasSecret(SecretManagerServiceClient client, String projectId, String secretId) {
    ProjectName projectName = ProjectName.of(projectId);
    ListSecretsPagedResponse pagedResponse = client.listSecrets(projectName);
    for (Secret secret : pagedResponse.iterateAll()) {
        String otherSecretId = extractSecretId(secret);
        if (secretId.equals(otherSecretId)) {
            return true;
        }
    }
    return false;
}
Also used : Secret(com.google.cloud.secretmanager.v1.Secret) ProjectName(com.google.cloud.secretmanager.v1.ProjectName) ListSecretsPagedResponse(com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse) ByteString(com.google.protobuf.ByteString)

Example 83 with ProjectName

use of com.google.recaptchaenterprise.v1.ProjectName in project java-secretmanager by googleapis.

the class NativeImageSecretManagerSample method createSecret.

static void createSecret(SecretManagerServiceClient client, String projectId, String secretId) {
    Secret secret = Secret.newBuilder().setReplication(Replication.newBuilder().setAutomatic(Replication.Automatic.newBuilder().build()).build()).build();
    ProjectName projectName = ProjectName.of(projectId);
    Secret createdSecret = client.createSecret(projectName, secretId, secret);
    System.out.println("Created secret: " + createdSecret.getName());
}
Also used : Secret(com.google.cloud.secretmanager.v1.Secret) ProjectName(com.google.cloud.secretmanager.v1.ProjectName)

Example 84 with ProjectName

use of com.google.recaptchaenterprise.v1.ProjectName in project java-secretmanager by googleapis.

the class CreateSecret method createSecret.

// Add a new version to the existing secret.
public void createSecret(String projectId, String secretId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        // Build the parent name from the project.
        ProjectName projectName = ProjectName.of(projectId);
        // Build the secret to create.
        Secret secret = Secret.newBuilder().setReplication(Replication.newBuilder().setAutomatic(Replication.Automatic.newBuilder().build()).build()).build();
        // Create the secret.
        Secret createdSecret = client.createSecret(projectName, secretId, secret);
        System.out.printf("Created secret %s\n", createdSecret.getName());
    }
}
Also used : Secret(com.google.cloud.secretmanager.v1.Secret) ProjectName(com.google.cloud.secretmanager.v1.ProjectName) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 85 with ProjectName

use of com.google.recaptchaenterprise.v1.ProjectName in project java-secretmanager by googleapis.

the class ListSecrets method listSecrets.

// List all secrets for a project
public void listSecrets(String projectId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        // Build the parent name.
        ProjectName projectName = ProjectName.of(projectId);
        // Get all secrets.
        ListSecretsPagedResponse pagedResponse = client.listSecrets(projectName);
        // List all secrets.
        pagedResponse.iterateAll().forEach(secret -> {
            System.out.printf("Secret %s\n", secret.getName());
        });
    }
}
Also used : ProjectName(com.google.cloud.secretmanager.v1.ProjectName) ListSecretsPagedResponse(com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Aggregations

Test (org.junit.Test)178 StatusRuntimeException (io.grpc.StatusRuntimeException)89 AbstractMessage (com.google.protobuf.AbstractMessage)76 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)74 ProjectName (com.google.monitoring.v3.ProjectName)62 ProjectName (com.google.pubsub.v1.ProjectName)44 ProjectName (com.google.logging.v2.ProjectName)31 ArrayList (java.util.ArrayList)31 ProjectName (com.google.privacy.dlp.v2.ProjectName)22 ByteString (com.google.protobuf.ByteString)20 DataTransferServiceClient (com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient)17 ProjectName (com.google.cloud.bigquery.datatransfer.v1.ProjectName)17 IOException (java.io.IOException)17 CreateTransferConfigRequest (com.google.cloud.bigquery.datatransfer.v1.CreateTransferConfigRequest)16 TransferConfig (com.google.cloud.bigquery.datatransfer.v1.TransferConfig)16 ProjectName (com.google.cloud.secretmanager.v1.ProjectName)16 ProjectName (com.google.containeranalysis.v1beta1.ProjectName)16 ApiException (com.google.api.gax.rpc.ApiException)15 MetricServiceClient (com.google.cloud.monitoring.v3.MetricServiceClient)15 TimeSeries (com.google.monitoring.v3.TimeSeries)15