Skip to content

Commit edd0a94

Browse files
authored
feat: added device code manager
2 parents 9212097 + cf9ba50 commit edd0a94

13 files changed

+695
-125
lines changed
Lines changed: 11 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
<?php
22

3-
namespace FattureInCloud\OAuth2;
3+
namespace FattureInCloud\OAuth2\OAuth2AuthorizationCode;
44

5+
use FattureInCloud\OAuth2\OAuth2Error;
6+
use FattureInCloud\OAuth2\OAuth2Manager;
7+
use FattureInCloud\OAuth2\OAuth2TokenResponse;
58
use GuzzleHttp\Client;
69

710
/**
811
* The Manager for OAuth2 Authorization Code flow.
912
*/
10-
class OAuth2AuthorizationCodeManager
13+
class OAuth2AuthorizationCodeManager extends OAuth2Manager
1114
{
1215
private const DEFAULT_BASE_URI = 'https://api-v2.fattureincloud.it';
1316

14-
/**
15-
* @var Client
16-
*/
17-
private $client;
18-
/**
19-
* @var string
20-
*/
21-
private $clientId;
2217
/**
2318
* @var string
2419
*/
@@ -27,10 +22,6 @@ class OAuth2AuthorizationCodeManager
2722
* @var string
2823
*/
2924
private $redirectUri;
30-
/**
31-
* @var string|null
32-
*/
33-
private $baseUri;
3425

3526
/**
3627
* @param string $clientId
@@ -41,51 +32,9 @@ class OAuth2AuthorizationCodeManager
4132
*/
4233
public function __construct(string $clientId, string $clientSecret, string $redirectUri, string $baseUri = self::DEFAULT_BASE_URI, Client $client = null)
4334
{
44-
if ($client === null) {
45-
$this->client = new Client();
46-
} else {
47-
$this->client = $client;
48-
}
49-
$this->clientId = $clientId;
35+
parent::__construct($clientId,$baseUri, $client);
5036
$this->clientSecret = $clientSecret;
5137
$this->redirectUri = $redirectUri;
52-
$this->baseUri = $baseUri;
53-
}
54-
55-
/**
56-
* @return Client
57-
*/
58-
public function getClient(): Client
59-
{
60-
return $this->client;
61-
}
62-
63-
/**
64-
* @param Client|null $client
65-
*/
66-
public function setClient(Client $client = null): void
67-
{
68-
if ($client === null) {
69-
$this->client = new Client();
70-
} else {
71-
$this->client = $client;
72-
}
73-
}
74-
75-
/**
76-
* @return string
77-
*/
78-
public function getClientId(): string
79-
{
80-
return $this->clientId;
81-
}
82-
83-
/**
84-
* @param string $clientId
85-
*/
86-
public function setClientId(string $clientId): void
87-
{
88-
$this->clientId = $clientId;
8938
}
9039

9140
/**
@@ -120,26 +69,6 @@ public function setRedirectUri(string $redirectUri): void
12069
$this->redirectUri = $redirectUri;
12170
}
12271

123-
/**
124-
* @return string
125-
*/
126-
public function getBaseUri(): ?string
127-
{
128-
if ($this->baseUri === null) {
129-
return self::DEFAULT_BASE_URI;
130-
} else {
131-
return $this->baseUri;
132-
}
133-
}
134-
135-
/**
136-
* @param string|null $baseUri
137-
*/
138-
public function setBaseUri(?string $baseUri): void
139-
{
140-
$this->baseUri = $baseUri;
141-
}
142-
14372
/**
14473
* @param array $scopes
14574
* @param string $state
@@ -172,7 +101,8 @@ public function getParamsFromUrl(string $url): OAuth2AuthorizationCodeParams
172101
}
173102

174103
/**
175-
* @param string $url
104+
* @param string $code
105+
* @return OAuth2Error|OAuth2TokenResponse
176106
*/
177107
public function fetchToken(string $code)
178108
{
@@ -184,11 +114,12 @@ public function fetchToken(string $code)
184114
'redirect_uri' => $this->redirectUri,
185115
'code' => $code
186116
];
187-
return $this->executePost($tokenUri, $body);
117+
return $this->executeTokenPost($tokenUri, $body);
188118
}
189119

