Dynamic OauthDriver scope (#544)

* Dynamic OauthDriver scope

* support migration for mysql

* Refactor default scopes for integrations

* Small UI changes

* fix flet select tooltip

* fix linter

* Fix google token size in DB

---------

Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
Chirag Chhatrala
2024-08-29 16:58:02 +05:30
committed by GitHub
parent 89513e3b4a
commit da0ea04475
12 changed files with 130 additions and 15 deletions

View File

@@ -93,6 +93,7 @@ class OAuthController extends Controller
$oauthProvider->update([
'access_token' => $socialiteUser->token,
'refresh_token' => $socialiteUser->refreshToken,
'scopes' => $socialiteUser->approvedScopes
]);
return $oauthProvider->user;
@@ -139,6 +140,7 @@ class OAuthController extends Controller
'refresh_token' => $socialiteUser->refreshToken,
'name' => $socialiteUser->getName(),
'email' => $socialiteUser->getEmail(),
'scopes' => $socialiteUser->approvedScopes
]
);
return $user;

View File

@@ -26,8 +26,10 @@ class OAuthProviderController extends Controller
$userId = Auth::id();
cache()->put("oauth-intention:{$userId}", $request->input('intention'), 60 * 5);
// Connecting an account for integrations purposes
// Adding full scopes to the driver
return response()->json([
'url' => $service->getDriver()->getRedirectUrl(),
'url' => $service->getDriver()->fullScopes()->getRedirectUrl(),
]);
}
@@ -47,6 +49,7 @@ class OAuthProviderController extends Controller
'refresh_token' => $driverUser->refreshToken,
'name' => $driverUser->getName(),
'email' => $driverUser->getEmail(),
'scopes' => $driverUser->approvedScopes
]
);

View File

@@ -32,6 +32,7 @@ class OAuthProviderResource extends JsonResource
fn () => OAuthProviderUserResource::make($this->resource->user),
null,
),
'scopes' => $this->resource->scopes
];
}
}

View File

@@ -7,7 +7,14 @@ use Laravel\Socialite\Contracts\User;
interface OAuthDriver
{
public function getRedirectUrl(): string;
public function setRedirectUrl($url): self;
public function setRedirectUrl(string $url): self;
public function setScopes(array $scopes): self;
public function getUser(): User;
public function canCreateUser(): bool;
/**
* Set up all the scopes required by OpnForm for various integrations.
* This method configures the necessary permissions for the current OAuth driver.
*/
public function fullScopes(): self;
}

View File

@@ -11,6 +11,7 @@ use Laravel\Socialite\Two\GoogleProvider;
class OAuthGoogleDriver implements OAuthDriver
{
private ?string $redirectUrl = null;
private ?array $scopes = [];
protected GoogleProvider $provider;
@@ -22,7 +23,7 @@ class OAuthGoogleDriver implements OAuthDriver
public function getRedirectUrl(): string
{
return $this->provider
->scopes([Sheets::DRIVE_FILE])
->scopes($this->scopes ?? [])
->stateless()
->redirectUrl($this->redirectUrl ?? config('services.google.redirect'))
->with([
@@ -46,10 +47,20 @@ class OAuthGoogleDriver implements OAuthDriver
return true;
}
public function setRedirectUrl($url): OAuthDriver
public function setRedirectUrl(string $url): OAuthDriver
{
$this->redirectUrl = $url;
return $this;
}
public function setScopes(array $scopes): OAuthDriver
{
$this->scopes = $scopes;
return $this;
}
public function fullScopes(): OAuthDriver
{
return $this->setScopes([Sheets::DRIVE_FILE]);
}
}

View File

@@ -30,7 +30,8 @@ class OAuthProvider extends Model
* @var array
*/
protected $hidden = [
'access_token', 'refresh_token',
'access_token',
'refresh_token',
];
protected function casts()
@@ -38,6 +39,7 @@ class OAuthProvider extends Model
return [
'provider' => OAuthProviderService::class,
'token_expires_at' => 'datetime',
'scopes' => 'array'
];
}