Skip to content

Commit 588dde4

Browse files
committed
feat(auth): enhance OAuth flow configuration
Added support for `listen` and `redirect` options, allowing dynamic host, port, and redirect URI configurations. Updated related logic to improve flexibility and align with new input options.
1 parent 0afefe2 commit 588dde4

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

app/Auth/OAuth2.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace App\Auth;
44

55
use App\Exceptions\InvalidAuthenticationException;
6-
use App\Exceptions\InvalidTokenException;
76
use App\GPhoto;
87
use Google\Auth\OAuth2 as GoogleOAuth2;
98
use Illuminate\Support\Arr;
@@ -23,7 +22,7 @@ public function __construct(protected GPhoto $gphoto)
2322
{
2423
parent::__construct([
2524
'scope' => $this->gphoto->scopes(),
26-
'redirectUri' => $this->gphoto->config('host'),
25+
'redirectUri' => $this->gphoto->config('redirect'),
2726
'clientId' => $this->gphoto->config('auth.client_id'),
2827
'clientSecret' => $this->gphoto->config('auth.client_secret'),
2928
'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
@@ -39,12 +38,12 @@ public function fetchNewAccessToken() : string
3938
{
4039
if (! file_exists($this->gphoto->path('storage/token'))) {
4140
throw new InvalidAuthenticationException(
42-
sprintf("'%s' auth name is not exists. Use --auth option to specify the authentication name", $this->gphoto->name())
41+
sprintf("'%s' auth name is not exists. Use --auth option to specify the authentication name", $this->gphoto->name()),
4342
);
4443
}
4544

4645
$this->setCode(file_get_contents($this->gphoto->path(
47-
'storage/token'
46+
'storage/token',
4847
)));
4948

5049
return Arr::get($this->fetchAuthToken(), 'access_token');

app/Commands/CreateAuthCommand.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,34 @@ public function handle(Config $config) : void
3737
$this->input->setArgument('name', 'default');
3838
}
3939

40-
foreach (['auth', 'host'] as $option) {
41-
if (! $this->option($option)) {
42-
$this->input->setOption($option, $this->ask(sprintf('ENTER %s', strtoupper(
43-
$option
44-
))));
40+
if (! $this->option('auth')) {
41+
$this->input->setOption('auth', $this->ask('ENTER AUTH FILE PATH'));
42+
}
43+
44+
if (! $this->option('listen')) {
45+
$listen = $this->ask('ENTER LISTEN HOST AND PORT', 'localhost:3000');
46+
47+
// Add http:// prefix if no scheme is provided
48+
if (! str_starts_with($listen, 'http://') && ! str_starts_with($listen, 'https://')) {
49+
$listen = 'http://' . $listen;
4550
}
51+
52+
// Force http:// by replacing https:// if present
53+
$listen = preg_replace('/^https:\/\//i', 'http://', $listen);
54+
55+
$this->input->setOption('listen', $listen);
56+
}
57+
58+
if (! $this->option('redirect')) {
59+
$this->input->setOption('redirect', $this->ask('ENTER REDIRECT URI', $this->option('listen')));
4660
}
4761

4862
$config->store([
49-
'auth' => $this->option('auth'),
50-
'host' => $this->option('host'),
51-
'name' => $this->argument(
52-
'name'
53-
),
63+
'name' => $this->argument('name'),
64+
//
65+
'auth' => $this->option('auth'),
66+
'listen' => $this->option('listen'),
67+
'redirect' => $this->option('redirect'),
5468
]);
5569

5670
$gphoto = new GPhoto($this->argument('name'));
@@ -91,7 +105,8 @@ public function getOptions() : array
91105
{
92106
return [
93107
['auth', null, InputOption::VALUE_REQUIRED, 'Credential file'],
94-
['host', null, InputOption::VALUE_REQUIRED, 'Authorised redirect URIs'],
108+
['listen', null, InputOption::VALUE_REQUIRED, 'Host and port to listen for OAuth callback (e.g., 127.0.0.1:8080)'],
109+
['redirect', null, InputOption::VALUE_OPTIONAL, 'Redirect URI (default: uses the listen input value)'],
95110
];
96111
}
97112
}

app/Listeners/TokenListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,6 @@ private function getTokenLocation() : string
117117
*/
118118
private function getHostAndPort() : string
119119
{
120-
return parse_url($this->gphoto->config('host'), PHP_URL_HOST) . ':' . parse_url($this->gphoto->config('host'), PHP_URL_PORT);
120+
return parse_url($this->gphoto->config('listen'), PHP_URL_HOST) . ':' . parse_url($this->gphoto->config('listen'), PHP_URL_PORT);
121121
}
122122
}

0 commit comments

Comments
 (0)