Skip to content
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

V2.0 #322

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

V2.0 #322

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions .github/actions/unittest/entrypoint.sh

This file was deleted.

24 changes: 0 additions & 24 deletions .github/actions/unittest/retry.php

This file was deleted.

15 changes: 15 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Code Styles Lint
on:
push:
branches:
- master
pull_request:
jobs:
php-cs-fixer:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --diff --dry-run
89 changes: 2 additions & 87 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
matrix:
operating-system: [ ubuntu-latest ]
php: [ "5.6", "7.0", "7.1", "7.2", "7.3", "7.4", "8.0" ]
php: [ 7.1, 7.2, 7.3, 7.4, "8.0" ]
name: PHP ${{matrix.php }} Unit Test
steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -42,7 +42,7 @@ jobs:
strategy:
matrix:
operating-system: [ ubuntu-latest ]
php: [ "5.6", "7.0", "7.1", "7.2" ]
php: [ 7.1 ]
name: PHP ${{matrix.php }} Unit Test Prefer Lowest
steps:
- uses: actions/checkout@v2
Expand All @@ -58,88 +58,3 @@ jobs:
command: composer update --prefer-lowest
- name: Run Script
run: vendor/bin/phpunit
guzzle6:
runs-on: ubuntu-latest
strategy:
matrix:
operating-system: [ ubuntu-latest ]
php: [ "5.6", "7.2" ]
name: PHP ${{ matrix.php }} Unit Test Guzzle 6
steps:
- uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
- name: Install Dependencies
uses: nick-invision/retry@v1
with:
timeout_minutes: 10
max_attempts: 3
command: composer require guzzlehttp/guzzle:^6 && composer update
- name: Run Script
run: vendor/bin/phpunit
# use dockerfiles for oooooolllllldddd versions of php, setup-php times out for those.
test_php55:
name: "PHP 5.5 Unit Test"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Run Unit Tests
uses: docker://php:5.5-cli
with:
entrypoint: ./.github/actions/unittest/entrypoint.sh
test_php55_lowest:
name: "PHP 5.5 Unit Test Prefer Lowest"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Run Unit Tests
uses: docker://php:5.5-cli
env:
composerargs: "--prefer-lowest"
with:
entrypoint: ./.github/actions/unittest/entrypoint.sh
test_php54:
name: "PHP 5.4 Unit Test"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Run Unit Tests
uses: docker://php:5.4-cli
with:
entrypoint: ./.github/actions/unittest/entrypoint.sh
test_php54_lowest:
name: "PHP 5.4 Unit Test Prefer Lowest"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Run Unit Tests
uses: docker://php:5.4-cli
env:
composerargs: "--prefer-lowest"
with:
entrypoint: ./.github/actions/unittest/entrypoint.sh
style:
runs-on: ubuntu-latest
name: PHP Style Check
steps:
- uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "7.4"
- name: Install Dependencies
uses: nick-invision/retry@v1
with:
timeout_minutes: 10
max_attempts: 3
command: composer install
- name: Run Script
run: |
composer require friendsofphp/php-cs-fixer:^3.0
vendor/bin/php-cs-fixer fix --dry-run --diff
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ composer.lock
.cache
.docs
.gitmodules
.phpunit.result.cache
.php_cs.cache

# IntelliJ
.idea
Expand Down
25 changes: 25 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

return PhpCsFixer\Config::create()
->setRules([
'@PhpCsFixer' => true,
'@PSR2' => true,
'no_superfluous_phpdoc_tags' => false,
'phpdoc_summary' => false,
'concat_space' => ['spacing' => 'one'],
'no_unused_imports' => true,
'ordered_imports' => true,
'method_argument_space' => false,
'whitespace_after_comma_in_array' => true,
'method_argument_space' => [
'keep_multiple_spaces_after_comma' => true
],
'return_type_declaration' => [
'space_before' => 'none'
],
])
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__)
)
;
95 changes: 31 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
<dt>Homepage</dt><dd><a href="http://www.github.com/google/google-auth-library-php">http://www.github.com/google/google-auth-library-php</a></dd>
<dt>Reference Docs</dt><dd><a href="https://googleapis.github.io/google-auth-library-php/main/">https://googleapis.github.io/google-auth-library-php/main/</a></dd>
<dt>Authors</dt>
<dd><a href="mailto:[email protected]">Tim Emiola</a></dd>
<dd><a href="mailto:[email protected]">Stanley Cheung</a></dd>
<dd><a href="mailto:[email protected]">Brent Shaffer</a></dd>
<dt>Copyright</dt><dd>Copyright © 2015 Google, Inc.</dd>
<dd><a href="mailto:[email protected]">David Supplee</a></dd>
<dt>Copyright</dt><dd>Copyright © 2020 Google LLC</dd>
<dt>License</dt><dd>Apache 2.0</dd>
</dl>

