1<?php
2
3/**
4 * SAML authentication plugin
5 *
6 * @author     Andreas Gohr <gohr@cosmocode.de>
7 * @author     Sam Yelman <sam.yelman@temple.edu>
8 */
9class action_plugin_saml extends DokuWiki_Action_Plugin
10{
11
12    /** @inheritdoc */
13    public function register(Doku_Event_Handler $controller)
14    {
15        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_request');
16        $controller->register_hook('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'handle_loginform');
17    }
18
19    /**
20     * Send the Federation Metadata about this Service Provider
21     * Otherwise, handle Logout for SAML plugin
22     *
23     * @param Doku_Event $event
24     * @param mixed $param
25     */
26    public function handle_request(Doku_Event $event, $param)
27    {
28        global $ID;
29		global $auth;
30        $act = act_clean($event->data);
31		if($act == "logout" && $this->getConf('use_slo') &&
32			(isset($_GET["SAMLResponse"]) || isset($_GET["SAMLRequest"]))) {
33			$auth->logOff();
34		}
35        if ($act != 'saml') return;
36        $event->preventDefault();
37        $event->stopPropagation();
38
39        /** @var helper_plugin_saml $hlp */
40        $hlp = plugin_load('helper', 'saml');
41        $saml = $hlp->getSamlLib();
42
43
44
45        try {
46            header('Content-Type: application/samlmetadata+xml');
47            header('Content-Disposition: attachment; filename="saml-metadata.xml"');
48            $xml = $saml->getSettings()->getSPMetadata();
49            echo $xml;
50            exit();
51        } catch (Exception $e) {
52            die(hsc($e->getMessage()));
53        }
54    }
55
56    /**
57     * Disable the login forma and instead use a link to trigger login
58     *
59     * @param Doku_Event $event
60     * @param $param
61     */
62    public function handle_loginform(Doku_Event $event, $param)
63    {
64        global $ID;
65        global $conf;
66        if ($conf['authtype'] != 'saml') return;
67
68        $event->data = new Doku_Form(array());
69        $event->data->addElement('<a href="' . wl($ID, array('do' => 'login')) . '">Login here</a>');
70    }
71}
72