-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/openid-support' of https://github.com/indykonin…
…g/2FAuth into indykoning-feature/openid-support
- Loading branch information
Showing
13 changed files
with
419 additions
and
3 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,44 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Auth; | ||
|
||
use App\Facades\Settings; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\User; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Str; | ||
use Laravel\Socialite\Facades\Socialite; | ||
|
||
class SocialiteController extends Controller | ||
{ | ||
public function redirect(Request $request, $driver) | ||
{ | ||
return Socialite::driver($driver)->redirect(); | ||
} | ||
|
||
public function callback(Request $request, $driver) | ||
{ | ||
$socialiteUser = Socialite::driver($driver)->user(); | ||
|
||
/** @var User $user */ | ||
$user = User::firstOrNew([ | ||
'email' => $socialiteUser->getEmail(), | ||
], [ | ||
'name' => $socialiteUser->getName(), | ||
'password' => bcrypt(Str::random()), | ||
]); | ||
|
||
if (!$user->exists && Settings::get('disableRegistrationSso')) { | ||
return response(401); | ||
} | ||
|
||
$user->last_seen_at = Carbon::now()->format('Y-m-d H:i:s'); | ||
$user->save(); | ||
|
||
Auth::guard()->login($user, true); | ||
|
||
return redirect('/accounts?authenticated'); | ||
} | ||
} |
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
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,90 @@ | ||
<?php | ||
|
||
namespace App\Providers\Socialite; | ||
|
||
use GuzzleHttp\RequestOptions; | ||
use InvalidArgumentException; | ||
use SocialiteProviders\Manager\OAuth2\AbstractProvider; | ||
use SocialiteProviders\Manager\OAuth2\User; | ||
use SocialiteProviders\Manager\SocialiteWasCalled; | ||
|
||
class OpenId extends AbstractProvider | ||
{ | ||
public const IDENTIFIER = 'OPENID'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $scopes = ['openid profile email']; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function additionalConfigKeys() | ||
{ | ||
return ['token_url', 'authorize_url', 'userinfo_url']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getAuthUrl($state) | ||
{ | ||
return $this->buildAuthUrlFromBase($this->getConfig('authorize_url'), $state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getTokenUrl() | ||
{ | ||
return $this->getConfig('token_url'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getUserByToken($token) | ||
{ | ||
$response = $this->getHttpClient()->get($this->getConfig('userinfo_url'), [ | ||
RequestOptions::HEADERS => [ | ||
'Authorization' => 'Bearer '.$token, | ||
], | ||
]); | ||
|
||
return json_decode((string) $response->getBody(), true); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function refreshToken($refreshToken) | ||
{ | ||
return $this->getHttpClient()->post($this->getTokenUrl(), [ | ||
RequestOptions::FORM_PARAMS => [ | ||
'client_id' => $this->clientId, | ||
'client_secret' => $this->clientSecret, | ||
'grant_type' => 'refresh_token', | ||
'refresh_token' => $refreshToken, | ||
], | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function mapUserToObject(array $user) | ||
{ | ||
return (new User())->setRaw($user)->map([ | ||
'email' => $user['email'] ?? null, | ||
'email_verified' => $user['email_verified'] ?? null, | ||
'name' => $user['name'] ?? null, | ||
'given_name' => $user['given_name'] ?? null, | ||
'family_name' => $user['family_name'] ?? null, | ||
'preferred_username' => $user['preferred_username'] ?? null, | ||
'nickname' => $user['nickname'] ?? null, | ||
'groups' => $user['groups'] ?? null, | ||
'id' => $user['sub'], | ||
]); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace App\Providers\Socialite; | ||
|
||
use SocialiteProviders\Manager\SocialiteWasCalled; | ||
|
||
class RegisterOpenId | ||
{ | ||
public function __invoke(SocialiteWasCalled $socialiteWasCalled) | ||
{ | ||
$socialiteWasCalled->extendSocialite('openid', OpenId::class); | ||
} | ||
} |
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.