aboutsummaryrefslogtreecommitdiff
path: root/vendor/edu-sharing-plugin/edu-sharing-auth-helper.php
blob: ef9dec1f8f0355f434c2cc086857911a4360be26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
require_once "edu-sharing-helper-abstract.php";

class EduSharingAuthHelper extends EduSharingHelperAbstract  {

    /**
     * Gets detailed information about a ticket
     * Will throw an exception if the given ticket is not valid anymore
     * @param string $ticket
     * The ticket, obtained by @getTicketForUser
     * @return array
     * Detailed information about the current session
     * @throws Exception
     * Thrown if the ticket is not valid anymore
     */
    public function getTicketAuthenticationInfo(string $ticket) {
        $curl = curl_init($this->base->baseUrl . '/rest/authentication/v1/validateSession');
        curl_setopt_array($curl, [
            CURLOPT_HTTPHEADER => [
                $this->getRESTAuthenticationHeader($ticket),
                'Accept: application/json',
                'Content-Type: application/json',
            ],
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_CONNECTTIMEOUT => 5,
            CURLOPT_TIMEOUT => 5
        ]);
        if ($this->base->http_proxy) {
            curl_setopt($curl, CURLOPT_PROXY, $this->base->http_proxy);
        }
        $data = json_decode(curl_exec($curl), true);
        curl_close($curl);
        if ( is_null( $data ) ) {
            throw new Exception( 'No answer from repository. Possibly a timeout while trying to connect' );
        }
        if($data['statusCode'] !== 'OK') {
            throw new Exception('The given ticket is not valid anymore');
        }
        return $data;
    }

    /**
     * Fetches the edu-sharing ticket for a given username
     * @param string $username
     * The username you want to generate a ticket for
     * @return string
     * The ticket, which you can use as an authentication header, see @getRESTAuthenticationHeader
     * @throws Exception
     */
    public function getTicketForUser(string $username, $bodyparams = null) {
        if ($bodyparams === null) {
            $bodyparams = [
                "primaryAffiliation" => "employee",
                "skills" => [
                    "string"
                ],
                "types" => [
                    "string"
                ],
                "extendedAttributes" => [
                    'affiliation' => ["employee"]
                ],
                "vcard" => "string",
                "firstName" => User::findCurrent()->vorname,
                "lastName" => User::findCurrent()->nachname,
                "email" => User::findCurrent()->email,
                "avatar" => "string",
                "about" => "string"
            ];
        }
        $curl = curl_init($this->base->baseUrl . '/rest/authentication/v1/appauth/' . rawurlencode($username));
        curl_setopt_array($curl, [
            CURLOPT_POST => 1,
            CURLOPT_FAILONERROR => false,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_HTTPHEADER => $this->getSignatureHeaders($username),
            CURLOPT_CONNECTTIMEOUT => 5,
            CURLOPT_TIMEOUT => 5
        ]);
        curl_setopt(
            $curl,
            CURLOPT_POSTFIELDS,
            is_array($bodyparams) ? json_encode($bodyparams) : (string) $bodyparams
        );
        if ($this->base->http_proxy) {
            curl_setopt($curl, CURLOPT_PROXY, $this->base->http_proxy);
        }

        $output = curl_exec($curl);
        $data = json_decode($output, true);

        $err     = curl_errno( $curl );
        $info = curl_getinfo($curl);
        curl_close($curl);
        if ($err === 0 && $info["http_code"] === 200 && $data['userId'] === $username) {
            return $data['ticket'];
        } else {
            if ( is_null( $data ) ) {
                $data = ['error' => $output];
            }
            throw new Exception(
                'edu-sharing ticket could not be retrieved: HTTP-Code ' .
                $info["http_code"] . ': ' . $data['error']
            );
        }
    }
}