Passed
Push — 1.10.x ( e37d9e...d684ae )
by Angel Fernando Quiroz
226:38 queued 187:01
created

AzureActiveDirectory::get_name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * AzureActiveDirectory plugin class
4
 * @author Angel Fernando Quiroz Campos <[email protected]>
5
 * @package chamilo.plugin.azure_active_directory
6
 */
7
class AzureActiveDirectory extends Plugin
8
{
9
    const SETTING_ENABLE = 'enable';
10
    const SETTING_APP_ID = 'app_id';
11
    const SETTING_TENANT = 'tenant';
12
    const SETTING_SIGNUP_POLICY = 'signup_policy';
13
    const SETTING_SIGNIN_POLICY = 'signin_policy';
14
    const SETTING_BLOCK_NAME = 'block_name';
15
    const URL_TYPE_SIGNUP = 'sign-up';
16
    const URL_TYPE_SIGNIN = 'sign-in';
17
    const URL_TYPE_SIGNOUT = 'sign-out';
18
19
    /**
20
     * AzureActiveDirectory constructor.
21
     */
22
    protected function __construct()
23
    {
24
        $settings = [
25
            self::SETTING_ENABLE => 'boolean',
26
            self::SETTING_APP_ID => 'text',
27
            self::SETTING_TENANT => 'text',
28
            self::SETTING_SIGNUP_POLICY => 'text',
29
            self::SETTING_SIGNIN_POLICY => 'text',
30
            self::SETTING_BLOCK_NAME => 'text'
31
        ];
32
33
        parent::__construct('1.0', 'Angel Fernando Quiroz Campos', $settings);
34
    }
35
36
    /**
37
     * Instance the plugin
38
     * @staticvar null $result
39
     * @return Tour
40
     */
41
    static function create()
42
    {
43
        static $result = null;
44
45
        return $result ? $result : $result = new self();
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function get_name()
52
    {
53
        return 'azure_active_directory';
54
    }
55
56
    /**
57
     * @param $urlType Type of URL to generate
58
     * @return string
59
     */
60
    public function getUrl($urlType)
61
    {
62
        $settingsInfo = $this->get_settings();
63
        $settings = [];
64
65
        foreach ($settingsInfo as $settingInfo) {
66
            $variable = str_replace($this->get_name() . '_', '', $settingInfo['variable']);
67
68
            $settings[$variable] = $settingInfo['selected_value'];
69
        }
70
71
        $url = "https://login.microsoftonline.com/{$settings[self::SETTING_TENANT]}/oauth2/v2.0/";
72
        $callback = api_get_path(WEB_PLUGIN_PATH) . $this->get_name() . '/src/callback.php';
73
74
        if ($urlType === self::URL_TYPE_SIGNOUT) {
75
            $action = 'logout';
76
            $urlParams = [
77
                'p' => $settings[self::SETTING_SIGNIN_POLICY],
78
                'post_logout_redirect_uri' => $callback
79
            ];
80
        } else {
81
            $action = 'authorize';
82
            $policy = $settings[self::SETTING_SIGNUP_POLICY];
83
84
            if ($urlType === self::URL_TYPE_SIGNIN) {
85
                $policy = $settings[self::SETTING_SIGNIN_POLICY];
86
            }
87
88
            $urlParams = [
89
                'client_id' => $settings[self::SETTING_APP_ID],
90
                'response_type' => 'id_token',
91
                'redirect_uri' => $callback,
92
                'scope' => 'openid',
93
                'response_mode' => 'form_post',
94
                'state' => time(),
95
                'nonce' => time(),
96
                'p' => $policy
97
            ];
98
        }
99
100
        return $url . $action . '?' . http_build_query($urlParams);
101
    }
102
}