Skip to content

Commit 9da7e3e

Browse files
committed
Refactoring WIP
1 parent c1f0182 commit 9da7e3e

File tree

5 files changed

+313
-69
lines changed

5 files changed

+313
-69
lines changed

Classes/Authorization.php

Lines changed: 116 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace Flownative\OAuth2\Client;
35

6+
/*
7+
* This file is part of the Flownative.OAuth2.Client package.
8+
*
9+
* (c) Robert Lemke, Flownative GmbH - www.flownative.com
10+
*
11+
* This package is Open Source Software. For the full copyright and license
12+
* information, please view the LICENSE file which was distributed with this
13+
* source code.
14+
*/
15+
416
use Doctrine\ORM\Mapping as ORM;
17+
use League\OAuth2\Client\Token\AccessToken;
18+
use League\OAuth2\Client\Token\AccessTokenInterface;
519
use Neos\Flow\Annotations as Flow;
620

721
/**
8-
* Cache / read model for an Oauth2 token
22+
* An OAuth2 Authorization
923
*
1024
* @Flow\Entity
1125
*/
@@ -15,55 +29,137 @@ class Authorization
1529
* @ORM\Id
1630
* @var string
1731
*/
18-
public $authorizationId;
32+
protected $authorizationId;
1933

20-
/**
2134
/**
2235
* @var string
2336
*/
24-
public $clientId;
37+
protected $serviceName;
2538

2639
/**
40+
* /**
2741
* @var string
2842
*/
29-
public $serviceName;
43+
protected $clientId;
3044

3145
/**
3246
* @var string
47+
* @ORM\Column(nullable = true, length=5000)
3348
*/
34-
public $grantType;
49+
protected $clientSecret;
3550

3651
/**
3752
* @var string
38-
* @ORM\Column(nullable = true, length=5000)
3953
*/
40-
public $clientSecret;
54+
protected $grantType;
4155

4256
/**
4357
* @var string
44-
* @ORM\Column(length=5000)
4558
*/
46-
public $accessToken;
59+
protected $scope;
4760

4861
/**
49-
* @var string
50-
* @ORM\Column(nullable = true, length=5000)
62+
* @var array
63+
* @ORM\Column(type="json_array", nullable = true)
5164
*/
52-
public $refreshToken;
65+
protected $serializedAccessToken;
5366

5467
/**
55-
* @var \DateTimeImmutable
56-
* @ORM\Column(nullable = true)
68+
* @param string $serviceName
69+
* @param string $clientId
70+
* @param string $clientSecret
71+
* @param string $grantType
72+
* @param string $scope
5773
*/
58-
public $expires;
74+
public function __construct(string $serviceName, string $clientId, string $clientSecret, string $grantType, string $scope)
75+
{
76+
$this->serviceName = $serviceName;
77+
$this->clientId = $clientId;
78+
$this->clientSecret = $clientSecret;
79+
$this->grantType = $grantType;
80+
$this->scope = $scope;
81+
$this->authorizationId = sha1($serviceName . $clientId . $grantType . $scope);
82+
}
5983

6084
/**
61-
* @var string
85+
* @return string
6286
*/
63-
public $scope;
87+
public function getAuthorizationId(): string
88+
{
89+
return $this->authorizationId;
90+
}
6491

6592
/**
66-
* @var array
93+
* @return string
94+
*/
95+
public function getServiceName(): string
96+
{
97+
return $this->serviceName;
98+
}
99+
100+
/**
101+
* @return string
102+
*/
103+
public function getClientId(): string
104+
{
105+
return $this->clientId;
106+
}
107+
108+
/**
109+
* @return string
110+
*/
111+
public function getClientSecret(): string
112+
{
113+
return $this->clientSecret;
114+
}
115+
116+
/**
117+
* @return string
118+
*/
119+
public function getGrantType(): string
120+
{
121+
return $this->grantType;
122+
}
123+
124+
/**
125+
* @return string
126+
* @return void
127+
*/
128+
public function getScope(): string
129+
{
130+
return $this->scope;
131+
}
132+
133+
/**
134+
* @return array
135+
*/
136+
public function getSerializedAccessToken(): array
137+
{
138+
return $this->serializedAccessToken ?? [];
139+
}
140+
141+
/**
142+
* @param array $serializedAccessToken
143+
*/
144+
public function setSerializedAccessToken(array $serializedAccessToken): void
145+
{
146+
$this->serializedAccessToken = $serializedAccessToken;
147+
}
148+
149+
/**
150+
* @param AccessTokenInterface $accessToken
151+
* @return void
152+
*/
153+
public function setAccessToken(AccessTokenInterface $accessToken): void
154+
{
155+
$this->serializedAccessToken = $accessToken->jsonSerialize();
156+
}
157+
158+
/**
159+
* @return AccessToken
67160
*/
68-
public $tokenValues;
161+
public function getAccessToken(): ?AccessTokenInterface
162+
{
163+
return !empty($this->serializedAccessToken) ? new AccessToken($this->serializedAccessToken) : null;
164+
}
69165
}

