Skip to content

Commit 953ae2a

Browse files
author
Holger Lösken
committed
#42, Can use private access tokens (Bearer) for non-public repositories
1 parent a5ce96b commit 953ae2a

File tree

7 files changed

+114
-10
lines changed

7 files changed

+114
-10
lines changed

README.md

Lines changed: 10 additions & 0 deletions
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/):

config/self-update.php

Lines changed: 2 additions & 0 deletions
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

Lines changed: 58 additions & 2 deletions
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;
44

@@ -15,11 +15,18 @@
1515
*/
1616
abstract class AbstractRepositoryType
1717
{
18+
const ACCESS_TOKEN_PREFIX = 'Bearer ';
19+
1820
/**
1921
* @var array
2022
*/
2123
protected $config;
2224

25+
/**
26+
* Access token for private repository access.
27+
*/
28+
private $accessToken = '';
29+
2330
/**
2431
* @var Finder|SplFileInfo[]
2532
*/
@@ -99,8 +106,21 @@ protected function hasCorrectPermissionForUpdate() : bool
99106
*/
100107
protected function downloadRelease(Client $client, $source, $storagePath)
101108
{
109+
$headers = [];
110+
111+
if($this->hasAccessToken()) {
112+
$headers = [
113+
'Authorization' => $this->getAccessToken()
114+
];
115+
}
116+
102117
return $client->request(
103-
'GET', $source, ['sink' => $storagePath]
118+
'GET',
119+
$source,
120+
[
121+
'sink' => $storagePath,
122+
'headers' => $headers,
123+
]
104124
);
105125
}
106126

@@ -159,4 +179,40 @@ public function createReleaseFolder($storagePath, $releaseName)
159179

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

src/SourceRepositoryTypes/GithubRepositoryType.php

Lines changed: 15 additions & 2 deletions
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\SourceRepositoryTypes;
44

@@ -40,6 +40,8 @@ public function __construct(Client $client, array $config)
4040
$this->config = $config;
4141
$this->config['version_installed'] = config('self-update.version_installed');
4242
$this->config['exclude_folders'] = config('self-update.exclude_folders');
43+
44+
$this->setAccessToken($config['private_access_token']);
4345
}
4446

4547
/**
@@ -214,9 +216,20 @@ protected function getRepositoryReleases()
214216
throw new \Exception('No repository specified. Please enter a valid Github repository owner and name in your config.');
215217
}
216218

219+
$headers = [];
220+
221+
if($this->hasAccessToken()) {
222+
$headers = [
223+
'Authorization' => $this->getAccessToken()
224+
];
225+
}
226+
217227
return $this->client->request(
218228
'GET',
219-
self::GITHUB_API_URL.'/repos/'.$this->config['repository_vendor'].'/'.$this->config['repository_name'].'/tags'
229+
self::GITHUB_API_URL.'/repos/'.$this->config['repository_vendor'].'/'.$this->config['repository_name'].'/tags',
230+
[
231+
'headers' => $headers
232+
]
220233
);
221234
}
222235

src/SourceRepositoryTypes/HttpRepositoryType.php

Lines changed: 9 additions & 5 deletions
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\SourceRepositoryTypes;
44

@@ -10,6 +10,7 @@
1010
use File;
1111
use GuzzleHttp\Client;
1212
use Illuminate\Database\Eloquent\Collection;
13+
use Psr\Http\Message\ResponseInterface;
1314
use Storage;
1415
use Symfony\Component\Finder\Finder;
1516

@@ -29,7 +30,7 @@ class HttpRepositoryType extends AbstractRepositoryType implements SourceReposit
2930
protected $client;
3031

3132
/**
32-
* @var Version prepand string
33+
* @var Version prepend string
3334
*/
3435
protected $prepend;
3536

@@ -53,6 +54,8 @@ public function __construct(Client $client, array $config)
5354
// Get prepend and append strings
5455
$this->prepend = preg_replace('/_VERSION_.*$/', '', $this->config['pkg_filename_format']);
5556
$this->append = preg_replace('/^.*_VERSION_/', '', $this->config['pkg_filename_format']);
57+
58+
$this->setAccessToken($config['private_access_token']);
5659
}
5760

5861
/**
@@ -194,9 +197,10 @@ public function getVersionInstalled($prepend = '', $append = '') : string
194197
* Example: 2.6.5 or v2.6.5.
195198
*
196199
* @param string $prepend Prepend a string to the latest version
197-
* @param string $append Append a string to the latest version
200+
* @param string $append Append a string to the latest version
198201
*
199202
* @return string
203+
* @throws \Exception
200204
*/
201205
public function getVersionAvailable($prepend = '', $append = '') : string
202206
{
@@ -217,9 +221,9 @@ public function getVersionAvailable($prepend = '', $append = '') : string
217221
/**
218222
* Retrieve html body with list of all releases from archive URL.
219223
*
220-
* @throws \Exception
224+
* @return mixed|ResponseInterface
225+
*@throws \Exception
221226
*
222-
* @return mixed|\Psr\Http\Message\ResponseInterface
223227
*/
224228
protected function getPackageReleases()
225229
{

tests/SourceRepositoryTypes/GithubRepositoryTypeTest.php

Lines changed: 18 additions & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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)