aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/api/oauth2/oauth2_controller.php
blob: e9208fce3aa26ec60aeeff7f9a9ec1d7b0d96123 (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
<?php

use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\Exception\OAuthServerException;
use Studip\OAuth2\NegotiatesWithPsr7;

abstract class OAuth2Controller extends StudipController
{
    use NegotiatesWithPsr7;

    /**
     * @return void
     */
    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        $this->set_layout(null);

        $this->container = new Studip\OAuth2\Container();
        $this->server = $this->getAuthorizationServer();
    }

    /**
     * Exception handler called when the performance of an action raises an
     * exception.
     *
     * @param Exception $exception the thrown exception
     */
    public function rescue($exception)
    {
        if ($exception instanceof OAuthServerException) {
            $psrResponse = $exception->generateHttpResponse($this->getPsrResponse());

            return $this->convertPsrResponse($psrResponse);
        }

        return new Trails\Response($exception->getMessage(), [], 500);
    }

    protected function getAuthorizationServer(): AuthorizationServer
    {
        return $this->container->get(AuthorizationServer::class);
    }
}