Skip to content

Commit

Permalink
Merge pull request #65 from dennis-koster/feature/extract-url-transfo…
Browse files Browse the repository at this point in the history
…rmation-to-separate-methods

Extracted the transformation of URL's for email verification and password resets to a separate method
  • Loading branch information
wimski authored Aug 5, 2021
2 parents 44fb5cf + 527b53c commit d8e4962
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/Contracts/Services/EmailVerificationServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

interface EmailVerificationServiceInterface
{
public function transformUrl(MustVerifyEmail $user, string $url): string;

public function setVerificationUrl(string $url): void;

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Contracts/Services/ResetPasswordServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
namespace DanielDeWit\LighthouseSanctum\Contracts\Services;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\CanResetPassword;
use Illuminate\Database\Eloquent\Model;

interface ResetPasswordServiceInterface
{
public function transformUrl(CanResetPassword $notifiable, string $token, string $url): string;

public function setResetPasswordUrl(string $url): void;

/**
Expand Down
23 changes: 14 additions & 9 deletions src/Services/EmailVerificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,22 @@ public function __construct(SignatureServiceInterface $signatureService, int $ex
$this->expiresIn = $expiresIn;
}

public function transformUrl(MustVerifyEmail $user, string $url): string
{
$parameters = $this->createUrlParameters($user);

return str_replace([
'__ID__',
'__HASH__',
'__EXPIRES__',
'__SIGNATURE__',
], $parameters, $url);
}

public function setVerificationUrl(string $url): void
{
VerifyEmail::createUrlUsing(function ($user) use ($url) {
$parameters = $this->createUrlParameters($user);

return str_replace([
'__ID__',
'__HASH__',
'__EXPIRES__',
'__SIGNATURE__',
], $parameters, $url);
VerifyEmail::createUrlUsing(function (MustVerifyEmail $user) use ($url) {
return $this->transformUrl($user, $url);
});
}

Expand Down
19 changes: 12 additions & 7 deletions src/Services/ResetPasswordService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@ public function __construct(Hasher $hash, Dispatcher $dispatcher)
$this->dispatcher = $dispatcher;
}

public function transformUrl(CanResetPassword $notifiable, string $token, string $url): string
{
return str_replace([
'__EMAIL__',
'__TOKEN__',
], [
$notifiable->getEmailForPasswordReset(),
$token,
], $url);
}

public function setResetPasswordUrl(string $url): void
{
ResetPasswordNotification::createUrlUsing(function (CanResetPassword $notifiable, string $token) use ($url) {
return str_replace([
'__EMAIL__',
'__TOKEN__',
], [
$notifiable->getEmailForPasswordReset(),
$token,
], $url);
return $this->transformUrl($notifiable, $token, $url);
});
}

Expand Down
43 changes: 43 additions & 0 deletions tests/Integration/Services/EmailVerificationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,49 @@ protected function setUp(): void
);
}

/**
* @test
*/
public function it_transforms_a_verification_url(): void
{
/** @var UserMustVerifyEmail $user */
$user = UserMustVerifyEmail::factory()->create([
'id' => 12345,
'email' => '[email protected]',
]);

$url = $this->service->transformUrl(
$user,
'https://mysite.com/verify-email/__ID__/__HASH__'
);

static::assertSame('https://mysite.com/verify-email/12345/' . sha1('[email protected]'), $url);
}

/**
* @test
*/
public function it_transforms_a_signed_verification_url(): void
{
Carbon::setTestNow(Carbon::createFromTimestamp(1609477200));

/** @var UserMustVerifyEmail $user */
$user = UserMustVerifyEmail::factory()->create([
'id' => 12345,
'email' => '[email protected]',
]);

$url = $this->service->transformUrl($user, 'https://mysite.com/verify-email/__ID__/__HASH__/__EXPIRES__/__SIGNATURE__');

$signature = hash_hmac('sha256', serialize([
'id' => 12345,
'hash' => sha1('[email protected]'),
'expires' => 1609480800,
]), $this->app['config']->get('app.key'));

static::assertSame('https://mysite.com/verify-email/12345/' . sha1('[email protected]') . '/1609480800/' . $signature, $url);
}

/**
* @test
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/Integration/Services/ResetPasswordServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ protected function setUp(): void
);
}

/**
* @test
*/
public function it_transforms_a_reset_password_url(): void
{
/** @var UserMustVerifyEmail $user */
$user = UserMustVerifyEmail::factory()->create([
'email' => '[email protected]',
]);

$token = 'token123';

$url = $this->service->transformUrl($user, $token, 'https://mysite.com/reset-password/__EMAIL__/__TOKEN__');

static::assertSame('https://mysite.com/reset-password/[email protected]/token123', $url);
}

/**
* @test
*/
Expand Down

0 comments on commit d8e4962

Please sign in to comment.