-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuthorizationCodeSession.php
87 lines (75 loc) · 2.5 KB
/
AuthorizationCodeSession.php
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace EasyBib\OAuth2\Client\AuthorizationCodeGrant;
use EasyBib\OAuth2\Client\AuthorizationCodeGrant\Authorization\AuthorizationResponse;
use EasyBib\OAuth2\Client\AuthorizationCodeGrant\State\StateMismatchException;
use EasyBib\OAuth2\Client\AuthorizationCodeGrant\State\StateStore;
use EasyBib\OAuth2\Client\TokenStore;
use GuzzleHttp\ClientInterface;
use Symfony\Component\HttpFoundation\Session\Session;
class AuthorizationCodeSession extends AbstractSession
{
/**
* @var StateStore
*/
private $stateStore;
/**
* @param ClientInterface $httpClient
* @param RedirectorInterface $redirector
* @param ClientConfig $clientConfig
* @param ServerConfig $serverConfig
*/
public function __construct(
ClientInterface $httpClient,
RedirectorInterface $redirector,
ClientConfig $clientConfig,
ServerConfig $serverConfig
) {
$this->httpClient = $httpClient;
$this->redirector = $redirector;
$this->clientConfig = $clientConfig;
$this->serverConfig = $serverConfig;
$this->tokenStore = new TokenStore(new Session());
$this->stateStore = new StateStore(new Session());
}
public function setStateStore(StateStore $stateStore)
{
$this->stateStore = $stateStore;
}
/**
* @param AuthorizationResponse $authResponse
* @throws StateMismatchException
*/
public function handleAuthorizationResponse(AuthorizationResponse $authResponse)
{
if (!$this->stateStore->validateResponse($authResponse)) {
throw new StateMismatchException('State does not match');
}
$tokenRequest = new TokenRequest(
$this->clientConfig,
$this->serverConfig,
$this->httpClient,
$authResponse
);
$tokenResponse = $tokenRequest->send();
$this->tokenStore->updateFromTokenResponse($tokenResponse);
}
/**
* @return string
*/
protected function getAuthorizeUrl()
{
$params = [
'response_type' => 'code',
'state' => $this->stateStore->getState(),
] + $this->clientConfig->getParams();
if ($this->scope) {
$params += $this->scope->getQuerystringParams();
}
return vsprintf('%s%s%s%s', [
$this->httpClient->getConfig('base_uri'),
$this->serverConfig->getParams()['authorization_endpoint'],
'?',
http_build_query($params),
]);
}
}