forked from devaslanphp/project-management
-
Notifications
You must be signed in to change notification settings - Fork 0
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
15 changed files
with
280 additions
and
7 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,86 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Auth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\Role; | ||
use App\Models\User; | ||
use App\Settings\GeneralSettings; | ||
use Illuminate\Http\Request; | ||
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; | ||
use League\OAuth2\Client\Provider\GenericProvider; | ||
|
||
class OidcAuthController extends Controller | ||
{ | ||
private $client; | ||
|
||
public function __construct() | ||
{ | ||
$this->client = new GenericProvider([ | ||
'clientId' => config('services.oidc.client_id'), | ||
'clientSecret' => config('services.oidc.client_secret'), | ||
'redirectUri' => config('services.oidc.redirect_uri'), | ||
'urlAuthorize' => config('services.oidc.url_authorize'), | ||
'urlAccessToken' => config('services.oidc.url_access_token'), | ||
'urlResourceOwnerDetails' => config('services.oidc.url_resource_owner_details'), | ||
'scopes' => config('services.oidc.scope') | ||
]); | ||
} | ||
|
||
public function redirect() | ||
{ | ||
$authUrl = $this->client->getAuthorizationUrl(); | ||
return redirect($authUrl); | ||
} | ||
|
||
public function callback(Request $request) | ||
{ | ||
try { | ||
$accessToken = $this->client->getAccessToken('authorization_code', [ | ||
'code' => $request->input('code') | ||
]); | ||
$user = $this->client->getResourceOwner($accessToken); | ||
|
||
// Perform any additional validation or user creation here | ||
if ($user) { | ||
$data = $user->toArray(); | ||
$user = User::where('email', $data['email'])->first(); | ||
if (!$user) { | ||
$user = User::create([ | ||
'name' => $data['given_name'] . ' ' . $data['family_name'], | ||
'email' => $data['email'], | ||
'oidc_username' => $data['preferred_username'], | ||
'email_verified_at' => $data['email_verified'] ? now() : null, | ||
'type' => 'oidc', | ||
'oidc_sub' => $data['sub'], | ||
'password' => null | ||
]); | ||
$defaultRoleSettings = app(GeneralSettings::class)->default_role; | ||
if ($defaultRoleSettings && $defaultRole = Role::where('id', $defaultRoleSettings)->first()) { | ||
$user->syncRoles([$defaultRole]); | ||
} | ||
} else { | ||
$user->update([ | ||
'name' => $data['given_name'] . ' ' . $data['family_name'], | ||
'email' => $data['email'], | ||
'oidc_username' => $data['preferred_username'], | ||
'type' => 'oidc', | ||
'oidc_sub' => $data['sub'], | ||
'password' => null | ||
]); | ||
$user->refresh(); | ||
} | ||
|
||
// Log the user in | ||
auth()->login($user); | ||
|
||
return redirect()->intended(); | ||
} | ||
session()->flash('oidc_error'); | ||
return redirect()->route('login'); | ||
} catch (IdentityProviderException $e) { | ||
session()->flash('oidc_error'); | ||
return redirect()->route('login'); | ||
} | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
38 changes: 38 additions & 0 deletions
38
database/migrations/2023_01_24_084637_update_users_for_oidc.php
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,38 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->string('password')->nullable(true)->change(); | ||
$table->string('type')->default('db'); | ||
$table->string('oidc_username')->nullable(); | ||
$table->string('oidc_sub')->nullable(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->string('password')->nullable(false)->change(); | ||
$table->dropColumn('type'); | ||
$table->dropColumn('oidc_username'); | ||
$table->dropColumn('oidc_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 |
---|---|---|
|
@@ -16,12 +16,14 @@ class DefaultUserSeeder extends Seeder | |
public function run() | ||
{ | ||
if (User::where('email', '[email protected]')->count() == 0) { | ||
User::create([ | ||
$user = User::create([ | ||
'name' => 'John DOE', | ||
'email' => '[email protected]', | ||
'password' => bcrypt('Passw@rd'), | ||
'email_verified_at' => now() | ||
]); | ||
$user->creation_token = null; | ||
$user->save(); | ||
} | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
database/settings/2022_11_12_173852_add_default_role_to_general_settings.php
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,12 @@ | ||
<?php | ||
|
||
use App\Models\Role; | ||
use Spatie\LaravelSettings\Migrations\SettingsMigration; | ||
|
||
class AddDefaultRoleToGeneralSettings extends SettingsMigration | ||
{ | ||
public function up(): void | ||
{ | ||
$this->migrator->add('general.default_role'); | ||
} | ||
} |
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
Oops, something went wrong.