Skip to content

Commit 7309665

Browse files
authored
Merge pull request #56 from devaslanphp/dev
Merge dev into Master
2 parents c6334de + 3945000 commit 7309665

87 files changed

Lines changed: 3971 additions & 190 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env.example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ APP_ENV=local
33
APP_KEY=
44
APP_DEBUG=true
55
APP_URL=http://localhost:8000
6+
APP_FORCE_HTTPS=false
67

78
LOG_CHANNEL=daily
89
LOG_DEPRECATIONS_CHANNEL=null
@@ -73,3 +74,14 @@ TWITTER_CLIENT_ID=
7374
TWITTER_CLIENT_SECRET=
7475
TWITTER_CLIENT_CALLBACK="${APP_URL}/oauth/callback/twitter"
7576

77+
# This example is based on Keycloak as an OIDC provider
78+
# Make sure you change it based on your own OIDC provider
79+
OIDC_CLIENT_ID=
80+
OIDC_CLIENT_SECRET=
81+
OIDC_DISCOVERY_ENDPOINT=
82+
OIDC_REDIRECT_URI="${APP_URL}/oidc/callback"
83+
OIDC_REALM="myrealm"
84+
OIDC_URL_AUTHORIZE="${OIDC_DISCOVERY_ENDPOINT}/realms/${OIDC_REALM}/protocol/openid-connect/auth"
85+
OIDC_URL_ACCESS_TOKEN="${OIDC_DISCOVERY_ENDPOINT}/realms/${OIDC_REALM}/protocol/openid-connect/token"
86+
OIDC_URL_RESOURCE_OWNER_DETAILS="${OIDC_DISCOVERY_ENDPOINT}/realms/${OIDC_REALM}/protocol/openid-connect/userinfo"
87+
OIDC_SCOPE="openid"

.rnd

1 KB
Binary file not shown.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
133133
- **Release 1.2.2**
134134
- Dockerize application #23
135135
- PR #45
136+
- **Release 1.2.3**
137+
- Update german language #52
138+
- SSO with OpenID (OIDC) #48
136139

137140
## Support us
138141

app/Filament/Pages/ManageGeneralSettings.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace App\Filament\Pages;
44

5+
use App\Models\Role;
56
use App\Settings\GeneralSettings;
67
use Filament\Forms\Components\Card;
78
use Filament\Forms\Components\Checkbox;
89
use Filament\Forms\Components\FileUpload;
910
use Filament\Forms\Components\Grid;
1011
use Filament\Forms\Components\Select;
1112
use Filament\Forms\Components\TextInput;
13+
use Filament\Forms\Components\Toggle;
1214
use Filament\Pages\Actions\Action;
1315
use Filament\Pages\SettingsPage;
1416
use Illuminate\Contracts\Support\Htmlable;
@@ -62,21 +64,33 @@ protected function getFormSchema(): array
6264
->default(fn() => config('app.name'))
6365
->required(),
6466

65-
Checkbox::make('enable_registration')
67+
Toggle::make('enable_registration')
6668
->label(__('Enable registration?'))
67-
->helperText(__('If enabled, any user can create an account in this platform.
68-
But an administration need to give them permissions.')),
69+
->helperText(__('If enabled, any user can create an account in this platform. But an administration need to give them permissions.')),
6970

70-
Checkbox::make('enable_social_login')
71+
Toggle::make('enable_social_login')
7172
->label(__('Enable social login?'))
72-
->helperText(__('If enabled, configured users can login via their
73-
social accounts.')),
73+
->helperText(__('If enabled, configured users can login via their social accounts.')),
74+
75+
Toggle::make('enable_login_form')
76+
->label(__('Enable form login?'))
77+
->helperText(__('If enabled, a login form will be visible on the login page.')),
78+
79+
Toggle::make('enable_oidc_login')
80+
->label(__('Enable OIDC login?'))
81+
->helperText(__('If enabled, an OIDC Connect button will be visible on the login page.')),
7482

7583
Select::make('site_language')
7684
->label(__('Site language'))
7785
->helperText(__('The language used by the platform.'))
7886
->searchable()
7987
->options($this->getLanguages()),
88+
89+
Select::make('default_role')
90+
->label(__('Default role'))
91+
->helperText(__('The platform default role (used mainly in OIDC Connect).'))
92+
->searchable()
93+
->options(Role::all()->pluck('name', 'id')->toArray()),
8094
]),
8195
]),
8296
]),
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Auth;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Models\Role;
7+
use App\Models\User;
8+
use App\Settings\GeneralSettings;
9+
use Illuminate\Http\Request;
10+
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
11+
use League\OAuth2\Client\Provider\GenericProvider;
12+
13+
class OidcAuthController extends Controller
14+
{
15+
private $client;
16+
17+
public function __construct()
18+
{
19+
$this->client = new GenericProvider([
20+
'clientId' => config('services.oidc.client_id'),
21+
'clientSecret' => config('services.oidc.client_secret'),
22+
'redirectUri' => config('services.oidc.redirect_uri'),
23+
'urlAuthorize' => config('services.oidc.url_authorize'),
24+
'urlAccessToken' => config('services.oidc.url_access_token'),
25+
'urlResourceOwnerDetails' => config('services.oidc.url_resource_owner_details'),
26+
'scopes' => config('services.oidc.scope')
27+
]);
28+
}
29+
30+
public function redirect()
31+
{
32+
$authUrl = $this->client->getAuthorizationUrl();
33+
return redirect($authUrl);
34+
}
35+
36+
public function callback(Request $request)
37+
{
38+
try {
39+
$accessToken = $this->client->getAccessToken('authorization_code', [
40+
'code' => $request->input('code')
41+
]);
42+
$user = $this->client->getResourceOwner($accessToken);
43+
44+
// Perform any additional validation or user creation here
45+
if ($user) {
46+
$data = $user->toArray();
47+
$user = User::where('email', $data['email'])->first();
48+
if (!$user) {
49+
$user = User::create([
50+
'name' => $data['given_name'] . ' ' . $data['family_name'],
51+
'email' => $data['email'],
52+
'oidc_username' => $data['preferred_username'],
53+
'email_verified_at' => $data['email_verified'] ? now() : null,
54+
'type' => 'oidc',
55+
'oidc_sub' => $data['sub'],
56+
'password' => null
57+
]);
58+
$defaultRoleSettings = app(GeneralSettings::class)->default_role;
59+
if ($defaultRoleSettings && $defaultRole = Role::where('id', $defaultRoleSettings)->first()) {
60+
$user->syncRoles([$defaultRole]);
61+
}
62+
} else {
63+
$user->update([
64+
'name' => $data['given_name'] . ' ' . $data['family_name'],
65+
'email' => $data['email'],
66+
'oidc_username' => $data['preferred_username'],
67+
'type' => 'oidc',
68+
'oidc_sub' => $data['sub'],
69+
'password' => null
70+
]);
71+
$user->refresh();
72+
}
73+
74+
// Log the user in
75+
auth()->login($user);
76+
77+
return redirect()->intended();
78+
}
79+
session()->flash('oidc_error');
80+
return redirect()->route('login');
81+
} catch (IdentityProviderException $e) {
82+
session()->flash('oidc_error');
83+
return redirect()->route('login');
84+
}
85+
}
86+
}

