Google-Ads-AdWords-Client

 view release on metacpan or  search on metacpan

lib/Google/Ads/Common/OAuth2ServiceAccountsHandler.pm  view on Meta::CPAN

    \"sub\":\"${delegated_email_address}\",
    \"scope\":\"${scope}\",
    \"aud\":\"" . OAUTH2_BASE_URL . "/token\",
    \"exp\":${exp},
    \"iat\":${iat}
  }";

  my $encoded_header = __encode_base64_url($header);
  my $encoded_claims = __encode_base64_url($claims);

  my $key = $self->get___crypt_module()->new_private_key($file) || return 0;
  $key->use_pkcs1_padding();
  $key->use_sha256_hash();

  my $signature         = $key->sign("${encoded_header}.${encoded_claims}");
  my $encoded_signature = __encode_base64_url($signature);
  my $assertion = "${encoded_header}.${encoded_claims}.${encoded_signature}";
  my $body =
    "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer" .
    "&assertion=" . $assertion;
  push my @headers, "Content-Type" => "application/x-www-form-urlencoded";

lib/Google/Ads/Common/OAuth2ServiceAccountsHandler.pm  view on Meta::CPAN


  my $content_hash = $self->__parse_auth_response($res->decoded_content());

  $self->set_access_token($content_hash->{access_token});
  $self->set_access_token_expires($iat + $content_hash->{expires_in});
}

# Return the private key string from either the PEM file or JSON file specified.
sub __read_certificate_file {
  my $self = shift;
  my $private_key;

  if (!$self->get_pem_file() and !$self->get_json_file()) {
    return 0;
  }

  # JSON File
  if ($self->get_json_file()) {
    my $file_str;
    open(MYFILE, $self->get_json_file()) || return 0;
    while (<MYFILE>) {
      $file_str .= $_;
    }
    my $json_values = parse_json ($file_str);
    $private_key = $json_values->{'private_key'};
    $self->set_email_address($json_values->{'client_email'});
    close(MYFILE);
  }
  # PEM File
  else {
    open(MYFILE, $self->get_pem_file()) || return 0;
    while (<MYFILE>) {
      $private_key .= $_;
    }
    close(MYFILE);
  }

  return $private_key;
}

sub __encode_base64_url($) {
  my ($s) = shift;
  $s = encode_base64($s);
  $s =~ tr{+/}{-_};
  $s =~ s/=*$//;
  $s =~ s/\n//g;
  return $s;
}

t/019_OAuth2ServiceAccountsHandler.t  view on Meta::CPAN

        (time + 1000) . "\n}");
    return $response;
  });

ok($handler->is_auth_enabled());
is($handler->get_access_token(), "access-token");
ok($handler->get_access_token_expires());

# Test access token generation.
$crypt_module_mock->mock(
  new_private_key => sub {
    my ($self, $file) = @_;

    my $key = Test::MockObject->new();
    $key->mock(use_pkcs1_padding => sub { 1 });
    $key->mock(use_sha256_hash   => sub { 1 });
    $key->mock(
      sign => sub {
        return "signed-claims";
      });

t/019_OAuth2ServiceAccountsHandler.t  view on Meta::CPAN

        (time + 1000) . "\n}");
    return $response;
  });

ok($handler->is_auth_enabled());
is($handler->get_access_token(), "access-token");
ok($handler->get_access_token_expires());

# Test access token generation.
$crypt_module_mock->mock(
  new_private_key => sub {
    my ($self, $file) = @_;

    my $key = Test::MockObject->new();
    $key->mock(use_pkcs1_padding => sub { 1 });
    $key->mock(use_sha256_hash   => sub { 1 });
    $key->mock(
      sign => sub {
        return "signed-claims";
      });

t/testdata/test-cert.json  view on Meta::CPAN

{
  "type": "service_account",
  "project_id": "swift-reef-0",
  "private_key_id": "9c635c98722a5d559d94cb512ebef155eaedaa4e",
  "private_key": "-----BEGIN PRIVATE KEY-----\nTHIS IS A FAKE CERTIFICATE FOR TESTING\n-----END PRIVATE KEY-----\n",
  "client_email": "fake_project@swift-reef-0.iam.gserviceaccount.com",
  "client_id": "0",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/fake_project%40swift-reef-0.iam.gserviceaccount.com"
}



( run in 0.242 second using v1.01-cache-2.11-cpan-4d50c553e7e )