Skip to content

Commit

Permalink
Change the object mappings to mapped superclasses (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker authored Apr 11, 2022
1 parent 50ebf85 commit 214c13e
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- [B/C Break] Changed the object mappings to mapped superclasses, this requires updating your app's configuration
- Added support for checking the request path in the `refresh_jwt` authenticator
- Deprecated not configuring the request path to check in the `refresh_jwt` authenticator
- Added feature to add the expiration timestamp on the response
Expand Down
10 changes: 10 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('refresh_token_class')
->defaultNull()
->info(sprintf('Set the refresh token class to use (default: %s)', RefreshToken::class))
->validate()
->ifTrue(static fn ($v): bool => null === $v)
->then(static function () {
trigger_deprecation(
'gesdinet/jwt-refresh-token-bundle',
'1.1',
'Not setting the "refresh_token_class" option is deprecated, as of 2.0 a class must be set.'
);
})
->end()
->end()
->scalarNode('object_manager')
->defaultNull()
Expand Down
57 changes: 54 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,58 @@ return [
];
```

### Step 3 (Symfony 5.4+)
### Step 3: Configure the Bundle

#### Symfony Flex Application

For an application using Symfony Flex, a recipe should have been applied to your application. If not, you will need to make the following changes:

1. Configure the refresh token class. Create the `config/packages/gesdinet_jwt_refresh_token.yaml` file with the below contents:

```yaml
gesdinet_jwt_refresh_token:
refresh_token_class: App\Entity\RefreshToken # This is the class name of the refresh token, you will need to adjust this to match the class your application will use
```
2. Create the object class.
If you are using the Doctrine ORM, the below contents should be placed at `src/Entity/RefreshToken.php`:

```php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gesdinet\JWTRefreshTokenBundle\Entity\RefreshToken as BaseRefreshToken;
/**
* @ORM\Entity
*/
class RefreshToken extends BaseRefreshToken
{
}
```

If you are using the Doctrine MongoDB ODM, the below contents should be placed at `src/Document/RefreshToken.php` (remember to update the `refresh_token_class` configuration above to match):

```php
<?php
namespace App\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Gesdinet\JWTRefreshTokenBundle\Document\RefreshToken as BaseRefreshToken;
/**
* @ODM\Document
*/
class RefreshToken extends BaseRefreshToken
{
}
```

### Step 4 (Symfony 5.4+)

#### Define the refresh token route

Expand Down Expand Up @@ -121,7 +172,7 @@ security:
# ...
```

### Step 3 (Symfony 4.4)
### Step 4 (Symfony 4.4)

#### Define the refresh token route

Expand Down Expand Up @@ -157,7 +208,7 @@ security:
# ...
```

### Step 4: Update your database schema
### Step 5: Update your database schema

You will need to add the table for the refresh tokens to your application's database.

Expand Down
4 changes: 2 additions & 2 deletions Resources/config/doctrine/RefreshToken.mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<document name="Gesdinet\JWTRefreshTokenBundle\Document\RefreshToken" collection="refresh_tokens" repository-class="Gesdinet\JWTRefreshTokenBundle\Document\RefreshTokenRepository">
<mapped-superclass name="Gesdinet\JWTRefreshTokenBundle\Document\RefreshToken" collection="refresh_tokens" repository-class="Gesdinet\JWTRefreshTokenBundle\Document\RefreshTokenRepository">
<id />
<field field-name="refreshToken" type="string" unique="true" />
<field field-name="username" type="string" />
<field field-name="valid" type="date"/>
</document>
</mapped-superclass>
</doctrine-mongo-mapping>
4 changes: 2 additions & 2 deletions Resources/config/doctrine/RefreshToken.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="Gesdinet\JWTRefreshTokenBundle\Entity\RefreshToken" table="refresh_tokens" repository-class="Gesdinet\JWTRefreshTokenBundle\Entity\RefreshTokenRepository">
<mapped-superclass name="Gesdinet\JWTRefreshTokenBundle\Entity\RefreshToken" table="refresh_tokens" repository-class="Gesdinet\JWTRefreshTokenBundle\Entity\RefreshTokenRepository">
<id name="id" type="integer">
<generator strategy="AUTO"/>
</id>

<field name="refreshToken" type="string" column="refresh_token" length="128" unique="true"/>
<field name="username" type="string" length="255" column="username"/>
<field name="valid" type="datetime"/>
</entity>
</mapped-superclass>
</doctrine-mapping>
2 changes: 1 addition & 1 deletion Tests/Functional/Document/RefreshTokenRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Gesdinet\JWTRefreshTokenBundle\Tests\Functional\Document;

use Gesdinet\JWTRefreshTokenBundle\Doctrine\RefreshTokenManager;
use Gesdinet\JWTRefreshTokenBundle\Document\RefreshToken;
use Gesdinet\JWTRefreshTokenBundle\Document\RefreshTokenRepository;
use Gesdinet\JWTRefreshTokenBundle\Generator\RefreshTokenGenerator;
use Gesdinet\JWTRefreshTokenBundle\Tests\Functional\Fixtures\Document\RefreshToken;
use Gesdinet\JWTRefreshTokenBundle\Tests\Functional\Fixtures\Document\User;
use Gesdinet\JWTRefreshTokenBundle\Tests\Functional\ODMTestCase;

Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/Entity/RefreshTokenRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use Doctrine\ORM\Tools\SchemaTool;
use Gesdinet\JWTRefreshTokenBundle\Doctrine\RefreshTokenManager;
use Gesdinet\JWTRefreshTokenBundle\Entity\RefreshToken;
use Gesdinet\JWTRefreshTokenBundle\Entity\RefreshTokenRepository;
use Gesdinet\JWTRefreshTokenBundle\Generator\RefreshTokenGenerator;
use Gesdinet\JWTRefreshTokenBundle\Tests\Functional\Fixtures\Entity\RefreshToken;
use Gesdinet\JWTRefreshTokenBundle\Tests\Functional\Fixtures\Entity\User;
use Gesdinet\JWTRefreshTokenBundle\Tests\Functional\ORMTestCase;

Expand Down
13 changes: 13 additions & 0 deletions Tests/Functional/Fixtures/Document/RefreshToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Gesdinet\JWTRefreshTokenBundle\Tests\Functional\Fixtures\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Gesdinet\JWTRefreshTokenBundle\Document\RefreshToken as BaseRefreshToken;

/**
* @ODM\Document
*/
class RefreshToken extends BaseRefreshToken
{
}
13 changes: 13 additions & 0 deletions Tests/Functional/Fixtures/Entity/RefreshToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Gesdinet\JWTRefreshTokenBundle\Tests\Functional\Fixtures\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gesdinet\JWTRefreshTokenBundle\Entity\RefreshToken as BaseRefreshToken;

/**
* @ORM\Entity()
*/
class RefreshToken extends BaseRefreshToken
{
}

0 comments on commit 214c13e

Please sign in to comment.