190120
/**
191121
* @param string $refreshToken
122+
* @return OAuth2Error|OAuth2TokenResponse
192123
*/
193124
public function refreshToken(string $refreshToken)
194125
{
@@ -199,19 +130,6 @@ public function refreshToken(string $refreshToken)
199130
'client_secret' => $this->clientSecret,
200131
'refresh_token' => $refreshToken,
201132
];
202-
return $this->executePost($tokenUri, $body);
203-
}
204-
205-
private function executePost(string $uri, array $body)
206-
{
207-
$r = $this->client->post($uri, ['json' => $body]);
208-
$statusCode = $r->getStatusCode();
209-
$resBodyJson = $r->getBody()->getContents();
210-
$resBody = json_decode($resBodyJson, true);
211-
if ($statusCode === 200) {
212-
return new OAuth2AuthorizationCodeTokenResponse($resBody['token_type'], $resBody['access_token'], $resBody['refresh_token'], $resBody['expires_in']);
213-
} else {
214-
return new OAuth2AuthorizationCodeError($statusCode, $resBody['error'], $resBody['error_description']);
215-
}
133+
return $this->executeTokenPost($tokenUri, $body);
216134
}
217135
}

lib/OAuth2/OAuth2AuthorizationCodeParams.php renamed to lib/OAuth2/OAuth2AuthorizationCode/OAuth2AuthorizationCodeParams.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace FattureInCloud\OAuth2;
3+
namespace FattureInCloud\OAuth2\OAuth2AuthorizationCode;
44

55
/**
66
* The Redirect Params of the OAuth2 Authorization Code flow.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace FattureInCloud\OAuth2\OAuth2DeviceCode;
4+
5+
use FattureInCloud\OAuth2\OAuth2AuthorizationCode\OAuth2AuthorizationCodeParams;
6+
use FattureInCloud\OAuth2\OAuth2Error;
7+
use FattureInCloud\OAuth2\OAuth2Manager;
8+
use FattureInCloud\OAuth2\OAuth2TokenResponse;
9+
use GuzzleHttp\Client;
10+
11+
/**
12+
* The Manager for OAuth2 Device Code flow.
13+
*/
14+
class OAuth2DeviceCodeManager extends OAuth2Manager
15+
{
16+
private const DEFAULT_BASE_URI = 'https://api-v2.fattureincloud.it';
17+
18+
/**
19+
* @param string $clientId
20+
* @param string|null $baseUri
21+
* @param Client|null $client
22+
*/
23+
public function __construct(string $clientId, string $baseUri = self::DEFAULT_BASE_URI, Client $client = null)
24+
{
25+
parent::__construct($clientId, $baseUri, $client);
26+
}
27+
28+
/**
29+
* @param array $scopes
30+
* @return OAuth2DeviceCodeResponse|OAuth2Error
31+
*/
32+
public function getDeviceCode(array $scopes)
33+
{
34+
$scopeStr = implode(' ', $scopes);
35+
$tokenUri = $this->getBaseUri() . '/oauth/device';
36+
$body = [
37+
'client_id' => $this->clientId,
38+
'scope' => $scopeStr,
39+
];
40+
$res = $this->executePost($tokenUri, $body)['data'];
41+
if (!$res instanceOf OAuth2Error) {
42+
return new OAuth2DeviceCodeResponse($res['device_code'], $res['user_code'], $res['scope'], $res['verification_uri'], $res['interval'], $res['expires_in']);
43+
} else {
44+
return $res;
45+
}
46+
}
47+
48+
/**
49+
* @param string $code
50+
* @return OAuth2Error|OAuth2TokenResponse
51+
*/
52+
public function fetchToken(string $code)
53+
{
54+
$tokenUri = $this->getBaseUri() . '/oauth/token';
55+
$body = [
56+
'grant_type' => 'urn:ietf:params:oauth:grant-type:device_code',
57+
'client_id' => $this->clientId,
58+
'device_code' => $code
59+
];
60+
return $this->executeTokenPost($tokenUri, $body);
61+
}
62+
63+
/**
64+
* @param string $refreshToken
65+
*/
66+
public function refreshToken(string $refreshToken)
67+
{
68+
$tokenUri = $this->getBaseUri() . '/oauth/token';
69+
$body = [
70+
'grant_type' => 'refresh_token',
71+
'client_id' => $this->clientId,
72+
'refresh_token' => $refreshToken,
73+
];
74+
return $this->executeTokenPost($tokenUri, $body);
75+
}
76+
}

0 commit comments

Comments
 (0)