Skip to content

Commit b065e39

Browse files
committed
init commit
0 parents  commit b065e39

21 files changed

+623
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.lock
2+
node_modules
3+
vendor

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Bagisto Europe
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<div align="center">
2+
<img src="https://bagisto.com/wp-content/themes/bagisto/images/logo.png" alt="Bagisto Logo" />
3+
<h2>Azure single sign-on (SSO)</h2>
4+
</div>
5+
6+
<div align="center">
7+
<img alt="GitHub version" src="http://poser.pugx.org/bagisto-eu/azure-auth/v">
8+
<img alt="GitHub license" src="https://img.shields.io/github/license/bagisto-europe/admin-azure-auth">
9+
</div>
10+
11+
Integrate Microsoft Azure Single Sign On and benefit from a secure login experience in the Bagisto admin panel.
12+
13+
![example](docs/bagisto-signin.png)
14+
15+
## Installation
16+
17+
1. Install the package using Composer:
18+
19+
```bash
20+
composer require bagisto-eu/azure-auth
21+
```
22+
23+
2. Run the following command
24+
25+
```bash
26+
php artisan azure:configure
27+
```

composer.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "bagisto-eu/azure-auth",
3+
"type": "library",
4+
"description": "Bagisto Azure Authentication Package",
5+
"keywords": ["bagisto", "admin", "azure", "authentication", "sso"],
6+
"homepage": "https://github.com/bagisto-europe/multisafepay",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Bagisto Europe",
11+
"email": "info@bagisto.eu"
12+
}
13+
],
14+
"require": {
15+
"laravel/prompts": "^0.1.13",
16+
"laravel/socialite": "^5.10",
17+
"socialiteproviders/microsoft-azure": "^5.1"
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"Bagisto\\AzureAuth\\": "src/"
22+
}
23+
},
24+
"config": {
25+
"sort-packages": true
26+
},
27+
"extra": {
28+
"laravel": {
29+
"providers": [
30+
"Bagisto\\AzureAuth\\Providers\\AzureAuthServiceProvider"
31+
]
32+
}
33+
},
34+
"minimum-stability": "dev",
35+
"prefer-stable": true
36+
}

docs/bagisto-signin.png

15.3 KB
Loading

src/Config/services.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
4+
return [
5+
'azure' => [
6+
'client_id' => env('AZURE_CLIENT_ID'),
7+
'client_secret' => env('AZURE_CLIENT_SECRET'),
8+
'redirect' => route('azure.callback'),
9+
'tenant' => env('AZURE_TENANT_ID')
10+
]
11+
];
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Bagisto\AzureAuth\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Artisan;
7+
use Illuminate\Support\Facades\File;
8+
use Illuminate\Support\Str;
9+
10+
use function Laravel\Prompts\text;
11+
use function Laravel\Prompts\info;
12+
use function Laravel\Prompts\confirm;
13+
14+
class ConfigureAzure extends Command
15+
{
16+
protected $signature = 'azure:configure';
17+
protected $description = 'Configure Azure environment variables';
18+
19+
public function handle()
20+
{
21+
info('Welcome to the Microsoft Azure SSO setup wizard');
22+
23+
if ($this->keysExist()) {
24+
$overwrite = confirm('Azure configuration keys already exist. This wizard will overwrite existing settings. Continue?', false);
25+
26+
if (!$overwrite) {
27+
return;
28+
}
29+
}
30+
31+
$clientId = text(
32+
label: 'Please enter your Client ID',
33+
required: true
34+
);
35+
36+
$this->updateEnvFile('AZURE_CLIENT_ID', $clientId);
37+
38+
$clientSecret = text(
39+
label: 'Please enter your client Secret',
40+
required: true
41+
);
42+
43+
$this->updateEnvFile('AZURE_CLIENT_SECRET', $clientSecret);
44+
45+
$tenantId = text(
46+
label: 'Please enter your Tenant ID',
47+
required: true
48+
);
49+
50+
$this->updateEnvFile('AZURE_TENANT_ID', $tenantId);
51+
52+
Artisan::call('optimize', [], $this->getOutput());
53+
54+
Artisan::call('vendor:publish', [
55+
'--provider' => "Bagisto\AzureAuth\Providers\AzureAuthServiceProvider",
56+
'--force' => true
57+
], $this->getOutput());
58+
59+
info('Azure SSO setup completed successfully.');
60+
}
61+
62+
protected function keysExist()
63+
{
64+
return env('AZURE_CLIENT_ID') !== null
65+
&& env('AZURE_CLIENT_SECRET') !== null
66+
&& env('AZURE_TENANT_ID') !== null;
67+
}
68+
69+
protected function updateEnvFile($key, $value)
70+
{
71+
$envFilePath = base_path('.env');
72+
73+
if (File::exists($envFilePath)) {
74+
$envContent = File::get($envFilePath);
75+
76+
if (Str::contains($envContent, "{$key}=")) {
77+
$envContent = preg_replace("/{$key}=.*/", "{$key}=\"{$value}\"", $envContent);
78+
} else {
79+
$envContent .= PHP_EOL . "{$key}=\"{$value}\"";
80+
}
81+
82+
File::put($envFilePath, $envContent);
83+
}
84+
}
85+
}

