Skip to content

Commit 2a4d30c

Browse files
League OAuth 2.0 server with Symfony - Access Token and Refresh Token
1 parent 6169575 commit 2a4d30c

File tree

14 files changed

+526
-0
lines changed

14 files changed

+526
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping">
2+
<entity name="App\Domain\Model\AccessToken">
3+
<id name="id" column="id" type="string"/>
4+
<field name="userId" type="string"/>
5+
<field name="clientId" type="string"/>
6+
<field name="scopes" type="simple_array"/>
7+
<field name="revoked" type="boolean"/>
8+
<field name="createdAt" type="datetime"/>
9+
<field name="updatedAt" type="datetime"/>
10+
<field name="expiresAt" type="datetime"/>
11+
</entity>
12+
</doctrine-mapping>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace App\Application\Repository\Doctrine;
4+
5+
use App\Domain\Model\AccessToken;
6+
use App\Domain\Repository\AccessTokenRepositoryInterface;
7+
use Doctrine\Common\Persistence\ObjectRepository;
8+
use Doctrine\ORM\EntityManagerInterface;
9+
10+
final class AccessTokenRepository implements AccessTokenRepositoryInterface
11+
{
12+
private const ENTITY = AccessToken::class;
13+
14+
/**
15+
* @var EntityManagerInterface
16+
*/
17+
private $entityManager;
18+
19+
/**
20+
* @var ObjectRepository
21+
*/
22+
private $objectRepository;
23+
24+
/**
25+
* UserRepository constructor.
26+
* @param EntityManagerInterface $entityManager
27+
*/
28+
public function __construct(
29+
EntityManagerInterface $entityManager
30+
) {
31+
$this->entityManager = $entityManager;
32+
$this->objectRepository = $this->entityManager->getRepository(self::ENTITY);
33+
}
34+
35+
public function find(string $accessTokenId): ?AccessToken
36+
{
37+
return $this->entityManager->find(self::ENTITY, $accessTokenId);
38+
}
39+
40+
public function save(AccessToken $accessToken): void
41+
{
42+
$this->entityManager->persist($accessToken);
43+
$this->entityManager->flush();
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace App\Application\Repository\Doctrine;
4+
5+
use App\Domain\Model\RefreshToken;
6+
use App\Domain\Repository\AccessTokenRepositoryInterface;
7+
use Doctrine\Common\Persistence\ObjectRepository;
8+
use Doctrine\ORM\EntityManagerInterface;
9+
10+
final class RefreshTokenRepository implements AccessTokenRepositoryInterface
11+
{
12+
private const ENTITY = RefreshToken::class;
13+
14+
/**
15+
* @var EntityManagerInterface
16+
*/
17+
private $entityManager;
18+
19+
/**
20+
* @var ObjectRepository
21+
*/
22+
private $objectRepository;
23+
24+
/**
25+
* UserRepository constructor.
26+
* @param EntityManagerInterface $entityManager
27+
*/
28+
public function __construct(
29+
EntityManagerInterface $entityManager
30+
) {
31+
$this->entityManager = $entityManager;
32+
$this->objectRepository = $this->entityManager->getRepository(self::ENTITY);
33+
}
34+
35+
public function find(string $accessTokenId): ?RefreshToken
36+
{
37+
return $this->entityManager->find(self::ENTITY, $accessTokenId);
38+
}
39+
40+
public function save(RefreshToken $accessToken): void
41+
{
42+
$this->entityManager->persist($accessToken);
43+
$this->entityManager->flush();
44+
}
45+
}

src/Domain/Model/AccessToken.php

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
namespace App\Domain\Model;
4+
5+
class AccessToken
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private $id;
11+
12+
/**
13+
* @var string
14+
*/
15+
private $userId;
16+
17+
/**
18+
* @var string
19+
*/
20+
private $clientId;
21+
22+
/**
23+
* @var array
24+
*/
25+
private $scopes;
26+
27+
/**
28+
* @var bool
29+
*/
30+
private $revoked;
31+
32+
/**
33+
* @var \DateTime
34+
*/
35+
private $createdAt;
36+
37+
/**
38+
* @var \DateTime
39+
*/
40+
private $updatedAt;
41+
42+
/**
43+
* @var \DateTime
44+
*/
45+
private $expiresAt;
46+
47+
/**
48+
* Token constructor.
49+
* @param string $id
50+
* @param string $userId
51+
* @param string $clientId
52+
* @param array $scopes
53+
* @param bool $revoked
54+
* @param \DateTime $createdAt
55+
* @param \DateTime $updatedAt
56+
* @param \DateTime $expiresAt
57+
*/
58+
public function __construct(
59+
string $id,
60+
string $userId,
61+
string $clientId,
62+
array $scopes,
63+
bool $revoked,
64+
\DateTime $createdAt,
65+
\DateTime $updatedAt,
66+
\DateTime $expiresAt
67+
) {
68+
$this->id = $id;
69+
$this->userId = $userId;
70+
$this->clientId = $clientId;
71+
$this->scopes = $scopes;
72+
$this->revoked = $revoked;
73+
$this->createdAt = $createdAt;
74+
$this->updatedAt = $updatedAt;
75+
$this->expiresAt = $expiresAt;
76+
}
77+
78+
/**
79+
* @return string
80+
*/
81+
public function getId(): string
82+
{
83+
return $this->id;
84+
}
85+
86+
/**
87+
* @return string
88+
*/
89+
public function getUserId(): string
90+
{
91+
return $this->userId;
92+
}
93+
94+
/**
95+
* @return string
96+
*/
97+
public function getClientId(): string
98+
{
99+
return $this->clientId;
100+
}
101+
102+
/**
103+
* @return array
104+
*/
105+
public function getScopes(): array
106+
{
107+
return $this->scopes;
108+
}
109+
110+
/**
111+
* @return bool
112+
*/
113+
public function isRevoked(): bool
114+
{
115+
return $this->revoked;
116+
}
117+
118+
public function revoke(): void
119+
{
120+
$this->revoked = true;
121+
}
122+
123+
/**
124+
* @return \DateTime
125+
*/
126+
public function getCreatedAt(): \DateTime
127+
{
128+
return $this->createdAt;
129+
}
130+
131+
/**
132+
* @return \DateTime
133+
*/
134+
public function getUpdatedAt(): \DateTime
135+
{
136+
return $this->updatedAt;
137+
}
138+
139+
/**
140+
* @param \DateTime $updatedAt
141+
*/
142+
public function setUpdatedAt(\DateTime $updatedAt): void
143+
{
144+
$this->updatedAt = $updatedAt;
145+
}
146+
147+
/**
148+
* @return \DateTime
149+
*/
150+
public function getExpiresAt(): \DateTime
151+
{
152+
return $this->expiresAt;
153+
}
154+
}

src/Domain/Model/ClientId.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace App\Domain\Model;
34

45
use Assert\Assertion;

src/Domain/Model/RefreshToken.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace App\Domain\Model;
4+
5+
class RefreshToken
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private $id;
11+
12+
/**
13+
* @var string
14+
*/
15+
private $accessTokenId;
16+
17+
/**
18+
* @var bool
19+
*/
20+
private $revoked = false;
21+
22+
/**
23+
* @var \DateTime
24+
*/
25+
private $expiresAt;
26+
27+
/**
28+
* RefreshToken constructor.
29+
* @param string $id
30+
* @param string $accessTokenId
31+
* @param \DateTime $expiresAt
32+
*/
33+
public function __construct(string $id, string $accessTokenId, \DateTime $expiresAt)
34+
{
35+
$this->id = $id;
36+
$this->accessTokenId = $accessTokenId;
37+
$this->expiresAt = $expiresAt;
38+
}
39+
40+
/**
41+
* @return string
42+
*/
43+
public function getAccessTokenId(): string
44+
{
45+
return $this->accessTokenId;
46+
}
47+
48+
/**
49+
* @return bool
50+
*/
51+
public function isRevoked(): bool
52+
{
53+
return $this->revoked;
54+
}
55+
56+
public function revoke(): void
57+
{
58+
$this->revoked = true;
59+
}
60+
61+
/**
62+
* @return \DateTime
63+
*/
64+
public function getExpiresAt(): \DateTime
65+
{
66+
return $this->expiresAt;
67+
}
68+
}

src/Domain/Model/User.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace App\Domain\Model;
34

45
use Ramsey\Uuid\Uuid;

src/Domain/Model/UserId.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace App\Domain\Model;
34

45
use Assert\Assertion;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Domain\Repository;
4+
5+
use App\Domain\Model\AccessToken;
6+
7+
interface AccessTokenRepositoryInterface
8+
{
9+
public function find(string $accessTokenId): ?AccessToken;
10+
11+
public function save(AccessToken $accessToken): void;
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Domain\Repository;
4+
5+
use App\Domain\Model\RefreshToken;
6+
7+
interface RefreshTokenRepositoryInterface
8+
{
9+
public function find(string $refreshTokenId): ?RefreshToken;
10+
11+
public function save(RefreshToken $refreshToken): void;
12+
}

0 commit comments

Comments
 (0)