Completed
Push — master ( 4b394d...0ca4f0 )
by Damien
09:55
created

Saml2Container::generateId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 0
1
<?php
2
3
4
namespace flipbox\saml\core\containers;
5
6
use flipbox\saml\core\AbstractPlugin;
7
use flipbox\saml\core\EnsureSAMLPlugin;
8
use flipbox\saml\core\helpers\MessageHelper;
9
use flipbox\saml\core\helpers\SerializeHelper;
10
use SAML2\Compat\AbstractContainer;
11
use flipbox\craft\psr3\Logger;
12
13
class Saml2Container extends AbstractContainer implements EnsureSAMLPlugin
14
{
15
16
    const TEMPLATE_PATH = 'saml-core/_components/post-binding-submit.twig';
17
    /**
18
     * @var \Psr\Log\LoggerInterface
19
     */
20
    protected $logger;
21
22
23
    /**
24
     * @var AbstractPlugin
25
     */
26
    protected $plugin;
27
28
    /**
29
     * Create a new SimpleSAMLphp compatible container.
30
     */
31
    public function __construct(AbstractPlugin $plugin)
32
    {
33
        $this->logger = new Logger();
34
        $this->plugin = $plugin;
35
    }
36
37
    public function getPlugin(): AbstractPlugin
38
    {
39
        return $this->plugin;
40
    }
41
42
43
    /**
44
     * {@inheritdoc}
45
     * @return \Psr\Log\LoggerInterface
46
     */
47
    public function getLogger()
48
    {
49
        return $this->logger;
50
    }
51
52
53
    /**
54
     * {@inheritdoc}
55
     * @return string
56
     */
57
    public function generateId()
58
    {
59
        return MessageHelper::generateId();
60
    }
61
62
63
    /**
64
     * {@inheritdoc}
65
     * @return void
66
     */
67
    public function debugMessage($message, $type)
68
    {
69
        if($message instanceof \DOMDocument || $message instanceof \DOMElement) {
70
            $message = $message->ownerDocument->saveXML();
71
        }
72
73
        $this->getLogger()->debug($message, ['type' => $type]);
74
    }
75
76
77
    /**
78
     * {@inheritdoc}
79
     * @param string $url
80
     * @param array $data
81
     * @return void
82
     */
83
    public function redirect($url, $data = [])
84
    {
85
86
        $url = SerializeHelper::redirectUrl($url, $data);
87
88
        \Craft::$app->response->redirect($url);
89
90
        // show a minimal web page with a clickable link to the URL
91
        echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
92
        echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"';
93
        echo ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' . "\n";
94
        echo '<html xmlns="http://www.w3.org/1999/xhtml">' . "\n";
95
        echo "  <head>\n";
96
        echo '    <meta http-equiv="content-type" content="text/html; charset=utf-8">' . "\n";
97
        echo '    <meta http-equiv="refresh" content="0;URL=\'' . htmlspecialchars($url) . '\'">' . "\n";
98
        echo "    <title>Redirect</title>\n";
99
        echo "  </head>\n";
100
        echo "  <body>\n";
101
        echo "    <h1>Redirect</h1>\n";
102
        echo '      <p>You were redirected to: <a id="redirlink" href="' . htmlspecialchars($url) . '">';
103
        echo htmlspecialchars($url) . "</a>\n";
104
        echo '        <script type="text/javascript">document.getElementById("redirlink").focus();</script>' . "\n";
105
        echo "      </p>\n";
106
        echo "  </body>\n";
107
        echo '</html>';
108
109
        \Craft::$app->end();
110
    }
111
112
113
    /**
114
     * {@inheritdoc}
115
     * @param string $url
116
     * @param array $data
117
     * @return void
118
     */
119
    public function postRedirect($url, $data = [])
120
    {
121
122
        $data['destination'] = $url;
123
124
        $view = \Craft::$app->getView();
125
        $view->setTemplateMode($view::TEMPLATE_MODE_CP);
126
        \Craft::$app->response->data = $view->renderTemplate(
127
            $this->getTemplatePath(),
128
            $data
129
        );
130
        \Craft::$app->response->send();
131
        \Craft::$app->end();
132
    }
133
134
    /**
135
     * SAML Plugin Utils
136
     */
137
138
    /**
139
     * @return string
140
     */
141
    protected function getTemplatePath()
142
    {
143
        return static::TEMPLATE_PATH;
144
    }
145
146
}