-
-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
44 changed files
with
1,806 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Enum; | ||
|
||
use App\Enum\Traits\DecorateBackedEnum; | ||
|
||
/** | ||
* Enum OauthProvidersType. | ||
* | ||
* Available providers | ||
*/ | ||
enum OauthProvidersType: string | ||
{ | ||
use DecorateBackedEnum; | ||
|
||
case AMAZON = 'amazon'; | ||
case APPLE = 'apple'; | ||
case FACEBOOK = 'facebook'; | ||
case GITHUB = 'github'; | ||
case GOOGLE = 'google'; | ||
case MASTODON = 'mastodon'; | ||
case MICROSOFT = 'microsoft'; | ||
case NEXTCLOUD = 'nextcloud'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Enum\OauthProvidersType; | ||
use App\Exceptions\Internal\LycheeInvalidArgumentException; | ||
use App\Exceptions\Internal\LycheeLogicException; | ||
use App\Exceptions\UnauthenticatedException; | ||
use App\Exceptions\UnauthorizedException; | ||
use App\Models\OauthCredential; | ||
use App\Models\User; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Routing\Controller; | ||
use Illuminate\Routing\Redirector; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\Request; | ||
use Illuminate\Support\Facades\Session; | ||
use Laravel\Socialite\Facades\Socialite; | ||
use Symfony\Component\HttpFoundation\RedirectResponse as HttpFoundationRedirectResponse; | ||
|
||
class Oauth extends Controller | ||
{ | ||
public const OAUTH_REGISTER = 'register'; | ||
|
||
/** | ||
* Provide a valid provider Enum from string. | ||
* | ||
* @param string $provider | ||
* | ||
* @return OauthProvidersType | ||
* | ||
* @throws LycheeInvalidArgumentException | ||
*/ | ||
private function validateProviderOrDie(string $provider): OauthProvidersType | ||
{ | ||
$providerEnum = OauthProvidersType::tryFrom($provider); | ||
if ($providerEnum === null) { | ||
throw new LycheeInvalidArgumentException('unkown Oauth provider type'); | ||
} | ||
|
||
return $providerEnum; | ||
} | ||
|
||
/** | ||
* Function callback from the Oauth server. | ||
* | ||
* @param string $provider | ||
* | ||
* @return Redirector|RedirectResponse | ||
*/ | ||
public function redirected(string $provider) | ||
{ | ||
$providerEnum = $this->validateProviderOrDie($provider); | ||
|
||
// We are already logged in: Registration operation | ||
if (Auth::check()) { | ||
return $this->registerOrDie($providerEnum); | ||
} | ||
|
||
// Authentication operation | ||
return $this->authenticateOrDie($providerEnum); | ||
} | ||
|
||
/** | ||
* Function called to authenticate a user to an Oauth server. | ||
* | ||
* @param string $provider | ||
* | ||
* @return HttpFoundationRedirectResponse | ||
*/ | ||
public function authenticate(string $provider) | ||
{ | ||
if (Auth::check()) { | ||
throw new UnauthorizedException('User already authenticated.'); | ||
} | ||
|
||
$providerEnum = $this->validateProviderOrDie($provider); | ||
|
||
return Socialite::driver($providerEnum->value)->redirect(); | ||
} | ||
|
||
/** | ||
* Add some security on registration. | ||
* | ||
* @param string $provider | ||
* | ||
* @return HttpFoundationRedirectResponse | ||
*/ | ||
public function register(string $provider) | ||
{ | ||
$providerEnum = $this->validateProviderOrDie($provider); | ||
|
||
Auth::user() ?? throw new UnauthenticatedException(); | ||
if (!Request::hasValidSignature(false)) { | ||
throw new UnauthorizedException('Registration attempted but not initialized.'); | ||
} | ||
|
||
Session::put($providerEnum->value, self::OAUTH_REGISTER); | ||
|
||
return Socialite::driver($providerEnum->value)->redirect(); | ||
} | ||
|
||
/** | ||
* Authenticate and redirect. | ||
* | ||
* @param OauthProvidersType $provider | ||
* | ||
* @return RedirectResponse | ||
*/ | ||
private function authenticateOrDie(OauthProvidersType $provider) | ||
{ | ||
$user = Socialite::driver($provider->value)->user(); | ||
|
||
$credential = OauthCredential::query() | ||
->with(['user']) | ||
->where('token_id', '=', $user->getId()) | ||
->where('provider', '=', $provider) | ||
->first(); | ||
|
||
if ($credential === null) { | ||
throw new UnauthorizedException('User not found!'); | ||
} | ||
|
||
Auth::login($credential->user); | ||
|
||
return redirect(route('livewire-gallery')); | ||
} | ||
|
||
/** | ||
* Authenticate and redirect. | ||
* | ||
* @param OauthProvidersType $provider | ||
* | ||
* @return RedirectResponse | ||
*/ | ||
private function registerOrDie(OauthProvidersType $provider) | ||
{ | ||
if (Session::get($provider->value) !== self::OAUTH_REGISTER) { | ||
throw new UnauthorizedException('Registration attempted but not authorized.'); | ||
} | ||
|
||
$user = Socialite::driver($provider->value)->user(); | ||
|
||
/** @var User $authedUser */ | ||
$authedUser = Auth::user(); | ||
|
||
$count_existing = OauthCredential::query() | ||
->where('provider', '=', $provider) | ||
->where('user_id', '=', $authedUser->id) | ||
->count(); | ||
if ($count_existing > 0) { | ||
throw new LycheeLogicException('Oauth credential for that provider already exists.'); | ||
} | ||
|
||
$credential = OauthCredential::create([ | ||
'provider' => $provider, | ||
'user_id' => $authedUser->id, | ||
'token_id' => $user->getId(), | ||
]); | ||
$credential->save(); | ||
|
||
return redirect(route('profile')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace App\Livewire\Components\Forms\Profile; | ||
|
||
use App\Enum\OauthProvidersType; | ||
use App\Exceptions\UnauthenticatedException; | ||
use App\Livewire\DTO\OauthData; | ||
use App\Models\OauthCredential; | ||
use App\Models\User; | ||
use App\Policies\UserPolicy; | ||
use Illuminate\Contracts\View\View; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\URL; | ||
use Livewire\Component; | ||
|
||
/** | ||
* Retrieve the API token for the current user. | ||
* This is the Modal integration. | ||
*/ | ||
class Oauth extends Component | ||
{ | ||
/** | ||
* Renders the modal content. | ||
* | ||
* @return View | ||
*/ | ||
public function render(): View | ||
{ | ||
$this->authorize(UserPolicy::CAN_EDIT, [User::class]); | ||
|
||
return view('livewire.forms.profile.oauth'); | ||
} | ||
|
||
public function clear(string $provider): void | ||
{ | ||
$this->authorize(UserPolicy::CAN_EDIT, [User::class]); | ||
$providerEnum = OauthProvidersType::from($provider); | ||
|
||
/** @var User $user */ | ||
$user = Auth::user() ?? throw new UnauthenticatedException(); | ||
$user->oauthCredentials()->where('provider', '=', $providerEnum)->delete(); | ||
} | ||
|
||
/** | ||
* Return computed property for OauthData. | ||
* | ||
* @return array<string,OauthData> | ||
*/ | ||
public function getOauthDataProperty(): array | ||
{ | ||
$oauthData = []; | ||
|
||
/** @var User $user */ | ||
$user = Auth::user() ?? throw new UnauthenticatedException(); | ||
|
||
$credentials = $user->oauthCredentials()->get(); | ||
|
||
foreach (OauthProvidersType::cases() as $provider) { | ||
$client_id = config('services.' . $provider->value . '.client_id'); | ||
if ($client_id === null || $client_id === '') { | ||
continue; | ||
} | ||
|
||
// We create a signed route for 5 minutes | ||
$route = URL::signedRoute( | ||
name: 'oauth-register', | ||
parameters: ['provider' => $provider->value], | ||
expiration: now()->addMinutes(5), | ||
absolute: false); | ||
|
||
$oauthData[$provider->value] = new OauthData( | ||
providerType: $provider->value, | ||
isEnabled: $credentials->search(fn (OauthCredential $c) => $c->provider === $provider) !== false, | ||
registrationRoute: $route, | ||
); | ||
} | ||
|
||
return $oauthData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.