diff --git a/src/JwtGuard/IdentityProvider.php b/src/JwtGuard/IdentityProvider.php index 66cafd6..97f54be 100644 --- a/src/JwtGuard/IdentityProvider.php +++ b/src/JwtGuard/IdentityProvider.php @@ -6,4 +6,6 @@ interface IdentityProvider public function getUserId(); public function getGroupId(); + + public function getIsAdmin(); } \ No newline at end of file diff --git a/src/JwtGuard/JwtTokenAuthenticator.php b/src/JwtGuard/JwtTokenAuthenticator.php index 6ae460c..a60b37a 100644 --- a/src/JwtGuard/JwtTokenAuthenticator.php +++ b/src/JwtGuard/JwtTokenAuthenticator.php @@ -17,6 +17,7 @@ class JwtTokenAuthenticator implements RequestAuthoriser, IdentityProvider const GROUP_ID = 'groupId'; const APP_ID = 'appId'; const EXPIRY = 'exp'; + const IS_ADMIN = 'isAdmin'; /** * @var AlgorithmInterface @@ -124,4 +125,9 @@ public function getGroupId() { return $this->getClaimOrNull( self::GROUP_ID ); } + + public function getIsAdmin() + { + return (bool)$this->getClaimOrNull( self::IS_ADMIN); + } } \ No newline at end of file diff --git a/test/JwtTokenAuthenticatorTest.php b/test/JwtTokenAuthenticatorTest.php index 635a11a..53cbd29 100644 --- a/test/JwtTokenAuthenticatorTest.php +++ b/test/JwtTokenAuthenticatorTest.php @@ -20,6 +20,7 @@ class JwtTokenAuthenticatorTest extends \PHPUnit_Framework_TestCase { const USER_ID = '1'; const GROUP_ID = '1'; + const IS_ADMIN = true; const WITH = 0; @@ -33,6 +34,8 @@ class JwtTokenAuthenticatorTest extends \PHPUnit_Framework_TestCase const VALID_EXPIRY_DATE = 3; + const VALID_IS_ADMIN = 4; + /** * @var Hs512 @@ -44,6 +47,11 @@ class JwtTokenAuthenticatorTest extends \PHPUnit_Framework_TestCase */ private $auth; + /** + * @var Token + */ + private $token; + /** * @return string */ @@ -65,7 +73,6 @@ private function getExpiredDate() } /** - * @param $withOrWithout * @param array $spec * @return Token */ @@ -75,7 +82,8 @@ private function getTokenWithout( array $spec ) self::VALID_USER_ID => new PublicClaim( 'userId', self::USER_ID ), self::VALID_GROUP_ID => new PublicClaim( 'groupId', self::GROUP_ID ), self::VALID_APP_ID => new PublicClaim( 'appId', 'labs' ), - self::VALID_EXPIRY_DATE => new PublicClaim('exp', $this->getNonExpiredDate()) + self::VALID_EXPIRY_DATE => new PublicClaim('exp', $this->getNonExpiredDate()), + self::VALID_IS_ADMIN => new PublicClaim('isAdmin', self::IS_ADMIN) ]; $spec = array_diff( array_keys( $mappings ), $spec ); @@ -255,10 +263,20 @@ public function givenTokenWithInvalidSignature_whenValidatingToken_returnFalse() /** * @test */ - public function givenValidToken_whenSettingToken_getCorrectUserAndGroupId() + public function givenTokenWithoutIsAdmin_whenGettingIsAdmin_returnFalse() + { + $this->authoriseToken($this->getTokenWithout([self::VALID_IS_ADMIN])); + $this->assertFalse($this->auth->getIsAdmin()); + } + + /** + * @test + */ + public function givenValidToken_whenSettingToken_getCorrectUserAndGroupIdAndIsAdmin() { $this->authoriseToken( $this->getValidToken() ); $this->assertEquals(self::GROUP_ID, $this->auth->getGroupId()); $this->assertEquals(self::USER_ID, $this->auth->getUserId()); + $this->assertEquals(self::IS_ADMIN, $this->auth->getIsAdmin()); } }