1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Metaclassing\EnterpriseAuth; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Auth; |
6
|
|
|
use Illuminate\Support\ServiceProvider as BaseServiceProvider; |
7
|
|
|
|
8
|
|
|
class ServiceProvider extends BaseServiceProvider |
9
|
|
|
{ |
10
|
|
|
public function register() |
11
|
|
|
{ |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
public function boot() |
15
|
|
|
{ |
16
|
|
|
// Make sure nobody is including or running this thing without all the required env settings |
17
|
|
|
if (config('enterpriseauth')) { |
18
|
|
|
foreach (config('enterpriseauth.credentials') as $config => $env) { |
19
|
|
|
if (! config('enterpriseauth.credentials.'.$config)) { |
20
|
|
|
throw new \Exception('enterpriseauth setup error: missing mandatory config value for enterpriseauth.credentials.'.$config.' check your .env file!'); |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// Actually I have my own oauth token cache based authentication guard now lol |
26
|
|
|
config(['auth.guards.api.driver' => 'oauthtoken']); |
27
|
|
|
Auth::extend('oauthtoken', function ($app, $name, array $config) { |
28
|
|
|
return new \Metaclassing\EnterpriseAuth\Middleware\OauthTokenGuard(Auth::createUserProvider($config['provider']), $app->make('request')); |
29
|
|
|
}); |
30
|
|
|
|
31
|
|
|
// Make sure that this vendor dir and the routes dir are in any scanned paths for swagger documentation |
32
|
|
|
$swaggerScanPaths = config('l5-swagger.paths.annotations'); |
33
|
|
|
if (! is_array($swaggerScanPaths)) { |
34
|
|
|
$swaggerScanPaths = [$swaggerScanPaths]; |
35
|
|
|
} |
36
|
|
|
if (! in_array(base_path('routes'), $swaggerScanPaths)) { |
37
|
|
|
$swaggerScanPaths[] = base_path('routes'); |
38
|
|
|
} |
39
|
|
|
if (! in_array(__DIR__.'/../routes/', $swaggerScanPaths)) { |
40
|
|
|
$swaggerScanPaths[] = __DIR__.'/../routes/'; |
41
|
|
|
} |
42
|
|
|
config(['l5-swagger.paths.annotations' => $swaggerScanPaths]); |
43
|
|
|
|
44
|
|
|
// Make sure the publish command picks up our config, migration, user model, and dummy API route files |
45
|
|
|
$this->publishes([ |
46
|
|
|
__DIR__.'/../publish/config/enterpriseauth.php' => config_path('enterpriseauth.php'), |
47
|
|
|
__DIR__.'/../publish/database/migrations/2018_02_19_152839_alter_users_table_for_azure_ad.php' => $this->app->databasePath().'/migrations/2018_02_19_152839_alter_users_table_for_azure_ad.php', |
48
|
|
|
__DIR__.'/../publish/app/User.php' => app_path().'/User.php', |
49
|
|
|
__DIR__.'/../publish/routes/api.php' => base_path('routes').'/api.php', |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
// Merge configs with the default configs |
53
|
|
|
$this->mergeConfigFrom( |
54
|
|
|
__DIR__.'/../publish/config/enterpriseauth.php', 'enterpriseauth' |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
// Load our HTTP routes for API and WEB authentication |
58
|
|
|
$this->loadRoutesFrom(__DIR__.'/../routes/api.microsoft.php'); |
59
|
|
|
$this->loadRoutesFrom(__DIR__.'/../routes/web.microsoft.php'); |
60
|
|
|
|
61
|
|
|
// Trigger generating our swagger oauth security settings based on application env file contents |
62
|
|
|
$this->generateSwaggerOauthSecurityScheme(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function generateSwaggerOauthSecurityScheme() |
66
|
|
|
{ |
67
|
|
|
// If the routes files for the swagger oauth config is NOT present, and we have all the right info, then generate it really quick |
68
|
|
|
$swaggerAzureadFile = __DIR__.'/../routes/swagger.azuread.php'; |
69
|
|
|
if (! file_exists($swaggerAzureadFile)) { |
70
|
|
|
$aad = new AzureActiveDirectory(config('enterpriseauth.credentials.tenant')); |
71
|
|
|
//$authorizationUrl = $aad->authorizationEndpoint . '?resource=https://graph.microsoft.com'; |
72
|
|
|
$authorizationUrl = $aad->authorizationEndpoint; |
73
|
|
|
$client_id = config('enterpriseauth.credentials.client_id'); |
74
|
|
|
$contents = <<<EOF |
75
|
|
|
<?php |
76
|
|
|
/** |
77
|
|
|
* @SWG\SecurityScheme( |
78
|
|
|
* securityDefinition="AzureAD", |
79
|
|
|
* type="oauth2", |
80
|
|
|
* authorizationUrl="$authorizationUrl", |
81
|
|
|
* flow="implicit", |
82
|
|
|
* scopes={ |
83
|
|
|
* "https://graph.microsoft.com/.default": "Use client_id: $client_id" |
84
|
|
|
* } |
85
|
|
|
* ) |
86
|
|
|
**/ |
87
|
|
|
EOF; |
88
|
|
|
file_put_contents($swaggerAzureadFile, $contents); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|