src/Helpers/AzureConfigHelper.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Bagisto\AzureAuth\Helpers;
4+
5+
class AzureConfigHelper
6+
{
7+
public static function isConfigured()
8+
{
9+
return (
10+
config('services.azure.client_id') &&
11+
config('services.azure.client_secret') &&
12+
config('services.azure.redirect') &&
13+
config('services.azure.tenant')
14+
);
15+
}
16+
}
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace Bagisto\AzureAuth\Http\Controllers;
4+
5+
use App\Http\Controllers\Controller;
6+
use Bagisto\AzureAuth\Helpers\AzureConfigHelper;
7+
8+
use Illuminate\Support\Str;
9+
use Illuminate\Support\Facades\Log;
10+
use Laravel\Socialite\Facades\Socialite;
11+
12+
use Webkul\User\Repositories\AdminRepository;
13+
14+
class SessionController extends Controller
15+
{
16+
/**
17+
* Create a new controller instance.
18+
*
19+
* @return void
20+
*/
21+
public function __construct(
22+
protected AdminRepository $adminRepository
23+
) {
24+
}
25+
26+
/**
27+
* Redirect the user to the Azure authentication page.
28+
*
29+
* @return \Illuminate\Http\RedirectResponse
30+
*/
31+
public function redirectToAzure()
32+
{
33+
if (!AzureConfigHelper::isConfigured()) {
34+
return view('azure-auth::errors.config');
35+
}
36+
37+
return Socialite::driver('azure')->redirect();
38+
}
39+
40+
/**
41+
* Handle the callback from Azure.
42+
*
43+
* @return \Illuminate\Http\RedirectResponse
44+
*/
45+
public function handleCallback()
46+
{
47+
try {
48+
$user = Socialite::driver('azure')->user();
49+
50+
$localUser = $this->adminRepository->where('email', $user->getEmail())->first();
51+
52+
if (!$localUser) {
53+
$randomPass = Str::random(80);
54+
55+
$userData = [
56+
'name' => $user->getName(),
57+
'email' => $user->getEmail(),
58+
'password' => bcrypt($randomPass),
59+
'role_id' => 1,
60+
'status' => true
61+
];
62+
63+
$adminUser = $this->adminRepository->create($userData);
64+
65+
if ($adminUser) {
66+
Log::info('Local user created for ', ['user_email' => $user->getEmail()]);
67+
}
68+
}
69+
70+
auth()->guard('admin')->login($userData);
71+
72+
if (! auth()->guard('admin')->user()->status) {
73+
session()->flash('warning', trans('admin::app.settings.users.activate-warning'));
74+
75+
auth()->guard('admin')->logout();
76+
77+
return redirect()->route('admin.session.create');
78+
}
79+
80+
Log::info('Azure Authentication Successful', ['user_email' => $user->getEmail()]);
81+
82+
return redirect()->route('admin.dashboard.index');
83+
} catch (\Exception $e) {
84+
Log::error('Azure Authentication Error: ' . $e->getMessage());
85+
86+
return redirect()->route('admin.session.create')->with('warning', 'Unable to authenticate with your Microsoft account. Please try again.');
87+
}
88+
}
89+
90+
/**
91+
* Show the error view for missing or invalid Azure configuration.
92+
*
93+
* @return \Illuminate\View\View
94+
*/
95+
public function showConfigError()
96+
{
97+
return view('azure-auth::errors.config');
98+
}
99+
100+
}
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Bagisto\AzureAuth\Providers;
4+
5+
use Bagisto\AzureAuth\Console\Commands\ConfigureAzure;
6+
use Bagisto\AzureAuth\Providers\EventServiceProvider;
7+
8+
use Illuminate\Support\ServiceProvider;
9+
use Illuminate\Contracts\Http\Kernel;
10+
11+
class AzureAuthServiceProvider extends ServiceProvider
12+
{
13+
/**
14+
* Register services.
15+
*
16+
* @return void
17+
*/
18+
public function register()
19+
{
20+
$this->app->register(EventServiceProvider::class);
21+
22+
$this->commands([
23+
ConfigureAzure::class,
24+
]);
25+
}
26+
27+
/**
28+
* Bootstrap services.
29+
*
30+
* @return void
31+
*/
32+
public function boot()
33+
{
34+
$this->loadRoutesFrom(__DIR__.'/../Routes/web.php');
35+
36+
$this->loadTranslationsFrom(__DIR__.'/../Resources/lang', 'azure-auth');
37+
38+
$this->loadViewsFrom(__DIR__.'/../Resources/views', 'azure-auth');
39+
40+
$this->app->booted(function () {
41+
$this->loadConfig();
42+
});
43+
44+
$this->publishes([
45+
__DIR__.'/../Resources/img' => public_path('vendor/azure-auth'),
46+
], 'public');
47+
48+
$this->publishes([
49+
__DIR__.'/../Resources/views/users' => resource_path('admin-themes/default/views'),
50+
], 'azure-auth');
51+
}
52+
53+
protected function loadConfig()
54+
{
55+
$this->mergeConfigFrom(
56+
__DIR__ . '/../Config/services.php',
57+
'services'
58+
);
59+
}
60+
}

0 commit comments

Comments
 (0)