Skip to content

Commit

Permalink
Remove uri references to string returns
Browse files Browse the repository at this point in the history
  • Loading branch information
Koen Eelen committed Apr 22, 2024
1 parent 2973af6 commit 6e83e7c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 21 deletions.
22 changes: 17 additions & 5 deletions src/Jwt/JwtOAuthCallbackHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,32 @@ public function __construct(
$this->userService = $userService;
}

public function handle(AccessToken $accessToken, UriInterface $destination): ResponseInterface
public function handle(AccessToken $accessToken, string $destination): ResponseInterface
{
$claims = $this->userService
->getUserClaims($accessToken)
->toArray();

$jwt = $this->encoderService->encode($claims);

$q = $destination->getQuery();

$q .= ($q?'&':'') . 'jwt=' . $jwt;
$destination = $this->addJwtTokenToRedirectUri($destination, $jwt);

return new RedirectResponse(
(string) $destination->withQuery($q)
$destination
);
}

private function addJwtTokenToRedirectUri(string $destination, \Lcobucci\JWT\Token $jwt)
{
$urlParts = parse_url($destination);

if (empty($urlParts['query'])) {
$newQuery = 'jwt=' . $jwt;
} else {
$newQuery = $urlParts['query'] . '&jwt=' . $jwt;
}

$destination = str_replace($urlParts['query'], $newQuery, $destination);
return $destination;
}
}
2 changes: 1 addition & 1 deletion src/OAuth/OAuthCallbackHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

interface OAuthCallbackHandlerInterface
{
public function handle(AccessToken $accessToken, UriInterface $destination): ResponseInterface;
public function handle(AccessToken $accessToken, string $destination): ResponseInterface;
}
4 changes: 2 additions & 2 deletions src/OAuth/OAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(
public function connect(ServerRequestInterface $request): ResponseInterface
{
try {
$callbackUrl = (string) $this->oAuthUrlHelper->createCallbackUri($request);
$callbackUrl = $this->oAuthUrlHelper->createCallbackUri($request);
} catch (InvalidArgumentException $e) {
return ResponseFactory::create(400, $e->getMessage());
}
Expand All @@ -53,7 +53,7 @@ public function connect(ServerRequestInterface $request): ResponseInterface
public function register(ServerRequestInterface $request): ResponseInterface
{
try {
$callbackUrl = (string) $this->oAuthUrlHelper->createCallbackUri($request);
$callbackUrl = $this->oAuthUrlHelper->createCallbackUri($request);
} catch (InvalidArgumentException $e) {
return ResponseFactory::create(400, $e->getMessage());
}
Expand Down
16 changes: 7 additions & 9 deletions src/OAuth/OAuthUrlHelper.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
namespace CultuurNet\UDB3\JwtProvider\OAuth;

use GuzzleHttp\Psr7\Uri;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use CultuurNet\Auth\TokenCredentials as RequestToken;
use Slim\Psr7\Uri;

class OAuthUrlHelper
{
Expand All @@ -24,14 +24,12 @@ public function __construct(
$this->authorizationPath = trim($authorizationPath, '/');
}

public function createCallbackUri(ServerRequestInterface $request): UriInterface
public function createCallbackUri(ServerRequestInterface $request): string
{
$baseUrl = $this->getBaseUrlFromRequest($request);
$query = http_build_query([self::DESTINATION => (string) $this->getDestinationUri($request)]);
$query = http_build_query([self::DESTINATION => $this->getDestinationUri($request)]);

$url = $baseUrl . '/' . $this->authorizationPath . '?' . $query;

return new Uri($url);
return $baseUrl . '/' . $this->authorizationPath . '?' . $query;
}

public function hasValidRequestToken(
Expand All @@ -51,13 +49,13 @@ public function getOAuthVerifier(ServerRequestInterface $request): ?string
$verifier = $request->getQueryParams()[self::OAUTH_VERIFIER] ?? null;

if ($verifier === null) {
return $verifier;
return null;
}

return (string) $verifier;
}

public function getDestinationUri(ServerRequestInterface $request): UriInterface
public function getDestinationUri(ServerRequestInterface $request): string
{
$destination = $request->getQueryParams()[self::DESTINATION] ?? null;

Expand All @@ -67,7 +65,7 @@ public function getDestinationUri(ServerRequestInterface $request): UriInterface
);
}

return new Uri($destination);
return $destination;
}

private function getBaseUrlFromRequest(ServerRequestInterface $request): string
Expand Down
4 changes: 1 addition & 3 deletions tests/Jwt/JwtOAuthCallbackHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

namespace CultuurNet\UDB3\JwtProvider\Jwt;

use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit\Framework\MockObject\MockObject;
use CultuurNet\Auth\TokenCredentials;
use CultuurNet\Auth\User as AccessToken;
use CultuurNet\UDB3\Jwt\JwtEncoderServiceInterface;
use CultuurNet\UDB3\JwtProvider\Http\RedirectResponse;
use CultuurNet\UDB3\JwtProvider\User\UserClaims;
use CultuurNet\UDB3\JwtProvider\User\UserServiceInterface;
use GuzzleHttp\Psr7\Uri;
use Lcobucci\JWT\Signature;
use Lcobucci\JWT\Token as Jwt;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -57,7 +55,7 @@ public function it_returns_a_redirect_response_to_the_destination_with_a_jwt_as_
)
);

$destination = new Uri('http://bar.com/sub/directory?query=value');
$destination = 'http://bar.com/sub/directory?query=value';

$userClaims = new UserClaims(
$userId,
Expand Down
2 changes: 1 addition & 1 deletion tests/OAuth/OAuthUrlHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function it_creates_callback_uri_from_request(): void

$callbackUri = $this->oAuthUrlHelper->createCallbackUri($request);

$this->assertEquals($expectedUri, (string) $callbackUri);
$this->assertEquals($expectedUri, $callbackUri);
}

/**
Expand Down

0 comments on commit 6e83e7c

Please sign in to comment.