-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c10a48
commit 7cfeb40
Showing
7 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Controller; | ||
|
||
use App\Entity\Alias; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Attribute\Route; | ||
|
||
class PostfixController extends AbstractController | ||
{ | ||
public function __construct(readonly private EntityManagerInterface $manager) | ||
{ | ||
} | ||
|
||
#[Route(path: '/api/postfix/alias/{alias}', name: 'api_postfix_alias', methods: ['GET'], stateless: true)] | ||
public function getAliasUsers(string $alias): Response | ||
{ | ||
$users = $this->manager->getRepository(Alias::class)->findBy(['deleted' => false, 'source' => $alias]); | ||
|
||
return $this->json(array_map(function (Alias $alias) { | ||
return $alias->getDestination(); | ||
}, $users)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Security; | ||
|
||
use Symfony\Component\Security\Core\Exception\BadCredentialsException; | ||
use Symfony\Component\Security\Http\AccessToken\AccessTokenHandlerInterface; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; | ||
|
||
readonly class PostfixAccessTokenHandler implements AccessTokenHandlerInterface | ||
{ | ||
public function __construct(private string $postfixApiAccessToken) | ||
{ | ||
} | ||
|
||
public function getUserBadgeFrom(#[\SensitiveParameter] string $accessToken): UserBadge | ||
{ | ||
if ($accessToken !== $this->postfixApiAccessToken) { | ||
throw new BadCredentialsException('Invalid access token'); | ||
} | ||
|
||
return new UserBadge('postfix'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Tests\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
|
||
class PostfixControllerTest extends WebTestCase | ||
{ | ||
public function testGetAliasUsersWrongApiToken(): void | ||
{ | ||
$client = static::createClient([], [ | ||
'HTTP_Authorization' => 'Bearer wrong', | ||
]); | ||
$client->request('GET', '/api/postfix/alias/alias@example.org'); | ||
|
||
self::assertResponseStatusCodeSame(401); | ||
} | ||
|
||
public function testGetAliasUsers(): void | ||
{ | ||
$client = static::createClient([], [ | ||
'HTTP_Authorization' => 'Bearer insecure', | ||
]); | ||
|
||
$client->request('GET', '/api/postfix/alias/alias@example.org'); | ||
self::assertResponseIsSuccessful(); | ||
self::assertJsonStringEqualsJsonString('["user2@example.org"]', $client->getResponse()->getContent()); | ||
|
||
$client->request('GET', '/api/postfix/alias/user@example.org'); | ||
self::assertResponseIsSuccessful(); | ||
self::assertJsonStringEqualsJsonString('[]', $client->getResponse()->getContent()); | ||
} | ||
} |