aboutsummaryrefslogtreecommitdiff
path: root/routes/integration/auth-request.php
blob: 51b54d885aba6a8294a2e159535b3a9da7826b6e (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
<?php

// nginx (and others?) auth_request style endpoint
// returns 200 for allowed requests
// otherwise, returns 401 and sends the login page url as a header

function addHeader(array $config, string $baseName, string $value): void {
    $prefix = $config['integration']['header-prefix'];
    header("$prefix-$baseName: $value");
}

function GET(array $config) {
    $session = Psso\Session::get();
    $identity = $session->getIdentity();
    if (isset($identity)) {
        // ok
        addHeader($config, 'User', $identity->user);
        addHeader($config, 'Groups', implode(',', $identity->groups));
        foreach ($identity->extras as $key => $value) {
            if ($value !== null) addHeader($config, ucfirst($key), $value);
        }
        return;
    }

    http_response_code(401);
    addHeader(
        $config, 'Location',
        'https://' . $config['site']['primary-domain'] . '/login'
    );
}