aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/restapi/consumer/HTTP.php
blob: 6d66c23a3b24621eb4931de4bff1b0f65c47e380 (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
<?php
namespace RESTAPI\Consumer;
use StudipAuthAbstract, RESTAPI\RouterException;

/**
 * Basic HTTP Authentication consumer for the rest api
 *
 * @author     Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license    GPL 2 or later
 * @since      Stud.IP 3.0
 * @deprecated Since Stud.IP 5.0. Will be removed in Stud.IP 6.0.
 */
class HTTP extends Base
{
    /**
     * Detects if a user is authenticated via basic http authentication.
     * The only supported authentication for now is via the url:
     *
     * http://username:password@host/path?query
     *
     * @param mixed $request_type Type of request (optional; defaults to any)
     * @return mixed Instance of self if authentication was detected, false
     *               otherwise
     * @throws RouterException if authentication fails
     * @todo Integrate and test HTTP_AUTHORIZATION header authentication
     */
    public static function detect($request_type = null)
    {
        if (
            isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])
            || isset($_SERVER['HTTP_AUTHORIZATION'])
        ) {
            $user_id = false;
            $username = '';
            $password = '';
            if (isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) {
                $username = $_SERVER['PHP_AUTH_USER'];
                $password = $_SERVER['PHP_AUTH_PW'];
            } elseif (isset($_SERVER['HTTP_AUTHORIZATION'])) {
                $chunks = explode(':', base64_decode(mb_substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
                $username = $chunks[0] ?? '';
                $password = $chunks[1] ?? '';
            }

            $check = StudipAuthAbstract::CheckAuthentication($username, $password);
            if ($check['uid'] && $check['uid'] !== 'nobody') {
                return new self(null, $check['uid']);
            }

        }
        return false;
    }
}