Passed
Push — master ( 54115d...67a0b3 )
by meta
02:30
created

ServiceProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B boot() 0 48 6
B generateSwaggerOauthSecurityScheme() 0 24 2
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
        $requiredVariables = ['AZURE_AD_CLIENT_ID', 'AZURE_AD_CLIENT_SECRET', 'AZURE_AD_TENANT', 'AZURE_AD_CALLBACK_URL'];
18
        foreach($requiredVariables as $env) {
19
            if (! env($env)) {
20
                throw new \Exception('enterpriseauth setup error: missing mandatory .env value for '.$env);
21
            }
22
        }
23
24
        // Actually I have my own oauth token cache based authentication guard now lol
25
        config(['auth.guards.api.driver' => 'oauthtoken']);
26
        Auth::extend('oauthtoken', function ($app, $name, array $config) {
27
            return new OauthTokenGuard(Auth::createUserProvider($config['provider']), $app->make('request'));
0 ignored issues
show
Bug introduced by
The type Metaclassing\EnterpriseAuth\OauthTokenGuard was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
        });
29
30
        // Make sure that this vendor dir and the routes dir are in any scanned paths for swagger documentation
31
        $swaggerScanPaths = config('l5-swagger.paths.annotations');
32
        if (! is_array($swaggerScanPaths)) {
33
            $swaggerScanPaths = [$swaggerScanPaths];
34
        }
35
        if (! in_array(base_path('routes'), $swaggerScanPaths)) {
36
            $swaggerScanPaths[] = base_path('routes');
37
        }
38
        if (! in_array(__DIR__.'/../routes/', $swaggerScanPaths)) {
39
            $swaggerScanPaths[] = __DIR__.'/../routes/';
40
        }
41
        config(['l5-swagger.paths.annotations' => $swaggerScanPaths]);
42
43
        // Make sure the publish command picks up our config, migration, user model, and dummy API route files
44
        $this->publishes([
45
            __DIR__.'/../publish/config/enterpriseauth.php'                                                => config_path('enterpriseauth.php'),
46
            __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',
47
            __DIR__.'/../publish/app/User.php'                                                             => app_path().'/User.php',
48
            __DIR__.'/../publish/routes/api.php'                                                           => base_path('routes').'/api.php',
49
        ]);
50
51
        // Merge configs with the default configs
52
        $this->mergeConfigFrom(
53
            __DIR__.'/../publish/config/enterpriseauth.php', 'enterpriseauth'
54
        );
55
56
        // Load our HTTP routes for API and WEB authentication
57
        $this->loadRoutesFrom(__DIR__.'/../routes/api.microsoft.php');
58
        $this->loadRoutesFrom(__DIR__.'/../routes/web.microsoft.php');
59
60
        // Trigger generating our swagger oauth security settings based on application env file contents
61
        $this->generateSwaggerOauthSecurityScheme();
62
    }
63
64
    protected function generateSwaggerOauthSecurityScheme()
65
    {
66
        // If the routes files for the swagger oauth config is NOT present, and we have all the right info, then generate it really quick
67
        $swaggerAzureadFile = __DIR__.'/../routes/swagger.azuread.php';
68
        if (! file_exists($swaggerAzureadFile)) {
69
            $aad = new AzureActiveDirectory(env('AZURE_AD_TENANT'));
70
            //$authorizationUrl = $aad->authorizationEndpoint . '?resource=https://graph.microsoft.com';
71
            $authorizationUrl = $aad->authorizationEndpoint;
72
            $client_id = env('AZURE_AD_CLIENT_ID');
73
            $contents = <<<EOF
74
<?php
75
/**
76
 * @SWG\SecurityScheme(
77
 *   securityDefinition="AzureAD",
78
 *   type="oauth2",
79
 *   authorizationUrl="$authorizationUrl",
80
 *   flow="implicit",
81
 *   scopes={
82
 *       "https://graph.microsoft.com/.default": "Use client_id: $client_id"
83
 *   }
84
 * )
85
 **/
86
EOF;
87
            file_put_contents($swaggerAzureadFile, $contents);
88
        }
89
    }
90
91
}
92