Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T15656 custom jwt #15657

Merged
merged 3 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## Changed
- Adjusted the constructor for `Phalcon\Storage\Adapter\*` and `Phalcon\Cache\Adapter\*` to allow an empty key prefix to be set if needed. [#15480](https://github.com/phalcon/cphalcon/issues/15480)

## Added
- Added `Phalcon\Security\JWT\Builder::addClaim` for custom JWT claims. [#15656](https://github.com/phalcon/cphalcon/issues/15656)

# [5.0.0alpha5](https://github.com/phalcon/cphalcon/releases/tag/v5.0.0alpha5) (2021-09-09)

## Changed
Expand Down
15 changes: 15 additions & 0 deletions phalcon/Security/JWT/Builder.zep
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ class Builder
return this;
}

/**
* Adds a custom claim
*
* @param string $name
* @param mixed $value
*
* @return Builder
*/
public function addClaim(string! name, var value) -> <Builder>
{
this->claims->set(name, value);

return this;
}

/**
* @return array|string
*/
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/Security/JWT/Builder/AddClaimCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Test\Unit\Security\JWT\Builder;

use Phalcon\Security\JWT\Builder;
use Phalcon\Security\JWT\Signer\Hmac;
use UnitTester;

class AddClaimCest
{
/**
* Unit Tests Phalcon\Security\JWT\Builder :: addClaim()
*
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTBuilderAddClaim(UnitTester $I)
{
$I->wantToTest('Security\JWT\Builder - addClaim()');

$signer = new Hmac();
$builder = new Builder($signer);

$I->assertEmpty($builder->getClaims());

$builder
->setId('this is an id')
->addClaim('username', 'Phalcon')
->addClaim('email', '[email protected]')
;

$expected = [
'email' => '[email protected]',
'jti' => 'this is an id',
'username' => 'Phalcon',
];
$actual = $builder->getClaims();
$I->assertCount(3, $actual);
$I->assertEquals($expected, $actual);
}
}
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Builder/ConstructCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class ConstructCest
/**
* Unit Tests Phalcon\Security\JWT\Builder :: __construct()
*
* @since 2019-12-19
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTBuilderConstruct(UnitTester $I)
{
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Security/JWT/Builder/GetClaimsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class GetClaimsCest
/**
* Unit Tests Phalcon\Security\JWT\Builder :: getClaims()
*
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTBuilderGetClaims(UnitTester $I)
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Builder/GetHeadersCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class GetHeadersCest
/**
* Unit Tests Phalcon\Security\JWT\Builder :: getHeaders()
*
* @since 2019-12-19
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTBuilderGetHeaders(UnitTester $I)
{
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Builder/GetSetAudienceCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class GetSetAudienceCest
/**
* Unit Tests Phalcon\Security\JWT\Builder :: getAudience()/setAudience()
*
* @since 2019-12-15
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTBuilderGetSetAudience(UnitTester $I)
{
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Builder/GetSetContentTypeCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class GetSetContentTypeCest
/**
* Unit Tests Phalcon\Security\JWT\Builder :: getContentType()/setContentType()
*
* @since 2019-12-19
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTBuilderGetSetContentType(UnitTester $I)
{
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Builder/GetSetExpirationTimeCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class GetSetExpirationTimeCest
* Unit Tests Phalcon\Security\JWT\Builder ::
* getExpirationTime()/setExpirationTime()
*
* @since 2019-12-15
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTBuilderGetSetExpirationTime(UnitTester $I)
{
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Builder/GetSetIdCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class GetSetIdCest
/**
* Unit Tests Phalcon\Security\JWT\Builder :: getId()/setId()
*
* @since 2019-12-15
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTBuilderGetSetId(UnitTester $I)
{
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Builder/GetSetIssuedAtCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class GetSetIssuedAtCest
/**
* Unit Tests Phalcon\Security\JWT\Builder :: getIssuedAt()/setIssuedAt()
*
* @since 2019-12-15
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTBuilderGetSetIssuedAt(UnitTester $I)
{
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Builder/GetSetIssuerCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class GetSetIssuerCest
/**
* Unit Tests Phalcon\Security\JWT\Builder :: getIssuer()/setIssuer()
*
* @since 2019-12-15
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTBuilderGetSetIssuer(UnitTester $I)
{
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Builder/GetSetNotBeforeCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class GetSetNotBeforeCest
/**
* Unit Tests Phalcon\Security\JWT\Builder :: getNotBefore()/setNotBefore()
*
* @since 2019-12-15
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTBuilderGetSetNotBefore(UnitTester $I)
{
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/Security/JWT/Builder/GetSetPassphraseCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class GetSetPassphraseCest
*
* @throws ValidatorException
* @throws UnsupportedAlgorithmException
* @since 2019-12-19
*
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTBuilderGetSetPassphrase(UnitTester $I)
{
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Builder/GetSetSubjectCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class GetSetSubjectCest
/**
* Unit Tests Phalcon\Security\JWT\Builder :: getSubject()/setSubject()
*
* @since 2019-12-15
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTBuilderGetSetSubject(UnitTester $I)
{
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/Security/JWT/Builder/GetTokenCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class GetTokenCest
/**
* Unit Tests Phalcon\Security\JWT\Builder :: getToken()
*
* @since 2019-12-19
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTBuilderGetToken(UnitTester $I)
{
Expand Down Expand Up @@ -71,9 +72,9 @@ public function securityJWTBuilderGetTokenException(UnitTester $I)
'Invalid passphrase (empty)'
),
function () {
$signer = new Hmac();
$signer = new Hmac();
$builder = new Builder($signer);
$token = $builder->getToken();
$token = $builder->getToken();
}
);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Builder/InitCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class InitCest
/**
* Unit Tests Phalcon\Security\JWT\Builder :: init()
*
* @since 2019-12-19
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTBuilderInit(UnitTester $I)
{
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Signer/Hmac/ConstructCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class ConstructCest
/**
* Unit Tests Phalcon\Security\JWT\Signer\Hmac :: __construct()
*
* @since 2019-12-15
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTSignerHmacConstruct(UnitTester $I)
{
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Signer/Hmac/GetAlgHeaderCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class GetAlgHeaderCest
/**
* Unit Tests Phalcon\Security\JWT\Signer\Hmac :: getAlgHeader()
*
* @since 2019-12-19
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTSignerHmacGetAlgHeader(UnitTester $I)
{
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Signer/Hmac/GetAlgorithmCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class GetAlgorithmCest
/**
* Unit Tests Phalcon\Security\JWT\Signer\Hmac :: getAlgorithm()
*
* @since 2019-12-15
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTSignerHmacGetAlgorithm(UnitTester $I)
{
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Signer/Hmac/SignCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class SignCest
/**
* Unit Tests Phalcon\Security\JWT\Signer\Hmac :: sign()
*
* @since 2019-12-15
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTSignerHmacSign(UnitTester $I)
{
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Signer/Hmac/VerifyCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class VerifyCest
/**
* Unit Tests Phalcon\Security\JWT\Signer\Hmac :: verify()
*
* @since 2019-12-15
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTSignerHmacVerify(UnitTester $I)
{
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Security/JWT/Signer/None/ConstructCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class ConstructCest
/**
* Unit Tests Phalcon\Security\JWT\Signer\None :: __construct()
*
* @since 2019-12-15
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTSignerNoneConstruct(UnitTester $I)
{
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Security/JWT/Signer/None/GetAlgHeaderCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class GetAlgHeaderCest
/**
* Unit Tests Phalcon\Security\JWT\Signer\None :: getAlgHeader()
*
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTSignerNoneGetAlgHeader(UnitTester $I)
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Security/JWT/Signer/None/GetAlgorithmCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class GetAlgorithmCest
/**
* Unit Tests Phalcon\Security\JWT\Signer\None :: getAlgorithm()
*
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTSignerNoneGetAlgorithm(UnitTester $I)
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Security/JWT/Signer/None/SignCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SignCest
/**
* Unit Tests Phalcon\Security\JWT\Signer\None :: sign()
*
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTSignerNoneSign(UnitTester $I)
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Security/JWT/Signer/None/VerifyCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class VerifyCest
/**
* Unit Tests Phalcon\Security\JWT\Signer\None :: verify()
*
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTSignerNoneVerify(UnitTester $I)
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Security/JWT/Token/Item/ConstructCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ConstructCest
/**
* Unit Tests Phalcon\Security\JWT\Token\Item :: __construct()
*
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTTokenItemConstruct(UnitTester $I)
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Security/JWT/Token/Item/GetCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class GetCest
/**
* Unit Tests Phalcon\Security\JWT\Token\Item :: get()
*
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTTokenItemGet(UnitTester $I)
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Security/JWT/Token/Item/HasCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class HasCest
/**
* Unit Tests Phalcon\Security\JWT\Token\Item :: has()
*
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTTokenItemHas(UnitTester $I)
Expand Down
1 change: 1 addition & 0 deletions tests/unit/Security/JWT/Token/Item/UnderscoreCallCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class UnderscoreCallCest
/**
* Unit Tests Phalcon\Security\JWT\Token\Item :: __call()
*
* @author Phalcon Team <[email protected]>
* @since 2019-12-22
*/
public function securityJWTTokenItemUnderscoreCall(UnitTester $I)
Expand Down
Loading