Expand All @@ -19,17 +18,10 @@ authorization and authentication with Google APIs.
### Installing via Composer

The recommended way to install the google auth library is through
[Composer](http://getcomposer.org).
[Composer](http://getcomposer.org). Run the Composer command to install the latest stable version:

```bash
# Install Composer
curl -sS https://getcomposer.org/installer | php
```

Next, run the Composer command to install the latest stable version:

```bash
composer.phar require google/auth
composer require google/auth
```

## Application Default Credentials
Expand Down Expand Up @@ -78,62 +70,40 @@ As long as you update the environment variable below to point to *your* JSON
credentials file, the following code should output a list of your Drive files.

```php
use Google\Auth\ApplicationDefaultCredentials;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Google\Auth\GoogleAuth;
use Google\Auth\Http\CredentialsClient;
use GuzzleHttp\Psr7\Request;

// specify the path to your application credentials
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json');

// define the scopes for your API call
$scopes = ['https://www.googleapis.com/auth/drive.readonly'];

// create middleware
$middleware = ApplicationDefaultCredentials::getMiddleware($scopes);
$stack = HandlerStack::create();
$stack->push($middleware);
// create the auth client
$googleAuth = new GoogleAuth();

// create the HTTP client
$client = new Client([
'handler' => $stack,
'base_uri' => 'https://www.googleapis.com',
'auth' => 'google_auth' // authorize all requests
]);
// authorize an http client
$credentials = $googleAuth->makeCredentials(['scope' => $scopes]);
$client = new CredentialsClient($credentials);

// make the request
$response = $client->get('drive/v2/files');
$request = new Request('GET', 'https://www.googleapis.com/drive/v2/files');
$response = $client->send($request);

// show the result!
print_r((string) $response->getBody());
```

##### Guzzle 5 Compatibility

If you are using [Guzzle 5][Guzzle 5], replace the `create middleware` and
`create the HTTP Client` steps with the following:

```php
// create the HTTP client
$client = new Client([
'base_url' => 'https://www.googleapis.com',
'auth' => 'google_auth' // authorize all requests
]);

// create subscriber
$subscriber = ApplicationDefaultCredentials::getSubscriber($scopes);
$client->getEmitter()->attach($subscriber);
```

#### Call using an ID Token
If your application is running behind Cloud Run, or using Cloud Identity-Aware
Proxy (IAP), you will need to fetch an ID token to access your application. For
this, use the static method `getIdTokenMiddleware` on
`ApplicationDefaultCredentials`.
`GoogleAuth`.

```php
use Google\Auth\ApplicationDefaultCredentials;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Google\Auth\GoogleAuth;
use GuzzleHttp\Psr7\Request;

// specify the path to your application credentials
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json');
Expand All @@ -144,21 +114,18 @@ putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json');
// $targetAudience = 'https://service-1234-uc.a.run.app';
$targetAudience = 'YOUR_ID_TOKEN_AUDIENCE';

// create middleware
$middleware = ApplicationDefaultCredentials::getIdTokenMiddleware($targetAudience);
$stack = HandlerStack::create();
$stack->push($middleware);
// create the auth client
$googleAuth = new GoogleAuth();

// create the HTTP client
$client = new Client([
'handler' => $stack,
'auth' => 'google_auth',
// Cloud Run, IAP, or custom resource URL
'base_uri' => 'https://YOUR_PROTECTED_RESOURCE',
// authorize an http client
$credentials = $googleAuth->makeCredentials([
'targetAudience' => $targetAudience
]);
$client = new CredentialsClient($credentials);

// make the request
$response = $client->get('/');
$request = new Request('GET', 'https://YOUR_PROTECTED_RESOURCE');
$response = $client->send($request);

// show the result!
print_r((string) $response->getBody());
Expand Down Expand Up @@ -263,10 +230,10 @@ If you are [using Google ID tokens to authenticate users][google-id-tokens], use
the `Google\Auth\AccessToken` class to verify the ID token:

```php
use Google\Auth\AccessToken;
use Google\Auth\GoogleAuth;

$auth = new AccessToken();
$auth->verify($idToken);
$googleAuth = new GoogleAuth();
$googleAuth->verify($idToken);
```

If your app is running behind [Google Identity-Aware Proxy][iap-id-tokens]
Expand All @@ -275,11 +242,11 @@ appropriate certificate URL for IAP. This is because IAP signs the ID
tokens with a different key than the Google Identity service:

```php
use Google\Auth\AccessToken;
use Google\Auth\GoogleAuth;

$auth = new AccessToken();
$auth = new GoogleAuth();
$auth->verify($idToken, [
'certsLocation' => AccessToken::IAP_CERT_URL
'certsLocation' => GoogleAuth::IAP_JWK_URL
]);
```

Expand Down
Loading