Last active
January 26, 2018 12:53
-
-
Save marinsagovac/6f651af810a16b119d0691cfcdc3dbb0 to your computer and use it in GitHub Desktop.
Symfony JWT implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ref: https://github.com/lexik/LexikJWTAuthenticationBundle | |
composer require lexik/jwt-authentication-bundle | |
Register in AppKernel: | |
public function registerBundles() | |
{ | |
return array( | |
// ... | |
new Lexik\Bundle\JWTAuthenticationBundle\LexikJWTAuthenticationBundle(), | |
); | |
} | |
mkdir -p var/jwt # For Symfony3+, no need of the -p option | |
openssl genrsa -out var/jwt/private.pem -aes256 4096 | |
openssl rsa -pubout -in var/jwt/private.pem -out var/jwt/public.pem | |
Save all passphrase of public and private keys. | |
parameters.yml: | |
jwt_private_key_path: '%kernel.root_dir%/../var/jwt/private.pem' # ssh private key path | |
jwt_public_key_path: '%kernel.root_dir%/../var/jwt/public.pem' # ssh public key path | |
jwt_key_pass_phrase: '' # ssh key pass phrase | |
jwt_token_ttl: 3600 | |
config.yml: | |
lexik_jwt_authentication: | |
private_key_path: '%jwt_private_key_path%' | |
public_key_path: '%jwt_public_key_path%' | |
pass_phrase: '%jwt_key_pass_phrase%' | |
token_ttl: '%jwt_token_ttl%' | |
security.yml: | |
security: | |
# ... | |
firewalls: | |
login: | |
pattern: ^/api/login | |
stateless: true | |
anonymous: true | |
form_login: | |
check_path: /api/login_check | |
success_handler: lexik_jwt_authentication.handler.authentication_success | |
failure_handler: lexik_jwt_authentication.handler.authentication_failure | |
require_previous_session: false | |
api: | |
pattern: ^/api | |
stateless: true | |
guard: | |
authenticators: | |
- lexik_jwt_authentication.jwt_token_authenticator | |
access_control: | |
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } | |
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY } | |
NOTE: Make sure that login block is before main. | |
routing.yml: | |
api_login_check: | |
path: /api/login_check | |
1. TEST | |
curl -X POST http://localhost:8000/api/login_check -d _username=test -d _password=test | |
{"code":401,"message":"JWT Token not found"} | |
Implement FOSUserBundle to work with JWT. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment