1313use CultuurNet \UDB3 \ApiGuard \Consumer \ConsumerReadRepository as ApiKeyConsumerReadRepository ;
1414use CultuurNet \UDB3 \ApiGuard \Consumer \Specification \ConsumerSpecification as ApiKeyConsumerSpecification ;
1515use CultuurNet \UDB3 \Http \ApiProblem \ApiProblem ;
16- use CultuurNet \UDB3 \Http \Auth \Jwt \JwtValidator ;
1716use CultuurNet \UDB3 \Http \Auth \Jwt \JsonWebToken ;
17+ use CultuurNet \UDB3 \Http \Auth \Jwt \JwtValidator ;
1818use CultuurNet \UDB3 \Role \ReadModel \Permissions \UserPermissionsReadRepositoryInterface ;
1919use CultuurNet \UDB3 \Role \ValueObjects \Permission ;
20+ use CultuurNet \UDB3 \User \ApiKeysMatchedToClientIds ;
21+ use CultuurNet \UDB3 \User \ClientIdResolver ;
2022use CultuurNet \UDB3 \User \CurrentUser ;
23+ use CultuurNet \UDB3 \User \Exceptions \UnmatchedApiKey ;
2124use InvalidArgumentException ;
2225use Psr \Http \Message \ResponseInterface ;
2326use Psr \Http \Message \ServerRequestInterface ;
2427use Psr \Http \Server \MiddlewareInterface ;
2528use Psr \Http \Server \RequestHandlerInterface ;
29+ use Psr \Log \LoggerAwareTrait ;
30+ use Psr \Log \NullLogger ;
2631
2732final class RequestAuthenticatorMiddleware implements MiddlewareInterface
2833{
34+ use LoggerAwareTrait;
35+
2936 private const BEARER = 'Bearer ' ;
3037
3138 /** @var PublicRouteRule[] */
@@ -44,20 +51,29 @@ final class RequestAuthenticatorMiddleware implements MiddlewareInterface
4451 private ApiKeyConsumerSpecification $ apiKeyConsumerPermissionCheck ;
4552 private UserPermissionsReadRepositoryInterface $ userPermissionReadRepository ;
4653
54+ private ClientIdResolver $ clientIdResolver ;
55+
56+ private ?ApiKeysMatchedToClientIds $ apiKeysMatchedToClientIds ;
57+
4758 public function __construct (
4859 JwtValidator $ uitIdV1JwtValidator ,
4960 JwtValidator $ uitIdV2JwtValidator ,
5061 ApiKeyAuthenticator $ apiKeyAuthenticator ,
5162 ApiKeyConsumerReadRepository $ apiKeyConsumerReadRepository ,
5263 ApiKeyConsumerSpecification $ apiKeyConsumerPermissionCheck ,
53- UserPermissionsReadRepositoryInterface $ userPermissionsReadRepository
64+ UserPermissionsReadRepositoryInterface $ userPermissionsReadRepository ,
65+ ClientIdResolver $ clientIdResolver ,
66+ ?ApiKeysMatchedToClientIds $ apiKeysMatchedToClientIds = null
5467 ) {
5568 $ this ->uitIdV1JwtValidator = $ uitIdV1JwtValidator ;
5669 $ this ->uitIdV2JwtValidator = $ uitIdV2JwtValidator ;
5770 $ this ->apiKeyAuthenticator = $ apiKeyAuthenticator ;
5871 $ this ->apiKeyConsumerReadRepository = $ apiKeyConsumerReadRepository ;
5972 $ this ->apiKeyConsumerPermissionCheck = $ apiKeyConsumerPermissionCheck ;
6073 $ this ->userPermissionReadRepository = $ userPermissionsReadRepository ;
74+ $ this ->clientIdResolver = $ clientIdResolver ;
75+ $ this ->apiKeysMatchedToClientIds = $ apiKeysMatchedToClientIds ;
76+ $ this ->logger = new NullLogger ();
6177 }
6278
6379 public function addPublicRoute (string $ pathPattern , array $ methods = [], ?string $ excludeQueryParam = null ): void
@@ -155,6 +171,18 @@ private function authenticateApiKey(ServerRequestInterface $request): void
155171 );
156172 }
157173
174+ if ($ this ->apiKeysMatchedToClientIds !== null ) {
175+ try {
176+ $ clientId = $ this ->apiKeysMatchedToClientIds ->getClientId ($ this ->apiKey ->toString ());
177+ if (!$ this ->clientIdResolver ->hasEntryAccess ($ clientId )) {
178+ throw ApiProblem::forbidden ('Given API key is not authorized to use Entry API. ' );
179+ }
180+ return ;
181+ } catch (UnmatchedApiKey $ unmatchedApiKey ) {
182+ $ this ->logger ->warning ($ unmatchedApiKey ->getMessage ());
183+ }
184+ }
185+
158186 try {
159187 $ this ->apiKeyAuthenticator ->authenticate ($ this ->apiKey );
160188 } catch (ApiKeyAuthenticationException $ e ) {
0 commit comments