metaclassing /
PHP7-Laravel5-EnterpriseAuth
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Metaclassing\EnterpriseAuth; |
||
| 4 | |||
| 5 | class ServiceProvider extends \Illuminate\Support\ServiceProvider |
||
| 6 | { |
||
| 7 | public function register() |
||
| 8 | { |
||
| 9 | } |
||
| 10 | |||
| 11 | public function boot() |
||
| 12 | { |
||
| 13 | // Make sure nobody is including or running this thing without all the required env settings |
||
| 14 | $this->checkMandatoryConfigsAreSet(); |
||
| 15 | |||
| 16 | // Install our API auth guard middleware |
||
| 17 | $this->installOauthTokenGuardMiddleware(); |
||
| 18 | |||
| 19 | // Make sure that this vendor dir and the routes dir are in any scanned paths for swagger documentation |
||
| 20 | $this->configureSwaggerToScanEnterpriseAuthRouteFiles(); |
||
| 21 | |||
| 22 | // Make sure the publish command picks up our config, migration, user model, and dummy API route files |
||
| 23 | $this->publishes([ |
||
| 24 | __DIR__.'/../publish/config/enterpriseauth.php' => config_path('enterpriseauth.php'), |
||
| 25 | __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', |
||
| 26 | __DIR__.'/../publish/app/User.php' => app_path().'/User.php', |
||
| 27 | __DIR__.'/../publish/routes/api.php' => base_path('routes').'/api.php', |
||
| 28 | ]); |
||
| 29 | |||
| 30 | // Merge configs with the default configs |
||
| 31 | $this->mergeConfigFrom( |
||
| 32 | __DIR__.'/../publish/config/enterpriseauth.php', 'enterpriseauth' |
||
| 33 | ); |
||
| 34 | |||
| 35 | // Load our HTTP routes for API and WEB authentication |
||
| 36 | $this->loadRoutesFrom(__DIR__.'/../routes/api.microsoft.php'); |
||
| 37 | $this->loadRoutesFrom(__DIR__.'/../routes/web.microsoft.php'); |
||
| 38 | |||
| 39 | // Trigger generating our swagger oauth security settings based on application env file contents |
||
| 40 | $this->generateSwaggerOauthSecurityScheme(); |
||
| 41 | |||
| 42 | //Register any CLI commands |
||
| 43 | $this->commands([Console\GenerateUIPreload::class]); |
||
| 44 | } |
||
| 45 | |||
| 46 | protected function checkMandatoryConfigsAreSet() |
||
| 47 | { |
||
| 48 | // On first run this will be false, after config file is installed it will be true |
||
| 49 | if (config('enterpriseauth')) { |
||
| 50 | // Go through all the credential config and make sure they are set in the .env or config file |
||
| 51 | foreach (config('enterpriseauth.credentials') as $config => $env) { |
||
| 52 | // If one isnt set, throw a red flat until the person fixes it |
||
| 53 | if (! config('enterpriseauth.credentials.'.$config)) { |
||
| 54 | throw new \Exception('enterpriseauth setup error: missing mandatory config value for enterpriseauth.credentials.'.$config.' check your .env file!'); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | protected function installOauthTokenGuardMiddleware() |
||
| 61 | { |
||
| 62 | // Override the application configuration to use our oauth token guard driver at runtime |
||
| 63 | config(['auth.guards.api.driver' => 'oauthtoken']); |
||
| 64 | // Now I have a machine gun. ho ho ho! |
||
| 65 | \Illuminate\Support\Facades\Auth::extend('oauthtoken', function ($app, $name, array $config) { |
||
| 66 | $userProvider = \Illuminate\Support\Facades\Auth::createUserProvider($config['provider']); |
||
| 67 | |||
| 68 | return new \Metaclassing\EnterpriseAuth\Middleware\OauthTokenGuard($userProvider, $app->make('request')); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 69 | }); |
||
| 70 | } |
||
| 71 | |||
| 72 | protected function configureSwaggerToScanEnterpriseAuthRouteFiles() |
||
| 73 | { |
||
| 74 | $swaggerScanPaths = config('l5-swagger.paths.annotations'); |
||
| 75 | if (! is_array($swaggerScanPaths)) { |
||
| 76 | $swaggerScanPaths = [$swaggerScanPaths]; |
||
| 77 | } |
||
| 78 | if (! in_array(base_path('routes'), $swaggerScanPaths)) { |
||
| 79 | $swaggerScanPaths[] = base_path('routes'); |
||
| 80 | } |
||
| 81 | if (! in_array(__DIR__.'/../routes/', $swaggerScanPaths)) { |
||
| 82 | $swaggerScanPaths[] = __DIR__.'/../routes/'; |
||
| 83 | } |
||
| 84 | config(['l5-swagger.paths.annotations' => $swaggerScanPaths]); |
||
| 85 | } |
||
| 86 | |||
| 87 | protected function generateSwaggerOauthSecurityScheme() |
||
| 88 | { |
||
| 89 | // If the routes files for the swagger oauth config is NOT present, and we have all the right info, then generate it really quick |
||
| 90 | $swaggerAzureadFile = __DIR__.'/../routes/swagger.azuread.php'; |
||
| 91 | if (! file_exists($swaggerAzureadFile)) { |
||
| 92 | $aad = new AzureActiveDirectory(config('enterpriseauth.credentials.tenant')); |
||
| 93 | //$authorizationUrl = $aad->authorizationEndpoint . '?resource=https://graph.microsoft.com'; |
||
| 94 | $authorizationUrl = $aad->authorizationEndpoint; |
||
| 95 | $client_id = config('enterpriseauth.credentials.client_id'); |
||
| 96 | $contents = <<<EOF |
||
| 97 | <?php |
||
| 98 | /** |
||
| 99 | * @SWG\SecurityScheme( |
||
| 100 | * securityDefinition="AzureAD", |
||
| 101 | * type="oauth2", |
||
| 102 | * authorizationUrl="$authorizationUrl", |
||
| 103 | * flow="implicit", |
||
| 104 | * scopes={ |
||
| 105 | * "api://$client_id/access_as_user": "Use client_id: $client_id", |
||
| 106 | * } |
||
| 107 | * ) |
||
| 108 | **/ |
||
| 109 | EOF; |
||
| 110 | // * "https://graph.microsoft.com/.default": "Use client_id: $client_id" |
||
| 111 | file_put_contents($swaggerAzureadFile, $contents); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 |