Completed
Push — master ( 0f5e6b...d874fd )
by Conrad
01:54
created

src/Controllers/AuthoriseController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace AdvancedLearning\Oauth2Server\Controllers;
4
5
use AdvancedLearning\Oauth2Server\AuthorizationServer\Generator;
6
use Exception;
7
use GuzzleHttp\Psr7\Response;
8
use League\OAuth2\Server\Exception\OAuthServerException;
9
use Psr\Http\Message\ResponseInterface;
10
use Robbie\Psr7\HttpRequestAdapter;
11
use Robbie\Psr7\HttpResponseAdapter;
12
use SilverStripe\Control\Controller;
13
use SilverStripe\Control\HTTP;
14
use SilverStripe\Control\HTTPResponse;
15
16
class AuthoriseController extends Controller
17
{
18
    /**
19
     * @var Generator
20
     */
21
    protected $serverGenerator;
22
23
    /**
24
     * AuthoriseController constructor. If no Authorization Service is passed a default one is created.
25
     *
26
     * @param Generator $serverGenerator
27
     */
28
    public function __construct(Generator $serverGenerator)
29
    {
30
        $this->serverGenerator = $serverGenerator;
31
        parent::__construct();
32
    }
33
34
    /**
35
     * Handles authorisation.
36
     *
37
     * @return HTTPResponse
38
     */
39
    public function index(): HTTPResponse
40
    {
41
        $body = null;
42
43
        if ($this->getRequest()->getHeader('Content-Type') === 'application/json') {
44
            $body = json_decode($this->getRequest()->getBody(), true);
45
        } else {
46
            $body = $this->getRequest()->postVars();
47
        }
48
49
        if (empty($body)) {
50
            return $this->jsonResponse(['error' => 'No parameters could be found in request body'], 500);
0 ignored issues
show
Documentation Bug introduced by
The method jsonResponse does not exist on object<AdvancedLearning\...rs\AuthoriseController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
51
        }
52
53
        // request needs parsed body
54
        $psrRequest = (new HttpRequestAdapter())->toPsr7($this->getRequest())
55
            ->withParsedBody($body);
56
        $psrResponse = new Response();
57
58
        $authServer = $this->serverGenerator->getServer();
59
60
        try {
61
            return (new HttpResponseAdapter())
62
                ->fromPsr7($authServer->respondToAccessTokenRequest($psrRequest, $psrResponse));
63
        } catch (OAuthServerException $e) {
64
            return $this->convertResponse($e->generateHttpResponse(new Response()));
65
        } catch (Exception $e) {
66
            return $this->getErrorResponse($e->getMessage());
67
        }
68
    }
69
70
    protected function getErrorResponse($message, $responseCode = 500)
71
    {
72
        $response = (new OAuthServerException($message, 100, 'server_error', $responseCode))
73
            ->generateHttpResponse(new Response());
74
75
        return $this->convertResponse($response);
76
    }
77
78
    protected function convertResponse(ResponseInterface $response)
79
    {
80
        return (new HttpResponseAdapter())->fromPsr7($response);
81
    }
82
}
83