Feat : implement JWT for api authentication
This commit is contained in:
@@ -57,6 +57,7 @@ class AppKernel extends Kernel
|
||||
new FOS\RestBundle\FOSRestBundle(),
|
||||
new WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle(),
|
||||
new Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle(),
|
||||
new Lexik\Bundle\JWTAuthenticationBundle\LexikJWTAuthenticationBundle(),
|
||||
];
|
||||
|
||||
if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
|
||||
|
||||
@@ -246,10 +246,9 @@ fos_rest:
|
||||
rules:
|
||||
- { path: '^/api/', priorities: ['json'], fallback_format: 'json' }
|
||||
param_fetcher_listener: true
|
||||
# versioning:
|
||||
# enabled: true
|
||||
# regex: '/(v|version)=(?P<version>[0-9\.]+)/'
|
||||
|
||||
# sensio_framework_extra:
|
||||
# view: { annotations: false }
|
||||
# request: { converters: true }
|
||||
lexik_jwt_authentication:
|
||||
secret_key: '%kernel.root_dir%/../app/config/jwt/private.pem' # required for token creation
|
||||
public_key: '%kernel.root_dir%/../app/config/jwt/public.pem' # required for token verification
|
||||
pass_phrase: 'talentstubeforever' # required for token creation, usage of an environment variable is recommended
|
||||
token_ttl: 3600
|
||||
@@ -0,0 +1,54 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
Proc-Type: 4,ENCRYPTED
|
||||
DEK-Info: AES-256-CBC,1B8B1CB3A64B49F820DD713907974037
|
||||
|
||||
0/ahxIYlJ0RiiOqRz78LbR7g+UvBC8r4RrMWFCIOuh/FPbTYyIc0VxRMObrTqvHv
|
||||
B4oUMnTzHzkOimvLWncZSIA39TL2OuQMTT7g/Cp8uh7Exnf29js7Ywv8EAQA2trw
|
||||
EsLXjH5St3vLhWLQdxVUBY6tWc5yuqTJ3ze9rFojiJ0mbIoeid7L8sI4mQAocfX2
|
||||
2/aMWwhQX83AJaRC7liU18nZAP4gAxYjvLAaC6zj7M3wxPv1r3Ox2PuSUfYZFkYA
|
||||
HbVYq6pi/Dw32CmCniufccSr2wd0f78fZdI/E20tdAviLN6awqrjM2aFgzqZ6zw/
|
||||
UCD3L1hLfGxRk6uYAFgEiFQB7dT4ZzqjopKNsVuho+DTc55kmx8iPD3xXuTMbeUY
|
||||
SgiV4KcGCbHRt1HGWWbJgyOkSaPQQQk5Bf63glS4nLVNqzIqzJmpVCKuwOpKCSnK
|
||||
DTeWpw9MIHLHM4SD2OM8Hh8S8l6v+Y4ash6mEtUNh6a7N+TQ1vEn/wyt8wyM7/Fh
|
||||
UY/97loUyu0HC2Ox89sP1y6VcNaRQlhNj9hZzLC/q5K+Z4zAQRnGPuTowZL7ztxZ
|
||||
BD+MTnNZc+Akrti+3c1vAHxzuUoBd7h8NX2pnNNUY2Wme9JqfZnCx9fV4AwVOvx6
|
||||
8mvTQL5YHOv8HpsGnDoOak1r+QzHM8IrL8SSrJ/DBXMIWmFefhcIDgrXZG8LV+ck
|
||||
RlSuP5Lsyy3e8AQ6hrM06DXkhjGi7gCeHIHySbzw0AiFERQz0d7XgU75DL/ALPqh
|
||||
0lyqlXdrx/+qqTc0wwjYIerNLNdVqN9kvLg+aqFSwZrfiZyEy6rYdUoqYXoInwle
|
||||
QBt2uqcjVUhMwdQYaDWiqHidSpIMP5nuTKDm6Z8J/SLf4iy+It1qXUEcoRKhDavm
|
||||
fy+dw3JvthpoNjP3Bq+moygP8J8msS5Ti3UgVug21Fsp89fmvUO8FO2JRXLXP8+U
|
||||
3HG1Ij/KKk1dxC6PT7qu9gVeoKM+W8maCVV/O84Qx/N3aXY+sHm1HsSssg0cLEC7
|
||||
zaEai16xhVGI8aagYwxBHVkVVXk/CBS7zq1JYBnZOIw56d1NTjvJ3YmTx619Hyek
|
||||
PJuMNjGV4mM0jONzHJbI6deE2B+f1/ej1JwoeGyBhynrZjwtTCd3JAtwRK9WO1fq
|
||||
owdoCAnDOV7yDcwfFEYaNV2ie/R/kQo/fIIanykQs59Op2M0yhLvmch+C3RghLVg
|
||||
qfR3Jngbqjwz/uh5ZQShZtHFirdAyer/eLD0CteDt4TrAiQxiHqrEOdqwF/G/STq
|
||||
+3SSkChf6uhqUUqfmJFwxaF6AiNTkdk0YyxucWY2JXYf3qyz7ADGnI/KZZ+1LchF
|
||||
2bLUKjyjnFKQoaFtDd7qMGxrTD80egN/fFV4Nybt+DTsGjdUjvCFS69j+YvZ0vIP
|
||||
8rZHK7etISTfiauP9SrIR9UKpNA8fGrUgJGOMo/5Wkz4YS6LmMceQkpDxj46p8Ia
|
||||
Iv+pOKez6NsckP1OiDfTxLtXL1S3xQHP3V0dYI5UcWt8Bczi2VylZCcS8JiV8Cqy
|
||||
+EYRaIB9Qi1X5uLQsbiyS70rJp9mJ3xUR2dIzD70njCIDtLPA2b9dXqXm1IyqdkB
|
||||
46W0MOnyI699XgiywvPovwhzTH8uO5Xn5gnjEgD92pHdJ2iTVQDTC9qtKhODrhbC
|
||||
ouOCbS/LPAnQMwlLD1rWAeJcBJ3PL1KdAlaxWq3Jd/iMhDh/hSboS88Lm88RYtBh
|
||||
3DhMCyxY1gu6jyzuWN2pHKzCvZ1Aqqm1SMS/8nAPe/KtPJO7zTRQJIrrfPK1pt2B
|
||||
u5tu1zupVKihoRkLZtV4FUwDBo8DE9V65b/LAJZtuId77ci7aWRoD5b1Lc9X9iyH
|
||||
cFJTQbMxzA76/8F9/Y1h8D9mdP+N9Tfw6iOvRxszqciGAsUasqo4Xm3ZOQqCcOo5
|
||||
djoywGnzqsfxrV733i2KHRqz7XUkLf8n6URloH2i1tSbHWl7fmYAC0N85IUs1PxQ
|
||||
DB0LUtQgS03gjE4pRJSlgf8JX/0KrG8sYpcvz9R5CqMKrkM6rDy6ehe4P5xVQ+3m
|
||||
WNCZFfGjCTcuEVLFDNx8CdF8qIV/BCcUSty7KXXoC3AMOAsVlZSwskmKN61GJmBS
|
||||
Sz1bDwZO6QCZgXR2fH0UMEnCkpksS4xf3f0oq5/WzWzC4ZaDHnwt5NqKE8XXeYpn
|
||||
isulqUFmCK2VhkqRX1rwND8E45yDG8+U5lyJSHp4y8Fe+AEQjxXUxXDefc80s09i
|
||||
LMXM7iNUtESFge3BZvocdRrqvp21tkPElzaDoW0nMb0VoNI4Xfe113vBeBdEq0Hr
|
||||
yuGvM8ZgvKjzl1T4h9PTaBu97kR1QjBSiY0PeEeSMtq/W1XbFO5jU4g/utVRluBm
|
||||
z64kldtRW1RC8vosrN4sNbdVDAvL43Dynl0wYMI+jgIN3HP2QnOTDH1MLQ5BJZg4
|
||||
aZFSMZ70e9kF2HXpFnfp+SsuT0lMhzZrSrLvKg0pkuD8jc7FtP/HoLcay2doBufd
|
||||
fS4VheiaGm7om9Ggmkowld1CUpuzfyi7NDcymWH6J37Ypcvprn1Ts9iBcBDaOsbZ
|
||||
T3pP0mU4glmRPFRq2IRZXOgTQFXIJKYFpnt0LCC2IuzbQLTKy0LVr6Ksc6ZD21j5
|
||||
AR/xblSp8dZDS4ltrN44r+rtcio7dN9KlDctURpoqdw8oatwpk5QLFfYeMKPRn6j
|
||||
yQA4tatWlTxr/UR8Xy8JNcJfg7JQIYjdf36atxpv5xYvnkf6Gs56xVnRthwNYawn
|
||||
h44emyq/Tuflc9FZqptUDwEPKzLvH0NWxVkNvGG92H0OtES3q9OpGPTnqhIPUTme
|
||||
Jrbu7knwD5sdzYRh64BhbXem8glPStYnW7B0dSdvyvGpArF1W2cKKURgnr7XokrB
|
||||
DqbeSpdRf7fE5bw9jHJgcpUJOZ3o6kGA3l309Cdcbigm7gnK0KiY0y/P8MSWEu00
|
||||
C4WZOFLxuaO1iVav+spACFbFkdVmLoRBPE2hFUc6BA3E/7qNHAjlVYXbUrIR3NA+
|
||||
iioXf1XFM7K0LG02mMKEFSuWIpZacDyAfjHUlrid7cFBBXLk/EWcwNyGSlRdQl7w
|
||||
rn7V+QfAdTxmk2FAyO80odbrdZQaBR6oLZaFgz/MzkT6dLJXwCjKG/qJrwwV/2sv
|
||||
-----END RSA PRIVATE KEY-----
|
||||
@@ -0,0 +1,14 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA0emZKHHTXLKphYp1XLeh
|
||||
YApXdcWoZenqaFeE9pa3yzP888bsvka7t41JZy7+LOACkjnZj8VQNhMl/JIwtuv8
|
||||
L276Tox94l1CeLSDvH0vxGAvCyZzpW4pgx2xAJk1xWndkEWcx/6A7/kgdePOFNJt
|
||||
41JdDsefqRMlsMjw5vCxxX4V1mlebi6owKkxVBXii476xheX1M2k3cfVbC95h++h
|
||||
Yd7h10DwA17SPbmIDN07QygKqRH7RGkEU5gK2Psc+NFstmYaYj4ew1NF4X9e2Erj
|
||||
5M3RnYoUfkQBaigv+GSjefL6zdp+yZwjrPtZo+YDC/M1SQLEPAsoxQZvOISrLtkH
|
||||
usqdvLB8rsVysZOipJyO3OClI+Vvqy9p4CggM3rHlpzMu+e+sgpZersaNxDEz/rX
|
||||
J/rMXpPTfkVSRMddkQ/s0e0w4oop/tr2Zi4fYfmaJ2Ha49PFBPT7buZeyabLyAOk
|
||||
utwRBkaGiUGmfVidlb0TMK2fQQOS5WottAJJOhEDzl7woCtCBgCeclsuM6v/EDd/
|
||||
9KCeMcWrx7/PHBH2I8RGSfoqTKrZ8plOOPccaY2G6zZjvceExyBjewlzOm5zXI7Z
|
||||
c/L5H9mghOdLjEJ+eRLZIYL9yw1LSDjY3Q0ENkxdvkMdVnZxmsf+UFMFdxBU3VjB
|
||||
hqeH8T9lC2ZWC8OJ5kA4y4ECAwEAAQ==
|
||||
-----END PUBLIC KEY-----
|
||||
@@ -23,3 +23,6 @@ hwi_oauth_redirect:
|
||||
|
||||
fairlane_cookie_consent:
|
||||
resource: "@FairlaneCookieConsentBundle/Resources/config/routing.yml"
|
||||
|
||||
api_login_check:
|
||||
path: /api/login_check
|
||||
@@ -11,12 +11,24 @@ security:
|
||||
pattern: ^/(_(profiler|wdt)|css|images|js)/
|
||||
security: false
|
||||
|
||||
api_login:
|
||||
pattern: ^/api/login_check
|
||||
stateless: true
|
||||
anonymous: true
|
||||
provider: fos_userbundle
|
||||
json_login:
|
||||
check_path: /api/login_check
|
||||
success_handler: lexik_jwt_authentication.handler.authentication_success
|
||||
failure_handler: lexik_jwt_authentication.handler.authentication_failure
|
||||
|
||||
api:
|
||||
pattern: ^/api
|
||||
stateless: true
|
||||
simple_preauth:
|
||||
authenticator: app.api_key_authenticator
|
||||
anonymous: true
|
||||
provider: fos_userbundle
|
||||
guard:
|
||||
authenticators:
|
||||
- lexik_jwt_authentication.jwt_token_authenticator
|
||||
|
||||
main:
|
||||
pattern: ^/
|
||||
@@ -53,6 +65,9 @@ security:
|
||||
- { path: ^/talent/recherche/, roles: ROLE_COMPANY }
|
||||
- { path: ^/entreprise/me/, roles: ROLE_COMPANY }
|
||||
- { path: ^/territoire/me/, roles: ROLE_TERRITORY }
|
||||
- { path: ^/api/login_check, roles: IS_AUTHENTICATED_ANONYMOUSLY}
|
||||
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY, methods: [POST, PUT, DELETE, PATCH] }
|
||||
- { path: ^/api, roles: IS_AUTHENTICATED_ANONYMOUSLY, methods: [GET]}
|
||||
|
||||
encoders:
|
||||
FOS\UserBundle\Model\UserInterface: bcrypt
|
||||
|
||||
@@ -56,7 +56,8 @@
|
||||
"friendsofsymfony/rest-bundle": "^2.4",
|
||||
"symfony/twig-bundle": "^4.1",
|
||||
"white-october/pagerfanta-bundle": "^1.2",
|
||||
"willdurand/hateoas-bundle": "^1.4"
|
||||
"willdurand/hateoas-bundle": "^1.4",
|
||||
"lexik/jwt-authentication-bundle": "^2.5"
|
||||
},
|
||||
"require-dev": {
|
||||
"sensio/generator-bundle": "^3.0",
|
||||
|
||||
Generated
+218
-1
@@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "fbdda65eb230886b1e84516b1efe5b55",
|
||||
"content-hash": "d757874fc53d1ad878cdbb52bd55041c",
|
||||
"packages": [
|
||||
{
|
||||
"name": "alcaeus/mongo-php-adapter",
|
||||
@@ -2967,6 +2967,160 @@
|
||||
],
|
||||
"time": "2016-11-11T18:43:20+00:00"
|
||||
},
|
||||
{
|
||||
"name": "lcobucci/jwt",
|
||||
"version": "3.2.4",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/lcobucci/jwt.git",
|
||||
"reference": "c9704b751315d21735dc98d78d4f37bd73596da7"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/lcobucci/jwt/zipball/c9704b751315d21735dc98d78d4f37bd73596da7",
|
||||
"reference": "c9704b751315d21735dc98d78d4f37bd73596da7",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-openssl": "*",
|
||||
"php": ">=5.5"
|
||||
},
|
||||
"require-dev": {
|
||||
"mdanter/ecc": "~0.3.1",
|
||||
"mikey179/vfsstream": "~1.5",
|
||||
"phpmd/phpmd": "~2.2",
|
||||
"phpunit/php-invoker": "~1.1",
|
||||
"phpunit/phpunit": "~4.5",
|
||||
"squizlabs/php_codesniffer": "~2.3"
|
||||
},
|
||||
"suggest": {
|
||||
"mdanter/ecc": "Required to use Elliptic Curves based algorithms."
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "3.1-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Lcobucci\\JWT\\": "src"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"BSD-3-Clause"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Luís Otávio Cobucci Oblonczyk",
|
||||
"email": "lcobucci@gmail.com",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"description": "A simple library to work with JSON Web Token and JSON Web Signature",
|
||||
"keywords": [
|
||||
"JWS",
|
||||
"jwt"
|
||||
],
|
||||
"time": "2018-08-03T11:23:50+00:00"
|
||||
},
|
||||
{
|
||||
"name": "lexik/jwt-authentication-bundle",
|
||||
"version": "v2.5.4",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/lexik/LexikJWTAuthenticationBundle.git",
|
||||
"reference": "f1c8c9a33c55eeb6105de5a2aedb9ba44a6efbed"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/lexik/LexikJWTAuthenticationBundle/zipball/f1c8c9a33c55eeb6105de5a2aedb9ba44a6efbed",
|
||||
"reference": "f1c8c9a33c55eeb6105de5a2aedb9ba44a6efbed",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"lcobucci/jwt": "^3.2",
|
||||
"namshi/jose": "^7.2",
|
||||
"php": "^5.5|^7.0",
|
||||
"symfony/framework-bundle": "^3.4|^4.0",
|
||||
"symfony/security-bundle": "^3.4|^4.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "^1.1|^2.8",
|
||||
"symfony/browser-kit": "^3.4|^4.0",
|
||||
"symfony/console": "^3.4|^4.0",
|
||||
"symfony/dom-crawler": "^3.4|^4.0",
|
||||
"symfony/phpunit-bridge": "^3.4|^4.0",
|
||||
"symfony/var-dumper": "^3.4|^4.0",
|
||||
"symfony/yaml": "^3.4|^4.0"
|
||||
},
|
||||
"suggest": {
|
||||
"gesdinet/jwt-refresh-token-bundle": "Implements a refresh token system over Json Web Tokens in Symfony",
|
||||
"spomky-labs/lexik-jose-bridge": "Provides a JWT Token encoder with encryption support"
|
||||
},
|
||||
"type": "symfony-bundle",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "2.x-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Lexik\\Bundle\\JWTAuthenticationBundle\\": ""
|
||||
},
|
||||
"exclude-from-classmap": [
|
||||
"/Tests/"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Robin Chalas",
|
||||
"email": "robin.chalas@gmail.com",
|
||||
"homepage": "https://github.com/chalasr"
|
||||
},
|
||||
{
|
||||
"name": "Jeremy Barthe",
|
||||
"email": "j.barthe@lexik.fr",
|
||||
"homepage": "https://github.com/jeremyb"
|
||||
},
|
||||
{
|
||||
"name": "Nicolas Cabot",
|
||||
"email": "n.cabot@lexik.fr",
|
||||
"homepage": "https://github.com/slashfan"
|
||||
},
|
||||
{
|
||||
"name": "Cedric Girard",
|
||||
"email": "c.girard@lexik.fr",
|
||||
"homepage": "https://github.com/cedric-g"
|
||||
},
|
||||
{
|
||||
"name": "Lexik Community",
|
||||
"homepage": "https://github.com/lexik/LexikJWTAuthenticationBundle/graphs/contributors"
|
||||
},
|
||||
{
|
||||
"name": "Dev Lexik",
|
||||
"email": "dev@lexik.fr",
|
||||
"homepage": "https://github.com/lexik"
|
||||
}
|
||||
],
|
||||
"description": "This bundle provides JWT authentication for your Symfony REST API",
|
||||
"homepage": "https://github.com/lexik/LexikJWTAuthenticationBundle",
|
||||
"keywords": [
|
||||
"Authentication",
|
||||
"JWS",
|
||||
"api",
|
||||
"bundle",
|
||||
"jwt",
|
||||
"rest",
|
||||
"symfony"
|
||||
],
|
||||
"time": "2018-08-02T19:32:29+00:00"
|
||||
},
|
||||
{
|
||||
"name": "liip/imagine-bundle",
|
||||
"version": "1.9.1",
|
||||
@@ -3211,6 +3365,69 @@
|
||||
],
|
||||
"time": "2017-06-19T01:22:40+00:00"
|
||||
},
|
||||
{
|
||||
"name": "namshi/jose",
|
||||
"version": "7.2.3",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/namshi/jose.git",
|
||||
"reference": "89a24d7eb3040e285dd5925fcad992378b82bcff"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/namshi/jose/zipball/89a24d7eb3040e285dd5925fcad992378b82bcff",
|
||||
"reference": "89a24d7eb3040e285dd5925fcad992378b82bcff",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-date": "*",
|
||||
"ext-hash": "*",
|
||||
"ext-json": "*",
|
||||
"ext-pcre": "*",
|
||||
"ext-spl": "*",
|
||||
"php": ">=5.5",
|
||||
"symfony/polyfill-php56": "^1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpseclib/phpseclib": "^2.0",
|
||||
"phpunit/phpunit": "^4.5|^5.0",
|
||||
"satooshi/php-coveralls": "^1.0"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-openssl": "Allows to use OpenSSL as crypto engine.",
|
||||
"phpseclib/phpseclib": "Allows to use Phpseclib as crypto engine, use version ^2.0."
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Namshi\\JOSE\\": "src/Namshi/JOSE/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Alessandro Nadalin",
|
||||
"email": "alessandro.nadalin@gmail.com"
|
||||
},
|
||||
{
|
||||
"name": "Alessandro Cinelli (cirpo)",
|
||||
"email": "alessandro.cinelli@gmail.com"
|
||||
}
|
||||
],
|
||||
"description": "JSON Object Signing and Encryption library for PHP.",
|
||||
"keywords": [
|
||||
"JSON Web Signature",
|
||||
"JSON Web Token",
|
||||
"JWS",
|
||||
"json",
|
||||
"jwt",
|
||||
"token"
|
||||
],
|
||||
"time": "2016-12-05T07:27:31+00:00"
|
||||
},
|
||||
{
|
||||
"name": "neutron/temporary-filesystem",
|
||||
"version": "2.3.0",
|
||||
|
||||
@@ -11,6 +11,8 @@ use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationExc
|
||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
||||
use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface;
|
||||
|
||||
// NOT USED AT THE MOMENT
|
||||
|
||||
class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface
|
||||
{
|
||||
public function createToken(Request $request, $providerKey)
|
||||
|
||||
@@ -230,7 +230,7 @@ class FOSUBUserProvider extends BaseFOSUBProvider
|
||||
|
||||
public function getUsernameForApiKey($apiKey)
|
||||
{
|
||||
$username = 'clementNv';
|
||||
$username = 'test';
|
||||
|
||||
return $username;
|
||||
}
|
||||
|
||||
@@ -18,9 +18,9 @@ return array(
|
||||
'8cff32064859f4559445b89279f3199c' => $vendorDir . '/php-http/message/src/filters.php',
|
||||
'2c102faa651ef8ea5874edb585946bce' => $vendorDir . '/swiftmailer/swiftmailer/lib/swift_required.php',
|
||||
'5255c38a0faeba867671b61dfda6d864' => $vendorDir . '/paragonie/random_compat/lib/random.php',
|
||||
'bd9634f2d41831496de0d3dfe4c94881' => $vendorDir . '/symfony/polyfill-php56/bootstrap.php',
|
||||
'32dcc8afd4335739640db7d200c1971d' => $vendorDir . '/symfony/polyfill-apcu/bootstrap.php',
|
||||
'6a47392539ca2329373e0d33e1dba053' => $vendorDir . '/symfony/polyfill-intl-icu/bootstrap.php',
|
||||
'bd9634f2d41831496de0d3dfe4c94881' => $vendorDir . '/symfony/polyfill-php56/bootstrap.php',
|
||||
'023d27dca8066ef29e6739335ea73bad' => $vendorDir . '/symfony/polyfill-php70/bootstrap.php',
|
||||
'ce89ac35a6c330c55f4710717db9ff78' => $vendorDir . '/kriswallsmith/assetic/src/functions.php',
|
||||
);
|
||||
|
||||
@@ -45,9 +45,12 @@ return array(
|
||||
'PackageVersions\\' => array($vendorDir . '/ocramius/package-versions/src/PackageVersions'),
|
||||
'Nmure\\CrawlerDetectBundle\\' => array($vendorDir . '/nmure/crawler-detect-bundle'),
|
||||
'Negotiation\\' => array($vendorDir . '/willdurand/negotiation/src/Negotiation'),
|
||||
'Namshi\\JOSE\\' => array($vendorDir . '/namshi/jose/src/Namshi/JOSE'),
|
||||
'Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'),
|
||||
'MongoDB\\' => array($vendorDir . '/mongodb/mongodb/src'),
|
||||
'Liip\\ImagineBundle\\' => array($vendorDir . '/liip/imagine-bundle'),
|
||||
'Lexik\\Bundle\\JWTAuthenticationBundle\\' => array($vendorDir . '/lexik/jwt-authentication-bundle'),
|
||||
'Lcobucci\\JWT\\' => array($vendorDir . '/lcobucci/jwt/src'),
|
||||
'Jaybizzle\\CrawlerDetect\\' => array($vendorDir . '/jaybizzle/crawler-detect/src'),
|
||||
'JMS\\SerializerBundle\\' => array($vendorDir . '/jms/serializer-bundle'),
|
||||
'Incenteev\\ParameterHandler\\' => array($vendorDir . '/incenteev/composer-parameter-handler'),
|
||||
|
||||
+16
-1
@@ -19,9 +19,9 @@ class ComposerStaticInit1b32d02bd8fea2d524bf79b36ada5830
|
||||
'8cff32064859f4559445b89279f3199c' => __DIR__ . '/..' . '/php-http/message/src/filters.php',
|
||||
'2c102faa651ef8ea5874edb585946bce' => __DIR__ . '/..' . '/swiftmailer/swiftmailer/lib/swift_required.php',
|
||||
'5255c38a0faeba867671b61dfda6d864' => __DIR__ . '/..' . '/paragonie/random_compat/lib/random.php',
|
||||
'bd9634f2d41831496de0d3dfe4c94881' => __DIR__ . '/..' . '/symfony/polyfill-php56/bootstrap.php',
|
||||
'32dcc8afd4335739640db7d200c1971d' => __DIR__ . '/..' . '/symfony/polyfill-apcu/bootstrap.php',
|
||||
'6a47392539ca2329373e0d33e1dba053' => __DIR__ . '/..' . '/symfony/polyfill-intl-icu/bootstrap.php',
|
||||
'bd9634f2d41831496de0d3dfe4c94881' => __DIR__ . '/..' . '/symfony/polyfill-php56/bootstrap.php',
|
||||
'023d27dca8066ef29e6739335ea73bad' => __DIR__ . '/..' . '/symfony/polyfill-php70/bootstrap.php',
|
||||
'ce89ac35a6c330c55f4710717db9ff78' => __DIR__ . '/..' . '/kriswallsmith/assetic/src/functions.php',
|
||||
);
|
||||
@@ -86,6 +86,7 @@ class ComposerStaticInit1b32d02bd8fea2d524bf79b36ada5830
|
||||
array (
|
||||
'Nmure\\CrawlerDetectBundle\\' => 26,
|
||||
'Negotiation\\' => 12,
|
||||
'Namshi\\JOSE\\' => 12,
|
||||
),
|
||||
'M' =>
|
||||
array (
|
||||
@@ -95,6 +96,8 @@ class ComposerStaticInit1b32d02bd8fea2d524bf79b36ada5830
|
||||
'L' =>
|
||||
array (
|
||||
'Liip\\ImagineBundle\\' => 19,
|
||||
'Lexik\\Bundle\\JWTAuthenticationBundle\\' => 37,
|
||||
'Lcobucci\\JWT\\' => 13,
|
||||
),
|
||||
'J' =>
|
||||
array (
|
||||
@@ -324,6 +327,10 @@ class ComposerStaticInit1b32d02bd8fea2d524bf79b36ada5830
|
||||
array (
|
||||
0 => __DIR__ . '/..' . '/willdurand/negotiation/src/Negotiation',
|
||||
),
|
||||
'Namshi\\JOSE\\' =>
|
||||
array (
|
||||
0 => __DIR__ . '/..' . '/namshi/jose/src/Namshi/JOSE',
|
||||
),
|
||||
'Monolog\\' =>
|
||||
array (
|
||||
0 => __DIR__ . '/..' . '/monolog/monolog/src/Monolog',
|
||||
@@ -336,6 +343,14 @@ class ComposerStaticInit1b32d02bd8fea2d524bf79b36ada5830
|
||||
array (
|
||||
0 => __DIR__ . '/..' . '/liip/imagine-bundle',
|
||||
),
|
||||
'Lexik\\Bundle\\JWTAuthenticationBundle\\' =>
|
||||
array (
|
||||
0 => __DIR__ . '/..' . '/lexik/jwt-authentication-bundle',
|
||||
),
|
||||
'Lcobucci\\JWT\\' =>
|
||||
array (
|
||||
0 => __DIR__ . '/..' . '/lcobucci/jwt/src',
|
||||
),
|
||||
'Jaybizzle\\CrawlerDetect\\' =>
|
||||
array (
|
||||
0 => __DIR__ . '/..' . '/jaybizzle/crawler-detect/src',
|
||||
|
||||
@@ -3127,6 +3127,164 @@
|
||||
"minification"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "lcobucci/jwt",
|
||||
"version": "3.2.4",
|
||||
"version_normalized": "3.2.4.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/lcobucci/jwt.git",
|
||||
"reference": "c9704b751315d21735dc98d78d4f37bd73596da7"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/lcobucci/jwt/zipball/c9704b751315d21735dc98d78d4f37bd73596da7",
|
||||
"reference": "c9704b751315d21735dc98d78d4f37bd73596da7",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-openssl": "*",
|
||||
"php": ">=5.5"
|
||||
},
|
||||
"require-dev": {
|
||||
"mdanter/ecc": "~0.3.1",
|
||||
"mikey179/vfsstream": "~1.5",
|
||||
"phpmd/phpmd": "~2.2",
|
||||
"phpunit/php-invoker": "~1.1",
|
||||
"phpunit/phpunit": "~4.5",
|
||||
"squizlabs/php_codesniffer": "~2.3"
|
||||
},
|
||||
"suggest": {
|
||||
"mdanter/ecc": "Required to use Elliptic Curves based algorithms."
|
||||
},
|
||||
"time": "2018-08-03T11:23:50+00:00",
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "3.1-dev"
|
||||
}
|
||||
},
|
||||
"installation-source": "dist",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Lcobucci\\JWT\\": "src"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"BSD-3-Clause"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Luís Otávio Cobucci Oblonczyk",
|
||||
"email": "lcobucci@gmail.com",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"description": "A simple library to work with JSON Web Token and JSON Web Signature",
|
||||
"keywords": [
|
||||
"JWS",
|
||||
"jwt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "lexik/jwt-authentication-bundle",
|
||||
"version": "v2.5.4",
|
||||
"version_normalized": "2.5.4.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/lexik/LexikJWTAuthenticationBundle.git",
|
||||
"reference": "f1c8c9a33c55eeb6105de5a2aedb9ba44a6efbed"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/lexik/LexikJWTAuthenticationBundle/zipball/f1c8c9a33c55eeb6105de5a2aedb9ba44a6efbed",
|
||||
"reference": "f1c8c9a33c55eeb6105de5a2aedb9ba44a6efbed",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"lcobucci/jwt": "^3.2",
|
||||
"namshi/jose": "^7.2",
|
||||
"php": "^5.5|^7.0",
|
||||
"symfony/framework-bundle": "^3.4|^4.0",
|
||||
"symfony/security-bundle": "^3.4|^4.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "^1.1|^2.8",
|
||||
"symfony/browser-kit": "^3.4|^4.0",
|
||||
"symfony/console": "^3.4|^4.0",
|
||||
"symfony/dom-crawler": "^3.4|^4.0",
|
||||
"symfony/phpunit-bridge": "^3.4|^4.0",
|
||||
"symfony/var-dumper": "^3.4|^4.0",
|
||||
"symfony/yaml": "^3.4|^4.0"
|
||||
},
|
||||
"suggest": {
|
||||
"gesdinet/jwt-refresh-token-bundle": "Implements a refresh token system over Json Web Tokens in Symfony",
|
||||
"spomky-labs/lexik-jose-bridge": "Provides a JWT Token encoder with encryption support"
|
||||
},
|
||||
"time": "2018-08-02T19:32:29+00:00",
|
||||
"type": "symfony-bundle",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "2.x-dev"
|
||||
}
|
||||
},
|
||||
"installation-source": "dist",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Lexik\\Bundle\\JWTAuthenticationBundle\\": ""
|
||||
},
|
||||
"exclude-from-classmap": [
|
||||
"/Tests/"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Robin Chalas",
|
||||
"email": "robin.chalas@gmail.com",
|
||||
"homepage": "https://github.com/chalasr"
|
||||
},
|
||||
{
|
||||
"name": "Jeremy Barthe",
|
||||
"email": "j.barthe@lexik.fr",
|
||||
"homepage": "https://github.com/jeremyb"
|
||||
},
|
||||
{
|
||||
"name": "Nicolas Cabot",
|
||||
"email": "n.cabot@lexik.fr",
|
||||
"homepage": "https://github.com/slashfan"
|
||||
},
|
||||
{
|
||||
"name": "Cedric Girard",
|
||||
"email": "c.girard@lexik.fr",
|
||||
"homepage": "https://github.com/cedric-g"
|
||||
},
|
||||
{
|
||||
"name": "Lexik Community",
|
||||
"homepage": "https://github.com/lexik/LexikJWTAuthenticationBundle/graphs/contributors"
|
||||
},
|
||||
{
|
||||
"name": "Dev Lexik",
|
||||
"email": "dev@lexik.fr",
|
||||
"homepage": "https://github.com/lexik"
|
||||
}
|
||||
],
|
||||
"description": "This bundle provides JWT authentication for your Symfony REST API",
|
||||
"homepage": "https://github.com/lexik/LexikJWTAuthenticationBundle",
|
||||
"keywords": [
|
||||
"Authentication",
|
||||
"JWS",
|
||||
"api",
|
||||
"bundle",
|
||||
"jwt",
|
||||
"rest",
|
||||
"symfony"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "liip/imagine-bundle",
|
||||
"version": "1.9.1",
|
||||
@@ -3377,6 +3535,71 @@
|
||||
"psr-3"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "namshi/jose",
|
||||
"version": "7.2.3",
|
||||
"version_normalized": "7.2.3.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/namshi/jose.git",
|
||||
"reference": "89a24d7eb3040e285dd5925fcad992378b82bcff"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/namshi/jose/zipball/89a24d7eb3040e285dd5925fcad992378b82bcff",
|
||||
"reference": "89a24d7eb3040e285dd5925fcad992378b82bcff",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-date": "*",
|
||||
"ext-hash": "*",
|
||||
"ext-json": "*",
|
||||
"ext-pcre": "*",
|
||||
"ext-spl": "*",
|
||||
"php": ">=5.5",
|
||||
"symfony/polyfill-php56": "^1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpseclib/phpseclib": "^2.0",
|
||||
"phpunit/phpunit": "^4.5|^5.0",
|
||||
"satooshi/php-coveralls": "^1.0"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-openssl": "Allows to use OpenSSL as crypto engine.",
|
||||
"phpseclib/phpseclib": "Allows to use Phpseclib as crypto engine, use version ^2.0."
|
||||
},
|
||||
"time": "2016-12-05T07:27:31+00:00",
|
||||
"type": "library",
|
||||
"installation-source": "dist",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Namshi\\JOSE\\": "src/Namshi/JOSE/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Alessandro Nadalin",
|
||||
"email": "alessandro.nadalin@gmail.com"
|
||||
},
|
||||
{
|
||||
"name": "Alessandro Cinelli (cirpo)",
|
||||
"email": "alessandro.cinelli@gmail.com"
|
||||
}
|
||||
],
|
||||
"description": "JSON Object Signing and Encryption library for PHP.",
|
||||
"keywords": [
|
||||
"JSON Web Signature",
|
||||
"JSON Web Token",
|
||||
"JWS",
|
||||
"json",
|
||||
"jwt",
|
||||
"token"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "neutron/temporary-filesystem",
|
||||
"version": "2.3.0",
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
vendor
|
||||
phpunit.xml
|
||||
@@ -0,0 +1,56 @@
|
||||
build:
|
||||
environment:
|
||||
mysql: false
|
||||
postgresql: false
|
||||
redis: false
|
||||
rabbitmq: false
|
||||
php:
|
||||
version: 5.6
|
||||
tools:
|
||||
php_sim: true
|
||||
php_pdepend: true
|
||||
php_analyzer: true
|
||||
php_changetracking: true
|
||||
php_code_sniffer:
|
||||
config:
|
||||
standard: "PSR2"
|
||||
php_mess_detector: true
|
||||
checks:
|
||||
php:
|
||||
code_rating: true
|
||||
duplication: true
|
||||
argument_type_checks: true
|
||||
assignment_of_null_return: true
|
||||
avoid_conflicting_incrementers: true
|
||||
avoid_useless_overridden_methods: true
|
||||
catch_class_exists: true
|
||||
closure_use_modifiable: true
|
||||
closure_use_not_conflicting: true
|
||||
deprecated_code_usage: true
|
||||
method_calls_on_non_object: true
|
||||
missing_arguments: true
|
||||
no_duplicate_arguments: true
|
||||
no_non_implemented_abstract_methods: true
|
||||
no_property_on_interface: true
|
||||
parameter_non_unique: true
|
||||
precedence_in_conditions: true
|
||||
precedence_mistakes: true
|
||||
require_php_tag_first: true
|
||||
security_vulnerabilities: true
|
||||
sql_injection_vulnerabilities: true
|
||||
too_many_arguments: true
|
||||
unreachable_code: true
|
||||
unused_methods: true
|
||||
unused_parameters: true
|
||||
unused_properties: true
|
||||
unused_variables: true
|
||||
use_statement_alias_conflict: true
|
||||
useless_calls: true
|
||||
variable_existence: true
|
||||
verify_access_scope_valid: true
|
||||
verify_argument_usable_as_reference: true
|
||||
verify_property_names: true
|
||||
|
||||
filter:
|
||||
excluded_paths:
|
||||
- test/*
|
||||
@@ -0,0 +1,22 @@
|
||||
language: php
|
||||
php:
|
||||
- 5.5
|
||||
- 5.6
|
||||
- 7.0
|
||||
- 7.1
|
||||
- 7.2
|
||||
- nightly
|
||||
- hhvm
|
||||
- hhvm-nightly
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
- php: 7.1
|
||||
- php: 7.2
|
||||
- php: nightly
|
||||
- php: hhvm
|
||||
- php: hhvm-nightly
|
||||
|
||||
before_script:
|
||||
- composer selfupdate
|
||||
- composer install --prefer-dist -o
|
||||
@@ -0,0 +1,27 @@
|
||||
Copyright (c) 2014-2015, Luís Otávio Cobucci Oblonczyk
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of the {organization} nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
@@ -0,0 +1,149 @@
|
||||
# JWT
|
||||
[](https://gitter.im/lcobucci/jwt?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [](https://packagist.org/packages/lcobucci/jwt) [](https://packagist.org/packages/lcobucci/jwt)
|
||||
|
||||

|
||||
[](http://travis-ci.org/#!/lcobucci/jwt)
|
||||
[](https://scrutinizer-ci.com/g/lcobucci/jwt/?branch=master)
|
||||
[](https://scrutinizer-ci.com/g/lcobucci/jwt/?branch=master)
|
||||
|
||||
A simple library to work with JSON Web Token and JSON Web Signature (requires PHP 5.5+).
|
||||
The implementation is based on the [current draft](http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-32).
|
||||
|
||||
## Installation
|
||||
|
||||
Package is available on [Packagist](http://packagist.org/packages/lcobucci/jwt),
|
||||
you can install it using [Composer](http://getcomposer.org).
|
||||
|
||||
```shell
|
||||
composer require lcobucci/jwt
|
||||
```
|
||||
|
||||
### Dependencies
|
||||
|
||||
- PHP 5.5+
|
||||
- OpenSSL Extension
|
||||
|
||||
## Basic usage
|
||||
|
||||
### Creating
|
||||
|
||||
Just use the builder to create a new JWT/JWS tokens:
|
||||
|
||||
```php
|
||||
use Lcobucci\JWT\Builder;
|
||||
|
||||
$token = (new Builder())->setIssuer('http://example.com') // Configures the issuer (iss claim)
|
||||
->setAudience('http://example.org') // Configures the audience (aud claim)
|
||||
->setId('4f1g23a12aa', true) // Configures the id (jti claim), replicating as a header item
|
||||
->setIssuedAt(time()) // Configures the time that the token was issue (iat claim)
|
||||
->setNotBefore(time() + 60) // Configures the time that the token can be used (nbf claim)
|
||||
->setExpiration(time() + 3600) // Configures the expiration time of the token (exp claim)
|
||||
->set('uid', 1) // Configures a new claim, called "uid"
|
||||
->getToken(); // Retrieves the generated token
|
||||
|
||||
|
||||
$token->getHeaders(); // Retrieves the token headers
|
||||
$token->getClaims(); // Retrieves the token claims
|
||||
|
||||
echo $token->getHeader('jti'); // will print "4f1g23a12aa"
|
||||
echo $token->getClaim('iss'); // will print "http://example.com"
|
||||
echo $token->getClaim('uid'); // will print "1"
|
||||
echo $token; // The string representation of the object is a JWT string (pretty easy, right?)
|
||||
```
|
||||
|
||||
### Parsing from strings
|
||||
|
||||
Use the parser to create a new token from a JWT string (using the previous token as example):
|
||||
|
||||
```php
|
||||
use Lcobucci\JWT\Parser;
|
||||
|
||||
$token = (new Parser())->parse((string) $token); // Parses from a string
|
||||
$token->getHeaders(); // Retrieves the token header
|
||||
$token->getClaims(); // Retrieves the token claims
|
||||
|
||||
echo $token->getHeader('jti'); // will print "4f1g23a12aa"
|
||||
echo $token->getClaim('iss'); // will print "http://example.com"
|
||||
echo $token->getClaim('uid'); // will print "1"
|
||||
```
|
||||
|
||||
### Validating
|
||||
|
||||
We can easily validate if the token is valid (using the previous token as example):
|
||||
|
||||
```php
|
||||
use Lcobucci\JWT\ValidationData;
|
||||
|
||||
$data = new ValidationData(); // It will use the current time to validate (iat, nbf and exp)
|
||||
$data->setIssuer('http://example.com');
|
||||
$data->setAudience('http://example.org');
|
||||
$data->setId('4f1g23a12aa');
|
||||
|
||||
var_dump($token->validate($data)); // false, because we created a token that cannot be used before of `time() + 60`
|
||||
|
||||
$data->setCurrentTime(time() + 60); // changing the validation time to future
|
||||
|
||||
var_dump($token->validate($data)); // true, because validation information is equals to data contained on the token
|
||||
|
||||
$data->setCurrentTime(time() + 4000); // changing the validation time to future
|
||||
|
||||
var_dump($token->validate($data)); // false, because token is expired since current time is greater than exp
|
||||
```
|
||||
|
||||
## Token signature
|
||||
|
||||
We can use signatures to be able to verify if the token was not modified after its generation. This library implements Hmac, RSA and ECDSA signatures (using 256, 384 and 512).
|
||||
|
||||
### Hmac
|
||||
|
||||
Hmac signatures are really simple to be used:
|
||||
|
||||
```php
|
||||
use Lcobucci\JWT\Builder;
|
||||
use Lcobucci\JWT\Signer\Hmac\Sha256;
|
||||
|
||||
$signer = new Sha256();
|
||||
|
||||
$token = (new Builder())->setIssuer('http://example.com') // Configures the issuer (iss claim)
|
||||
->setAudience('http://example.org') // Configures the audience (aud claim)
|
||||
->setId('4f1g23a12aa', true) // Configures the id (jti claim), replicating as a header item
|
||||
->setIssuedAt(time()) // Configures the time that the token was issue (iat claim)
|
||||
->setNotBefore(time() + 60) // Configures the time that the token can be used (nbf claim)
|
||||
->setExpiration(time() + 3600) // Configures the expiration time of the token (exp claim)
|
||||
->set('uid', 1) // Configures a new claim, called "uid"
|
||||
->sign($signer, 'testing') // creates a signature using "testing" as key
|
||||
->getToken(); // Retrieves the generated token
|
||||
|
||||
|
||||
var_dump($token->verify($signer, 'testing 1')); // false, because the key is different
|
||||
var_dump($token->verify($signer, 'testing')); // true, because the key is the same
|
||||
```
|
||||
|
||||
### RSA and ECDSA
|
||||
|
||||
RSA and ECDSA signatures are based on public and private keys so you have to generate using the private key and verify using the public key:
|
||||
|
||||
```php
|
||||
use Lcobucci\JWT\Builder;
|
||||
use Lcobucci\JWT\Signer\Keychain; // just to make our life simpler
|
||||
use Lcobucci\JWT\Signer\Rsa\Sha256; // you can use Lcobucci\JWT\Signer\Ecdsa\Sha256 if you're using ECDSA keys
|
||||
|
||||
$signer = new Sha256();
|
||||
|
||||
$keychain = new Keychain();
|
||||
|
||||
$token = (new Builder())->setIssuer('http://example.com') // Configures the issuer (iss claim)
|
||||
->setAudience('http://example.org') // Configures the audience (aud claim)
|
||||
->setId('4f1g23a12aa', true) // Configures the id (jti claim), replicating as a header item
|
||||
->setIssuedAt(time()) // Configures the time that the token was issue (iat claim)
|
||||
->setNotBefore(time() + 60) // Configures the time that the token can be used (nbf claim)
|
||||
->setExpiration(time() + 3600) // Configures the expiration time of the token (nbf claim)
|
||||
->set('uid', 1) // Configures a new claim, called "uid"
|
||||
->sign($signer, $keychain->getPrivateKey('file://{path to your private key}')) // creates a signature using your private key
|
||||
->getToken(); // Retrieves the generated token
|
||||
|
||||
|
||||
var_dump($token->verify($signer, $keychain->getPublicKey('file://{path to your public key}'))); // true when the public key was generated by the private one =)
|
||||
```
|
||||
|
||||
**It's important to say that if you're using RSA keys you shouldn't invoke ECDSA signers (and vice-versa), otherwise ```sign()``` and ```verify()``` will raise an exception!**
|
||||
@@ -0,0 +1,52 @@
|
||||
{
|
||||
"name": "lcobucci/jwt",
|
||||
"description": "A simple library to work with JSON Web Token and JSON Web Signature",
|
||||
"type": "library",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Luís Otávio Cobucci Oblonczyk",
|
||||
"email": "lcobucci@gmail.com",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"keywords": [
|
||||
"JWT",
|
||||
"JWS"
|
||||
],
|
||||
"license": [
|
||||
"BSD-3-Clause"
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.5",
|
||||
"ext-openssl": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "~4.5",
|
||||
"squizlabs/php_codesniffer": "~2.3",
|
||||
"phpmd/phpmd": "~2.2",
|
||||
"phpunit/php-invoker": "~1.1",
|
||||
"mikey179/vfsStream": "~1.5",
|
||||
"mdanter/ecc": "~0.3.1"
|
||||
},
|
||||
"suggest":{
|
||||
"mdanter/ecc": "Required to use Elliptic Curves based algorithms."
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Lcobucci\\JWT\\": "src"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Lcobucci\\JWT\\": [
|
||||
"test/unit",
|
||||
"test/functional"
|
||||
]
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "3.1-dev"
|
||||
}
|
||||
}
|
||||
}
|
||||
+1898
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
|
||||
backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
bootstrap="vendor/autoload.php"
|
||||
colors="true"
|
||||
verbose="true"
|
||||
beStrictAboutOutputDuringTests="true"
|
||||
beStrictAboutTestSize="true"
|
||||
beStrictAboutTestsThatDoNotTestAnything="true"
|
||||
beStrictAboutTodoAnnotatedTests="true"
|
||||
checkForUnintentionallyCoveredCode="true"
|
||||
forceCoversAnnotation="true">
|
||||
<testsuites>
|
||||
<testsuite name="Unit Test Suite">
|
||||
<directory>test/unit</directory>
|
||||
</testsuite>
|
||||
<testsuite name="Integration Test Suite">
|
||||
<directory>test/functional</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<filter>
|
||||
<whitelist processUncoveredFilesFromWhitelist="true">
|
||||
<directory suffix=".php">src</directory>
|
||||
</whitelist>
|
||||
|
||||
<blacklist>
|
||||
<directory suffix=".php">vendor</directory>
|
||||
</blacklist>
|
||||
</filter>
|
||||
</phpunit>
|
||||
@@ -0,0 +1,277 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT;
|
||||
|
||||
use BadMethodCallException;
|
||||
use Lcobucci\JWT\Claim\Factory as ClaimFactory;
|
||||
use Lcobucci\JWT\Parsing\Encoder;
|
||||
use Lcobucci\JWT\Signer\Key;
|
||||
|
||||
/**
|
||||
* This class makes easier the token creation process
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class Builder
|
||||
{
|
||||
/**
|
||||
* The token header
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $headers;
|
||||
|
||||
/**
|
||||
* The token claim set
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $claims;
|
||||
|
||||
/**
|
||||
* The token signature
|
||||
*
|
||||
* @var Signature
|
||||
*/
|
||||
private $signature;
|
||||
|
||||
/**
|
||||
* The data encoder
|
||||
*
|
||||
* @var Encoder
|
||||
*/
|
||||
private $encoder;
|
||||
|
||||
/**
|
||||
* The factory of claims
|
||||
*
|
||||
* @var ClaimFactory
|
||||
*/
|
||||
private $claimFactory;
|
||||
|
||||
/**
|
||||
* Initializes a new builder
|
||||
*
|
||||
* @param Encoder $encoder
|
||||
* @param ClaimFactory $claimFactory
|
||||
*/
|
||||
public function __construct(
|
||||
Encoder $encoder = null,
|
||||
ClaimFactory $claimFactory = null
|
||||
) {
|
||||
$this->encoder = $encoder ?: new Encoder();
|
||||
$this->claimFactory = $claimFactory ?: new ClaimFactory();
|
||||
$this->headers = ['typ'=> 'JWT', 'alg' => 'none'];
|
||||
$this->claims = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the audience
|
||||
*
|
||||
* @param string $audience
|
||||
* @param boolean $replicateAsHeader
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function setAudience($audience, $replicateAsHeader = false)
|
||||
{
|
||||
return $this->setRegisteredClaim('aud', (string) $audience, $replicateAsHeader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the expiration time
|
||||
*
|
||||
* @param int $expiration
|
||||
* @param boolean $replicateAsHeader
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function setExpiration($expiration, $replicateAsHeader = false)
|
||||
{
|
||||
return $this->setRegisteredClaim('exp', (int) $expiration, $replicateAsHeader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the token id
|
||||
*
|
||||
* @param string $id
|
||||
* @param boolean $replicateAsHeader
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function setId($id, $replicateAsHeader = false)
|
||||
{
|
||||
return $this->setRegisteredClaim('jti', (string) $id, $replicateAsHeader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the time that the token was issued
|
||||
*
|
||||
* @param int $issuedAt
|
||||
* @param boolean $replicateAsHeader
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function setIssuedAt($issuedAt, $replicateAsHeader = false)
|
||||
{
|
||||
return $this->setRegisteredClaim('iat', (int) $issuedAt, $replicateAsHeader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the issuer
|
||||
*
|
||||
* @param string $issuer
|
||||
* @param boolean $replicateAsHeader
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function setIssuer($issuer, $replicateAsHeader = false)
|
||||
{
|
||||
return $this->setRegisteredClaim('iss', (string) $issuer, $replicateAsHeader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the time before which the token cannot be accepted
|
||||
*
|
||||
* @param int $notBefore
|
||||
* @param boolean $replicateAsHeader
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function setNotBefore($notBefore, $replicateAsHeader = false)
|
||||
{
|
||||
return $this->setRegisteredClaim('nbf', (int) $notBefore, $replicateAsHeader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the subject
|
||||
*
|
||||
* @param string $subject
|
||||
* @param boolean $replicateAsHeader
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function setSubject($subject, $replicateAsHeader = false)
|
||||
{
|
||||
return $this->setRegisteredClaim('sub', (string) $subject, $replicateAsHeader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures a registed claim
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
* @param boolean $replicate
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
protected function setRegisteredClaim($name, $value, $replicate)
|
||||
{
|
||||
$this->set($name, $value);
|
||||
|
||||
if ($replicate) {
|
||||
$this->headers[$name] = $this->claims[$name];
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures a header item
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return Builder
|
||||
*
|
||||
* @throws BadMethodCallException When data has been already signed
|
||||
*/
|
||||
public function setHeader($name, $value)
|
||||
{
|
||||
if ($this->signature) {
|
||||
throw new BadMethodCallException('You must unsign before make changes');
|
||||
}
|
||||
|
||||
$this->headers[(string) $name] = $this->claimFactory->create($name, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures a claim item
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return Builder
|
||||
*
|
||||
* @throws BadMethodCallException When data has been already signed
|
||||
*/
|
||||
public function set($name, $value)
|
||||
{
|
||||
if ($this->signature) {
|
||||
throw new BadMethodCallException('You must unsign before making changes');
|
||||
}
|
||||
|
||||
$this->claims[(string) $name] = $this->claimFactory->create($name, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Signs the data
|
||||
*
|
||||
* @param Signer $signer
|
||||
* @param Key|string $key
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function sign(Signer $signer, $key)
|
||||
{
|
||||
$signer->modifyHeader($this->headers);
|
||||
|
||||
$this->signature = $signer->sign(
|
||||
$this->getToken()->getPayload(),
|
||||
$key
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the signature from the builder
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function unsign()
|
||||
{
|
||||
$this->signature = null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the resultant token
|
||||
*
|
||||
* @return Token
|
||||
*/
|
||||
public function getToken()
|
||||
{
|
||||
$payload = [
|
||||
$this->encoder->base64UrlEncode($this->encoder->jsonEncode($this->headers)),
|
||||
$this->encoder->base64UrlEncode($this->encoder->jsonEncode($this->claims))
|
||||
];
|
||||
|
||||
if ($this->signature !== null) {
|
||||
$payload[] = $this->encoder->base64UrlEncode($this->signature);
|
||||
}
|
||||
|
||||
return new Token($this->headers, $this->claims, $this->signature, $payload);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT;
|
||||
|
||||
use JsonSerializable;
|
||||
|
||||
/**
|
||||
* Basic interface for token claims
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.0.0
|
||||
*/
|
||||
interface Claim extends JsonSerializable
|
||||
{
|
||||
/**
|
||||
* Returns the claim name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
|
||||
/**
|
||||
* Returns the claim value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getValue();
|
||||
|
||||
/**
|
||||
* Returns the string representation of the claim
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString();
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Claim;
|
||||
|
||||
use Lcobucci\JWT\Claim;
|
||||
|
||||
/**
|
||||
* The default claim
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class Basic implements Claim
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
private $value;
|
||||
|
||||
/**
|
||||
* Initializes the claim
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __construct($name, $value)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getValue()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return (string) $this->value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Claim;
|
||||
|
||||
use Lcobucci\JWT\Claim;
|
||||
use Lcobucci\JWT\ValidationData;
|
||||
|
||||
/**
|
||||
* Validatable claim that checks if value is strictly equals to the given data
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class EqualsTo extends Basic implements Claim, Validatable
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate(ValidationData $data)
|
||||
{
|
||||
if ($data->has($this->getName())) {
|
||||
return $this->getValue() === $data->get($this->getName());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Claim;
|
||||
|
||||
use Lcobucci\JWT\Claim;
|
||||
|
||||
/**
|
||||
* Class that create claims
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class Factory
|
||||
{
|
||||
/**
|
||||
* The list of claim callbacks
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $callbacks;
|
||||
|
||||
/**
|
||||
* Initializes the factory, registering the default callbacks
|
||||
*
|
||||
* @param array $callbacks
|
||||
*/
|
||||
public function __construct(array $callbacks = [])
|
||||
{
|
||||
$this->callbacks = array_merge(
|
||||
[
|
||||
'iat' => [$this, 'createLesserOrEqualsTo'],
|
||||
'nbf' => [$this, 'createLesserOrEqualsTo'],
|
||||
'exp' => [$this, 'createGreaterOrEqualsTo'],
|
||||
'iss' => [$this, 'createEqualsTo'],
|
||||
'aud' => [$this, 'createEqualsTo'],
|
||||
'sub' => [$this, 'createEqualsTo'],
|
||||
'jti' => [$this, 'createEqualsTo']
|
||||
],
|
||||
$callbacks
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new claim
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return Claim
|
||||
*/
|
||||
public function create($name, $value)
|
||||
{
|
||||
if (!empty($this->callbacks[$name])) {
|
||||
return call_user_func($this->callbacks[$name], $name, $value);
|
||||
}
|
||||
|
||||
return $this->createBasic($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a claim that can be compared (greator or equals)
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return GreaterOrEqualsTo
|
||||
*/
|
||||
private function createGreaterOrEqualsTo($name, $value)
|
||||
{
|
||||
return new GreaterOrEqualsTo($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a claim that can be compared (greator or equals)
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return LesserOrEqualsTo
|
||||
*/
|
||||
private function createLesserOrEqualsTo($name, $value)
|
||||
{
|
||||
return new LesserOrEqualsTo($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a claim that can be compared (equals)
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return EqualsTo
|
||||
*/
|
||||
private function createEqualsTo($name, $value)
|
||||
{
|
||||
return new EqualsTo($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a basic claim
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return Basic
|
||||
*/
|
||||
private function createBasic($name, $value)
|
||||
{
|
||||
return new Basic($name, $value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Claim;
|
||||
|
||||
use Lcobucci\JWT\Claim;
|
||||
use Lcobucci\JWT\ValidationData;
|
||||
|
||||
/**
|
||||
* Validatable claim that checks if value is greater or equals the given data
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class GreaterOrEqualsTo extends Basic implements Claim, Validatable
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate(ValidationData $data)
|
||||
{
|
||||
if ($data->has($this->getName())) {
|
||||
return $this->getValue() >= $data->get($this->getName());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Claim;
|
||||
|
||||
use Lcobucci\JWT\Claim;
|
||||
use Lcobucci\JWT\ValidationData;
|
||||
|
||||
/**
|
||||
* Validatable claim that checks if value is lesser or equals to the given data
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class LesserOrEqualsTo extends Basic implements Claim, Validatable
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate(ValidationData $data)
|
||||
{
|
||||
if ($data->has($this->getName())) {
|
||||
return $this->getValue() <= $data->get($this->getName());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Claim;
|
||||
|
||||
use Lcobucci\JWT\ValidationData;
|
||||
|
||||
/**
|
||||
* Basic interface for validatable token claims
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.0.0
|
||||
*/
|
||||
interface Validatable
|
||||
{
|
||||
/**
|
||||
* Returns if claim is valid according with given data
|
||||
*
|
||||
* @param ValidationData $data
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function validate(ValidationData $data);
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Lcobucci\JWT\Claim\Factory as ClaimFactory;
|
||||
use Lcobucci\JWT\Parsing\Decoder;
|
||||
|
||||
/**
|
||||
* This class parses the JWT strings and convert them into tokens
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class Parser
|
||||
{
|
||||
/**
|
||||
* The data decoder
|
||||
*
|
||||
* @var Decoder
|
||||
*/
|
||||
private $decoder;
|
||||
|
||||
/**
|
||||
* The claims factory
|
||||
*
|
||||
* @var ClaimFactory
|
||||
*/
|
||||
private $claimFactory;
|
||||
|
||||
/**
|
||||
* Initializes the object
|
||||
*
|
||||
* @param Decoder $decoder
|
||||
* @param ClaimFactory $claimFactory
|
||||
*/
|
||||
public function __construct(
|
||||
Decoder $decoder = null,
|
||||
ClaimFactory $claimFactory = null
|
||||
) {
|
||||
$this->decoder = $decoder ?: new Decoder();
|
||||
$this->claimFactory = $claimFactory ?: new ClaimFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the JWT and returns a token
|
||||
*
|
||||
* @param string $jwt
|
||||
*
|
||||
* @return Token
|
||||
*/
|
||||
public function parse($jwt)
|
||||
{
|
||||
$data = $this->splitJwt($jwt);
|
||||
$header = $this->parseHeader($data[0]);
|
||||
$claims = $this->parseClaims($data[1]);
|
||||
$signature = $this->parseSignature($header, $data[2]);
|
||||
|
||||
foreach ($claims as $name => $value) {
|
||||
if (isset($header[$name])) {
|
||||
$header[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
if ($signature === null) {
|
||||
unset($data[2]);
|
||||
}
|
||||
|
||||
return new Token($header, $claims, $signature, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits the JWT string into an array
|
||||
*
|
||||
* @param string $jwt
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws InvalidArgumentException When JWT is not a string or is invalid
|
||||
*/
|
||||
protected function splitJwt($jwt)
|
||||
{
|
||||
if (!is_string($jwt)) {
|
||||
throw new InvalidArgumentException('The JWT string must have two dots');
|
||||
}
|
||||
|
||||
$data = explode('.', $jwt);
|
||||
|
||||
if (count($data) != 3) {
|
||||
throw new InvalidArgumentException('The JWT string must have two dots');
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the header from a string
|
||||
*
|
||||
* @param string $data
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws InvalidArgumentException When an invalid header is informed
|
||||
*/
|
||||
protected function parseHeader($data)
|
||||
{
|
||||
$header = (array) $this->decoder->jsonDecode($this->decoder->base64UrlDecode($data));
|
||||
|
||||
if (isset($header['enc'])) {
|
||||
throw new InvalidArgumentException('Encryption is not supported yet');
|
||||
}
|
||||
|
||||
return $header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the claim set from a string
|
||||
*
|
||||
* @param string $data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function parseClaims($data)
|
||||
{
|
||||
$claims = (array) $this->decoder->jsonDecode($this->decoder->base64UrlDecode($data));
|
||||
|
||||
foreach ($claims as $name => &$value) {
|
||||
$value = $this->claimFactory->create($name, $value);
|
||||
}
|
||||
|
||||
return $claims;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the signature from given data
|
||||
*
|
||||
* @param array $header
|
||||
* @param string $data
|
||||
*
|
||||
* @return Signature
|
||||
*/
|
||||
protected function parseSignature(array $header, $data)
|
||||
{
|
||||
if ($data == '' || !isset($header['alg']) || $header['alg'] == 'none') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$hash = $this->decoder->base64UrlDecode($data);
|
||||
|
||||
return new Signature($hash);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Parsing;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Class that decodes data according with the specs of RFC-4648
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @link http://tools.ietf.org/html/rfc4648#section-5
|
||||
*/
|
||||
class Decoder
|
||||
{
|
||||
/**
|
||||
* Decodes from JSON, validating the errors (will return an associative array
|
||||
* instead of objects)
|
||||
*
|
||||
* @param string $json
|
||||
* @return mixed
|
||||
*
|
||||
* @throws RuntimeException When something goes wrong while decoding
|
||||
*/
|
||||
public function jsonDecode($json)
|
||||
{
|
||||
$data = json_decode($json);
|
||||
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
throw new RuntimeException('Error while decoding to JSON: ' . json_last_error_msg());
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes from base64url
|
||||
*
|
||||
* @param string $data
|
||||
* @return string
|
||||
*/
|
||||
public function base64UrlDecode($data)
|
||||
{
|
||||
if ($remainder = strlen($data) % 4) {
|
||||
$data .= str_repeat('=', 4 - $remainder);
|
||||
}
|
||||
|
||||
return base64_decode(strtr($data, '-_', '+/'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Parsing;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Class that encodes data according with the specs of RFC-4648
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*
|
||||
* @link http://tools.ietf.org/html/rfc4648#section-5
|
||||
*/
|
||||
class Encoder
|
||||
{
|
||||
/**
|
||||
* Encodes to JSON, validating the errors
|
||||
*
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
*
|
||||
* @throws RuntimeException When something goes wrong while encoding
|
||||
*/
|
||||
public function jsonEncode($data)
|
||||
{
|
||||
$json = json_encode($data);
|
||||
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
throw new RuntimeException('Error while encoding to JSON: ' . json_last_error_msg());
|
||||
}
|
||||
|
||||
return $json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes to base64url
|
||||
*
|
||||
* @param string $data
|
||||
* @return string
|
||||
*/
|
||||
public function base64UrlEncode($data)
|
||||
{
|
||||
return str_replace('=', '', strtr(base64_encode($data), '+/', '-_'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT;
|
||||
|
||||
/**
|
||||
* This class represents a token signature
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class Signature
|
||||
{
|
||||
/**
|
||||
* The resultant hash
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $hash;
|
||||
|
||||
/**
|
||||
* Initializes the object
|
||||
*
|
||||
* @param string $hash
|
||||
*/
|
||||
public function __construct($hash)
|
||||
{
|
||||
$this->hash = $hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies if the current hash matches with with the result of the creation of
|
||||
* a new signature with given data
|
||||
*
|
||||
* @param Signer $signer
|
||||
* @param string $payload
|
||||
* @param string $key
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function verify(Signer $signer, $payload, $key)
|
||||
{
|
||||
return $signer->verify($this->hash, $payload, $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current hash as a string representation of the signature
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->hash;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Lcobucci\JWT\Signer\Key;
|
||||
|
||||
/**
|
||||
* Basic interface for token signers
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
interface Signer
|
||||
{
|
||||
/**
|
||||
* Returns the algorithm id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAlgorithmId();
|
||||
|
||||
/**
|
||||
* Apply changes on headers according with algorithm
|
||||
*
|
||||
* @param array $headers
|
||||
*/
|
||||
public function modifyHeader(array &$headers);
|
||||
|
||||
/**
|
||||
* Returns a signature for given data
|
||||
*
|
||||
* @param string $payload
|
||||
* @param Key|string $key
|
||||
*
|
||||
* @return Signature
|
||||
*
|
||||
* @throws InvalidArgumentException When given key is invalid
|
||||
*/
|
||||
public function sign($payload, $key);
|
||||
|
||||
/**
|
||||
* Returns if the expected hash matches with the data and key
|
||||
*
|
||||
* @param string $expected
|
||||
* @param string $payload
|
||||
* @param Key|string $key
|
||||
*
|
||||
* @return boolean
|
||||
*
|
||||
* @throws InvalidArgumentException When given key is invalid
|
||||
*/
|
||||
public function verify($expected, $payload, $key);
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer;
|
||||
|
||||
use Lcobucci\JWT\Signature;
|
||||
use Lcobucci\JWT\Signer;
|
||||
|
||||
/**
|
||||
* Base class for signers
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
abstract class BaseSigner implements Signer
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function modifyHeader(array &$headers)
|
||||
{
|
||||
$headers['alg'] = $this->getAlgorithmId();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function sign($payload, $key)
|
||||
{
|
||||
return new Signature($this->createHash($payload, $this->getKey($key)));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function verify($expected, $payload, $key)
|
||||
{
|
||||
return $this->doVerify($expected, $payload, $this->getKey($key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Key|string $key
|
||||
*
|
||||
* @return Key
|
||||
*/
|
||||
private function getKey($key)
|
||||
{
|
||||
if (is_string($key)) {
|
||||
$key = new Key($key);
|
||||
}
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a hash with the given data
|
||||
*
|
||||
* @param string $payload
|
||||
* @param Key $key
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function createHash($payload, Key $key);
|
||||
|
||||
/**
|
||||
* Creates a hash with the given data
|
||||
*
|
||||
* @param string $expected
|
||||
* @param string $payload
|
||||
* @param Key $key
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
abstract public function doVerify($expected, $payload, Key $key);
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer;
|
||||
|
||||
use Lcobucci\JWT\Signer\Ecdsa\KeyParser;
|
||||
use Mdanter\Ecc\Crypto\Signature\Signature;
|
||||
use Mdanter\Ecc\Crypto\Signature\Signer;
|
||||
use Mdanter\Ecc\EccFactory;
|
||||
use Mdanter\Ecc\Math\MathAdapterInterface as Adapter;
|
||||
use Mdanter\Ecc\Random\RandomGeneratorFactory;
|
||||
use Mdanter\Ecc\Random\RandomNumberGeneratorInterface;
|
||||
|
||||
/**
|
||||
* Base class for ECDSA signers
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
abstract class Ecdsa extends BaseSigner
|
||||
{
|
||||
/**
|
||||
* @var Adapter
|
||||
*/
|
||||
private $adapter;
|
||||
|
||||
/**
|
||||
* @var Signer
|
||||
*/
|
||||
private $signer;
|
||||
|
||||
/**
|
||||
* @var KeyParser
|
||||
*/
|
||||
private $parser;
|
||||
|
||||
/**
|
||||
* @param Adapter $adapter
|
||||
* @param EcdsaSigner $signer
|
||||
* @param KeyParser $parser
|
||||
*/
|
||||
public function __construct(Adapter $adapter = null, Signer $signer = null, KeyParser $parser = null)
|
||||
{
|
||||
$this->adapter = $adapter ?: EccFactory::getAdapter();
|
||||
$this->signer = $signer ?: EccFactory::getSigner($this->adapter);
|
||||
$this->parser = $parser ?: new KeyParser($this->adapter);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createHash(
|
||||
$payload,
|
||||
Key $key,
|
||||
RandomNumberGeneratorInterface $generator = null
|
||||
) {
|
||||
$privateKey = $this->parser->getPrivateKey($key);
|
||||
$generator = $generator ?: RandomGeneratorFactory::getRandomGenerator();
|
||||
|
||||
return $this->createSignatureHash(
|
||||
$this->signer->sign(
|
||||
$privateKey,
|
||||
$this->createSigningHash($payload),
|
||||
$generator->generate($privateKey->getPoint()->getOrder())
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a binary signature with R and S coordinates
|
||||
*
|
||||
* @param Signature $signature
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function createSignatureHash(Signature $signature)
|
||||
{
|
||||
$length = $this->getSignatureLength();
|
||||
|
||||
return pack(
|
||||
'H*',
|
||||
sprintf(
|
||||
'%s%s',
|
||||
str_pad($this->adapter->decHex($signature->getR()), $length, '0', STR_PAD_LEFT),
|
||||
str_pad($this->adapter->decHex($signature->getS()), $length, '0', STR_PAD_LEFT)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a hash using the signer algorithm with given payload
|
||||
*
|
||||
* @param string $payload
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
private function createSigningHash($payload)
|
||||
{
|
||||
return $this->adapter->hexDec(hash($this->getAlgorithm(), $payload));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function doVerify($expected, $payload, Key $key)
|
||||
{
|
||||
return $this->signer->verify(
|
||||
$this->parser->getPublicKey($key),
|
||||
$this->extractSignature($expected),
|
||||
$this->createSigningHash($payload)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts R and S values from given data
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return \Mdanter\Ecc\Crypto\Signature\Signature
|
||||
*/
|
||||
private function extractSignature($value)
|
||||
{
|
||||
$length = $this->getSignatureLength();
|
||||
$value = unpack('H*', $value)[1];
|
||||
|
||||
return new Signature(
|
||||
$this->adapter->hexDec(substr($value, 0, $length)),
|
||||
$this->adapter->hexDec(substr($value, $length))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the lenght of signature parts
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
abstract public function getSignatureLength();
|
||||
|
||||
/**
|
||||
* Returns the name of algorithm to be used to create the signing hash
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getAlgorithm();
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Ecdsa;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Lcobucci\JWT\Signer\Key;
|
||||
use Mdanter\Ecc\Math\MathAdapterInterface;
|
||||
use Mdanter\Ecc\Serializer\PrivateKey\DerPrivateKeySerializer;
|
||||
use Mdanter\Ecc\Serializer\PrivateKey\PemPrivateKeySerializer;
|
||||
use Mdanter\Ecc\Serializer\PrivateKey\PrivateKeySerializerInterface;
|
||||
use Mdanter\Ecc\Serializer\PublicKey\DerPublicKeySerializer;
|
||||
use Mdanter\Ecc\Serializer\PublicKey\PemPublicKeySerializer;
|
||||
use Mdanter\Ecc\Serializer\PublicKey\PublicKeySerializerInterface;
|
||||
|
||||
/**
|
||||
* Base class for ECDSA signers
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 3.0.4
|
||||
*/
|
||||
class KeyParser
|
||||
{
|
||||
/**
|
||||
* @var PrivateKeySerializerInterface
|
||||
*/
|
||||
private $privateKeySerializer;
|
||||
|
||||
/**
|
||||
* @var PublicKeySerializerInterface
|
||||
*/
|
||||
private $publicKeySerializer;
|
||||
|
||||
/**
|
||||
* @param MathAdapterInterface $adapter
|
||||
* @param PrivateKeySerializerInterface $privateKeySerializer
|
||||
* @param PublicKeySerializerInterface $publicKeySerializer
|
||||
*/
|
||||
public function __construct(
|
||||
MathAdapterInterface $adapter,
|
||||
PrivateKeySerializerInterface $privateKeySerializer = null,
|
||||
PublicKeySerializerInterface $publicKeySerializer = null
|
||||
) {
|
||||
$this->privateKeySerializer = $privateKeySerializer ?: new PemPrivateKeySerializer(new DerPrivateKeySerializer($adapter));
|
||||
$this->publicKeySerializer = $publicKeySerializer ?: new PemPublicKeySerializer(new DerPublicKeySerializer($adapter));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a public key from the given PEM content
|
||||
*
|
||||
* @param Key $key
|
||||
*
|
||||
* @return \Mdanter\Ecc\Crypto\Key\PublicKeyInterface
|
||||
*/
|
||||
public function getPublicKey(Key $key)
|
||||
{
|
||||
return $this->publicKeySerializer->parse($this->getKeyContent($key, 'PUBLIC KEY'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a private key from the given PEM content
|
||||
*
|
||||
* @param Key $key
|
||||
*
|
||||
* @return \Mdanter\Ecc\Crypto\Key\PrivateKeyInterface
|
||||
*/
|
||||
public function getPrivateKey(Key $key)
|
||||
{
|
||||
return $this->privateKeySerializer->parse($this->getKeyContent($key, 'EC PRIVATE KEY'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the base 64 value from the PEM certificate
|
||||
*
|
||||
* @param Key $key
|
||||
* @param string $header
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws InvalidArgumentException When given key is not a ECDSA key
|
||||
*/
|
||||
private function getKeyContent(Key $key, $header)
|
||||
{
|
||||
$match = null;
|
||||
|
||||
preg_match(
|
||||
'/[\-]{5}BEGIN ' . $header . '[\-]{5}(.*)[\-]{5}END ' . $header . '[\-]{5}/',
|
||||
str_replace([PHP_EOL, "\n", "\r"], '', $key->getContent()),
|
||||
$match
|
||||
);
|
||||
|
||||
if (isset($match[1])) {
|
||||
return $match[1];
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('This is not a valid ECDSA key.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Ecdsa;
|
||||
|
||||
use Lcobucci\JWT\Signer\Ecdsa;
|
||||
|
||||
/**
|
||||
* Signer for ECDSA SHA-256
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha256 extends Ecdsa
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithmId()
|
||||
{
|
||||
return 'ES256';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithm()
|
||||
{
|
||||
return 'sha256';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSignatureLength()
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Ecdsa;
|
||||
|
||||
use Lcobucci\JWT\Signer\Ecdsa;
|
||||
|
||||
/**
|
||||
* Signer for ECDSA SHA-384
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha384 extends Ecdsa
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithmId()
|
||||
{
|
||||
return 'ES384';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithm()
|
||||
{
|
||||
return 'sha384';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSignatureLength()
|
||||
{
|
||||
return 96;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Ecdsa;
|
||||
|
||||
use Lcobucci\JWT\Signer\Ecdsa;
|
||||
|
||||
/**
|
||||
* Signer for ECDSA SHA-512
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha512 extends Ecdsa
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithmId()
|
||||
{
|
||||
return 'ES512';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithm()
|
||||
{
|
||||
return 'sha512';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSignatureLength()
|
||||
{
|
||||
return 132;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer;
|
||||
|
||||
/**
|
||||
* Base class for hmac signers
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
abstract class Hmac extends BaseSigner
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createHash($payload, Key $key)
|
||||
{
|
||||
return hash_hmac($this->getAlgorithm(), $payload, $key->getContent(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function doVerify($expected, $payload, Key $key)
|
||||
{
|
||||
if (!is_string($expected)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$callback = function_exists('hash_equals') ? 'hash_equals' : [$this, 'hashEquals'];
|
||||
|
||||
return call_user_func($callback, $expected, $this->createHash($payload, $key));
|
||||
}
|
||||
|
||||
/**
|
||||
* PHP < 5.6 timing attack safe hash comparison
|
||||
*
|
||||
* @param string $expected
|
||||
* @param string $generated
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function hashEquals($expected, $generated)
|
||||
{
|
||||
$expectedLength = strlen($expected);
|
||||
|
||||
if ($expectedLength !== strlen($generated)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$res = 0;
|
||||
|
||||
for ($i = 0; $i < $expectedLength; ++$i) {
|
||||
$res |= ord($expected[$i]) ^ ord($generated[$i]);
|
||||
}
|
||||
|
||||
return $res === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the algorithm name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getAlgorithm();
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Hmac;
|
||||
|
||||
use Lcobucci\JWT\Signer\Hmac;
|
||||
|
||||
/**
|
||||
* Signer for HMAC SHA-256
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class Sha256 extends Hmac
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithmId()
|
||||
{
|
||||
return 'HS256';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithm()
|
||||
{
|
||||
return 'sha256';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Hmac;
|
||||
|
||||
use Lcobucci\JWT\Signer\Hmac;
|
||||
|
||||
/**
|
||||
* Signer for HMAC SHA-384
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class Sha384 extends Hmac
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithmId()
|
||||
{
|
||||
return 'HS384';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithm()
|
||||
{
|
||||
return 'sha384';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Hmac;
|
||||
|
||||
use Lcobucci\JWT\Signer\Hmac;
|
||||
|
||||
/**
|
||||
* Signer for HMAC SHA-512
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class Sha512 extends Hmac
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithmId()
|
||||
{
|
||||
return 'HS512';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithm()
|
||||
{
|
||||
return 'sha512';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer;
|
||||
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use SplFileObject;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 3.0.4
|
||||
*/
|
||||
final class Key
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $content;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $passphrase;
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @param string $passphrase
|
||||
*/
|
||||
public function __construct($content, $passphrase = null)
|
||||
{
|
||||
$this->setContent($content);
|
||||
$this->passphrase = $passphrase;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
private function setContent($content)
|
||||
{
|
||||
if (strpos($content, 'file://') === 0) {
|
||||
$content = $this->readFile($content);
|
||||
}
|
||||
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
private function readFile($content)
|
||||
{
|
||||
try {
|
||||
$file = new SplFileObject(substr($content, 7));
|
||||
$content = '';
|
||||
|
||||
while (! $file->eof()) {
|
||||
$content .= $file->fgets();
|
||||
}
|
||||
|
||||
return $content;
|
||||
} catch (Exception $exception) {
|
||||
throw new InvalidArgumentException('You must inform a valid key file', 0, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getContent()
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPassphrase()
|
||||
{
|
||||
return $this->passphrase;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer;
|
||||
|
||||
/**
|
||||
* A utilitarian class that encapsulates the retrieval of public and private keys
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*
|
||||
* @deprecated Since we've removed OpenSSL from ECDSA there's no reason to use this class
|
||||
*/
|
||||
class Keychain
|
||||
{
|
||||
/**
|
||||
* Returns a private key from file path or content
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $passphrase
|
||||
*
|
||||
* @return Key
|
||||
*/
|
||||
public function getPrivateKey($key, $passphrase = null)
|
||||
{
|
||||
return new Key($key, $passphrase);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a public key from file path or content
|
||||
*
|
||||
* @param string $certificate
|
||||
*
|
||||
* @return Key
|
||||
*/
|
||||
public function getPublicKey($certificate)
|
||||
{
|
||||
return new Key($certificate);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Base class for RSASSA-PKCS1 signers
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
abstract class Rsa extends BaseSigner
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createHash($payload, Key $key)
|
||||
{
|
||||
$key = openssl_get_privatekey($key->getContent(), $key->getPassphrase());
|
||||
$this->validateKey($key);
|
||||
|
||||
$signature = '';
|
||||
|
||||
if (!openssl_sign($payload, $signature, $key, $this->getAlgorithm())) {
|
||||
throw new InvalidArgumentException(
|
||||
'There was an error while creating the signature: ' . openssl_error_string()
|
||||
);
|
||||
}
|
||||
|
||||
return $signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function doVerify($expected, $payload, Key $key)
|
||||
{
|
||||
$key = openssl_get_publickey($key->getContent());
|
||||
$this->validateKey($key);
|
||||
|
||||
return openssl_verify($payload, $expected, $key, $this->getAlgorithm()) === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates if the given key is a valid RSA public/private key
|
||||
*
|
||||
* @param resource $key
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
private function validateKey($key)
|
||||
{
|
||||
if ($key === false) {
|
||||
throw new InvalidArgumentException(
|
||||
'It was not possible to parse your key, reason: ' . openssl_error_string()
|
||||
);
|
||||
}
|
||||
|
||||
$details = openssl_pkey_get_details($key);
|
||||
|
||||
if (!isset($details['key']) || $details['type'] !== OPENSSL_KEYTYPE_RSA) {
|
||||
throw new InvalidArgumentException('This key is not compatible with RSA signatures');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the algorithm name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getAlgorithm();
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Rsa;
|
||||
|
||||
use Lcobucci\JWT\Signer\Rsa;
|
||||
|
||||
/**
|
||||
* Signer for RSA SHA-256
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha256 extends Rsa
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithmId()
|
||||
{
|
||||
return 'RS256';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithm()
|
||||
{
|
||||
return OPENSSL_ALGO_SHA256;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Rsa;
|
||||
|
||||
use Lcobucci\JWT\Signer\Rsa;
|
||||
|
||||
/**
|
||||
* Signer for RSA SHA-384
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha384 extends Rsa
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithmId()
|
||||
{
|
||||
return 'RS384';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithm()
|
||||
{
|
||||
return OPENSSL_ALGO_SHA384;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Rsa;
|
||||
|
||||
use Lcobucci\JWT\Signer\Rsa;
|
||||
|
||||
/**
|
||||
* Signer for RSA SHA-512
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha512 extends Rsa
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithmId()
|
||||
{
|
||||
return 'RS512';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlgorithm()
|
||||
{
|
||||
return OPENSSL_ALGO_SHA512;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT;
|
||||
|
||||
use BadMethodCallException;
|
||||
use DateTime;
|
||||
use DateTimeInterface;
|
||||
use Generator;
|
||||
use Lcobucci\JWT\Claim\Validatable;
|
||||
use OutOfBoundsException;
|
||||
|
||||
/**
|
||||
* Basic structure of the JWT
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class Token
|
||||
{
|
||||
/**
|
||||
* The token headers
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $headers;
|
||||
|
||||
/**
|
||||
* The token claim set
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $claims;
|
||||
|
||||
/**
|
||||
* The token signature
|
||||
*
|
||||
* @var Signature
|
||||
*/
|
||||
private $signature;
|
||||
|
||||
/**
|
||||
* The encoded data
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $payload;
|
||||
|
||||
/**
|
||||
* Initializes the object
|
||||
*
|
||||
* @param array $headers
|
||||
* @param array $claims
|
||||
* @param array $payload
|
||||
* @param Signature $signature
|
||||
*/
|
||||
public function __construct(
|
||||
array $headers = ['alg' => 'none'],
|
||||
array $claims = [],
|
||||
Signature $signature = null,
|
||||
array $payload = ['', '']
|
||||
) {
|
||||
$this->headers = $headers;
|
||||
$this->claims = $claims;
|
||||
$this->signature = $signature;
|
||||
$this->payload = $payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the token headers
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getHeaders()
|
||||
{
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if the header is configured
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasHeader($name)
|
||||
{
|
||||
return array_key_exists($name, $this->headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of a token header
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $default
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws OutOfBoundsException
|
||||
*/
|
||||
public function getHeader($name, $default = null)
|
||||
{
|
||||
if ($this->hasHeader($name)) {
|
||||
return $this->getHeaderValue($name);
|
||||
}
|
||||
|
||||
if ($default === null) {
|
||||
throw new OutOfBoundsException('Requested header is not configured');
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value stored in header
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function getHeaderValue($name)
|
||||
{
|
||||
$header = $this->headers[$name];
|
||||
|
||||
if ($header instanceof Claim) {
|
||||
return $header->getValue();
|
||||
}
|
||||
|
||||
return $header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the token claim set
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getClaims()
|
||||
{
|
||||
return $this->claims;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if the claim is configured
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasClaim($name)
|
||||
{
|
||||
return array_key_exists($name, $this->claims);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of a token claim
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $default
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws OutOfBoundsException
|
||||
*/
|
||||
public function getClaim($name, $default = null)
|
||||
{
|
||||
if ($this->hasClaim($name)) {
|
||||
return $this->claims[$name]->getValue();
|
||||
}
|
||||
|
||||
if ($default === null) {
|
||||
throw new OutOfBoundsException('Requested claim is not configured');
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if the key matches with the one that created the signature
|
||||
*
|
||||
* @param Signer $signer
|
||||
* @param string $key
|
||||
*
|
||||
* @return boolean
|
||||
*
|
||||
* @throws BadMethodCallException When token is not signed
|
||||
*/
|
||||
public function verify(Signer $signer, $key)
|
||||
{
|
||||
if ($this->signature === null) {
|
||||
throw new BadMethodCallException('This token is not signed');
|
||||
}
|
||||
|
||||
if ($this->headers['alg'] !== $signer->getAlgorithmId()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->signature->verify($signer, $this->getPayload(), $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates if the token is valid
|
||||
*
|
||||
* @param ValidationData $data
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function validate(ValidationData $data)
|
||||
{
|
||||
foreach ($this->getValidatableClaims() as $claim) {
|
||||
if (!$claim->validate($data)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the token is expired.
|
||||
*
|
||||
* @param DateTimeInterface $now Defaults to the current time.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isExpired(DateTimeInterface $now = null)
|
||||
{
|
||||
$exp = $this->getClaim('exp', false);
|
||||
|
||||
if ($exp === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$now = $now ?: new DateTime();
|
||||
|
||||
$expiresAt = new DateTime();
|
||||
$expiresAt->setTimestamp($exp);
|
||||
|
||||
return $now > $expiresAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Yields the validatable claims
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
private function getValidatableClaims()
|
||||
{
|
||||
foreach ($this->claims as $claim) {
|
||||
if ($claim instanceof Validatable) {
|
||||
yield $claim;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the token payload
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPayload()
|
||||
{
|
||||
return $this->payload[0] . '.' . $this->payload[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an encoded representation of the token
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$data = implode('.', $this->payload);
|
||||
|
||||
if ($this->signature === null) {
|
||||
$data .= '.';
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT;
|
||||
|
||||
/**
|
||||
* Class that wraps validation values
|
||||
*
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class ValidationData
|
||||
{
|
||||
/**
|
||||
* The list of things to be validated
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $items;
|
||||
|
||||
/**
|
||||
* Initializes the object
|
||||
*
|
||||
* @param int $currentTime
|
||||
*/
|
||||
public function __construct($currentTime = null)
|
||||
{
|
||||
$currentTime = $currentTime ?: time();
|
||||
|
||||
$this->items = [
|
||||
'jti' => null,
|
||||
'iss' => null,
|
||||
'aud' => null,
|
||||
'sub' => null,
|
||||
'iat' => $currentTime,
|
||||
'nbf' => $currentTime,
|
||||
'exp' => $currentTime
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the id
|
||||
*
|
||||
* @param string $id
|
||||
*/
|
||||
public function setId($id)
|
||||
{
|
||||
$this->items['jti'] = (string) $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the issuer
|
||||
*
|
||||
* @param string $issuer
|
||||
*/
|
||||
public function setIssuer($issuer)
|
||||
{
|
||||
$this->items['iss'] = (string) $issuer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the audience
|
||||
*
|
||||
* @param string $audience
|
||||
*/
|
||||
public function setAudience($audience)
|
||||
{
|
||||
$this->items['aud'] = (string) $audience;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the subject
|
||||
*
|
||||
* @param string $subject
|
||||
*/
|
||||
public function setSubject($subject)
|
||||
{
|
||||
$this->items['sub'] = (string) $subject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the time that "iat", "nbf" and "exp" should be based on
|
||||
*
|
||||
* @param int $currentTime
|
||||
*/
|
||||
public function setCurrentTime($currentTime)
|
||||
{
|
||||
$this->items['iat'] = (int) $currentTime;
|
||||
$this->items['nbf'] = (int) $currentTime;
|
||||
$this->items['exp'] = (int) $currentTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the requested item
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
return isset($this->items[$name]) ? $this->items[$name] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if the item is present
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
return !empty($this->items[$name]);
|
||||
}
|
||||
}
|
||||
+320
@@ -0,0 +1,320 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\FunctionalTests;
|
||||
|
||||
use Lcobucci\JWT\Builder;
|
||||
use Lcobucci\JWT\Parser;
|
||||
use Lcobucci\JWT\Signer\Key;
|
||||
use Lcobucci\JWT\Token;
|
||||
use Lcobucci\JWT\Signature;
|
||||
use Lcobucci\JWT\Signer\Ecdsa\Sha256;
|
||||
use Lcobucci\JWT\Signer\Ecdsa\Sha512;
|
||||
use Lcobucci\JWT\Signer\Keychain;
|
||||
use Lcobucci\JWT\Keys;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class EcdsaTokenTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
use Keys;
|
||||
|
||||
/**
|
||||
* @var Sha256
|
||||
*/
|
||||
private $signer;
|
||||
|
||||
/**
|
||||
* @before
|
||||
*/
|
||||
public function createSigner()
|
||||
{
|
||||
$this->signer = new Sha256();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256
|
||||
*/
|
||||
public function builderShouldRaiseExceptionWhenKeyIsInvalid()
|
||||
{
|
||||
$user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
|
||||
|
||||
(new Builder())->setId(1)
|
||||
->setAudience('http://client.abc.com')
|
||||
->setIssuer('http://api.abc.com')
|
||||
->set('user', $user)
|
||||
->sign($this->signer, new Key('testing'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256
|
||||
*/
|
||||
public function builderShouldRaiseExceptionWhenKeyIsNotEcdsaCompatible()
|
||||
{
|
||||
$user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
|
||||
|
||||
(new Builder())->setId(1)
|
||||
->setAudience('http://client.abc.com')
|
||||
->setIssuer('http://api.abc.com')
|
||||
->set('user', $user)
|
||||
->sign($this->signer, static::$rsaKeys['private']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256
|
||||
*/
|
||||
public function builderCanGenerateAToken()
|
||||
{
|
||||
$user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
|
||||
|
||||
$token = (new Builder())->setId(1)
|
||||
->setAudience('http://client.abc.com')
|
||||
->setIssuer('http://api.abc.com')
|
||||
->set('user', $user)
|
||||
->setHeader('jki', '1234')
|
||||
->sign($this->signer, static::$ecdsaKeys['private'])
|
||||
->getToken();
|
||||
|
||||
$this->assertAttributeInstanceOf(Signature::class, 'signature', $token);
|
||||
$this->assertEquals('1234', $token->getHeader('jki'));
|
||||
$this->assertEquals('http://client.abc.com', $token->getClaim('aud'));
|
||||
$this->assertEquals('http://api.abc.com', $token->getClaim('iss'));
|
||||
$this->assertEquals($user, $token->getClaim('user'));
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Parsing\Decoder
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
*/
|
||||
public function parserCanReadAToken(Token $generated)
|
||||
{
|
||||
$read = (new Parser())->parse((string) $generated);
|
||||
|
||||
$this->assertEquals($generated, $read);
|
||||
$this->assertEquals('testing', $read->getClaim('user')->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256
|
||||
*/
|
||||
public function verifyShouldReturnFalseWhenKeyIsNotRight(Token $token)
|
||||
{
|
||||
$this->assertFalse($token->verify($this->signer, static::$ecdsaKeys['public2']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha512
|
||||
*/
|
||||
public function verifyShouldReturnFalseWhenAlgorithmIsDifferent(Token $token)
|
||||
{
|
||||
$this->assertFalse($token->verify(new Sha512(), static::$ecdsaKeys['public1']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @expectedException \RuntimeException
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256
|
||||
*/
|
||||
public function verifyShouldRaiseExceptionWhenKeyIsNotEcdsaCompatible(Token $token)
|
||||
{
|
||||
$this->assertFalse($token->verify($this->signer, static::$rsaKeys['public']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256
|
||||
*/
|
||||
public function verifyShouldReturnTrueWhenKeyIsRight(Token $token)
|
||||
{
|
||||
$this->assertTrue($token->verify($this->signer, static::$ecdsaKeys['public1']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256
|
||||
*/
|
||||
public function everythingShouldWorkWithAKeyWithParams()
|
||||
{
|
||||
$user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
|
||||
|
||||
$token = (new Builder())->setId(1)
|
||||
->setAudience('http://client.abc.com')
|
||||
->setIssuer('http://api.abc.com')
|
||||
->set('user', $user)
|
||||
->setHeader('jki', '1234')
|
||||
->sign($this->signer, static::$ecdsaKeys['private-params'])
|
||||
->getToken();
|
||||
|
||||
$this->assertTrue($token->verify($this->signer, static::$ecdsaKeys['public-params']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha512
|
||||
* @covers Lcobucci\JWT\Signer\Keychain
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Parsing\Decoder
|
||||
*/
|
||||
public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs()
|
||||
{
|
||||
$data = 'eyJhbGciOiJFUzUxMiIsInR5cCI6IkpXVCJ9.eyJoZWxsbyI6IndvcmxkIn0.'
|
||||
. 'AQx1MqdTni6KuzfOoedg2-7NUiwe-b88SWbdmviz40GTwrM0Mybp1i1tVtm'
|
||||
. 'TSQ91oEXGXBdtwsN6yalzP9J-sp2YATX_Tv4h-BednbdSvYxZsYnUoZ--ZU'
|
||||
. 'dL10t7g8Yt3y9hdY_diOjIptcha6ajX8yzkDGYG42iSe3f5LywSuD6FO5c';
|
||||
|
||||
$key = '-----BEGIN PUBLIC KEY-----' . PHP_EOL
|
||||
. 'MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQAcpkss6wI7PPlxj3t7A1RqMH3nvL4' . PHP_EOL
|
||||
. 'L5Tzxze/XeeYZnHqxiX+gle70DlGRMqqOq+PJ6RYX7vK0PJFdiAIXlyPQq0B3KaU' . PHP_EOL
|
||||
. 'e86IvFeQSFrJdCc0K8NfiH2G1loIk3fiR+YLqlXk6FAeKtpXJKxR1pCQCAM+vBCs' . PHP_EOL
|
||||
. 'mZudf1zCUZ8/4eodlHU=' . PHP_EOL
|
||||
. '-----END PUBLIC KEY-----';
|
||||
|
||||
$keychain = new Keychain();
|
||||
$token = (new Parser())->parse((string) $data);
|
||||
|
||||
$this->assertEquals('world', $token->getClaim('hello'));
|
||||
$this->assertTrue($token->verify(new Sha512(), $keychain->getPublicKey($key)));
|
||||
}
|
||||
}
|
||||
+186
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\FunctionalTests;
|
||||
|
||||
use Lcobucci\JWT\Builder;
|
||||
use Lcobucci\JWT\Parser;
|
||||
use Lcobucci\JWT\Token;
|
||||
use Lcobucci\JWT\Signature;
|
||||
use Lcobucci\JWT\Signer\Hmac\Sha256;
|
||||
use Lcobucci\JWT\Signer\Hmac\Sha512;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class HmacTokenTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var Sha256
|
||||
*/
|
||||
private $signer;
|
||||
|
||||
/**
|
||||
* @before
|
||||
*/
|
||||
public function createSigner()
|
||||
{
|
||||
$this->signer = new Sha256();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Hmac
|
||||
* @covers Lcobucci\JWT\Signer\Hmac\Sha256
|
||||
*/
|
||||
public function builderCanGenerateAToken()
|
||||
{
|
||||
$user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
|
||||
|
||||
$token = (new Builder())->setId(1)
|
||||
->setAudience('http://client.abc.com')
|
||||
->setIssuer('http://api.abc.com')
|
||||
->set('user', $user)
|
||||
->setHeader('jki', '1234')
|
||||
->sign($this->signer, 'testing')
|
||||
->getToken();
|
||||
|
||||
$this->assertAttributeInstanceOf(Signature::class, 'signature', $token);
|
||||
$this->assertEquals('1234', $token->getHeader('jki'));
|
||||
$this->assertEquals('http://client.abc.com', $token->getClaim('aud'));
|
||||
$this->assertEquals('http://api.abc.com', $token->getClaim('iss'));
|
||||
$this->assertEquals($user, $token->getClaim('user'));
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Parsing\Decoder
|
||||
*/
|
||||
public function parserCanReadAToken(Token $generated)
|
||||
{
|
||||
$read = (new Parser())->parse((string) $generated);
|
||||
|
||||
$this->assertEquals($generated, $read);
|
||||
$this->assertEquals('testing', $read->getClaim('user')->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Hmac
|
||||
* @covers Lcobucci\JWT\Signer\Hmac\Sha256
|
||||
*/
|
||||
public function verifyShouldReturnFalseWhenKeyIsNotRight(Token $token)
|
||||
{
|
||||
$this->assertFalse($token->verify($this->signer, 'testing1'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Hmac
|
||||
* @covers Lcobucci\JWT\Signer\Hmac\Sha256
|
||||
* @covers Lcobucci\JWT\Signer\Hmac\Sha512
|
||||
*/
|
||||
public function verifyShouldReturnFalseWhenAlgorithmIsDifferent(Token $token)
|
||||
{
|
||||
$this->assertFalse($token->verify(new Sha512(), 'testing'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Hmac
|
||||
* @covers Lcobucci\JWT\Signer\Hmac\Sha256
|
||||
*/
|
||||
public function verifyShouldReturnTrueWhenKeyIsRight(Token $token)
|
||||
{
|
||||
$this->assertTrue($token->verify($this->signer, 'testing'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Hmac
|
||||
* @covers Lcobucci\JWT\Signer\Hmac\Sha256
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Parsing\Decoder
|
||||
*/
|
||||
public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs()
|
||||
{
|
||||
$data = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJoZWxsbyI6IndvcmxkIn0.Rh'
|
||||
. '7AEgqCB7zae1PkgIlvOpeyw9Ab8NGTbeOH7heHO0o';
|
||||
|
||||
$token = (new Parser())->parse((string) $data);
|
||||
|
||||
$this->assertEquals('world', $token->getClaim('hello'));
|
||||
$this->assertTrue($token->verify($this->signer, 'testing'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
namespace Lcobucci\JWT;
|
||||
|
||||
use Lcobucci\JWT\Signer\Keychain;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
*/
|
||||
trait Keys
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected static $rsaKeys;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected static $ecdsaKeys;
|
||||
|
||||
/**
|
||||
* @beforeClass
|
||||
*/
|
||||
public static function createRsaKeys()
|
||||
{
|
||||
$keychain = new Keychain();
|
||||
$dir = 'file://' . __DIR__;
|
||||
|
||||
static::$rsaKeys = [
|
||||
'private' => $keychain->getPrivateKey($dir . '/rsa/private.key'),
|
||||
'public' => $keychain->getPublicKey($dir . '/rsa/public.key'),
|
||||
'encrypted-private' => $keychain->getPrivateKey($dir . '/rsa/encrypted-private.key', 'testing'),
|
||||
'encrypted-public' => $keychain->getPublicKey($dir . '/rsa/encrypted-public.key')
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @beforeClass
|
||||
*/
|
||||
public static function createEcdsaKeys()
|
||||
{
|
||||
$keychain = new Keychain();
|
||||
$dir = 'file://' . __DIR__;
|
||||
|
||||
static::$ecdsaKeys = [
|
||||
'private' => $keychain->getPrivateKey($dir . '/ecdsa/private.key'),
|
||||
'private-params' => $keychain->getPrivateKey($dir . '/ecdsa/private2.key'),
|
||||
'public1' => $keychain->getPublicKey($dir . '/ecdsa/public1.key'),
|
||||
'public2' => $keychain->getPublicKey($dir . '/ecdsa/public2.key'),
|
||||
'public-params' => $keychain->getPublicKey($dir . '/ecdsa/public3.key'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\FunctionalTests;
|
||||
|
||||
use Lcobucci\JWT\Builder;
|
||||
use Lcobucci\JWT\Keys;
|
||||
use Lcobucci\JWT\Parser;
|
||||
use Lcobucci\JWT\Signer\Key;
|
||||
use Lcobucci\JWT\Token;
|
||||
use Lcobucci\JWT\Signature;
|
||||
use Lcobucci\JWT\Signer\Rsa\Sha256;
|
||||
use Lcobucci\JWT\Signer\Rsa\Sha512;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class RsaTokenTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
use Keys;
|
||||
|
||||
/**
|
||||
* @var Sha256
|
||||
*/
|
||||
private $signer;
|
||||
|
||||
/**
|
||||
* @before
|
||||
*/
|
||||
public function createSigner()
|
||||
{
|
||||
$this->signer = new Sha256();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Rsa
|
||||
* @covers Lcobucci\JWT\Signer\Rsa\Sha256
|
||||
*/
|
||||
public function builderShouldRaiseExceptionWhenKeyIsInvalid()
|
||||
{
|
||||
$user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
|
||||
|
||||
(new Builder())->setId(1)
|
||||
->setAudience('http://client.abc.com')
|
||||
->setIssuer('http://api.abc.com')
|
||||
->set('user', $user)
|
||||
->sign($this->signer, new Key('testing'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Rsa
|
||||
* @covers Lcobucci\JWT\Signer\Rsa\Sha256
|
||||
*/
|
||||
public function builderShouldRaiseExceptionWhenKeyIsNotRsaCompatible()
|
||||
{
|
||||
$user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
|
||||
|
||||
(new Builder())->setId(1)
|
||||
->setAudience('http://client.abc.com')
|
||||
->setIssuer('http://api.abc.com')
|
||||
->set('user', $user)
|
||||
->sign($this->signer, static::$ecdsaKeys['private']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Rsa
|
||||
* @covers Lcobucci\JWT\Signer\Rsa\Sha256
|
||||
*/
|
||||
public function builderCanGenerateAToken()
|
||||
{
|
||||
$user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
|
||||
|
||||
$token = (new Builder())->setId(1)
|
||||
->setAudience('http://client.abc.com')
|
||||
->setIssuer('http://api.abc.com')
|
||||
->set('user', $user)
|
||||
->setHeader('jki', '1234')
|
||||
->sign($this->signer, static::$rsaKeys['private'])
|
||||
->getToken();
|
||||
|
||||
$this->assertAttributeInstanceOf(Signature::class, 'signature', $token);
|
||||
$this->assertEquals('1234', $token->getHeader('jki'));
|
||||
$this->assertEquals('http://client.abc.com', $token->getClaim('aud'));
|
||||
$this->assertEquals('http://api.abc.com', $token->getClaim('iss'));
|
||||
$this->assertEquals($user, $token->getClaim('user'));
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Parsing\Decoder
|
||||
*/
|
||||
public function parserCanReadAToken(Token $generated)
|
||||
{
|
||||
$read = (new Parser())->parse((string) $generated);
|
||||
|
||||
$this->assertEquals($generated, $read);
|
||||
$this->assertEquals('testing', $read->getClaim('user')->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Rsa
|
||||
* @covers Lcobucci\JWT\Signer\Rsa\Sha256
|
||||
*/
|
||||
public function verifyShouldReturnFalseWhenKeyIsNotRight(Token $token)
|
||||
{
|
||||
$this->assertFalse($token->verify($this->signer, self::$rsaKeys['encrypted-public']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Rsa
|
||||
* @covers Lcobucci\JWT\Signer\Rsa\Sha256
|
||||
* @covers Lcobucci\JWT\Signer\Rsa\Sha512
|
||||
*/
|
||||
public function verifyShouldReturnFalseWhenAlgorithmIsDifferent(Token $token)
|
||||
{
|
||||
$this->assertFalse($token->verify(new Sha512(), self::$rsaKeys['public']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Rsa
|
||||
* @covers Lcobucci\JWT\Signer\Rsa\Sha256
|
||||
*/
|
||||
public function verifyShouldRaiseExceptionWhenKeyIsNotRsaCompatible(Token $token)
|
||||
{
|
||||
$this->assertFalse($token->verify($this->signer, self::$ecdsaKeys['public1']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Rsa
|
||||
* @covers Lcobucci\JWT\Signer\Rsa\Sha256
|
||||
*/
|
||||
public function verifyShouldReturnTrueWhenKeyIsRight(Token $token)
|
||||
{
|
||||
$this->assertTrue($token->verify($this->signer, self::$rsaKeys['public']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Signature
|
||||
* @covers Lcobucci\JWT\Signer\Key
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner
|
||||
* @covers Lcobucci\JWT\Signer\Rsa
|
||||
* @covers Lcobucci\JWT\Signer\Rsa\Sha256
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Parsing\Decoder
|
||||
*/
|
||||
public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs()
|
||||
{
|
||||
$data = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJoZWxsbyI6IndvcmxkIn0.s'
|
||||
. 'GYbB1KrmnESNfJ4D9hOe1Zad_BMyxdb8G4p4LNP7StYlOyBWck6q7XPpPj_6gB'
|
||||
. 'Bo1ohD3MA2o0HY42lNIrAStaVhfsFKGdIou8TarwMGZBPcif_3ThUV1pGS3fZc'
|
||||
. 'lFwF2SP7rqCngQis_xcUVCyqa8E1Wa_v28grnl1QZrnmQFO8B5JGGLqcrfUHJO'
|
||||
. 'nJCupP-Lqh4TmIhftIimSCgLNmJg80wyrpUEfZYReE7hPuEmY0ClTqAGIMQoNS'
|
||||
. '98ljwDxwhfbSuL2tAdbV4DekbTpWzspe3dOJ7RSzmPKVZ6NoezaIazKqyqkmHZfcMaHI1lQeGia6LTbHU1bp0gINi74Vw';
|
||||
|
||||
$token = (new Parser())->parse((string) $data);
|
||||
|
||||
$this->assertEquals('world', $token->getClaim('hello'));
|
||||
$this->assertTrue($token->verify($this->signer, self::$rsaKeys['public']));
|
||||
}
|
||||
}
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\FunctionalTests;
|
||||
|
||||
use Lcobucci\JWT\Builder;
|
||||
use Lcobucci\JWT\Parser;
|
||||
use Lcobucci\JWT\Token;
|
||||
use Lcobucci\JWT\ValidationData;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class UnsignedTokenTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
const CURRENT_TIME = 100000;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
*/
|
||||
public function builderCanGenerateAToken()
|
||||
{
|
||||
$user = (object) ['name' => 'testing', 'email' => 'testing@abc.com'];
|
||||
|
||||
$token = (new Builder())->setId(1)
|
||||
->setAudience('http://client.abc.com')
|
||||
->setIssuer('http://api.abc.com')
|
||||
->setExpiration(self::CURRENT_TIME + 3000)
|
||||
->set('user', $user)
|
||||
->getToken();
|
||||
|
||||
$this->assertAttributeEquals(null, 'signature', $token);
|
||||
$this->assertEquals('http://client.abc.com', $token->getClaim('aud'));
|
||||
$this->assertEquals('http://api.abc.com', $token->getClaim('iss'));
|
||||
$this->assertEquals(self::CURRENT_TIME + 3000, $token->getClaim('exp'));
|
||||
$this->assertEquals($user, $token->getClaim('user'));
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Parsing\Decoder
|
||||
*/
|
||||
public function parserCanReadAToken(Token $generated)
|
||||
{
|
||||
$read = (new Parser())->parse((string) $generated);
|
||||
|
||||
$this->assertEquals($generated, $read);
|
||||
$this->assertEquals('testing', $read->getClaim('user')->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\ValidationData
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Claim\EqualsTo
|
||||
* @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Parsing\Decoder
|
||||
*/
|
||||
public function tokenValidationShouldReturnWhenEverythingIsFine(Token $generated)
|
||||
{
|
||||
$data = new ValidationData(self::CURRENT_TIME - 10);
|
||||
$data->setAudience('http://client.abc.com');
|
||||
$data->setIssuer('http://api.abc.com');
|
||||
|
||||
$this->assertTrue($generated->validate($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @dataProvider invalidValidationData
|
||||
*
|
||||
* @depends builderCanGenerateAToken
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder
|
||||
* @covers Lcobucci\JWT\Parser
|
||||
* @covers Lcobucci\JWT\Token
|
||||
* @covers Lcobucci\JWT\ValidationData
|
||||
* @covers Lcobucci\JWT\Claim\Factory
|
||||
* @covers Lcobucci\JWT\Claim\Basic
|
||||
* @covers Lcobucci\JWT\Claim\EqualsTo
|
||||
* @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder
|
||||
* @covers Lcobucci\JWT\Parsing\Decoder
|
||||
*/
|
||||
public function tokenValidationShouldReturnFalseWhenExpectedDataDontMatch(ValidationData $data, Token $generated)
|
||||
{
|
||||
$this->assertFalse($generated->validate($data));
|
||||
}
|
||||
|
||||
public function invalidValidationData()
|
||||
{
|
||||
$expired = new ValidationData(self::CURRENT_TIME + 3020);
|
||||
$expired->setAudience('http://client.abc.com');
|
||||
$expired->setIssuer('http://api.abc.com');
|
||||
|
||||
$invalidAudience = new ValidationData(self::CURRENT_TIME - 10);
|
||||
$invalidAudience->setAudience('http://cclient.abc.com');
|
||||
$invalidAudience->setIssuer('http://api.abc.com');
|
||||
|
||||
$invalidIssuer = new ValidationData(self::CURRENT_TIME - 10);
|
||||
$invalidIssuer->setAudience('http://client.abc.com');
|
||||
$invalidIssuer->setIssuer('http://aapi.abc.com');
|
||||
|
||||
return [[$expired], [$invalidAudience], [$invalidIssuer]];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
-----BEGIN EC PRIVATE KEY-----
|
||||
MHcCAQEEIBGpMoZJ64MMSzuo5JbmXpf9V4qSWdLIl/8RmJLcfn/qoAoGCCqGSM49
|
||||
AwEHoUQDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpnd0wxa2iFruiI2tsEdGFTLTsy
|
||||
U+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==
|
||||
-----END EC PRIVATE KEY-----
|
||||
@@ -0,0 +1,8 @@
|
||||
-----BEGIN EC PARAMETERS-----
|
||||
BggqhkjOPQMBBw==
|
||||
-----END EC PARAMETERS-----
|
||||
-----BEGIN EC PRIVATE KEY-----
|
||||
MHcCAQEEIM6G7WZ6SqoPwrHwGXhOJkYD+ErT8dfRvrNifgBQvSb7oAoGCCqGSM49
|
||||
AwEHoUQDQgAE09Hkp/u0tIGdzlQ99R/sXCOr9DTZAfLex4D4Po0C1L3qUqHrzZ0m
|
||||
B3bAhe+pwEDQ/jqVqdzxhA9i4PqT7F4Aew==
|
||||
-----END EC PRIVATE KEY-----
|
||||
@@ -0,0 +1,4 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpn
|
||||
d0wxa2iFruiI2tsEdGFTLTsyU+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==
|
||||
-----END PUBLIC KEY-----
|
||||
@@ -0,0 +1,4 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEdgxRxlhzhHGj+v6S2ikp+33LoGp5
|
||||
QWbEWv8BORsr2Ayg6C7deDDRM/s/f0R++4zZqXro1gDTVF5VDv7nE+EfEw==
|
||||
-----END PUBLIC KEY-----
|
||||
@@ -0,0 +1,4 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE09Hkp/u0tIGdzlQ99R/sXCOr9DTZ
|
||||
AfLex4D4Po0C1L3qUqHrzZ0mB3bAhe+pwEDQ/jqVqdzxhA9i4PqT7F4Aew==
|
||||
-----END PUBLIC KEY-----
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
Proc-Type: 4,ENCRYPTED
|
||||
DEK-Info: AES-128-CBC,0D71668CE71033CB9150ED82FC87F4A1
|
||||
|
||||
uLzPNDdlHnZ77tAGMHyPYERDMBcdV4SsQJYcSjiHhR2o0dLGTdgOpQrXTHPX4GJF
|
||||
LlEWLhAAV9wx2mM/2kHDWB4uZwThtT9/v+RFoW1WbVO/d3lhI9fg4/73/DWAH/7/
|
||||
afMRc7ZOVoAmVCESotPx4khCHoE97RdY/JtkLTzc3+peqmL53AbYXrg9rTN1B+ZV
|
||||
U3w4ciQS8Uki87zDYIBjYtaOCyMUTvug25CvdssvUMBoc/Jc0xps6/vAyXrnzlGT
|
||||
pZD0Tst8idswfDi613BhAaxJspeY0AErWA59qJ3eGzbiQq5RDWcbJe/Tz5r/6+NN
|
||||
DkvNQ7DaEZ6LpeWX0MUq6/QWfrM8yE95XhjyC1d3LYn32lXHUygbgTFWIgLDoOE6
|
||||
nBhu34SWtbLAnqYGewaJFxhlYVS9rb/uvYQg70r5X9Sx6alCQPiPyIv39IItezn2
|
||||
HF2GRfE91MPZUeDhdqdvvOlSZVM5KnYc1fhamGAwM48gdDDXe8Czu/JEGoANNvC3
|
||||
l/Z1p5RtGF4hrel9WpeX9zQq3pvtfVcVIiWuRUwCOSQytXlieRK37sMuYeggvmjV
|
||||
VvaCods3mS/panWg9T/D/deIXjhzNJLvyiJg8+3sY5H4yNe0XpbaAc/ySwt9Rcxy
|
||||
FzFQ+5pghLSZgR1uV3AhdcnzXBU2GkYhdGKt2tUsH0UeVQ2BXxTlBFsCOh2dWqcj
|
||||
y3suIG65bukDAAWidQ4q3S6ZIMpXBhhCj7nwB5jQ7wSlU3U9So0ndr7zxdUILiMm
|
||||
chHi3q5apVZnMGcwv2B33rt4nD7HgGEmRKkCelrSrBATY1ut+T4rCDzKDqDs3jpv
|
||||
hYIWrlNPTkJyQz3eWly6Db+FJEfdYGadYJusc7/nOxCh/QmUu8Sh3NhKT6TH0bS7
|
||||
1AAqd8H+2hJ9I32Dhd2qwAF7PkNe2LGi+P8tbAtepKGim5w65wnsPePMnrfxumsG
|
||||
PeDnMrqeCKy+fME7a/MS5kmEBpmD4BMhVC6/OhFVz8gBty1f8yIEZggHNQN2QK7m
|
||||
NIrG+PwqW2w8HoxOlAi2Ix4LTPifrdfsH02U7aM1pgo1rZzD4AOzqvzCaK43H2VB
|
||||
BHLeTBGoLEUxXA9C+iGbeQlKXkMC00QKkjK5+nvkvnvePFfsrTQIpuyGufD/MoPb
|
||||
6fpwsyHZDxhxMN1PJk1b1lPq2Ui4hXpVNOYd4Q6OQz7bwxTMRX9XQromUlKMMgAT
|
||||
edX8v2NdM7Ssy1IwHuGVbDEpZdjoeaWZ1iNRV17i/EaJAqwYDQLfsuHBlzZL1ov1
|
||||
xkKVJdL8Y3q80oRAzTQDVdzL/rI44LLAfv609YByCnw29feYJY2W6gV0O7ZSw413
|
||||
XUkc5CaEbR1LuG8NtnOOPJV4Tb/hNsIDtvVm7Hl5npBKBe4iVgQ2LNuC2eT69d/z
|
||||
uvzgjISlumPiO5ivuYe0QtLPuJSc+/Bl8bPL8gcNQEtqkzj7IftHPPZNs+bJC2uY
|
||||
bPjq5KoDNAMF6VHuKHwu48MBYpnXDIg3ZenmJwGRULRBhK6324hDS6NJ7ULTBU2M
|
||||
TZCHmg89ySLBfCAspVeo63o/R7bs9a7BP9x2h5uwCBogSvkEwhhPKnboVN45bp9c
|
||||
-----END RSA PRIVATE KEY-----
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwLpbUP8a9yflt5LKUUS3
|
||||
NPuRM7yEouPWg0VKeY5AURu4i8bqQ20K5jwfRJ+w05FvlywG4EuxpnpTFTVS2/do
|
||||
q3xufzTf/C3KIDOAHEifkdx4140btKxxm4mD9Eu2CQ32adZyScha50KUFlfnAAic
|
||||
Hb8wYxjFyWo3PAbGYmCQCn2z97Ab0Ar6NR1e+V9f8EL9Orr2f04puKJfQTZdWVDF
|
||||
UJR4w7QZ/CPY0LEsiFLW3QQCNraka1mtrLJwPqreBtDEkj8IoISNkrguu/97RQZz
|
||||
miJgBQkVjr6OfqG5WIFr0MzbRZc1/aK9g8ft88nhhQm0E3GqkCxBKTwgA03HtK07
|
||||
qQIDAQAB
|
||||
-----END PUBLIC KEY-----
|
||||
@@ -0,0 +1,28 @@
|
||||
-----BEGIN PRIVATE KEY-----
|
||||
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDTvwE87MtgREYL
|
||||
TL4aHhQo3ZzogmxxvMUsKnPzyxRs1YrXOSOpwN0npsXarBKKVIUMNLfFODp/vnQn
|
||||
2Zp06N8XG59WAOKwvC4MfxLDQkA+JXggzHlkbVoTN+dUkdYIFqSKuAPGwiWToRK2
|
||||
SxEhij3rE2FON8jQZvDxZkiP9a4vxJO3OTPQwKredXFiObsXD/c3RtLFhKctjCyH
|
||||
OIrP0bQEsee/m7JNtG4ry6BPusN6wb+vJo5ieBYPa3c19akNq6q/nYWhplhkkJSu
|
||||
aOrL5xXEFzI5TvcvnXR568GVcxK8YLfFkdxpsXGt5rAbeh0h/U5kILEAqv8P9PGT
|
||||
ZpicKbrnAgMBAAECggEAd3yTQEQHR91/ASVfKPHMQns77eCbPVtekFusbugsMHYY
|
||||
EPdHbqVMpvFvOMRc+f5Tzd15ziq6qBdbCJm8lThLm4iU0z1QrpaiDZ8vgUvDYM5Y
|
||||
CXoZDli+uZWUTp60/n94fmb0ipZIChScsI2PrzOJWTvobvD/uso8MJydWc8zafQm
|
||||
uqYzygOfjFZvU4lSfgzpefhpquy0JUy5TiKRmGUnwLb3TtcsVavjsn4QmNwLYgOF
|
||||
2OE+R12ex3pAKTiRE6FcnE1xFIo1GKhBa2Otgw3MDO6Gg+kn8Q4alKz6C6RRlgaH
|
||||
R7sYzEfJhsk/GGFTYOzXKQz2lSaStKt9wKCor04RcQKBgQDzPOu5jCTfayUo7xY2
|
||||
jHtiogHyKLLObt9l3qbwgXnaD6rnxYNvCrA0OMvT+iZXsFZKJkYzJr8ZOxOpPROk
|
||||
10WdOaefiwUyL5dypueSwlIDwVm+hI4Bs82MajHtzOozh+73wA+aw5rPs84Uix9w
|
||||
VbbwaVR6qP/BV09yJYS5kQ7fmwKBgQDe2xjywX2d2MC+qzRr+LfU+1+gq0jjhBCX
|
||||
WHqRN6IECB0xTnXUf9WL/VCoI1/55BhdbbEja+4btYgcXSPmlXBIRKQ4VtFfVmYB
|
||||
kPXeD8oZ7LyuNdCsbKNe+x1IHXDe6Wfs3L9ulCfXxeIE84wy3fd66mQahyXV9iD9
|
||||
CkuifMqUpQKBgQCiydHlY1LGJ/o9tA2Ewm5Na6mrvOs2V2Ox1NqbObwoYbX62eiF
|
||||
53xX5u8bVl5U75JAm+79it/4bd5RtKux9dUETbLOhwcaOFm+hM+VG/IxyzRZ2nMD
|
||||
1qcpY2U5BpxzknUvYF3RMTop6edxPk7zKpp9ubCtSu+oINvtxAhY/SkcIwKBgGP1
|
||||
upcImyO2GZ5shLL5eNubdSVILwV+M0LveOqyHYXZbd6z5r5OKKcGFKuWUnJwEU22
|
||||
6gGNY9wh7M9sJ7JBzX9c6pwqtPcidda2AtJ8GpbOTUOG9/afNBhiYpv6OKqD3w2r
|
||||
ZmJfKg/qvpqh83zNezgy8nvDqwDxyZI2j/5uIx/RAoGBAMWRmxtv6H2cKhibI/aI
|
||||
MTJM4QRjyPNxQqvAQsv+oHUbid06VK3JE+9iQyithjcfNOwnCaoO7I7qAj9QEfJS
|
||||
MZQc/W/4DHJebo2kd11yoXPVTXXOuEwLSKCejBXABBY0MPNuPUmiXeU0O3Tyi37J
|
||||
TUKzrgcd7NvlA41Y4xKcOqEA
|
||||
-----END PRIVATE KEY-----
|
||||
@@ -0,0 +1,9 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA078BPOzLYERGC0y+Gh4U
|
||||
KN2c6IJscbzFLCpz88sUbNWK1zkjqcDdJ6bF2qwSilSFDDS3xTg6f750J9madOjf
|
||||
FxufVgDisLwuDH8Sw0JAPiV4IMx5ZG1aEzfnVJHWCBakirgDxsIlk6EStksRIYo9
|
||||
6xNhTjfI0Gbw8WZIj/WuL8STtzkz0MCq3nVxYjm7Fw/3N0bSxYSnLYwshziKz9G0
|
||||
BLHnv5uyTbRuK8ugT7rDesG/ryaOYngWD2t3NfWpDauqv52FoaZYZJCUrmjqy+cV
|
||||
xBcyOU73L510eevBlXMSvGC3xZHcabFxreawG3odIf1OZCCxAKr/D/Txk2aYnCm6
|
||||
5wIDAQAB
|
||||
-----END PUBLIC KEY-----
|
||||
@@ -0,0 +1,699 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT;
|
||||
|
||||
use Lcobucci\JWT\Claim\Factory as ClaimFactory;
|
||||
use Lcobucci\JWT\Parsing\Encoder;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class BuilderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var Encoder|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $encoder;
|
||||
|
||||
/**
|
||||
* @var ClaimFactory|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $claimFactory;
|
||||
|
||||
/**
|
||||
* @var Claim|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $defaultClaim;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$this->encoder = $this->getMock(Encoder::class);
|
||||
$this->claimFactory = $this->getMock(ClaimFactory::class, [], [], '', false);
|
||||
$this->defaultClaim = $this->getMock(Claim::class);
|
||||
|
||||
$this->claimFactory->expects($this->any())
|
||||
->method('create')
|
||||
->willReturn($this->defaultClaim);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Builder
|
||||
*/
|
||||
private function createBuilder()
|
||||
{
|
||||
return new Builder($this->encoder, $this->claimFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::__construct
|
||||
*/
|
||||
public function constructMustInitializeTheAttributes()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
|
||||
$this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
|
||||
$this->assertAttributeEquals([], 'claims', $builder);
|
||||
$this->assertAttributeEquals(null, 'signature', $builder);
|
||||
$this->assertAttributeSame($this->encoder, 'encoder', $builder);
|
||||
$this->assertAttributeSame($this->claimFactory, 'claimFactory', $builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setAudience
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setAudienceMustChangeTheAudClaim()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
$builder->setAudience('test');
|
||||
|
||||
$this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
|
||||
$this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setAudience
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setAudienceCanReplicateItemOnHeader()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
$builder->setAudience('test', true);
|
||||
|
||||
$this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $builder);
|
||||
|
||||
$this->assertAttributeEquals(
|
||||
['alg' => 'none', 'typ' => 'JWT', 'aud' => $this->defaultClaim],
|
||||
'headers',
|
||||
$builder
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setAudience
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setAudienceMustKeepAFluentInterface()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
|
||||
$this->assertSame($builder, $builder->setAudience('test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setExpiration
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setExpirationMustChangeTheExpClaim()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
$builder->setExpiration('2');
|
||||
|
||||
$this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
|
||||
$this->assertAttributeEquals(['exp' => $this->defaultClaim], 'claims', $builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setExpiration
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setExpirationCanReplicateItemOnHeader()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
$builder->setExpiration('2', true);
|
||||
|
||||
$this->assertAttributeEquals(['exp' => $this->defaultClaim], 'claims', $builder);
|
||||
|
||||
$this->assertAttributeEquals(
|
||||
['alg' => 'none', 'typ' => 'JWT', 'exp' => $this->defaultClaim],
|
||||
'headers',
|
||||
$builder
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setExpiration
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setExpirationMustKeepAFluentInterface()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
|
||||
$this->assertSame($builder, $builder->setExpiration('2'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setId
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setIdMustChangeTheJtiClaim()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
$builder->setId('2');
|
||||
|
||||
$this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
|
||||
$this->assertAttributeEquals(['jti' => $this->defaultClaim], 'claims', $builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setId
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setIdCanReplicateItemOnHeader()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
$builder->setId('2', true);
|
||||
|
||||
$this->assertAttributeEquals(['jti' => $this->defaultClaim], 'claims', $builder);
|
||||
|
||||
$this->assertAttributeEquals(
|
||||
['alg' => 'none', 'typ' => 'JWT', 'jti' => $this->defaultClaim],
|
||||
'headers',
|
||||
$builder
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setId
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setIdMustKeepAFluentInterface()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
|
||||
$this->assertSame($builder, $builder->setId('2'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setIssuedAt
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setIssuedAtMustChangeTheIatClaim()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
$builder->setIssuedAt('2');
|
||||
|
||||
$this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
|
||||
$this->assertAttributeEquals(['iat' => $this->defaultClaim], 'claims', $builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setIssuedAt
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setIssuedAtCanReplicateItemOnHeader()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
$builder->setIssuedAt('2', true);
|
||||
|
||||
$this->assertAttributeEquals(['iat' => $this->defaultClaim], 'claims', $builder);
|
||||
|
||||
$this->assertAttributeEquals(
|
||||
['alg' => 'none', 'typ' => 'JWT', 'iat' => $this->defaultClaim],
|
||||
'headers',
|
||||
$builder
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setIssuedAt
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setIssuedAtMustKeepAFluentInterface()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
|
||||
$this->assertSame($builder, $builder->setIssuedAt('2'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setIssuer
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setIssuerMustChangeTheIssClaim()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
$builder->setIssuer('2');
|
||||
|
||||
$this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
|
||||
$this->assertAttributeEquals(['iss' => $this->defaultClaim], 'claims', $builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setIssuer
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setIssuerCanReplicateItemOnHeader()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
$builder->setIssuer('2', true);
|
||||
|
||||
$this->assertAttributeEquals(['iss' => $this->defaultClaim], 'claims', $builder);
|
||||
|
||||
$this->assertAttributeEquals(
|
||||
['alg' => 'none', 'typ' => 'JWT', 'iss' => $this->defaultClaim],
|
||||
'headers',
|
||||
$builder
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setIssuer
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setIssuerMustKeepAFluentInterface()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
|
||||
$this->assertSame($builder, $builder->setIssuer('2'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setNotBefore
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setNotBeforeMustChangeTheNbfClaim()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
$builder->setNotBefore('2');
|
||||
|
||||
$this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
|
||||
$this->assertAttributeEquals(['nbf' => $this->defaultClaim], 'claims', $builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setNotBefore
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setNotBeforeCanReplicateItemOnHeader()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
$builder->setNotBefore('2', true);
|
||||
|
||||
$this->assertAttributeEquals(['nbf' => $this->defaultClaim], 'claims', $builder);
|
||||
|
||||
$this->assertAttributeEquals(
|
||||
['alg' => 'none', 'typ' => 'JWT', 'nbf' => $this->defaultClaim],
|
||||
'headers',
|
||||
$builder
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setNotBefore
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setNotBeforeMustKeepAFluentInterface()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
|
||||
$this->assertSame($builder, $builder->setNotBefore('2'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setSubject
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setSubjectMustChangeTheSubClaim()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
$builder->setSubject('2');
|
||||
|
||||
$this->assertAttributeEquals(['alg' => 'none', 'typ' => 'JWT'], 'headers', $builder);
|
||||
$this->assertAttributeEquals(['sub' => $this->defaultClaim], 'claims', $builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setSubject
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setSubjectCanReplicateItemOnHeader()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
$builder->setSubject('2', true);
|
||||
|
||||
$this->assertAttributeEquals(['sub' => $this->defaultClaim], 'claims', $builder);
|
||||
|
||||
$this->assertAttributeEquals(
|
||||
['alg' => 'none', 'typ' => 'JWT', 'sub' => $this->defaultClaim],
|
||||
'headers',
|
||||
$builder
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setSubject
|
||||
* @covers Lcobucci\JWT\Builder::setRegisteredClaim
|
||||
*/
|
||||
public function setSubjectMustKeepAFluentInterface()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
|
||||
$this->assertSame($builder, $builder->setSubject('2'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::set
|
||||
*/
|
||||
public function setMustConfigureTheGivenClaim()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
$builder->set('userId', 2);
|
||||
|
||||
$this->assertAttributeEquals(['userId' => $this->defaultClaim], 'claims', $builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::set
|
||||
*/
|
||||
public function setMustKeepAFluentInterface()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
|
||||
$this->assertSame($builder, $builder->set('userId', 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setHeader
|
||||
*/
|
||||
public function setHeaderMustConfigureTheGivenClaim()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
$builder->setHeader('userId', 2);
|
||||
|
||||
$this->assertAttributeEquals(
|
||||
['alg' => 'none', 'typ' => 'JWT', 'userId' => $this->defaultClaim],
|
||||
'headers',
|
||||
$builder
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setHeader
|
||||
*/
|
||||
public function setHeaderMustKeepAFluentInterface()
|
||||
{
|
||||
$builder = $this->createBuilder();
|
||||
|
||||
$this->assertSame($builder, $builder->setHeader('userId', 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::getToken
|
||||
* @uses Lcobucci\JWT\Token
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::sign
|
||||
*/
|
||||
public function signMustChangeTheSignature()
|
||||
{
|
||||
$signer = $this->getMock(Signer::class);
|
||||
$signature = $this->getMock(Signature::class, [], [], '', false);
|
||||
|
||||
$signer->expects($this->any())
|
||||
->method('sign')
|
||||
->willReturn($signature);
|
||||
|
||||
$builder = $this->createBuilder();
|
||||
$builder->sign($signer, 'test');
|
||||
|
||||
$this->assertAttributeSame($signature, 'signature', $builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::getToken
|
||||
* @uses Lcobucci\JWT\Token
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::sign
|
||||
*/
|
||||
public function signMustKeepAFluentInterface()
|
||||
{
|
||||
$signer = $this->getMock(Signer::class);
|
||||
$signature = $this->getMock(Signature::class, [], [], '', false);
|
||||
|
||||
$signer->expects($this->any())
|
||||
->method('sign')
|
||||
->willReturn($signature);
|
||||
|
||||
$builder = $this->createBuilder();
|
||||
|
||||
$this->assertSame($builder, $builder->sign($signer, 'test'));
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends signMustKeepAFluentInterface
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::unsign
|
||||
*/
|
||||
public function unsignMustRemoveTheSignature(Builder $builder)
|
||||
{
|
||||
$builder->unsign();
|
||||
|
||||
$this->assertAttributeSame(null, 'signature', $builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends signMustKeepAFluentInterface
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::unsign
|
||||
*/
|
||||
public function unsignMustKeepAFluentInterface(Builder $builder)
|
||||
{
|
||||
$this->assertSame($builder, $builder->unsign());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::sign
|
||||
* @uses Lcobucci\JWT\Builder::getToken
|
||||
* @uses Lcobucci\JWT\Token
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::set
|
||||
*
|
||||
* @expectedException BadMethodCallException
|
||||
*/
|
||||
public function setMustRaiseExceptionWhenTokenHasBeenSigned()
|
||||
{
|
||||
$signer = $this->getMock(Signer::class);
|
||||
$signature = $this->getMock(Signature::class, [], [], '', false);
|
||||
|
||||
$signer->expects($this->any())
|
||||
->method('sign')
|
||||
->willReturn($signature);
|
||||
|
||||
$builder = $this->createBuilder();
|
||||
$builder->sign($signer, 'test');
|
||||
$builder->set('test', 123);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::sign
|
||||
* @uses Lcobucci\JWT\Builder::getToken
|
||||
* @uses Lcobucci\JWT\Token
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::setHeader
|
||||
*
|
||||
* @expectedException BadMethodCallException
|
||||
*/
|
||||
public function setHeaderMustRaiseExceptionWhenTokenHasBeenSigned()
|
||||
{
|
||||
$signer = $this->getMock(Signer::class);
|
||||
$signature = $this->getMock(Signature::class, [], [], '', false);
|
||||
|
||||
$signer->expects($this->any())
|
||||
->method('sign')
|
||||
->willReturn($signature);
|
||||
|
||||
$builder = $this->createBuilder();
|
||||
$builder->sign($signer, 'test');
|
||||
$builder->setHeader('test', 123);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Builder::__construct
|
||||
* @uses Lcobucci\JWT\Builder::set
|
||||
* @uses Lcobucci\JWT\Token
|
||||
*
|
||||
* @covers Lcobucci\JWT\Builder::getToken
|
||||
*/
|
||||
public function getTokenMustReturnANewTokenWithCurrentConfiguration()
|
||||
{
|
||||
$signature = $this->getMock(Signature::class, [], [], '', false);
|
||||
|
||||
$this->encoder->expects($this->exactly(2))
|
||||
->method('jsonEncode')
|
||||
->withConsecutive([['typ'=> 'JWT', 'alg' => 'none']], [['test' => $this->defaultClaim]])
|
||||
->willReturnOnConsecutiveCalls('1', '2');
|
||||
|
||||
$this->encoder->expects($this->exactly(3))
|
||||
->method('base64UrlEncode')
|
||||
->withConsecutive(['1'], ['2'], [$signature])
|
||||
->willReturnOnConsecutiveCalls('1', '2', '3');
|
||||
|
||||
$builder = $this->createBuilder()->set('test', 123);
|
||||
|
||||
$builderSign = new \ReflectionProperty($builder, 'signature');
|
||||
$builderSign->setAccessible(true);
|
||||
$builderSign->setValue($builder, $signature);
|
||||
|
||||
$token = $builder->getToken();
|
||||
|
||||
$tokenSign = new \ReflectionProperty($token, 'signature');
|
||||
$tokenSign->setAccessible(true);
|
||||
|
||||
$this->assertAttributeEquals(['1', '2', '3'], 'payload', $token);
|
||||
$this->assertAttributeEquals($token->getHeaders(), 'headers', $builder);
|
||||
$this->assertAttributeEquals($token->getClaims(), 'claims', $builder);
|
||||
$this->assertAttributeSame($tokenSign->getValue($token), 'signature', $builder);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Claim;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class BasicTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\Basic::__construct
|
||||
*/
|
||||
public function constructorShouldConfigureTheAttributes()
|
||||
{
|
||||
$claim = new Basic('test', 1);
|
||||
|
||||
$this->assertAttributeEquals('test', 'name', $claim);
|
||||
$this->assertAttributeEquals(1, 'value', $claim);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\Basic::getName
|
||||
*/
|
||||
public function getNameShouldReturnTheClaimName()
|
||||
{
|
||||
$claim = new Basic('test', 1);
|
||||
|
||||
$this->assertEquals('test', $claim->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\Basic::getValue
|
||||
*/
|
||||
public function getValueShouldReturnTheClaimValue()
|
||||
{
|
||||
$claim = new Basic('test', 1);
|
||||
|
||||
$this->assertEquals(1, $claim->getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\Basic::jsonSerialize
|
||||
*/
|
||||
public function jsonSerializeShouldReturnTheClaimValue()
|
||||
{
|
||||
$claim = new Basic('test', 1);
|
||||
|
||||
$this->assertEquals(1, $claim->jsonSerialize());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\Basic::__toString
|
||||
*/
|
||||
public function toStringShouldReturnTheClaimValue()
|
||||
{
|
||||
$claim = new Basic('test', 1);
|
||||
|
||||
$this->assertEquals('1', (string) $claim);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Claim;
|
||||
|
||||
use Lcobucci\JWT\ValidationData;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class EqualsToTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getName
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
* @uses Lcobucci\JWT\ValidationData::has
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\EqualsTo::validate
|
||||
*/
|
||||
public function validateShouldReturnTrueWhenValidationDontHaveTheClaim()
|
||||
{
|
||||
$claim = new EqualsTo('iss', 'test');
|
||||
|
||||
$this->assertTrue($claim->validate(new ValidationData()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getName
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getValue
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
* @uses Lcobucci\JWT\ValidationData::setIssuer
|
||||
* @uses Lcobucci\JWT\ValidationData::has
|
||||
* @uses Lcobucci\JWT\ValidationData::get
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\EqualsTo::validate
|
||||
*/
|
||||
public function validateShouldReturnTrueWhenValueIsEqualsToValidationData()
|
||||
{
|
||||
$claim = new EqualsTo('iss', 'test');
|
||||
|
||||
$data = new ValidationData();
|
||||
$data->setIssuer('test');
|
||||
|
||||
$this->assertTrue($claim->validate($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getName
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getValue
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
* @uses Lcobucci\JWT\ValidationData::setIssuer
|
||||
* @uses Lcobucci\JWT\ValidationData::has
|
||||
* @uses Lcobucci\JWT\ValidationData::get
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\EqualsTo::validate
|
||||
*/
|
||||
public function validateShouldReturnFalseWhenValueIsNotEqualsToValidationData()
|
||||
{
|
||||
$claim = new EqualsTo('iss', 'test');
|
||||
|
||||
$data = new ValidationData();
|
||||
$data->setIssuer('test1');
|
||||
|
||||
$this->assertFalse($claim->validate($data));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Claim;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class FactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\Factory::__construct
|
||||
*/
|
||||
public function constructMustConfigureTheCallbacks()
|
||||
{
|
||||
$callback = function () {
|
||||
};
|
||||
$factory = new Factory(['test' => $callback]);
|
||||
|
||||
$expected = [
|
||||
'iat' => [$factory, 'createLesserOrEqualsTo'],
|
||||
'nbf' => [$factory, 'createLesserOrEqualsTo'],
|
||||
'exp' => [$factory, 'createGreaterOrEqualsTo'],
|
||||
'iss' => [$factory, 'createEqualsTo'],
|
||||
'aud' => [$factory, 'createEqualsTo'],
|
||||
'sub' => [$factory, 'createEqualsTo'],
|
||||
'jti' => [$factory, 'createEqualsTo'],
|
||||
'test' => $callback
|
||||
];
|
||||
|
||||
$this->assertAttributeEquals($expected, 'callbacks', $factory);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Factory::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\Factory::create
|
||||
* @covers Lcobucci\JWT\Claim\Factory::createLesserOrEqualsTo
|
||||
*/
|
||||
public function createShouldReturnALesserOrEqualsToClaimForIssuedAt()
|
||||
{
|
||||
$claim = new Factory();
|
||||
|
||||
$this->assertInstanceOf(LesserOrEqualsTo::class, $claim->create('iat', 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Factory::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\Factory::create
|
||||
* @covers Lcobucci\JWT\Claim\Factory::createLesserOrEqualsTo
|
||||
*/
|
||||
public function createShouldReturnALesserOrEqualsToClaimForNotBefore()
|
||||
{
|
||||
$claim = new Factory();
|
||||
|
||||
$this->assertInstanceOf(LesserOrEqualsTo::class, $claim->create('nbf', 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Factory::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\Factory::create
|
||||
* @covers Lcobucci\JWT\Claim\Factory::createGreaterOrEqualsTo
|
||||
*/
|
||||
public function createShouldReturnAGreaterOrEqualsToClaimForExpiration()
|
||||
{
|
||||
$claim = new Factory();
|
||||
|
||||
$this->assertInstanceOf(GreaterOrEqualsTo::class, $claim->create('exp', 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Factory::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\Factory::create
|
||||
* @covers Lcobucci\JWT\Claim\Factory::createEqualsTo
|
||||
*/
|
||||
public function createShouldReturnAnEqualsToClaimForId()
|
||||
{
|
||||
$claim = new Factory();
|
||||
|
||||
$this->assertInstanceOf(EqualsTo::class, $claim->create('jti', 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Factory::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\Factory::create
|
||||
* @covers Lcobucci\JWT\Claim\Factory::createEqualsTo
|
||||
*/
|
||||
public function createShouldReturnAnEqualsToClaimForIssuer()
|
||||
{
|
||||
$claim = new Factory();
|
||||
|
||||
$this->assertInstanceOf(EqualsTo::class, $claim->create('iss', 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Factory::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\Factory::create
|
||||
* @covers Lcobucci\JWT\Claim\Factory::createEqualsTo
|
||||
*/
|
||||
public function createShouldReturnAnEqualsToClaimForAudience()
|
||||
{
|
||||
$claim = new Factory();
|
||||
|
||||
$this->assertInstanceOf(EqualsTo::class, $claim->create('aud', 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Factory::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\Factory::create
|
||||
* @covers Lcobucci\JWT\Claim\Factory::createEqualsTo
|
||||
*/
|
||||
public function createShouldReturnAnEqualsToClaimForSubject()
|
||||
{
|
||||
$claim = new Factory();
|
||||
|
||||
$this->assertInstanceOf(EqualsTo::class, $claim->create('sub', 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Factory::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\Factory::create
|
||||
* @covers Lcobucci\JWT\Claim\Factory::createBasic
|
||||
*/
|
||||
public function createShouldReturnABasiclaimForOtherClaims()
|
||||
{
|
||||
$claim = new Factory();
|
||||
|
||||
$this->assertInstanceOf(Basic::class, $claim->create('test', 1));
|
||||
}
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Claim;
|
||||
|
||||
use Lcobucci\JWT\ValidationData;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class GreaterOrEqualsToTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getName
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
* @uses Lcobucci\JWT\ValidationData::has
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo::validate
|
||||
*/
|
||||
public function validateShouldReturnTrueWhenValidationDontHaveTheClaim()
|
||||
{
|
||||
$claim = new GreaterOrEqualsTo('iss', 10);
|
||||
|
||||
$this->assertTrue($claim->validate(new ValidationData()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getName
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getValue
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
* @uses Lcobucci\JWT\ValidationData::setIssuer
|
||||
* @uses Lcobucci\JWT\ValidationData::has
|
||||
* @uses Lcobucci\JWT\ValidationData::get
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo::validate
|
||||
*/
|
||||
public function validateShouldReturnTrueWhenValueIsGreaterThanValidationData()
|
||||
{
|
||||
$claim = new GreaterOrEqualsTo('iss', 11);
|
||||
|
||||
$data = new ValidationData();
|
||||
$data->setIssuer(10);
|
||||
|
||||
$this->assertTrue($claim->validate($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getName
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getValue
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
* @uses Lcobucci\JWT\ValidationData::setIssuer
|
||||
* @uses Lcobucci\JWT\ValidationData::has
|
||||
* @uses Lcobucci\JWT\ValidationData::get
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo::validate
|
||||
*/
|
||||
public function validateShouldReturnTrueWhenValueIsEqualsToValidationData()
|
||||
{
|
||||
$claim = new GreaterOrEqualsTo('iss', 10);
|
||||
|
||||
$data = new ValidationData();
|
||||
$data->setIssuer(10);
|
||||
|
||||
$this->assertTrue($claim->validate($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getName
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getValue
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
* @uses Lcobucci\JWT\ValidationData::setIssuer
|
||||
* @uses Lcobucci\JWT\ValidationData::has
|
||||
* @uses Lcobucci\JWT\ValidationData::get
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo::validate
|
||||
*/
|
||||
public function validateShouldReturnFalseWhenValueIsLesserThanValidationData()
|
||||
{
|
||||
$claim = new GreaterOrEqualsTo('iss', 10);
|
||||
|
||||
$data = new ValidationData();
|
||||
$data->setIssuer(11);
|
||||
|
||||
$this->assertFalse($claim->validate($data));
|
||||
}
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Claim;
|
||||
|
||||
use Lcobucci\JWT\ValidationData;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class LesserOrEqualsToTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getName
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
* @uses Lcobucci\JWT\ValidationData::has
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\LesserOrEqualsTo::validate
|
||||
*/
|
||||
public function validateShouldReturnTrueWhenValidationDontHaveTheClaim()
|
||||
{
|
||||
$claim = new LesserOrEqualsTo('iss', 10);
|
||||
|
||||
$this->assertTrue($claim->validate(new ValidationData()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getName
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getValue
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
* @uses Lcobucci\JWT\ValidationData::setIssuer
|
||||
* @uses Lcobucci\JWT\ValidationData::has
|
||||
* @uses Lcobucci\JWT\ValidationData::get
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\LesserOrEqualsTo::validate
|
||||
*/
|
||||
public function validateShouldReturnTrueWhenValueIsLesserThanValidationData()
|
||||
{
|
||||
$claim = new LesserOrEqualsTo('iss', 10);
|
||||
|
||||
$data = new ValidationData();
|
||||
$data->setIssuer(11);
|
||||
|
||||
$this->assertTrue($claim->validate($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getName
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getValue
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
* @uses Lcobucci\JWT\ValidationData::setIssuer
|
||||
* @uses Lcobucci\JWT\ValidationData::has
|
||||
* @uses Lcobucci\JWT\ValidationData::get
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\LesserOrEqualsTo::validate
|
||||
*/
|
||||
public function validateShouldReturnTrueWhenValueIsEqualsToValidationData()
|
||||
{
|
||||
$claim = new LesserOrEqualsTo('iss', 10);
|
||||
|
||||
$data = new ValidationData();
|
||||
$data->setIssuer(10);
|
||||
|
||||
$this->assertTrue($claim->validate($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getName
|
||||
* @uses Lcobucci\JWT\Claim\Basic::getValue
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
* @uses Lcobucci\JWT\ValidationData::setIssuer
|
||||
* @uses Lcobucci\JWT\ValidationData::has
|
||||
* @uses Lcobucci\JWT\ValidationData::get
|
||||
*
|
||||
* @covers Lcobucci\JWT\Claim\LesserOrEqualsTo::validate
|
||||
*/
|
||||
public function validateShouldReturnFalseWhenValueIsGreaterThanValidationData()
|
||||
{
|
||||
$claim = new LesserOrEqualsTo('iss', 11);
|
||||
|
||||
$data = new ValidationData();
|
||||
$data->setIssuer(10);
|
||||
|
||||
$this->assertFalse($claim->validate($data));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT;
|
||||
|
||||
use Lcobucci\JWT\Claim\Factory as ClaimFactory;
|
||||
use Lcobucci\JWT\Parsing\Decoder;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class ParserTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var Decoder|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $decoder;
|
||||
|
||||
/**
|
||||
* @var ClaimFactory|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $claimFactory;
|
||||
|
||||
/**
|
||||
* @var Claim|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $defaultClaim;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$this->decoder = $this->getMock(Decoder::class);
|
||||
$this->claimFactory = $this->getMock(ClaimFactory::class, [], [], '', false);
|
||||
$this->defaultClaim = $this->getMock(Claim::class);
|
||||
|
||||
$this->claimFactory->expects($this->any())
|
||||
->method('create')
|
||||
->willReturn($this->defaultClaim);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Parser
|
||||
*/
|
||||
private function createParser()
|
||||
{
|
||||
return new Parser($this->decoder, $this->claimFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Parser::__construct
|
||||
*/
|
||||
public function constructMustConfigureTheAttributes()
|
||||
{
|
||||
$parser = $this->createParser();
|
||||
|
||||
$this->assertAttributeSame($this->decoder, 'decoder', $parser);
|
||||
$this->assertAttributeSame($this->claimFactory, 'claimFactory', $parser);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Parser::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Parser::parse
|
||||
* @covers Lcobucci\JWT\Parser::splitJwt
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function parseMustRaiseExceptionWhenJWSIsNotAString()
|
||||
{
|
||||
$parser = $this->createParser();
|
||||
$parser->parse(['asdasd']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Parser::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Parser::parse
|
||||
* @covers Lcobucci\JWT\Parser::splitJwt
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function parseMustRaiseExceptionWhenJWSDontHaveThreeParts()
|
||||
{
|
||||
$parser = $this->createParser();
|
||||
$parser->parse('');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Parser::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Parser::parse
|
||||
* @covers Lcobucci\JWT\Parser::splitJwt
|
||||
* @covers Lcobucci\JWT\Parser::parseHeader
|
||||
*
|
||||
* @expectedException RuntimeException
|
||||
*/
|
||||
public function parseMustRaiseExceptionWhenHeaderCannotBeDecoded()
|
||||
{
|
||||
$this->decoder->expects($this->any())
|
||||
->method('jsonDecode')
|
||||
->willThrowException(new RuntimeException());
|
||||
|
||||
$parser = $this->createParser();
|
||||
$parser->parse('asdfad.asdfasdf.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Parser::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Parser::parse
|
||||
* @covers Lcobucci\JWT\Parser::splitJwt
|
||||
* @covers Lcobucci\JWT\Parser::parseHeader
|
||||
*
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function parseMustRaiseExceptionWhenHeaderIsFromAnEncryptedToken()
|
||||
{
|
||||
$this->decoder->expects($this->any())
|
||||
->method('jsonDecode')
|
||||
->willReturn(['enc' => 'AAA']);
|
||||
|
||||
$parser = $this->createParser();
|
||||
$parser->parse('a.a.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Parser::__construct
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Parser::parse
|
||||
* @covers Lcobucci\JWT\Parser::splitJwt
|
||||
* @covers Lcobucci\JWT\Parser::parseHeader
|
||||
* @covers Lcobucci\JWT\Parser::parseClaims
|
||||
* @covers Lcobucci\JWT\Parser::parseSignature
|
||||
*
|
||||
*/
|
||||
public function parseMustReturnANonSignedTokenWhenSignatureIsNotInformed()
|
||||
{
|
||||
$this->decoder->expects($this->at(1))
|
||||
->method('jsonDecode')
|
||||
->willReturn(['typ' => 'JWT', 'alg' => 'none']);
|
||||
|
||||
$this->decoder->expects($this->at(3))
|
||||
->method('jsonDecode')
|
||||
->willReturn(['aud' => 'test']);
|
||||
|
||||
$parser = $this->createParser();
|
||||
$token = $parser->parse('a.a.');
|
||||
|
||||
$this->assertAttributeEquals(['typ' => 'JWT', 'alg' => 'none'], 'headers', $token);
|
||||
$this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $token);
|
||||
$this->assertAttributeEquals(null, 'signature', $token);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Parser::__construct
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Parser::parse
|
||||
* @covers Lcobucci\JWT\Parser::splitJwt
|
||||
* @covers Lcobucci\JWT\Parser::parseHeader
|
||||
* @covers Lcobucci\JWT\Parser::parseClaims
|
||||
* @covers Lcobucci\JWT\Parser::parseSignature
|
||||
*/
|
||||
public function parseShouldReplicateClaimValueOnHeaderWhenNeeded()
|
||||
{
|
||||
$this->decoder->expects($this->at(1))
|
||||
->method('jsonDecode')
|
||||
->willReturn(['typ' => 'JWT', 'alg' => 'none', 'aud' => 'test']);
|
||||
|
||||
$this->decoder->expects($this->at(3))
|
||||
->method('jsonDecode')
|
||||
->willReturn(['aud' => 'test']);
|
||||
|
||||
$parser = $this->createParser();
|
||||
$token = $parser->parse('a.a.');
|
||||
|
||||
$this->assertAttributeEquals(
|
||||
['typ' => 'JWT', 'alg' => 'none', 'aud' => $this->defaultClaim],
|
||||
'headers',
|
||||
$token
|
||||
);
|
||||
|
||||
$this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $token);
|
||||
$this->assertAttributeEquals(null, 'signature', $token);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Parser::__construct
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\Signature::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Parser::parse
|
||||
* @covers Lcobucci\JWT\Parser::splitJwt
|
||||
* @covers Lcobucci\JWT\Parser::parseHeader
|
||||
* @covers Lcobucci\JWT\Parser::parseClaims
|
||||
* @covers Lcobucci\JWT\Parser::parseSignature
|
||||
*/
|
||||
public function parseMustReturnASignedTokenWhenSignatureIsInformed()
|
||||
{
|
||||
$this->decoder->expects($this->at(1))
|
||||
->method('jsonDecode')
|
||||
->willReturn(['typ' => 'JWT', 'alg' => 'HS256']);
|
||||
|
||||
$this->decoder->expects($this->at(3))
|
||||
->method('jsonDecode')
|
||||
->willReturn(['aud' => 'test']);
|
||||
|
||||
$this->decoder->expects($this->at(4))
|
||||
->method('base64UrlDecode')
|
||||
->willReturn('aaa');
|
||||
|
||||
$parser = $this->createParser();
|
||||
$token = $parser->parse('a.a.a');
|
||||
|
||||
$this->assertAttributeEquals(['typ' => 'JWT', 'alg' => 'HS256'], 'headers', $token);
|
||||
$this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $token);
|
||||
$this->assertAttributeEquals(new Signature('aaa'), 'signature', $token);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Parsing;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class DecoderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Parsing\Decoder::jsonDecode
|
||||
*/
|
||||
public function jsonDecodeMustReturnTheDecodedData()
|
||||
{
|
||||
$decoder = new Decoder();
|
||||
|
||||
$this->assertEquals(
|
||||
(object) ['test' => 'test'],
|
||||
$decoder->jsonDecode('{"test":"test"}')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Parsing\Decoder::jsonDecode
|
||||
*
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function jsonDecodeMustRaiseExceptionWhenAnErrorHasOccured()
|
||||
{
|
||||
$decoder = new Decoder();
|
||||
$decoder->jsonDecode('{"test":\'test\'}');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Parsing\Decoder::base64UrlDecode
|
||||
*/
|
||||
public function base64UrlDecodeMustReturnTheRightData()
|
||||
{
|
||||
$data = base64_decode('0MB2wKB+L3yvIdzeggmJ+5WOSLaRLTUPXbpzqUe0yuo=');
|
||||
|
||||
$decoder = new Decoder();
|
||||
$this->assertEquals($data, $decoder->base64UrlDecode('0MB2wKB-L3yvIdzeggmJ-5WOSLaRLTUPXbpzqUe0yuo'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Parsing;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class EncoderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder::jsonEncode
|
||||
*/
|
||||
public function jsonEncodeMustReturnAJSONString()
|
||||
{
|
||||
$encoder = new Encoder();
|
||||
|
||||
$this->assertEquals('{"test":"test"}', $encoder->jsonEncode(['test' => 'test']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder::jsonEncode
|
||||
*
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function jsonEncodeMustRaiseExceptionWhenAnErrorHasOccured()
|
||||
{
|
||||
$encoder = new Encoder();
|
||||
$encoder->jsonEncode("\xB1\x31");
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Parsing\Encoder::base64UrlEncode
|
||||
*/
|
||||
public function base64UrlEncodeMustReturnAnUrlSafeBase64()
|
||||
{
|
||||
$data = base64_decode('0MB2wKB+L3yvIdzeggmJ+5WOSLaRLTUPXbpzqUe0yuo=');
|
||||
|
||||
$encoder = new Encoder();
|
||||
$this->assertEquals('0MB2wKB-L3yvIdzeggmJ-5WOSLaRLTUPXbpzqUe0yuo', $encoder->base64UrlEncode($data));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class SignatureTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var Signer|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $signer;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$this->signer = $this->getMock(Signer::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signature::__construct
|
||||
*/
|
||||
public function constructorMustConfigureAttributes()
|
||||
{
|
||||
$signature = new Signature('test');
|
||||
|
||||
$this->assertAttributeEquals('test', 'hash', $signature);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signature::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signature::__toString
|
||||
*/
|
||||
public function toStringMustReturnTheHash()
|
||||
{
|
||||
$signature = new Signature('test');
|
||||
|
||||
$this->assertEquals('test', (string) $signature);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signature::__construct
|
||||
* @uses Lcobucci\JWT\Signature::__toString
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signature::verify
|
||||
*/
|
||||
public function verifyMustReturnWhatSignerSays()
|
||||
{
|
||||
$this->signer->expects($this->any())
|
||||
->method('verify')
|
||||
->willReturn(true);
|
||||
|
||||
$signature = new Signature('test');
|
||||
|
||||
$this->assertTrue($signature->verify($this->signer, 'one', 'key'));
|
||||
}
|
||||
}
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer;
|
||||
|
||||
use Lcobucci\JWT\Signature;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class BaseSignerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var BaseSigner|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $signer;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$this->signer = $this->getMockForAbstractClass(BaseSigner::class);
|
||||
|
||||
$this->signer->method('getAlgorithmId')
|
||||
->willReturn('TEST123');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner::modifyHeader
|
||||
*/
|
||||
public function modifyHeaderShouldChangeAlgorithm()
|
||||
{
|
||||
$headers = ['typ' => 'JWT'];
|
||||
|
||||
$this->signer->modifyHeader($headers);
|
||||
|
||||
$this->assertEquals($headers['typ'], 'JWT');
|
||||
$this->assertEquals($headers['alg'], 'TEST123');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signature::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner::sign
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner::getKey
|
||||
*/
|
||||
public function signMustReturnANewSignature()
|
||||
{
|
||||
$key = new Key('123');
|
||||
|
||||
$this->signer->expects($this->once())
|
||||
->method('createHash')
|
||||
->with('test', $key)
|
||||
->willReturn('test');
|
||||
|
||||
$this->assertEquals(new Signature('test'), $this->signer->sign('test', $key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signature::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner::sign
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner::getKey
|
||||
*/
|
||||
public function signShouldConvertKeyWhenItsNotAnObject()
|
||||
{
|
||||
$this->signer->expects($this->once())
|
||||
->method('createHash')
|
||||
->with('test', new Key('123'))
|
||||
->willReturn('test');
|
||||
|
||||
$this->assertEquals(new Signature('test'), $this->signer->sign('test', '123'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signature::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner::verify
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner::getKey
|
||||
*/
|
||||
public function verifyShouldDelegateTheCallToAbstractMethod()
|
||||
{
|
||||
$key = new Key('123');
|
||||
|
||||
$this->signer->expects($this->once())
|
||||
->method('doVerify')
|
||||
->with('test', 'test', $key)
|
||||
->willReturn(true);
|
||||
|
||||
$this->assertTrue($this->signer->verify('test', 'test', $key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signature::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner::verify
|
||||
* @covers Lcobucci\JWT\Signer\BaseSigner::getKey
|
||||
*/
|
||||
public function verifyShouldConvertKeyWhenItsNotAnObject()
|
||||
{
|
||||
$this->signer->expects($this->once())
|
||||
->method('doVerify')
|
||||
->with('test', 'test', new Key('123'))
|
||||
->willReturn(true);
|
||||
|
||||
$this->assertTrue($this->signer->verify('test', 'test', '123'));
|
||||
}
|
||||
}
|
||||
+178
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Ecdsa;
|
||||
|
||||
use Mdanter\Ecc\Crypto\Key\PrivateKeyInterface;
|
||||
use Mdanter\Ecc\Crypto\Key\PublicKeyInterface;
|
||||
use Mdanter\Ecc\Math\MathAdapterInterface;
|
||||
use Mdanter\Ecc\Serializer\PrivateKey\PrivateKeySerializerInterface;
|
||||
use Mdanter\Ecc\Serializer\PublicKey\PublicKeySerializerInterface;
|
||||
use Lcobucci\JWT\Signer\Key;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 3.0.4
|
||||
*/
|
||||
class KeyParserTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var MathAdapterInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $adapter;
|
||||
|
||||
/**
|
||||
* @var PrivateKeySerializerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $privateKeySerializer;
|
||||
|
||||
/**
|
||||
* @var PublicKeySerializerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $publicKeySerializer;
|
||||
|
||||
/**
|
||||
* @before
|
||||
*/
|
||||
public function createDependencies()
|
||||
{
|
||||
$this->adapter = $this->getMock(MathAdapterInterface::class);
|
||||
$this->privateKeySerializer = $this->getMock(PrivateKeySerializerInterface::class);
|
||||
$this->publicKeySerializer = $this->getMock(PublicKeySerializerInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
|
||||
*/
|
||||
public function constructShouldConfigureDependencies()
|
||||
{
|
||||
$parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
|
||||
|
||||
$this->assertAttributeSame($this->privateKeySerializer, 'privateKeySerializer', $parser);
|
||||
$this->assertAttributeSame($this->publicKeySerializer, 'publicKeySerializer', $parser);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getPrivateKey
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getKeyContent
|
||||
*/
|
||||
public function getPrivateKeyShouldAskSerializerToParseTheKey()
|
||||
{
|
||||
$privateKey = $this->getMock(PrivateKeyInterface::class);
|
||||
|
||||
$keyContent = 'MHcCAQEEIBGpMoZJ64MMSzuo5JbmXpf9V4qSWdLIl/8RmJLcfn/qoAoGC'
|
||||
. 'CqGSM49AwEHoUQDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpnd0wxa2iF'
|
||||
. 'ruiI2tsEdGFTLTsyU+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==';
|
||||
|
||||
$this->privateKeySerializer->expects($this->once())
|
||||
->method('parse')
|
||||
->with($keyContent)
|
||||
->willReturn($privateKey);
|
||||
|
||||
$parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
|
||||
$this->assertSame($privateKey, $parser->getPrivateKey($this->getPrivateKey()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getPrivateKey
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getKeyContent
|
||||
*/
|
||||
public function getPrivateKeyShouldRaiseExceptionWhenAWrongKeyWasGiven()
|
||||
{
|
||||
$this->privateKeySerializer->expects($this->never())
|
||||
->method('parse');
|
||||
|
||||
$parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
|
||||
$parser->getPrivateKey($this->getPublicKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getPublicKey
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getKeyContent
|
||||
*/
|
||||
public function getPublicKeyShouldAskSerializerToParseTheKey()
|
||||
{
|
||||
$publicKey = $this->getMock(PublicKeyInterface::class);
|
||||
|
||||
$keyContent = 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpn'
|
||||
. 'd0wxa2iFruiI2tsEdGFTLTsyU+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==';
|
||||
|
||||
$this->publicKeySerializer->expects($this->once())
|
||||
->method('parse')
|
||||
->with($keyContent)
|
||||
->willReturn($publicKey);
|
||||
|
||||
$parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
|
||||
$this->assertSame($publicKey, $parser->getPublicKey($this->getPublicKey()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getPublicKey
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\KeyParser::getKeyContent
|
||||
*/
|
||||
public function getPublicKeyShouldRaiseExceptionWhenAWrongKeyWasGiven()
|
||||
{
|
||||
$this->publicKeySerializer->expects($this->never())
|
||||
->method('parse');
|
||||
|
||||
$parser = new KeyParser($this->adapter, $this->privateKeySerializer, $this->publicKeySerializer);
|
||||
$parser->getPublicKey($this->getPrivateKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Key
|
||||
*/
|
||||
private function getPrivateKey()
|
||||
{
|
||||
return new Key(
|
||||
"-----BEGIN EC PRIVATE KEY-----\n"
|
||||
. "MHcCAQEEIBGpMoZJ64MMSzuo5JbmXpf9V4qSWdLIl/8RmJLcfn/qoAoGCCqGSM49\n"
|
||||
. "AwEHoUQDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpnd0wxa2iFruiI2tsEdGFTLTsy\n"
|
||||
. "U+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==\n"
|
||||
. "-----END EC PRIVATE KEY-----"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Key
|
||||
*/
|
||||
private function getPublicKey()
|
||||
{
|
||||
return new Key(
|
||||
"-----BEGIN PUBLIC KEY-----\n"
|
||||
. "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE7it/EKmcv9bfpcV1fBreLMRXxWpn\n"
|
||||
. "d0wxa2iFruiI2tsEdGFTLTsyU+GeRqC7zN0aTnTQajarUylKJ3UWr/r1kg==\n"
|
||||
. "-----END PUBLIC KEY-----"
|
||||
);
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Ecdsa;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha256Test extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256::getAlgorithmId
|
||||
*/
|
||||
public function getAlgorithmIdMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha256();
|
||||
|
||||
$this->assertEquals('ES256', $signer->getAlgorithmId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256::getAlgorithm
|
||||
*/
|
||||
public function getAlgorithmMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha256();
|
||||
|
||||
$this->assertEquals('sha256', $signer->getAlgorithm());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha256::getSignatureLength
|
||||
*/
|
||||
public function getSignatureLengthMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha256();
|
||||
|
||||
$this->assertEquals(64, $signer->getSignatureLength());
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Ecdsa;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha384Test extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha384::getAlgorithmId
|
||||
*/
|
||||
public function getAlgorithmIdMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha384();
|
||||
|
||||
$this->assertEquals('ES384', $signer->getAlgorithmId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha384::getAlgorithm
|
||||
*/
|
||||
public function getAlgorithmMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha384();
|
||||
|
||||
$this->assertEquals('sha384', $signer->getAlgorithm());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha384::getSignatureLength
|
||||
*/
|
||||
public function getSignatureLengthMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha384();
|
||||
|
||||
$this->assertEquals(96, $signer->getSignatureLength());
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Ecdsa;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha512Test extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha512::getAlgorithmId
|
||||
*/
|
||||
public function getAlgorithmIdMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha512();
|
||||
|
||||
$this->assertEquals('ES512', $signer->getAlgorithmId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha512::getAlgorithm
|
||||
*/
|
||||
public function getAlgorithmMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha512();
|
||||
|
||||
$this->assertEquals('sha512', $signer->getAlgorithm());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa\KeyParser
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa\Sha512::getSignatureLength
|
||||
*/
|
||||
public function getSignatureLengthMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha512();
|
||||
|
||||
$this->assertEquals(132, $signer->getSignatureLength());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer;
|
||||
|
||||
use Lcobucci\JWT\Signer\Ecdsa\KeyParser;
|
||||
use Mdanter\Ecc\Crypto\Signature\Signature;
|
||||
use Mdanter\Ecc\Crypto\Signature\Signer;
|
||||
use Mdanter\Ecc\Crypto\Key\PrivateKeyInterface;
|
||||
use Mdanter\Ecc\Crypto\Key\PublicKeyInterface;
|
||||
use Mdanter\Ecc\Math\MathAdapterInterface as Adapter;
|
||||
use Mdanter\Ecc\Primitives\PointInterface;
|
||||
use Mdanter\Ecc\Random\RandomNumberGeneratorInterface;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class EcdsaTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var Adapter|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $adapter;
|
||||
|
||||
/**
|
||||
* @var Signer|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $signer;
|
||||
|
||||
/**
|
||||
* @var RandomNumberGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $randomGenerator;
|
||||
|
||||
/**
|
||||
* @var KeyParser|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $parser;
|
||||
|
||||
/**
|
||||
* @before
|
||||
*/
|
||||
public function createDependencies()
|
||||
{
|
||||
$this->adapter = $this->getMock(Adapter::class);
|
||||
$this->signer = $this->getMock(Signer::class, [], [$this->adapter]);
|
||||
$this->randomGenerator = $this->getMock(RandomNumberGeneratorInterface::class);
|
||||
$this->parser = $this->getMock(KeyParser::class, [], [], '', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Ecdsa
|
||||
*/
|
||||
private function getSigner()
|
||||
{
|
||||
$signer = $this->getMockForAbstractClass(
|
||||
Ecdsa::class,
|
||||
[$this->adapter, $this->signer, $this->parser]
|
||||
);
|
||||
|
||||
$signer->method('getSignatureLength')
|
||||
->willReturn(64);
|
||||
|
||||
$signer->method('getAlgorithm')
|
||||
->willReturn('sha256');
|
||||
|
||||
$signer->method('getAlgorithmId')
|
||||
->willReturn('ES256');
|
||||
|
||||
return $signer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa::__construct
|
||||
*/
|
||||
public function constructShouldConfigureDependencies()
|
||||
{
|
||||
$signer = $this->getSigner();
|
||||
|
||||
$this->assertAttributeSame($this->adapter, 'adapter', $signer);
|
||||
$this->assertAttributeSame($this->signer, 'signer', $signer);
|
||||
$this->assertAttributeSame($this->parser, 'parser', $signer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa::createHash
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa::createSigningHash
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa::createSignatureHash
|
||||
*/
|
||||
public function createHashShouldReturnAHashUsingPrivateKey()
|
||||
{
|
||||
$signer = $this->getSigner();
|
||||
$key = new Key('testing');
|
||||
$privateKey = $this->getMock(PrivateKeyInterface::class);
|
||||
$point = $this->getMock(PointInterface::class);
|
||||
|
||||
$privateKey->method('getPoint')
|
||||
->willReturn($point);
|
||||
|
||||
$point->method('getOrder')
|
||||
->willReturn('1');
|
||||
|
||||
$this->parser->expects($this->once())
|
||||
->method('getPrivateKey')
|
||||
->with($key)
|
||||
->willReturn($privateKey);
|
||||
|
||||
$this->randomGenerator->expects($this->once())
|
||||
->method('generate')
|
||||
->with('1')
|
||||
->willReturn('123');
|
||||
|
||||
$this->adapter->expects($this->once())
|
||||
->method('hexDec')
|
||||
->willReturn('123');
|
||||
|
||||
$this->adapter->expects($this->exactly(2))
|
||||
->method('decHex')
|
||||
->willReturn('123');
|
||||
|
||||
$this->signer->expects($this->once())
|
||||
->method('sign')
|
||||
->with($privateKey, $this->isType('string'), $this->isType('string'))
|
||||
->willReturn(new Signature('1234', '456'));
|
||||
|
||||
$this->assertInternalType('string', $signer->createHash('testing', $key, $this->randomGenerator));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Ecdsa::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa::doVerify
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa::createSigningHash
|
||||
* @covers Lcobucci\JWT\Signer\Ecdsa::extractSignature
|
||||
*/
|
||||
public function doVerifyShouldDelegateToEcdsaSignerUsingPublicKey()
|
||||
{
|
||||
$signer = $this->getSigner();
|
||||
$key = new Key('testing');
|
||||
$publicKey = $this->getMock(PublicKeyInterface::class);
|
||||
|
||||
$this->parser->expects($this->once())
|
||||
->method('getPublicKey')
|
||||
->with($key)
|
||||
->willReturn($publicKey);
|
||||
|
||||
$this->adapter->expects($this->exactly(3))
|
||||
->method('hexDec')
|
||||
->willReturn('123');
|
||||
|
||||
$this->signer->expects($this->once())
|
||||
->method('verify')
|
||||
->with($publicKey, $this->isInstanceOf(Signature::class), $this->isType('string'))
|
||||
->willReturn(true);
|
||||
|
||||
$this->assertTrue($signer->doVerify('testing', 'testing2', $key));
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Hmac;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class Sha256Test extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Hmac\Sha256::getAlgorithmId
|
||||
*/
|
||||
public function getAlgorithmIdMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha256();
|
||||
|
||||
$this->assertEquals('HS256', $signer->getAlgorithmId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Hmac\Sha256::getAlgorithm
|
||||
*/
|
||||
public function getAlgorithmMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha256();
|
||||
|
||||
$this->assertEquals('sha256', $signer->getAlgorithm());
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Hmac;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class Sha384Test extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Hmac\Sha384::getAlgorithmId
|
||||
*/
|
||||
public function getAlgorithmIdMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha384();
|
||||
|
||||
$this->assertEquals('HS384', $signer->getAlgorithmId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Hmac\Sha384::getAlgorithm
|
||||
*/
|
||||
public function getAlgorithmMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha384();
|
||||
|
||||
$this->assertEquals('sha384', $signer->getAlgorithm());
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Hmac;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class Sha512Test extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Hmac\Sha512::getAlgorithmId
|
||||
*/
|
||||
public function getAlgorithmIdMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha512();
|
||||
|
||||
$this->assertEquals('HS512', $signer->getAlgorithmId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Hmac\Sha512::getAlgorithm
|
||||
*/
|
||||
public function getAlgorithmMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha512();
|
||||
|
||||
$this->assertEquals('sha512', $signer->getAlgorithm());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class HmacTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var Hmac|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $signer;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$this->signer = $this->getMockForAbstractClass(Hmac::class);
|
||||
|
||||
$this->signer->expects($this->any())
|
||||
->method('getAlgorithmId')
|
||||
->willReturn('TEST123');
|
||||
|
||||
$this->signer->expects($this->any())
|
||||
->method('getAlgorithm')
|
||||
->willReturn('sha256');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Hmac::createHash
|
||||
*/
|
||||
public function createHashMustReturnAHashAccordingWithTheAlgorithm()
|
||||
{
|
||||
$hash = hash_hmac('sha256', 'test', '123', true);
|
||||
|
||||
$this->assertEquals($hash, $this->signer->createHash('test', new Key('123')));
|
||||
|
||||
return $hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends createHashMustReturnAHashAccordingWithTheAlgorithm
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Hmac::createHash
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Hmac::doVerify
|
||||
*/
|
||||
public function doVerifyShouldReturnTrueWhenExpectedHashWasCreatedWithSameInformation($expected)
|
||||
{
|
||||
$this->assertTrue($this->signer->doVerify($expected, 'test', new Key('123')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends createHashMustReturnAHashAccordingWithTheAlgorithm
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Hmac::createHash
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Hmac::doVerify
|
||||
*/
|
||||
public function doVerifyShouldReturnFalseWhenExpectedHashWasNotCreatedWithSameInformation($expected)
|
||||
{
|
||||
$this->assertFalse($this->signer->doVerify($expected, 'test', new Key('1234')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Hmac::doVerify
|
||||
*/
|
||||
public function doVerifyShouldReturnFalseWhenExpectedHashIsNotString()
|
||||
{
|
||||
$this->assertFalse($this->signer->doVerify(false, 'test', new Key('1234')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Hmac::hashEquals
|
||||
*/
|
||||
public function hashEqualsShouldReturnFalseWhenExpectedHashHasDifferentLengthThanGenerated()
|
||||
{
|
||||
$this->assertFalse($this->signer->hashEquals('123', '1234'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends createHashMustReturnAHashAccordingWithTheAlgorithm
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Hmac::createHash
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Hmac::hashEquals
|
||||
*/
|
||||
public function hashEqualsShouldReturnFalseWhenExpectedHashIsDifferentThanGenerated($expected)
|
||||
{
|
||||
$this->assertFalse($this->signer->hashEquals($expected, $this->signer->createHash('test', new Key('1234'))));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @depends createHashMustReturnAHashAccordingWithTheAlgorithm
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Hmac::createHash
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Hmac::hashEquals
|
||||
*/
|
||||
public function hashEqualsShouldReturnTrueWhenExpectedHashIsEqualsThanGenerated($expected)
|
||||
{
|
||||
$this->assertTrue($this->signer->hashEquals($expected, $this->signer->createHash('test', new Key('123'))));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer;
|
||||
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 3.0.4
|
||||
*/
|
||||
class KeyTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @before
|
||||
*/
|
||||
public function configureRootDir()
|
||||
{
|
||||
vfsStream::setup(
|
||||
'root',
|
||||
null,
|
||||
[
|
||||
'test.pem' => 'testing',
|
||||
'emptyFolder' => []
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Key::__construct
|
||||
* @covers Lcobucci\JWT\Signer\Key::setContent
|
||||
*/
|
||||
public function constructShouldConfigureContentAndPassphrase()
|
||||
{
|
||||
$key = new Key('testing', 'test');
|
||||
|
||||
$this->assertAttributeEquals('testing', 'content', $key);
|
||||
$this->assertAttributeEquals('test', 'passphrase', $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Key::__construct
|
||||
* @covers Lcobucci\JWT\Signer\Key::setContent
|
||||
* @covers Lcobucci\JWT\Signer\Key::readFile
|
||||
*/
|
||||
public function constructShouldBeAbleToConfigureContentFromFile()
|
||||
{
|
||||
$key = new Key('file://' . vfsStream::url('root/test.pem'));
|
||||
|
||||
$this->assertAttributeEquals('testing', 'content', $key);
|
||||
$this->assertAttributeEquals(null, 'passphrase', $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Key::__construct
|
||||
* @covers Lcobucci\JWT\Signer\Key::setContent
|
||||
* @covers Lcobucci\JWT\Signer\Key::readFile
|
||||
*/
|
||||
public function constructShouldRaiseExceptionWhenFileDoesNotExists()
|
||||
{
|
||||
new Key('file://' . vfsStream::url('root/test2.pem'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @expectedException \InvalidArgumentException
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Key::__construct
|
||||
* @covers Lcobucci\JWT\Signer\Key::setContent
|
||||
* @covers Lcobucci\JWT\Signer\Key::readFile
|
||||
*/
|
||||
public function constructShouldRaiseExceptionWhenFileGetContentsFailed()
|
||||
{
|
||||
new Key('file://' . vfsStream::url('root/emptyFolder'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Key::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key::setContent
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Key::getContent
|
||||
*/
|
||||
public function getContentShouldReturnConfiguredData()
|
||||
{
|
||||
$key = new Key('testing', 'test');
|
||||
|
||||
$this->assertEquals('testing', $key->getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Key::__construct
|
||||
* @uses Lcobucci\JWT\Signer\Key::setContent
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Key::getPassphrase
|
||||
*/
|
||||
public function getPassphraseShouldReturnConfiguredData()
|
||||
{
|
||||
$key = new Key('testing', 'test');
|
||||
|
||||
$this->assertEquals('test', $key->getPassphrase());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class KeychainTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Keychain::getPrivateKey
|
||||
*/
|
||||
public function getPrivateKeyShouldReturnAKey()
|
||||
{
|
||||
$keychain = new Keychain();
|
||||
$key = $keychain->getPrivateKey('testing', 'test');
|
||||
|
||||
$this->assertInstanceOf(Key::class, $key);
|
||||
$this->assertAttributeEquals('testing', 'content', $key);
|
||||
$this->assertAttributeEquals('test', 'passphrase', $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Signer\Key
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Keychain::getPublicKey
|
||||
*/
|
||||
public function getPublicKeyShouldReturnAValidResource()
|
||||
{
|
||||
$keychain = new Keychain();
|
||||
$key = $keychain->getPublicKey('testing');
|
||||
|
||||
$this->assertInstanceOf(Key::class, $key);
|
||||
$this->assertAttributeEquals('testing', 'content', $key);
|
||||
$this->assertAttributeEquals(null, 'passphrase', $key);
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Rsa;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha256Test extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Rsa\Sha256::getAlgorithmId
|
||||
*/
|
||||
public function getAlgorithmIdMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha256();
|
||||
|
||||
$this->assertEquals('RS256', $signer->getAlgorithmId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Rsa\Sha256::getAlgorithm
|
||||
*/
|
||||
public function getAlgorithmMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha256();
|
||||
|
||||
$this->assertEquals(OPENSSL_ALGO_SHA256, $signer->getAlgorithm());
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Rsa;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha384Test extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Rsa\Sha384::getAlgorithmId
|
||||
*/
|
||||
public function getAlgorithmIdMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha384();
|
||||
|
||||
$this->assertEquals('RS384', $signer->getAlgorithmId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Rsa\Sha384::getAlgorithm
|
||||
*/
|
||||
public function getAlgorithmMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha384();
|
||||
|
||||
$this->assertEquals(OPENSSL_ALGO_SHA384, $signer->getAlgorithm());
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT\Signer\Rsa;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.1.0
|
||||
*/
|
||||
class Sha512Test extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Rsa\Sha512::getAlgorithmId
|
||||
*/
|
||||
public function getAlgorithmIdMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha512();
|
||||
|
||||
$this->assertEquals('RS512', $signer->getAlgorithmId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Signer\Rsa\Sha512::getAlgorithm
|
||||
*/
|
||||
public function getAlgorithmMustBeCorrect()
|
||||
{
|
||||
$signer = new Sha512();
|
||||
|
||||
$this->assertEquals(OPENSSL_ALGO_SHA512, $signer->getAlgorithm());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,502 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT;
|
||||
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use Lcobucci\JWT\Claim\Basic;
|
||||
use Lcobucci\JWT\Claim\EqualsTo;
|
||||
use Lcobucci\JWT\Claim\GreaterOrEqualsTo;
|
||||
use Lcobucci\JWT\Claim\LesserOrEqualsTo;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
class TokenTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::__construct
|
||||
*/
|
||||
public function constructMustInitializeAnEmptyPlainTextTokenWhenNoArgumentsArePassed()
|
||||
{
|
||||
$token = new Token();
|
||||
|
||||
$this->assertAttributeEquals(['alg' => 'none'], 'headers', $token);
|
||||
$this->assertAttributeEquals([], 'claims', $token);
|
||||
$this->assertAttributeEquals(null, 'signature', $token);
|
||||
$this->assertAttributeEquals(['', ''], 'payload', $token);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::hasHeader
|
||||
*/
|
||||
public function hasHeaderMustReturnTrueWhenItIsConfigured()
|
||||
{
|
||||
$token = new Token(['test' => 'testing']);
|
||||
|
||||
$this->assertTrue($token->hasHeader('test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::hasHeader
|
||||
*/
|
||||
public function hasHeaderMustReturnFalseWhenItIsNotConfigured()
|
||||
{
|
||||
$token = new Token(['test' => 'testing']);
|
||||
|
||||
$this->assertFalse($token->hasHeader('testing'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\Token::hasHeader
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::getHeader
|
||||
*
|
||||
* @expectedException \OutOfBoundsException
|
||||
*/
|
||||
public function getHeaderMustRaiseExceptionWhenHeaderIsNotConfigured()
|
||||
{
|
||||
$token = new Token(['test' => 'testing']);
|
||||
|
||||
$token->getHeader('testing');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\Token::hasHeader
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::getHeader
|
||||
*/
|
||||
public function getHeaderMustReturnTheDefaultValueWhenIsNotConfigured()
|
||||
{
|
||||
$token = new Token(['test' => 'testing']);
|
||||
|
||||
$this->assertEquals('blah', $token->getHeader('testing', 'blah'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\Token::hasHeader
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::getHeader
|
||||
* @covers Lcobucci\JWT\Token::getHeaderValue
|
||||
*/
|
||||
public function getHeaderMustReturnTheRequestedHeader()
|
||||
{
|
||||
$token = new Token(['test' => 'testing']);
|
||||
|
||||
$this->assertEquals('testing', $token->getHeader('test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\Token::hasHeader
|
||||
* @uses Lcobucci\JWT\Claim\Basic
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::getHeader
|
||||
* @covers Lcobucci\JWT\Token::getHeaderValue
|
||||
*/
|
||||
public function getHeaderMustReturnValueWhenItIsAReplicatedClaim()
|
||||
{
|
||||
$token = new Token(['jti' => new EqualsTo('jti', 1)]);
|
||||
|
||||
$this->assertEquals(1, $token->getHeader('jti'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::getHeaders
|
||||
*/
|
||||
public function getHeadersMustReturnTheConfiguredHeader()
|
||||
{
|
||||
$token = new Token(['test' => 'testing']);
|
||||
|
||||
$this->assertEquals(['test' => 'testing'], $token->getHeaders());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::getClaims
|
||||
*/
|
||||
public function getClaimsMustReturnTheConfiguredClaims()
|
||||
{
|
||||
$token = new Token([], ['test' => 'testing']);
|
||||
|
||||
$this->assertEquals(['test' => 'testing'], $token->getClaims());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::hasClaim
|
||||
*/
|
||||
public function hasClaimMustReturnTrueWhenItIsConfigured()
|
||||
{
|
||||
$token = new Token([], ['test' => new Basic('test', 'testing')]);
|
||||
|
||||
$this->assertTrue($token->hasClaim('test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::hasClaim
|
||||
*/
|
||||
public function hasClaimMustReturnFalseWhenItIsNotConfigured()
|
||||
{
|
||||
$token = new Token([], ['test' => new Basic('test', 'testing')]);
|
||||
|
||||
$this->assertFalse($token->hasClaim('testing'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\Token::hasClaim
|
||||
* @uses Lcobucci\JWT\Claim\Basic
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::getClaim
|
||||
*/
|
||||
public function getClaimMustReturnTheDefaultValueWhenIsNotConfigured()
|
||||
{
|
||||
$token = new Token([], ['test' => new Basic('test', 'testing')]);
|
||||
|
||||
$this->assertEquals('blah', $token->getClaim('testing', 'blah'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\Token::hasClaim
|
||||
* @uses Lcobucci\JWT\Claim\Basic
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::getClaim
|
||||
*
|
||||
* @expectedException \OutOfBoundsException
|
||||
*/
|
||||
public function getClaimShouldRaiseExceptionWhenClaimIsNotConfigured()
|
||||
{
|
||||
$token = new Token();
|
||||
$token->getClaim('testing');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\Token::hasClaim
|
||||
* @uses Lcobucci\JWT\Claim\Basic
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::getClaim
|
||||
*/
|
||||
public function getClaimShouldReturnTheClaimValueWhenItExists()
|
||||
{
|
||||
$token = new Token([], ['testing' => new Basic('testing', 'test')]);
|
||||
|
||||
$this->assertEquals('test', $token->getClaim('testing'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::verify
|
||||
*
|
||||
* @expectedException BadMethodCallException
|
||||
*/
|
||||
public function verifyMustRaiseExceptionWhenTokenIsUnsigned()
|
||||
{
|
||||
$signer = $this->getMock(Signer::class);
|
||||
|
||||
$token = new Token();
|
||||
$token->verify($signer, 'test');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::verify
|
||||
* @covers Lcobucci\JWT\Token::getPayload
|
||||
*/
|
||||
public function verifyShouldReturnFalseWhenTokenAlgorithmIsDifferent()
|
||||
{
|
||||
$signer = $this->getMock(Signer::class);
|
||||
$signature = $this->getMock(Signature::class, [], [], '', false);
|
||||
|
||||
$signer->expects($this->any())
|
||||
->method('getAlgorithmId')
|
||||
->willReturn('HS256');
|
||||
|
||||
$signature->expects($this->never())
|
||||
->method('verify');
|
||||
|
||||
$token = new Token(['alg' => 'RS256'], [], $signature);
|
||||
|
||||
$this->assertFalse($token->verify($signer, 'test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::verify
|
||||
* @covers Lcobucci\JWT\Token::getPayload
|
||||
*/
|
||||
public function verifyMustDelegateTheValidationToSignature()
|
||||
{
|
||||
$signer = $this->getMock(Signer::class);
|
||||
$signature = $this->getMock(Signature::class, [], [], '', false);
|
||||
|
||||
$signer->expects($this->any())
|
||||
->method('getAlgorithmId')
|
||||
->willReturn('HS256');
|
||||
|
||||
$signature->expects($this->once())
|
||||
->method('verify')
|
||||
->with($signer, $this->isType('string'), 'test')
|
||||
->willReturn(true);
|
||||
|
||||
$token = new Token(['alg' => 'HS256'], [], $signature);
|
||||
|
||||
$this->assertTrue($token->verify($signer, 'test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::validate
|
||||
* @covers Lcobucci\JWT\Token::getValidatableClaims
|
||||
*/
|
||||
public function validateShouldReturnTrueWhenClaimsAreEmpty()
|
||||
{
|
||||
$token = new Token();
|
||||
|
||||
$this->assertTrue($token->validate(new ValidationData()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
* @uses Lcobucci\JWT\Claim\Basic::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::validate
|
||||
* @covers Lcobucci\JWT\Token::getValidatableClaims
|
||||
*/
|
||||
public function validateShouldReturnTrueWhenThereAreNoValidatableClaims()
|
||||
{
|
||||
$token = new Token([], ['testing' => new Basic('testing', 'test')]);
|
||||
|
||||
$this->assertTrue($token->validate(new ValidationData()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\ValidationData
|
||||
* @uses Lcobucci\JWT\Claim\Basic
|
||||
* @uses Lcobucci\JWT\Claim\EqualsTo
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::validate
|
||||
* @covers Lcobucci\JWT\Token::getValidatableClaims
|
||||
*/
|
||||
public function validateShouldReturnFalseWhenThereIsAtLeastOneFailedValidatableClaim()
|
||||
{
|
||||
$token = new Token(
|
||||
[],
|
||||
[
|
||||
'iss' => new EqualsTo('iss', 'test'),
|
||||
'testing' => new Basic('testing', 'test')
|
||||
]
|
||||
);
|
||||
|
||||
$data = new ValidationData();
|
||||
$data->setIssuer('test1');
|
||||
|
||||
$this->assertFalse($token->validate($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\ValidationData
|
||||
* @uses Lcobucci\JWT\Claim\Basic
|
||||
* @uses Lcobucci\JWT\Claim\EqualsTo
|
||||
* @uses Lcobucci\JWT\Claim\LesserOrEqualsTo
|
||||
* @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::validate
|
||||
* @covers Lcobucci\JWT\Token::getValidatableClaims
|
||||
*/
|
||||
public function validateShouldReturnTrueWhenThereAreNoFailedValidatableClaims()
|
||||
{
|
||||
$now = time();
|
||||
$token = new Token(
|
||||
[],
|
||||
[
|
||||
'iss' => new EqualsTo('iss', 'test'),
|
||||
'iat' => new LesserOrEqualsTo('iat', $now),
|
||||
'exp' => new GreaterOrEqualsTo('exp', $now + 500),
|
||||
'testing' => new Basic('testing', 'test')
|
||||
]
|
||||
);
|
||||
|
||||
$data = new ValidationData($now + 10);
|
||||
$data->setIssuer('test');
|
||||
|
||||
$this->assertTrue($token->validate($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::isExpired
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\Token::getClaim
|
||||
* @uses Lcobucci\JWT\Token::hasClaim
|
||||
*/
|
||||
public function isExpiredShouldReturnFalseWhenTokenDoesNotExpires()
|
||||
{
|
||||
$token = new Token(['alg' => 'none']);
|
||||
|
||||
$this->assertFalse($token->isExpired());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::isExpired
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\Token::getClaim
|
||||
* @uses Lcobucci\JWT\Token::hasClaim
|
||||
* @uses Lcobucci\JWT\Claim\Basic
|
||||
* @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo
|
||||
*/
|
||||
public function isExpiredShouldReturnFalseWhenTokenIsNotExpired()
|
||||
{
|
||||
$token = new Token(
|
||||
['alg' => 'none'],
|
||||
['exp' => new GreaterOrEqualsTo('exp', time() + 500)]
|
||||
);
|
||||
|
||||
$this->assertFalse($token->isExpired());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::isExpired
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\Token::getClaim
|
||||
* @uses Lcobucci\JWT\Token::hasClaim
|
||||
* @uses Lcobucci\JWT\Claim\Basic
|
||||
* @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo
|
||||
*/
|
||||
public function isExpiredShouldReturnTrueAfterTokenExpires()
|
||||
{
|
||||
$token = new Token(
|
||||
['alg' => 'none'],
|
||||
['exp' => new GreaterOrEqualsTo('exp', time())]
|
||||
);
|
||||
|
||||
$this->assertTrue($token->isExpired(new DateTime('+10 days')));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::getPayload
|
||||
*/
|
||||
public function getPayloadShouldReturnAStringWithTheTwoEncodePartsThatGeneratedTheToken()
|
||||
{
|
||||
$token = new Token(['alg' => 'none'], [], null, ['test1', 'test2', 'test3']);
|
||||
|
||||
$this->assertEquals('test1.test2', $token->getPayload());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\Token::getPayload
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::__toString
|
||||
*/
|
||||
public function toStringMustReturnEncodedDataWithEmptySignature()
|
||||
{
|
||||
$token = new Token(['alg' => 'none'], [], null, ['test', 'test']);
|
||||
|
||||
$this->assertEquals('test.test.', (string) $token);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\Token::__construct
|
||||
* @uses Lcobucci\JWT\Token::getPayload
|
||||
*
|
||||
* @covers Lcobucci\JWT\Token::__toString
|
||||
*/
|
||||
public function toStringMustReturnEncodedData()
|
||||
{
|
||||
$signature = $this->getMock(Signature::class, [], [], '', false);
|
||||
|
||||
$token = new Token(['alg' => 'none'], [], $signature, ['test', 'test', 'test']);
|
||||
|
||||
$this->assertEquals('test.test.test', (string) $token);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
|
||||
*
|
||||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace Lcobucci\JWT;
|
||||
|
||||
/**
|
||||
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class ValidationDataTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @covers Lcobucci\JWT\ValidationData::__construct
|
||||
*/
|
||||
public function constructorShouldConfigureTheItems()
|
||||
{
|
||||
$expected = $this->createExpectedData();
|
||||
$data = new ValidationData(1);
|
||||
|
||||
$this->assertAttributeSame($expected, 'items', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @dataProvider claimValues
|
||||
*
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\ValidationData::setId
|
||||
*/
|
||||
public function setIdShouldChangeTheId($id)
|
||||
{
|
||||
$expected = $this->createExpectedData($id);
|
||||
$data = new ValidationData(1);
|
||||
$data->setId($id);
|
||||
|
||||
$this->assertAttributeSame($expected, 'items', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @dataProvider claimValues
|
||||
*
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\ValidationData::setIssuer
|
||||
*/
|
||||
public function setIssuerShouldChangeTheIssuer($iss)
|
||||
{
|
||||
$expected = $this->createExpectedData(null, null, $iss);
|
||||
$data = new ValidationData(1);
|
||||
$data->setIssuer($iss);
|
||||
|
||||
$this->assertAttributeSame($expected, 'items', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @dataProvider claimValues
|
||||
*
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\ValidationData::setAudience
|
||||
*/
|
||||
public function setAudienceShouldChangeTheAudience($aud)
|
||||
{
|
||||
$expected = $this->createExpectedData(null, null, null, $aud);
|
||||
$data = new ValidationData(1);
|
||||
$data->setAudience($aud);
|
||||
|
||||
$this->assertAttributeSame($expected, 'items', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @dataProvider claimValues
|
||||
*
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\ValidationData::setSubject
|
||||
*/
|
||||
public function setSubjectShouldChangeTheSubject($sub)
|
||||
{
|
||||
$expected = $this->createExpectedData(null, $sub);
|
||||
$data = new ValidationData(1);
|
||||
$data->setSubject($sub);
|
||||
|
||||
$this->assertAttributeSame($expected, 'items', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\ValidationData::setCurrentTime
|
||||
*/
|
||||
public function setCurrentTimeShouldChangeTheTimeBasedValues()
|
||||
{
|
||||
$expected = $this->createExpectedData(null, null, null, null, 2);
|
||||
$data = new ValidationData(1);
|
||||
$data->setCurrentTime(2);
|
||||
|
||||
$this->assertAttributeSame($expected, 'items', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\ValidationData::has
|
||||
*/
|
||||
public function hasShouldReturnTrueWhenItemIsNotEmpty()
|
||||
{
|
||||
$data = new ValidationData(1);
|
||||
|
||||
$this->assertTrue($data->has('iat'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\ValidationData::has
|
||||
*/
|
||||
public function hasShouldReturnFalseWhenItemIsEmpty()
|
||||
{
|
||||
$data = new ValidationData(1);
|
||||
|
||||
$this->assertFalse($data->has('jti'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\ValidationData::has
|
||||
*/
|
||||
public function hasShouldReturnFalseWhenItemIsNotDefined()
|
||||
{
|
||||
$data = new ValidationData(1);
|
||||
|
||||
$this->assertFalse($data->has('test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\ValidationData::get
|
||||
*/
|
||||
public function getShouldReturnTheItemValue()
|
||||
{
|
||||
$data = new ValidationData(1);
|
||||
|
||||
$this->assertEquals(1, $data->get('iat'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*
|
||||
* @uses Lcobucci\JWT\ValidationData::__construct
|
||||
*
|
||||
* @covers Lcobucci\JWT\ValidationData::get
|
||||
*/
|
||||
public function getShouldReturnNullWhenItemIsNotDefined()
|
||||
{
|
||||
$data = new ValidationData(1);
|
||||
|
||||
$this->assertNull($data->get('test'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function claimValues()
|
||||
{
|
||||
return [
|
||||
[1],
|
||||
['test']
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
* @param string $sub
|
||||
* @param string $iss
|
||||
* @param string $aud
|
||||
* @param int $time
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function createExpectedData(
|
||||
$id = null,
|
||||
$sub = null,
|
||||
$iss = null,
|
||||
$aud = null,
|
||||
$time = 1
|
||||
) {
|
||||
return [
|
||||
'jti' => $id !== null ? (string) $id : null,
|
||||
'iss' => $iss !== null ? (string) $iss : null,
|
||||
'aud' => $aud !== null ? (string) $aud : null,
|
||||
'sub' => $sub !== null ? (string) $sub : null,
|
||||
'iat' => $time,
|
||||
'nbf' => $time,
|
||||
'exp' => $time
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
phpunit.xml
|
||||
build
|
||||
vendor
|
||||
composer.lock
|
||||
composer.phar
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
$finder = Symfony\CS\Finder\DefaultFinder::create()
|
||||
->in(array(__DIR__))
|
||||
;
|
||||
|
||||
return Symfony\CS\Config\Config::create()
|
||||
->level(Symfony\CS\FixerInterface::SYMFONY_LEVEL)
|
||||
->fixers(array(
|
||||
'-phpdoc_scalar',
|
||||
'-extra_empty_lines',
|
||||
'-unalign_double_arrow',
|
||||
'-unalign_equals',
|
||||
'align_equals',
|
||||
'align_double_arrow',
|
||||
'newline_after_open_tag',
|
||||
'ordered_use',
|
||||
'short_array_syntax',
|
||||
))
|
||||
->setUsingCache(false)
|
||||
->finder($finder)
|
||||
;
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
build:
|
||||
tests:
|
||||
override:
|
||||
- 'vendor/bin/simple-phpunit'
|
||||
@@ -0,0 +1,19 @@
|
||||
preset: symfony
|
||||
|
||||
enabled:
|
||||
- ordered_imports
|
||||
- short_array_syntax
|
||||
- align_equals
|
||||
- align_double_arrow
|
||||
|
||||
disabled:
|
||||
- phpdoc_scalar
|
||||
- extra_empty_lines
|
||||
- unalign_double_arrow
|
||||
- unalign_equals
|
||||
|
||||
finder:
|
||||
not-path:
|
||||
- "Tests/Stubs/"
|
||||
name:
|
||||
- "*.php"
|
||||
@@ -0,0 +1,35 @@
|
||||
language: php
|
||||
|
||||
sudo: false
|
||||
|
||||
matrix:
|
||||
fast_finish: true
|
||||
include:
|
||||
- php: 5.5
|
||||
env: SYMFONY_VERSION=3.4.*
|
||||
- php: 7.1
|
||||
env: SYMFONY_VERSION=4.0.*
|
||||
- php: 7.2
|
||||
env: SYMFONY_VERSION=4.1.*
|
||||
- php: 7.2
|
||||
env: SYMFONY_VERSION=4.2.* DEPENDENCIES=dev
|
||||
- php: hhvm-stable
|
||||
sudo: required
|
||||
dist: trusty
|
||||
group: edge
|
||||
allow_failures:
|
||||
- php: hhvm-stable
|
||||
- php: 7.2
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- $HOME/.composer/cache
|
||||
|
||||
before_install:
|
||||
- phpenv config-rm xdebug.ini || true
|
||||
- if [ "$DEPENDENCIES" = "dev" ]; then perl -pi -e 's/^}$/,"minimum-stability":"dev"}/' composer.json; fi;
|
||||
- if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --no-update; fi;
|
||||
|
||||
install: composer update --no-interaction
|
||||
|
||||
script: composer test
|
||||
@@ -0,0 +1,204 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
For a diff between two versions https://github.com/lexik/LexikJWTAuthenticationBundle/compare/v1.0.0...v2.5.4
|
||||
|
||||
## [2.5.4](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.5.4) (2018-08-2)
|
||||
|
||||
* bug [\#542](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/542) Fix missing implements breaking JWT header alteration ([tucksaun](https://github.com/tucksaun))
|
||||
|
||||
## [2.5.3](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.5.3) (2018-07-6)
|
||||
|
||||
* bug [\#525](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/525) Make openssl key loader service deprecated ([Faecie](https://github.com/Faecie))
|
||||
|
||||
## [2.5.2](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.5.2) (2018-07-3)
|
||||
|
||||
* bug [\#522](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/522) Fix clock skew + deprecation message ([chalasr](https://github.com/chalasr))
|
||||
|
||||
## [2.5.1](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.5.1) (2018-06-30)
|
||||
|
||||
* bug [\#515](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/515) Re-add namshi/jose as an hard requirement until 3.0 ([chalasr](https://github.com/chalasr))
|
||||
|
||||
## [2.5.0](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.5.0) (2018-06-29)
|
||||
|
||||
* feature [\#508](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/508) Replace namshi/jose by lcobucci/jwt ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#485](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/485) Add a `lexik:jwt:generate-token` command ([sroze](https://github.com/sroze))
|
||||
* feature [\#369](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/369) Fix HMAC support ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#492](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/492) Clock skew ([patrickjDE](https://github.com/patrickjDE))
|
||||
* feature [\#433](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/433) Added setPayload to JWTDecodedEvent analogous to JWTCreatedEvent. ([vgeyer](https://github.com/vgeyer))
|
||||
* feature [\#412](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/412) Make the token type case insensitive ([greg0ire](https://github.com/greg0ire))
|
||||
* feature [\#404](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/404) CheckConfigCommand should not be container aware ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#352](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/352) JWT header alteration ([Spomky](https://github.com/Spomky))
|
||||
* feature [\#344](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/344) Add an extension point on the PayloadAwareUserProviderInterface ([sroze](https://github.com/sroze))
|
||||
|
||||
## [2.4.3](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.4.3) (2017-11-6)
|
||||
|
||||
* bug [\#408](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/408) Response classes shouldn't have the @internal PhpDoc tag ([lashae](https://github.com/lashae))
|
||||
* bug [\#403](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/403) Switch to PSR-4 namespaces for PHPUnit ([chalasr](https://github.com/chalasr))
|
||||
* bug [\#399](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/399) Fix sf3.4 command autoregistration deprecation ([ogizanagi](https://github.com/ogizanagi))
|
||||
|
||||
## [2.4.2](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.4.2) (2017-10-19)
|
||||
|
||||
* bug [\#398](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/398) Fix Symfony 4 compatibility ([benji07](https://github.com/benji07))
|
||||
* bug [\#383](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/383) Don't register lcobucci encoder if lcobucci/jwt is not installed ([chalasr](https://github.com/chalasr))
|
||||
|
||||
## [2.4.1](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.4.1) (2017-08-29)
|
||||
|
||||
* bug [\#356](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/356) Dont use DefinitionDecorator on Symfony 3.3+ ([chalasr](https://github.com/chalasr))
|
||||
|
||||
## [2.4.0](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.4.0) (2017-05-10)
|
||||
|
||||
* feature [\#330](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/330) Allow empty ttl for testing purpose ([chalasr](https://github.com/chalasr))
|
||||
* bug [\#328](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/328) Fix autowiring for upcoming Symfony 3.3 ([chalasr](https://github.com/chalasr))
|
||||
|
||||
## [2.3.0](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.3.0) (2017-04-14)
|
||||
|
||||
* bug [\#325](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/325) Move ttl `is_numeric` check from build time to runtime to allow use of %env()% ([DrBenton](https://github.com/DrBenton))
|
||||
* feature [\#320](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/320) Allow for Response Body without JWT Token ([Batch1211](https://github.com/Batch1211))
|
||||
* feature [\#317](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/317) Use symfony/phpunit-bridge for testing ([chalasr](https://github.com/chalasr))
|
||||
|
||||
## [2.2.0](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.2.0) (2017-03-09)
|
||||
|
||||
* feature [\#312](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/312) Ease sharing keys between parties ([chalasr](https://github.com/chalasr))
|
||||
* bug [\#311](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/311) Handle empty or null authorization header prefix ([chteuchteu](https://github.com/chteuchteu))
|
||||
* feature [\#303](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/303) Throw less missleading exception if SSL keys could not be loaded ([phansys](https://github.com/phansys))
|
||||
|
||||
## [2.1.1](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.1.1) (2017-01-23)
|
||||
|
||||
* bug [\#302](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/302) Return user object from User Provider refresh ([MisterGlass](https://github.com/MisterGlass))
|
||||
|
||||
## [2.1.0](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.1.0) (2016-12-30)
|
||||
|
||||
* feature [\#278](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/278) Add JWTUserProvider for loading users from the JWT itself ([chalasr](https://github.com/chalasr))
|
||||
* bug [\#287](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/287) Avoid override existing properties in failure response ([kevin-lot](https://github.com/kevin-lot))
|
||||
|
||||
## [2.0.3](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.0.3) (2016-12-05)
|
||||
* bug [\#285](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/285) Avoid validating key paths before container compilation ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#283](https//github.com/lexik/LexikJWTAuthenticationBundle/pull/283) Ease creating tokens programatically ([chalasr](https://github.com/chalasr))
|
||||
* bug [\#282](https//github.com/lexik/LexikJWTAuthenticationBundle/pull/282) Catch exception from lcobucci parser on invalid but correctly formatted token ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#276](https//github.com/lexik/LexikJWTAuthenticationBundle/pull/276) Added `getProviderKey()` to JWTUserToken ([eXtreme](https://github.com/eXtreme))
|
||||
* [\#280](https//github.com/lexik/LexikJWTAuthenticationBundle/pull/280) Travis: build on sf 3.2 + highest/lowest deps, fix build on hhvm ([chalasr](https://github.com/chalasr))
|
||||
* [\#269](https//github.com/lexik/LexikJWTAuthenticationBundle/pull/269) Improve the structure of the documentation ([chalasr](https://github.com/chalasr))
|
||||
|
||||
## [2.0.2](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.0.2) (2016-10-27)
|
||||
|
||||
* feature [\#262](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/262) Add composer test script ([chalasr](https://github.com/chalasr))
|
||||
* bug [\#261](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/261) The security token must be authenticated no matter of the user's roles ([chalasr](https://github.com/chalasr))
|
||||
|
||||
## [2.0.1](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.0.1) (2016-10-20)
|
||||
|
||||
* feature [\#257](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/257) Set autowiring types on services with many alternatives
|
||||
|
||||
## [2.0.0](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v2.0.0) (2016-10-16)
|
||||
|
||||
* feature [\#249](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/249) Avoid setting exp claim from JWTManager ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#246](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/246) Add a simple built-in encoder based on lcobucci/jwt ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#240](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/240) Add iat check ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#230](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/230) Introduce JWTExpiredEvent ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#184](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/184) [Security] Deprecate current system in favor of a JWTTokenAuthenticator (Guard) ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#218](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/218) Add more flexibility in token extractors configuration ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#217](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/217) Refactor TokenExtractors loadi ng for easy overriding ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#202](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/202) Exceptions simplified ([Spomky](https://github.com/Spomky))
|
||||
* feature [\#201](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/201) Remove deprecated request injections ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#196](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/196) Make *_key_path config options not mandatory ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#177](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/177) Add JWTAuthenticationResponse ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#162](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/162) [Encoder] Handle OpenSSL/phpseclib engines and algorithms ([chalasr](https://github.com/chalasr))
|
||||
|
||||
* [\#175](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/175) Stop ensuring support for PHP versions smaller than 5.0 ([chalasr](https://github.com/chalasr))
|
||||
|
||||
* [\#167](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/167) and [\#169](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/169) Stop ensuring support Symfony versions smaller than 2.8 ([chalasr](https://github.com/chalasr))
|
||||
|
||||
## [1.7.0](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.7.0) (2016-08-06)
|
||||
|
||||
* feature [\#200](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/200) Deprecate injection of Request instances ([chalasr](https://github.com/chalasr))
|
||||
|
||||
## [v1.6.0](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.6.0) (2016-07-07)
|
||||
|
||||
* feature [\#188](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/188) Add JWTNotFoundEvent ([chalasr](https://github.com/chalasr))
|
||||
|
||||
## [v1.5.1](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.5.1) (2016-04-11)
|
||||
|
||||
* bug [\#159](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/159) Fix anonymous access by removing the AuthenticationCredentialsNotFoundException ([chalasr](https://github.com/chalasr))
|
||||
|
||||
## [v1.5.0](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.5.0) (2016-04-07)
|
||||
|
||||
* feature [\#157](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/157) Allow to set a custom response in case of authentication failure or invalid/not found token ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#154](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/154) Add OpenSSLKeyLoader ([chalasr](https://github.com/chalasr))
|
||||
* feature [\#147](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/147) Made the public and private key paths not required… ([ovidiumght](https://github.com/ovidiumght))
|
||||
* bug [\#142](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/142) Add response message in case of invalid token ([chalasr](https://github.com/chalasr))
|
||||
|
||||
## [v1.4.3](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.4.3) (2016-01-30)
|
||||
|
||||
* feature [\#133](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/133) Always call for master request from request stack ([stloyd](https://github.com/stloyd))
|
||||
|
||||
## [v1.4.1](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.4.1) (2016-01-21)
|
||||
|
||||
* feature [\#126](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/126) Use requestStack instead of request ([SmurfyFR](https://github.com/SmurfyFR))
|
||||
|
||||
## [v1.4.0](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.4.0) (2016-01-20)
|
||||
|
||||
* feature [\#117](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/117) Allow empty ttl ([soyuka](https://github.com/soyuka))
|
||||
* feature [\#113](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/113) Add symfony 3.0 support ([Ener-Getick](https://github.com/Ener-Getick))
|
||||
* feature [\#110](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/110) Updated to newest namshi/jose. Dropped support for PHP 5.3 ([TiS](https://github.com/TiS))
|
||||
* feature [\#103](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/103) added functional boot test ([slashfan](https://github.com/slashfan))
|
||||
* feature [\#96](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/96) Add custom authorization header name ([pdoreau](https://github.com/pdoreau))
|
||||
|
||||
## [v1.3.1](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.3.1) (2015-10-21)
|
||||
|
||||
* bug [\#101](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/101) Fatal error on console cache:clear ([ngandemer](https://github.com/ngandemer))
|
||||
|
||||
## [v1.3.0](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.3.0) (2015-10-21)
|
||||
|
||||
* feature [\#100](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/100) Add authentication_listener option ([yelmontaser](https://github.com/yelmontaser))
|
||||
|
||||
## [v1.2.0](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.2.0) (2015-09-28)
|
||||
|
||||
* bug [\#92](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/92) Fix authentication event propagation ([mRoca](https://github.com/mRoca))
|
||||
* feature [\#88](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/88) Add WWW-Authenticate response header on 401 ([teohhanhui](https://github.com/teohhanhui))
|
||||
* feature [\#76](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/76) Add cookie token extractor ([tnucera](https://github.com/tnucera))
|
||||
|
||||
## [v1.1.0](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.1.0) (2015-07-08)
|
||||
|
||||
* feature [\#73](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/73) add JWTEncodedEvent so JWT string is available after its creation ([9orky](https://github.com/9orky))
|
||||
* feature [\#69](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/69) Added new event when token is authenticated ([gamringer](https://github.com/gamringer))
|
||||
|
||||
## [v1.0.10](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.0.10) (2015-06-05)
|
||||
|
||||
* feature [\#71](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/71) Fixing a missing use statement for Reference ([adetwiler](https://github.com/adetwiler))
|
||||
|
||||
## [v1.0.9](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.0.9) (2015-06-05)
|
||||
|
||||
* bug [\#70](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/70) fixed deprecated errors for symfony 2.6 plus ([slashfan](https://github.com/slashfan))
|
||||
* feature [\#67](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/67) Move security details to parameters.yml.dist ([Maltronic](https://github.com/Maltronic))
|
||||
|
||||
## [v1.0.8](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.0.8) (2015-04-20)
|
||||
|
||||
* feature [\#63](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/63) Improve JWTProvider ([JJK801](https://github.com/JJK801))
|
||||
|
||||
## [v1.0.6](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.0.6) (2015-02-17)
|
||||
|
||||
* feature [\#45](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/45) Adding AuthenticationException to the AuthenticationFailureEvent ([ghost](https://github.com/ghost))
|
||||
* feature [\#43](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/43) Added identity field funcionality and its unit test. ([victuxbb](https://github.com/victuxbb))
|
||||
* feature [\#40](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/40) Add flexibilty to the provider and manager ([slashfan](https://github.com/slashfan))
|
||||
|
||||
## [v1.0.5](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.0.5) (2014-09-16)
|
||||
|
||||
* feature [\#28](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/28) Improve response and dispatch event in AuthenticationFailureHandler ([EmmanuelVella](https://github.com/EmmanuelVella))
|
||||
|
||||
## [v1.0.4](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.0.4) (2014-08-13)
|
||||
|
||||
* feature [\#27](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/27) Added encoder / decoder service customization \(\#24\) ([slashfan](https://github.com/slashfan))
|
||||
* feature [\#19](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/19) Add response in success event ([EmmanuelVella](https://github.com/EmmanuelVella))
|
||||
* feature [\#18](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/18) Improve json 401 exception ([EmmanuelVella](https://github.com/EmmanuelVella))
|
||||
|
||||
## [v1.0.2](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.0.2) (2014-07-11)
|
||||
|
||||
* feature [\#15](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/15) Added JWT Creator service ([gfreeau](https://github.com/gfreeau))
|
||||
|
||||
## [v1.0.0](https://github.com/lexik/LexikJWTAuthenticationBundle/tree/v1.0.0) (2014-05-16)
|
||||
|
||||
* feature [\#10](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/10) Added ability to throw exceptions for handling later and to disable the catch-all entry point ([gfreeau](https://github.com/gfreeau))
|
||||
* feature [\#9](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/9) Changed entry point to contain a message and return json ([gfreeau](https://github.com/gfreeau))
|
||||
* bug [\#7](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/7) Jwt entry point fix \#6 ([jaugustin](https://github.com/jaugustin))
|
||||
* feature [\#5](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/5) Firewall config ([slashfan](https://github.com/slashfan))
|
||||
* feature [\#2](https://github.com/lexik/LexikJWTAuthenticationBundle/pull/2) Symfony2.3+ compatibility ([slashfan](https://github.com/slashfan))
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user