app/Models/User.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ class User extends Authenticatable implements MustVerifyEmail, FilamentUser
3434
'name',
3535
'email',
3636
'password',
37-
'creation_token'
37+
'creation_token',
38+
'type',
39+
'oidc_username',
40+
'email_verified_at',
3841
];
3942

4043
/**
@@ -61,12 +64,16 @@ public static function boot()
6164
parent::boot();
6265

6366
static::creating(function (User $item) {
64-
$item->password = bcrypt(uniqid());
65-
$item->creation_token = Uuid::uuid4()->toString();
67+
if ($item->type == 'db') {
68+
$item->password = bcrypt(uniqid());
69+
$item->creation_token = Uuid::uuid4()->toString();
70+
}
6671
});
6772

6873
static::created(function (User $item) {
69-
$item->notify(new UserCreatedNotification($item));
74+
if ($item->type == 'db') {
75+
$item->notify(new UserCreatedNotification($item));
76+
}
7077
});
7178
}
7279

app/Providers/AppServiceProvider.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
use App\Settings\GeneralSettings;
66
use Filament\Facades\Filament;
7-
use Filament\Navigation\UserMenuItem;
87
use Illuminate\Database\QueryException;
98
use Illuminate\Foundation\Vite;
109
use Illuminate\Support\Facades\Config;
11-
use Illuminate\Support\Facades\Schema;
10+
use Illuminate\Support\Facades\URL;
1211
use Illuminate\Support\HtmlString;
1312
use Illuminate\Support\ServiceProvider;
1413

@@ -69,6 +68,11 @@ public function boot()
6968
__('Security'),
7069
__('Settings'),
7170
]);
71+
72+
// Force HTTPS over HTTP
73+
if (env('APP_FORCE_HTTPS') ?? false) {
74+
URL::forceScheme('https');
75+
}
7276
}
7377

7478
private function configureApp(): void
@@ -85,6 +89,8 @@ private function configureApp(): void
8589
Config::set('filament-breezy.enable_registration', $settings->enable_registration ?? false);
8690
Config::set('filament-socialite.registration', $settings->enable_registration ?? false);
8791
Config::set('filament-socialite.enabled', $settings->enable_social_login ?? false);
92+
Config::set('system.login_form.is_enabled', $settings->enable_login_form ?? false);
93+
Config::set('services.oidc.is_enabled', $settings->enable_oidc_login ?? false);
8894
} catch (QueryException $e) {
8995
// Error: No database configured yet
9096
}

app/Settings/GeneralSettings.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class GeneralSettings extends Settings
1212
public string|null $site_logo;
1313
public string|null $enable_social_login;
1414
public string|null $site_language;
15+
public string|null $default_role;
16+
public string|null $enable_login_form;
17+
public string|null $enable_oidc_login;
1518

1619
public static function group(): string
1720
{

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"laravel/framework": "^9.19",
2020
"laravel/sanctum": "^3.0",
2121
"laravel/tinker": "^2.7",
22+
"league/oauth2-client": "^2.6",
2223
"maatwebsite/excel": "^3.1",
2324
"owenvoke/blade-fontawesome": "^2.1",
2425
"protonemedia/laravel-verify-new-email": "^1.6",

composer.lock

Lines changed: 71 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)