Skip to content

Commit 88521ac

Browse files
authored
Merge pull request #68 from codedge/#42-private-repo-access
#42 private repo access
2 parents a5ce96b + 980732f commit 88521ac

File tree

7 files changed

+124
-6
lines changed

7 files changed

+124
-6
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ Usually you need this when distributing a self-hosted Laravel application
1616
that needs some updating mechanism, as you do not want to bother your
1717
lovely users with Git and/or Composer commands ;-)
1818

19+
## Compatibility
20+
21+
* PHP:
22+
* 7.2
23+
* 7.3
24+
* 7.4
25+
* Laravel:
26+
* 5.8
27+
* 6.x
28+
1929
## Install with Composer
2030

2131
To install the latest version from the master using [Composer](https://getcomposer.org/):
@@ -125,6 +135,13 @@ You can specify these values by adding `SELF_UPDATER_MAILTO_NAME` and
125135
| SELF_UPDATER_MAILTO_UPDATE_AVAILABLE_SUBJECT | Subject of update available email |
126136
| SELF_UPDATER_MAILTO_UPDATE_SUCCEEDED_SUBJECT | Subject of update succeeded email |
127137

138+
### Private repositories
139+
140+
Private repositories can be accessed via (Bearer) tokens. Each repository inside the config file should have
141+
a `private_access_token` field, where you can set the token.
142+
143+
**Note:** Do not prefix the token with `Bearer `. This is done automatically.
144+
128145
## Usage
129146
To start an update process, i. e. in a controller, just use:
130147
```php

config/self-update.php

+2
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@
4343
'repository_name' => env('SELF_UPDATER_REPO_NAME', ''),
4444
'repository_url' => '',
4545
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
46+
'private_access_token' => env('SELF_UPDATER_GITHUB_PRIVATE_ACCESS_TOKEN', ''),
4647
],
4748
'http' => [
4849
'type' => 'http',
4950
'repository_url' => env('SELF_UPDATER_REPO_URL', ''),
5051
'pkg_filename_format' => env('SELF_UPDATER_PKG_FILENAME_FORMAT', 'v_VERSION_'),
5152
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
53+
'private_access_token' => env('SELF_UPDATER_HTTP_PRIVATE_ACCESS_TOKEN', ''),
5254
],
5355
],
5456

src/AbstractRepositoryType.php

+59-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Codedge\Updater;
46

57
use Codedge\Updater\Events\HasWrongPermissions;
@@ -15,11 +17,18 @@
1517
*/
1618
abstract class AbstractRepositoryType
1719
{
20+
const ACCESS_TOKEN_PREFIX = 'Bearer ';
21+
1822
/**
1923
* @var array
2024
*/
2125
protected $config;
2226

27+
/**
28+
* Access token for private repository access.
29+
*/
30+
private $accessToken = '';
31+
2332
/**
2433
* @var Finder|SplFileInfo[]
2534
*/
@@ -99,8 +108,21 @@ protected function hasCorrectPermissionForUpdate() : bool
99108
*/
100109
protected function downloadRelease(Client $client, $source, $storagePath)
101110
{
111+
$headers = [];
112+
113+
if ($this->hasAccessToken()) {
114+
$headers = [
115+
'Authorization' => $this->getAccessToken(),
116+
];
117+
}
118+
102119
return $client->request(
103-
'GET', $source, ['sink' => $storagePath]
120+
'GET',
121+
$source,
122+
[
123+
'sink' => $storagePath,
124+
'headers' => $headers,
125+
]
104126
);
105127
}
106128

@@ -159,4 +181,40 @@ public function createReleaseFolder($storagePath, $releaseName)
159181

160182
File::deleteDirectory($subDirName[0]);
161183
}
184+
185+
/**
186+
* Get the access token.
187+
*
188+
* @param bool $withPrefix
189+
*
190+
* @return string
191+
*/
192+
public function getAccessToken($withPrefix = true): string
193+
{
194+
if ($withPrefix) {
195+
return self::ACCESS_TOKEN_PREFIX.$this->accessToken;
196+
}
197+
198+
return $this->accessToken;
199+
}
200+
201+
/**
202+
* Set access token.
203+
*
204+
* @param string $token
205+
*/
206+
public function setAccessToken(string $token): void
207+
{
208+
$this->accessToken = $token;
209+
}
210+
211+
/**
212+
* Check if an access token has been set.
213+
*
214+
* @return bool
215+
*/
216+
public function hasAccessToken(): bool
217+
{
218+
return ! empty($this->accessToken);
219+
}
162220
}

src/SourceRepositoryTypes/GithubRepositoryType.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Codedge\Updater\SourceRepositoryTypes;
46

57
use Codedge\Updater\AbstractRepositoryType;
@@ -40,6 +42,8 @@ public function __construct(Client $client, array $config)
4042
$this->config = $config;
4143
$this->config['version_installed'] = config('self-update.version_installed');
4244
$this->config['exclude_folders'] = config('self-update.exclude_folders');
45+
46+
$this->setAccessToken($config['private_access_token']);
4347
}
4448

4549
/**
@@ -214,9 +218,20 @@ protected function getRepositoryReleases()
214218
throw new \Exception('No repository specified. Please enter a valid Github repository owner and name in your config.');
215219
}
216220

221+
$headers = [];
222+
223+
if ($this->hasAccessToken()) {
224+
$headers = [
225+
'Authorization' => $this->getAccessToken(),
226+
];
227+
}
228+
217229
return $this->client->request(
218230
'GET',
219-
self::GITHUB_API_URL.'/repos/'.$this->config['repository_vendor'].'/'.$this->config['repository_name'].'/tags'
231+
self::GITHUB_API_URL.'/repos/'.$this->config['repository_vendor'].'/'.$this->config['repository_name'].'/tags',
232+
[
233+
'headers' => $headers,
234+
]
220235
);
221236
}
222237

src/SourceRepositoryTypes/HttpRepositoryType.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Codedge\Updater\SourceRepositoryTypes;
46

57
use Codedge\Updater\AbstractRepositoryType;
@@ -10,6 +12,7 @@
1012
use File;
1113
use GuzzleHttp\Client;
1214
use Illuminate\Database\Eloquent\Collection;
15+
use Psr\Http\Message\ResponseInterface;
1316
use Storage;
1417
use Symfony\Component\Finder\Finder;
1518

@@ -29,7 +32,7 @@ class HttpRepositoryType extends AbstractRepositoryType implements SourceReposit
2932
protected $client;
3033

3134
/**
32-
* @var Version prepand string
35+
* @var Version prepend string
3336
*/
3437
protected $prepend;
3538

@@ -53,6 +56,8 @@ public function __construct(Client $client, array $config)
5356
// Get prepend and append strings
5457
$this->prepend = preg_replace('/_VERSION_.*$/', '', $this->config['pkg_filename_format']);
5558
$this->append = preg_replace('/^.*_VERSION_/', '', $this->config['pkg_filename_format']);
59+
60+
$this->setAccessToken($config['private_access_token']);
5661
}
5762

5863
/**
@@ -196,6 +201,8 @@ public function getVersionInstalled($prepend = '', $append = '') : string
196201
* @param string $prepend Prepend a string to the latest version
197202
* @param string $append Append a string to the latest version
198203
*
204+
* @throws \Exception
205+
*
199206
* @return string
200207
*/
201208
public function getVersionAvailable($prepend = '', $append = '') : string
@@ -217,9 +224,9 @@ public function getVersionAvailable($prepend = '', $append = '') : string
217224
/**
218225
* Retrieve html body with list of all releases from archive URL.
219226
*
220-
* @throws \Exception
227+
*@throws \Exception
221228
*
222-
* @return mixed|\Psr\Http\Message\ResponseInterface
229+
* @return mixed|ResponseInterface
223230
*/
224231
protected function getPackageReleases()
225232
{

tests/SourceRepositoryTypes/GithubRepositoryTypeTest.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Codedge\Updater\Tests\SourceRepositoryTypes;
44

@@ -106,4 +106,21 @@ public function testFetchingFailsWithException()
106106
$this->expectException(Exception::class);
107107
$class->fetch();
108108
}
109+
110+
public function testHasAccessTokenSet()
111+
{
112+
$config = $this->config;
113+
$config['private_access_token'] = 'abc123';
114+
115+
$class = new GithubRepositoryType($this->client, $config);
116+
$this->assertTrue($class->hasAccessToken());
117+
$this->assertEquals($class->getAccessToken(), 'Bearer abc123');
118+
}
119+
120+
public function testHasAccessTokenNotSet()
121+
{
122+
$class = new GithubRepositoryType($this->client, $this->config);
123+
$this->assertFalse($class->hasAccessToken());
124+
$this->assertEquals($class->getAccessToken(), 'Bearer ');
125+
}
109126
}

tests/TestCase.php

+2
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ protected function getEnvironmentSetUp($app)
2828
'repository_name' => 'laravel',
2929
'repository_url' => '',
3030
'download_path' => '/tmp',
31+
'private_access_token' => '',
3132
],
3233
'http' => [
3334
'type' => 'http',
3435
'repository_url' => env('SELF_UPDATER_REPO_URL', ''),
3536
'pkg_filename_format' => env('SELF_UPDATER_PKG_FILENAME_FORMAT', 'v_VERSION_'),
3637
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
38+
'private_access_token' => '',
3739
],
3840
],
3941
'log_events' => false,

0 commit comments

Comments
 (0)