Completed
Push — master ( 1223fc...6af157 )
by Damien
05:06
created

Saml::onRegisterSiteUrlRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 0
cts 22
cp 0
rs 9.472
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * @copyright  Copyright (c) Flipbox Digital Limited
4
 * @license    https://flipboxfactory.com/software/saml-sp/license
5
 * @link       https://www.flipboxfactory.com/software/saml-sp/
6
 */
7
8
namespace flipbox\saml\sp;
9
10
use Craft;
11
use craft\console\Application as ConsoleApplication;
12
use craft\events\RegisterComponentTypesEvent;
13
use craft\events\RegisterUrlRulesEvent;
14
use craft\services\Fields;
15
use craft\web\UrlManager;
16
use flipbox\saml\core\models\SettingsInterface;
17
use flipbox\saml\core\SamlPluginInterface;
18
use flipbox\saml\core\AbstractPlugin;
19
use flipbox\saml\sp\fields\ExternalIdentity;
20
use flipbox\saml\sp\models\Settings;
21
use flipbox\saml\sp\services\Cp;
22
use flipbox\saml\sp\services\messages\AuthnRequest;
23
use flipbox\saml\sp\services\messages\LogoutRequest;
24
use flipbox\saml\sp\services\messages\LogoutResponse;
25
use flipbox\saml\sp\services\messages\Metadata;
26
use flipbox\saml\sp\services\messages\Response;
27
use flipbox\saml\sp\services\bindings\HttpPost;
28
use flipbox\saml\sp\services\bindings\HttpRedirect;
29
use flipbox\saml\sp\services\Login;
30
use flipbox\saml\sp\services\Provider;
31
use flipbox\saml\sp\services\ProviderIdentity;
32
use flipbox\saml\core\services\Session;
33
use yii\base\Event;
34
35
/**
36
 * Class Saml
37
 * @package flipbox\saml\sp
38
 */
39
class Saml extends AbstractPlugin implements SamlPluginInterface
40
{
41
    /**
42
     * @inheritdoc
43
     */
44
    public function init()
45
    {
46
        parent::init();
47
48
        $this->initComponents();
49
        $this->initEvents();
50
51
        // Switch target to console controllers
52
        if (Craft::$app instanceof ConsoleApplication) {
53
            $this->controllerNamespace = __NAMESPACE__ . '\cli';
54
            $this->controllerMap = [
55
                'metadata' => \flipbox\saml\sp\cli\Metadata::class,
56
                'keychain' => \flipbox\saml\sp\cli\KeyChain::class,
57
            ];
58
        }
59
    }
60
61
    /**
62
     * Events
63
     */
64
    protected function initEvents()
65
    {
66
        /**
67
         * CP routes
68
         */
69
        Event::on(
70
            UrlManager::class,
71
            UrlManager::EVENT_REGISTER_CP_URL_RULES,
72
            [self::class, 'onRegisterCpUrlRules']
73
        );
74
75
76
        /**
77
         * Clean Frontend Endpoints
78
         */
79
        Event::on(
80
            UrlManager::class,
81
            UrlManager::EVENT_REGISTER_SITE_URL_RULES,
82
            [static::class, 'onRegisterSiteUrlRules']
83
        );
84
85
        Event::on(
86
            Fields::class,
87
            Fields::EVENT_REGISTER_FIELD_TYPES,
88
            function (RegisterComponentTypesEvent $event) {
89
                $event->types[] = ExternalIdentity::class;
90
            }
91
        );
92
    }
93
94
    /**
95
     * Components
96
     */
97
    public function initComponents()
98
    {
99
        $this->setComponents(
100
            [
101
                'authnRequest'     => AuthnRequest::class,
102
                'httpPost'         => HttpPost::class,
103
                'httpRedirect'     => HttpRedirect::class,
104
                'login'            => Login::class,
105
                'logoutRequest'    => LogoutRequest::class,
106
                'logoutResponse'   => LogoutResponse::class,
107
                'provider'         => Provider::class,
108
                'providerIdentity' => ProviderIdentity::class,
109
                'metadata'         => Metadata::class,
110
                'response'         => Response::class,
111
                'session'          => Session::class,
112
                'cp'               => Cp::class,
113
            ]
114
        );
115
    }
116
117
    /**
118
     * @param RegisterUrlRulesEvent $event
119
     */
120
    public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event)
