Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
✨ Authentification without Json File (#53)
Browse files Browse the repository at this point in the history
* ✨ Add possibility to authenticate directly without json file

* 📝 Update Readme

* Use early return instead of else.

* Fix Bracket

* Apply suggestions from code review (usage of array_get)

Co-Authored-By: rommni <rombatie@hotmail.fr>
  • Loading branch information
rommni authored and nicja committed Apr 15, 2019
1 parent a2a8f5d commit 3b91702
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Add a new disk to your `filesystems.php` config

The Google Client uses a few methods to determine how it should authenticate with the Google API.

1. If you specify a `key_file` in the disk config, that json credentials file will be used.
1. If you specify a path in the key `key_file` in disk config, that json credentials file will be used.
2. If the `GOOGLE_APPLICATION_CREDENTIALS` env var is set, it will use that.
```php
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
Expand All @@ -55,6 +55,20 @@ The Google Client uses a few methods to determine how it should authenticate wit

4. If running in **Google App Engine**, the built-in service account associated with the application will be used.
5. If running in **Google Compute Engine**, the built-in service account associated with the virtual machine instance will be used.
6. If you want to authenticate directly without using a json file, you can specify an array for `key_file` in disk config with this data:
```php
'key_file' => [
'type' => env('GOOGLE_CLOUD_ACCOUNT_TYPE'),
'private_key_id' => env('GOOGLE_CLOUD_PRIVATE_KEY_ID'),
'private_key' => env('GOOGLE_CLOUD_PRIVATE_KEY'),
'client_email' => env('GOOGLE_CLOUD_CLIENT_EMAIL'),
'client_id' => env('GOOGLE_CLOUD_CLIENT_ID'),
'auth_uri' => env('GOOGLE_CLOUD_AUTH_URI'),
'token_uri' => env('GOOGLE_CLOUD_TOKEN_URI'),
'auth_provider_x509_cert_url' => env('GOOGLE_CLOUD_AUTH_PROVIDER_CERT_URL'),
'client_x509_cert_url' => env('GOOGLE_CLOUD_CLIENT_CERT_URL'),
],
```

### Public URLs

Expand Down
28 changes: 24 additions & 4 deletions src/GoogleCloudStorageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ public function boot()
$factory = $this->app->make('filesystem');
/* @var FilesystemManager $factory */
$factory->extend('gcs', function ($app, $config) {
$storageClient = new StorageClient([
'projectId' => $config['project_id'],
'keyFilePath' => array_get($config, 'key_file'),
]);
$storageClient = $this->createClient($config);

$bucket = $storageClient->bucket($config['bucket']);
$pathPrefix = array_get($config, 'path_prefix');
$storageApiUri = array_get($config, 'storage_api_uri');
Expand All @@ -78,6 +76,28 @@ public function boot()
});
}

/**
* Create a new StorageClient
*
* @param mixed $config
* @return \Google\Cloud\Storage\StorageClient
*/
private function createClient($config)
{
if (is_string(array_get($config, 'key_file'))) {
return new StorageClient([
'projectId' => $config['project_id'],
'keyFilePath' => array_get($config, 'key_file'),
]);
}

return new StorageClient([
'projectId' => $config['project_id'],
'keyFile' => array_merge([
"project_id" => $config['project_id']
], array_get($config, 'key_file', [])

This comment has been minimized.

Copy link
@RazmikSaghoyan

RazmikSaghoyan Apr 15, 2019

syntax error, please fix

This comment has been minimized.

Copy link
@RazmikSaghoyan

RazmikSaghoyan Apr 15, 2019

return new StorageClient([
'projectId' => $config['project_id'],
'keyFile' => array_merge([
"project_id" => $config['project_id']
], array_get($config, 'key_file', []))
]);

]);
}

/**
* Register bindings in the container.
Expand Down

0 comments on commit 3b91702

Please sign in to comment.