Classes/Command/OAuthTokenCommandController.php renamed to Classes/Command/OAuthCommandController.php

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Neos\Flow\Cli\CommandController;
1010
use Neos\Flow\Persistence\Doctrine\Query;
1111

12-
final class OAuthTokenCommandController extends CommandController
12+
final class OAuthCommandController extends CommandController
1313
{
1414
/**
1515
* @var DoctrineEntityManager
@@ -26,48 +26,52 @@ public function injectEntityManager(DoctrineObjectManager $entityManager): void
2626
}
2727

2828
/**
29-
* List tokens
29+
* List authorizations
3030
*
31-
* This command lists all known OAuth tokens
31+
* This command lists all known OAuth authorizations. With authorizations we keep track
32+
* of access tokens for a given OAuth connection. An authorization is identified by
33+
* a hash over service name, client id, grant type and scope.
3234
*
3335
* @return void
3436
*/
35-
public function listCommand(): void
37+
public function listAuthorizationsCommand(): void
3638
{
3739
$query = new Query(Authorization::class);
38-
$oAuthTokens = $query->execute();
40+
$authorizations = $query->execute();
3941

4042
$rows = [];
41-
foreach ($oAuthTokens as $oAuthToken) {
42-
assert($oAuthToken instanceof Authorization);
43+
foreach ($authorizations as $authorization) {
44+
assert($authorization instanceof Authorization);
45+
$accessToken = $authorization->getAccessToken();
46+
$expires = $accessToken ? \DateTimeImmutable::createFromFormat('U', $accessToken->getExpires())->format('d.m.Y H:i:s') : '';
47+
$values = $accessToken ? implode(', ', array_keys($accessToken->getValues())) : '';
48+
4349
$rows[] = [
44-
$oAuthToken->authorizationId,
45-
$oAuthToken->serviceName,
46-
$oAuthToken->clientId,
47-
$oAuthToken->grantType,
48-
$oAuthToken->scope,
49-
$oAuthToken->expires->format('d. M Y H:i:s'),
50-
implode(', ', array_keys($oAuthToken->tokenValues))
50+
$authorization->getAuthorizationId(),
51+
$authorization->getServiceName(),
52+
$authorization->getClientId(),
53+
$authorization->getGrantType(),
54+
$authorization->getScope(),
55+
$expires,
56+
$values
5157
];
5258
}
5359
$this->output->outputTable($rows, ['Authorization Id', 'Service Name', 'Client ID', 'Grant Type', 'Scope', 'Expiration Time', 'Values']);
5460
}
5561

5662
/**
57-
* Remove token
63+
* Remove authorization
5864
*
59-
* This command removes one or all existing OAuth tokens
65+
* This command removes one or all existing authorizations
6066
*
61-
* @param string $authorizationId
67+
* @param string $id
6268
* @param bool $all
6369
* @return void
64-
* @throws ORMException
65-
* @throws OptimisticLockException
66-
* @throws \Doctrine\ORM\TransactionRequiredException
70+
* @throws
6771
*/
68-
public function removeCommand(string $authorizationId = '', bool $all = false): void
72+
public function removeAuthorizationsCommand(string $id = '', bool $all = false): void
6973
{
70-
if (empty($authorizationId) && !$all) {
74+
if (empty($id) && !$all) {
7175
$this->outputLine('<error>Please specify either --authorization-id or --all.</error>');
7276
exit(1);
7377
}
@@ -80,7 +84,7 @@ public function removeCommand(string $authorizationId = '', bool $all = false):
8084
$this->entityManager->remove($authorization);
8185
}
8286
} else {
83-
$authorization = $this->entityManager->find(Authorization::class, ['authorizationId' => $authorizationId]);
87+
$authorization = $this->entityManager->find(Authorization::class, ['authorizationId' => $id]);
8488
if (!$authorization) {
8589
$this->outputLine('<error>Specified authorization was not found.</error>');
8690
exit(1);

0 commit comments

Comments
 (0)