121
    {
122
        $event->rules = array_merge(
123
            $event->rules,
124
            [
125
                'saml-sp/'                          => 'saml-sp/cp/view/general/setup',
126
                'saml-sp/settings'                  => 'saml-sp/cp/view/general/settings',
127
128
                /**
129
                 * Keychain
130
                 */
131
                'saml-sp/keychain'                  => 'saml-sp/cp/view/keychain/index',
132
                'saml-sp/keychain/new'              => 'saml-sp/cp/view/keychain/edit',
133
                'saml-sp/keychain/new-openssl'      => 'saml-sp/cp/view/keychain/edit/openssl',
134
                'saml-sp/keychain/<keypairId:\d+>'  => 'saml-sp/cp/view/keychain/edit',
135
136
                /**
137
                 * Metadata
138
                 */
139
                'saml-sp/metadata'                  => 'saml-sp/cp/view/metadata/default',
140
                'saml-sp/metadata/new'              => 'saml-sp/cp/view/metadata/edit',
141
                'saml-sp/metadata/new-idp'          => 'saml-sp/cp/view/metadata/edit/new-idp',
142
                'saml-sp/metadata/new-sp'           => 'saml-sp/cp/view/metadata/edit/new-sp',
143
                'saml-sp/metadata/my-provider'      => 'saml-sp/cp/view/metadata/edit/my-provider',
144
                'saml-sp/metadata/<providerId:\d+>' => 'saml-sp/cp/view/metadata/edit',
145
            ],
146
            static::getInstance()->getSettings()->enableCpLoginButtons ?
147
                [
148
                    'login' => 'saml-sp/cp/view/login',
149
                ] : []
150
        );
151
    }
152
153
    /**
154
     * @param RegisterUrlRulesEvent $event
155
     */
156
    public static function onRegisterSiteUrlRules(RegisterUrlRulesEvent $event)
157
    {
158
        $event->rules = array_merge(
159
            $event->rules,
160
            [
161
                /**
162
                 * LOGIN
163
                 */
164
                'POST,GET /sso/login'  => 'saml-sp/login',
165
                sprintf(
166
                    'GET %s',
167
                    (string)static::getInstance()->getSettings()->loginRequestEndpoint
168
                )                      => 'saml-sp/login/request',
169
                sprintf(
170
                    'GET %s/<uid:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}>',
171
                    (string)static::getInstance()->getSettings()->loginRequestEndpoint
172
                )                      => 'saml-sp/login/request',
173
                /**
174
                 * LOGOUT
175
                 */
176
                'POST,GET /sso/logout' => 'saml-sp/logout',
177
                sprintf(
178
                    'GET %s',
179
                    (string)static::getInstance()->getSettings()->logoutRequestEndpoint
180
                )                      => 'saml-sp/logout/request',
181
            ]
182
        );
183
    }
184
185
186
    /**
187
     * @return Settings
188
     */
189
    public function getSettings(): SettingsInterface
190
    {
191
        return parent::getSettings();
192
    }
193
194
    /**
195
     * @inheritdoc
196
     */
197
    public function createSettingsModel()
198
    {
199
        return new Settings();
200
    }
201
202
    /**
203
     * Components
204
     */
205
206
    /**
207
     * @noinspection PhpDocMissingThrowsInspection
208
     * @return AuthnRequest
209
     */
210
    public function getAuthnRequest()
211
    {
212
        /** @noinspection PhpUnhandledExceptionInspection */
213
        /** @noinspection PhpIncompatibleReturnTypeInspection */
214
        return $this->get('authnRequest');
215
    }
216
217
    /**
218
     * @noinspection PhpDocMissingThrowsInspection
219
     * @return Response
220
     */
221
    public function getResponse()
222
    {
223
        /** @noinspection PhpUnhandledExceptionInspection */
224
        /** @noinspection PhpIncompatibleReturnTypeInspection */
225
        return $this->get('response');
226
    }
227
228
    /**
229
     * @noinspection PhpDocMissingThrowsInspection
230
     * @return Login
231
     */
232
    public function getLogin()
233
    {
234
        /** @noinspection PhpUnhandledExceptionInspection */
235
        /** @noinspection PhpIncompatibleReturnTypeInspection */
236
        return $this->get('login');
237
    }
238
239
    /**
240
     * @noinspection PhpDocMissingThrowsInspection
241
     * @return Session
242
     * @throws \yii\base\InvalidConfigException
243
     */
244
    public function getSession()
245
    {
246
        /** @noinspection PhpUnhandledExceptionInspection */
247
        /** @noinspection PhpIncompatibleReturnTypeInspection */
248
        return $this->get('session');
249
    }
250
251
    /**
252
     * Util Methods
253
     */
254
255
    public function getMyType()
256
    {
257
        return static::SP;
258
    }
259
}
260