1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AdvancedLearning\Oauth2Server\Controllers; |
4
|
|
|
|
5
|
|
|
use AdvancedLearning\Oauth2Server\Repositories\AccessTokenRepository; |
6
|
|
|
use AdvancedLearning\Oauth2Server\Repositories\ClientRepository; |
7
|
|
|
use AdvancedLearning\Oauth2Server\Repositories\ScopeRepository; |
8
|
|
|
use DateInterval; |
9
|
|
|
use Exception; |
10
|
|
|
use GuzzleHttp\Psr7\Response; |
11
|
|
|
use GuzzleHttp\Psr7\ServerRequest; |
12
|
|
|
use League\OAuth2\Server\AuthorizationServer; |
13
|
|
|
use League\OAuth2\Server\Grant\ClientCredentialsGrant; |
14
|
|
|
use Robbie\Psr7\HttpRequestAdapter; |
15
|
|
|
use Robbie\Psr7\HttpResponseAdapter; |
16
|
|
|
use SilverStripe\Control\Controller; |
17
|
|
|
use SilverStripe\Control\Director; |
18
|
|
|
use SilverStripe\Control\HTTPRequest; |
19
|
|
|
use SilverStripe\Control\HTTPResponse; |
20
|
|
|
use SilverStripe\Core\Environment; |
21
|
|
|
|
22
|
|
|
class AuthoriseController extends Controller |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Handles authorisation. |
26
|
|
|
* |
27
|
|
|
* @return HTTPResponse |
28
|
|
|
*/ |
29
|
|
|
public function index(): HTTPResponse |
30
|
|
|
{ |
31
|
|
|
$psrRequest = (new HttpRequestAdapter())->toPsr7($this->getRequest()); |
32
|
|
|
$psrResponse = new Response(); |
33
|
|
|
|
34
|
|
|
$authServer = $this->getAuthorisationServer(); |
35
|
|
|
|
36
|
|
|
try { |
37
|
|
|
return (new HttpResponseAdapter()) |
38
|
|
|
->fromPsr7($authServer->respondToAccessTokenRequest($psrRequest, $psrResponse)); |
39
|
|
|
} catch (Exception $e) { |
40
|
|
|
return new HTTPResponse($e->getMessage(), 500); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Gets the OAuth2 AuthorizationServer. |
46
|
|
|
* |
47
|
|
|
* @return AuthorizationServer |
48
|
|
|
*/ |
49
|
|
|
protected function getAuthorisationServer(): AuthorizationServer |
50
|
|
|
{ |
51
|
|
|
// Init our repositories |
52
|
|
|
$clientRepository = new ClientRepository(); // instance of ClientRepositoryInterface |
53
|
|
|
$scopeRepository = new ScopeRepository(); // instance of ScopeRepositoryInterface |
54
|
|
|
$accessTokenRepository = new AccessTokenRepository(); // instance of AccessTokenRepositoryInterface |
55
|
|
|
|
56
|
|
|
// Path to public and private keys |
57
|
|
|
$privateKey = Environment::getEnv('OAUTH_PRIVATE_KEY_PATH'); |
58
|
|
|
// inject base bath if necessary |
59
|
|
|
$privateKey = str_replace('{BASE_DIR}', Director::baseFolder(), $privateKey); |
60
|
|
|
|
61
|
|
|
$encryptionKey = Environment::getEnv('OAUTH_ENCRYPTION_KEY'); |
62
|
|
|
|
63
|
|
|
// Setup the authorization server |
64
|
|
|
$server = new AuthorizationServer( |
65
|
|
|
$clientRepository, |
66
|
|
|
$accessTokenRepository, |
67
|
|
|
$scopeRepository, |
68
|
|
|
$privateKey, |
69
|
|
|
$encryptionKey |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
// Enable the client credentials grant on the server |
73
|
|
|
$server->enableGrantType( |
74
|
|
|
new ClientCredentialsGrant(), |
75
|
|
|
new DateInterval('PT1H') // access tokens will expire after 1 hour |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
return $server; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|