Completed
Push — master ( b0c080...f7d773 )
by Damien
05:39
created

Saml::getSettingsResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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