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

AzureActiveDirectory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A checkCachedOpenIdConfig() 0 12 2
A parseOpenIdConfig() 0 6 1
A __construct() 0 4 1
A getApplicationAccessToken() 0 15 1
A buildOpenIdConfigUrl() 0 6 1
A setTenantName() 0 12 3
A downloadOpenIdConfig() 0 7 1
1
<?php
2
3
namespace Metaclassing\EnterpriseAuth;
4
5
class AzureActiveDirectory
6
{
7
    // Tenant name something.onmicrosoft.com
8
    public $tenantName = '';
9
    // Azure AD base url to use
10
    public $baseUrl = 'https://login.microsoftonline.com';
11
    // Azure AD version
12
    public $version = 'v2.0';
13
    // .well-known/openid-config
14
    public $wellKnownOpenIdConfig = '.well-known/openid-configuration';
15
    // URL to download the latest openid config
16
    public $openIdConfigUrl = '';
17
    // Contents of the openid config assoc array parsed from json
18
    public $openIdConfig = [];
19
    // AAD authorization endpoint
20
    public $authorizationEndpoint = '';
21
    // AAD token endpoint
22
    public $tokenEndpoint = '';
23
    // AAD logout endpoint
24
    public $endSessionEndpoint = '';
25
26
    public function __construct($tenantName = 'common')
27
    {
28
        $this->setTenantName($tenantName);
29
        $this->parseOpenIdConfig();
30
    }
31
32
    public function setTenantName($tenantName)
33
    {
34
        // IF we are not using the common tenant
35
        if ($tenantName != 'common') {
36
            // Make sure the tenant is formatted like xyzcorp.onmicrosoft.com
37
            $regex = '/\.onmicrosoft\.com/';
38
            if (! preg_match($regex, $tenantName, $hits)) {
39
                // Append the suffix if it is missing
40
                $tenantName .= '.onmicrosoft.com';
41
            }
42
        }
43
        $this->tenantName = $tenantName;
44
    }
45
46
    public function buildOpenIdConfigUrl()
47
    {
48
        $this->openIdConfigUrl = $this->baseUrl.'/'
49
                               .$this->tenantName.'/'
50
                               .$this->version.'/'
51
                               .$this->wellKnownOpenIdConfig;
52
    }
53
54
    public function downloadOpenIdConfig()
55
    {
56
        $this->buildOpenIdConfigUrl();
57
        $guzzle = new \GuzzleHttp\Client();
58
        $response = $guzzle->get($this->openIdConfigUrl);
59
        $json = $response->getBody();
60
        $this->openIdConfig = json_decode($json, true);
61
    }
62
63
    public function parseOpenIdConfig()
64
    {
65
        $this->checkCachedOpenIdConfig();
66
        $this->authorizationEndpoint = $this->openIdConfig['authorization_endpoint'];
67
        $this->tokenEndpoint = $this->openIdConfig['token_endpoint'];
68
        $this->endSessionEndpoint = $this->openIdConfig['end_session_endpoint'];
69
    }
70
71
    public function checkCachedOpenIdConfig()
72
    {
73
        // See if we already have this tenants aad config cached
74
        $key = '/azureactivedirectory/'.$this->tenantName.'/config';
75
        if (\Cache::has($key)) {
76
            // Use the cached version if available
77
            $this->openIdConfig = \Cache::get($key);
78
        } else {
79
            // Download it if we dont have it
80
            $this->downloadOpenIdConfig();
81
            // Keep it around for 60 minutes
82
            \Cache::put($key, $this->openIdConfig, 60);
83
        }
84
    }
85
86
    public function getApplicationAccessToken($clientId, $clientSecret)
87
    {
88
        $guzzle = new \GuzzleHttp\Client();
89
        $url = $this->tokenEndpoint;
90
        $parameters = [
91
            'form_params' => [
92
                'scope'         => 'https://graph.microsoft.com/.default',
93
                'grant_type'    => 'client_credentials',
94
                'client_id'     => $clientId,
95
                'client_secret' => $clientSecret,
96
            ],
97
        ];
98
        $response = $guzzle->post($url, $parameters);
99
        $responseObject = json_decode($response->getBody());
100
        return $responseObject->access_token;
101
    }
102
103
}
104