Share
Embed
Transcript
130 slides
-
WHO AM I? • Head of • Trainer & Developer
• Enjoying sharer -
WHAT’S THE PLAN? 1. Security in Symfony: Authentication + Authorization
2. Brand new: Guard -
WHAT IS A USER? The object stores: • credentials •
associated roles -
WHAT DO WE HAVE TO WRITE? security: encoders: Symfony\Component\Security\Core\User\User: bcrypt
providers: in_memory: memory: users: sarah: password: $2a$12$LCY0M… roles: 'ROLE_USER' firewalls: admin: provider: in_memory pattern: /^admin form_login: login_path: /login check_path: /login_check -
WHAT DO WE HAVE TO WRITE? security: encoders: Symfony\Component\Security\Core\User\User: bcrypt
providers: in_memory: memory: users: sarah: password: $2a$12$LCY0M… roles: 'ROLE_USER' firewalls: admin: provider: in_memory pattern: /^admin form_login: login_path: /login check_path: /login_check -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); Encoder Factory $factory-‐>getEncoder($user); $encoder $encoder-‐>isPasswordValid('mypwd'); Encoder true authenticated false | 401: Unauthorized /admin/myprofile or -
Determines whether or not the user needs to be authenticated.
AUTHENTICATION: FIREWALL -
Your application all routes begin with /admin all routes beginning
with /shop all routes beginning with /blog -
Your application all routes begin with /admin all routes beginning
with /shop all routes beginning with /blog all other routes… -
Your application all routes begin with /admin all routes beginning
with /shop all routes beginning with /blog all other routes… The user needs to be authenticated to access this part of the app -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
authenticated 401: Unauthorized or /admin/myprofile -
WHAT DO WE HAVE TO WRITE? security: firewalls: admin: provider:
in_memory pattern: /^admin form_login: login_path: /login check_path: /login_check -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Provider authenticated 401: Unauthorized /admin/myprofile or -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Provider $provider-‐>loadUserByUsername('sarah.khalil'); authenticated 401: Unauthorized /admin/myprofile or -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); authenticated 401: Unauthorized /admin/myprofile or -
interface UserProviderInterface { /** * Loads the user for the
given username. * * This method must throw UsernameNotFoundException if the user is not * found. */ public function loadUserByUsername($username); /** * Refreshes the user for the account interface. * * It is up to the implementation to decide if the user data should be * totally reloaded (e.g. from the database), or if the UserInterface * object can just be merged into some internal array of users / identity * map. */ public function refreshUser(UserInterface $user); /** * Whether this provider supports the given user class. */ public function supportsClass($class); } -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
authenticated Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Encoder Factory authenticated Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Encoder Factory $factory-‐>getEncoder($user); authenticated Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Encoder Factory $factory-‐>getEncoder($user); $encoder authenticated Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Encoder Factory $factory-‐>getEncoder($user); $encoder Encoder authenticated Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Encoder Factory $factory-‐>getEncoder($user); $encoder Encoder $encoder-‐>isPasswordValid('mypwd'); authenticated Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Encoder Factory $factory-‐>getEncoder($user); $encoder Encoder $encoder-‐>isPasswordValid('mypwd'); authenticated true Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Encoder Factory $factory-‐>getEncoder($user); $encoder Encoder $encoder-‐>isPasswordValid('mypwd'); authenticated true Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); /admin/myprofile false | 401: Unauthorized or -
interface PasswordEncoderInterface { /** * Encodes the raw password. */
public function encodePassword($raw, $salt); /** * Checks a raw password against an encoded password. */ public function isPasswordValid($encoded, $raw, $salt); } -
security: encoders: Symfony\Component\Security\Core\User\User: bcrypt providers: in_memory: memory: users: sarah: password:
$2a$12$LCY0M… roles: 'ROLE_USER' firewalls: admin: provider: in_memory pattern: /^admin form_login: login_path: /login check_path: /login_check -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Provider /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Provider $provider-‐>loadUserByUsername('sarah.khalil'); /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); Encoder Factory /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); Encoder Factory $factory-‐>getEncoder($user); /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); Encoder Factory $factory-‐>getEncoder($user); $encoder /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); Encoder Factory $factory-‐>getEncoder($user); $encoder Encoder /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); Encoder Factory $factory-‐>getEncoder($user); $encoder Encoder $encoder-‐>isPasswordValid('mypwd'); /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); Encoder Factory $factory-‐>getEncoder($user); $encoder Encoder $encoder-‐>isPasswordValid('mypwd'); true /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); Encoder Factory $factory-‐>getEncoder($user); $encoder Encoder $encoder-‐>isPasswordValid('mypwd'); true authenticated /admin/myprofile -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); Encoder Factory $factory-‐>getEncoder($user); $encoder Encoder $encoder-‐>isPasswordValid('mypwd'); true authenticated /admin/myprofile or -
AUTHENTICATION FLOW Firewall backend username = ‘sarah.khalil’ password = ‘mypwd’
Provider $provider-‐>loadUserByUsername('sarah.khalil'); $user = new User(‘sarah.khalil’); Encoder Factory $factory-‐>getEncoder($user); $encoder Encoder $encoder-‐>isPasswordValid('mypwd'); true authenticated false | 401: Unauthorized /admin/myprofile or -
$this-‐>denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!'); $decision =
$this-‐>get('security.authorization_checker')-‐>isGranted('ROLE_ADMIN'); if (false === $decision) { throw $this-‐>createAccessDeniedException('Unable to access this page!'); } What you have usually in a controller or -
Or in a template {% if is_granted('ROLE_ADMIN') %}
<a href="...">Delete</a> {% endif %} -
Or in a template {% if is_granted('ROLE_ADMIN') %}
<a href="...">Delete</a> {% endif %} Symfony\Bridge\Twig\Extension\SecurityExtension -
final public function isGranted($attributes, $object = null)
{ // Checks if there is someone authenticated (if there is a token) // Makes sure that the attributes in an array // returns true or false return $this-‐>accessDecisionManager-‐>decide($token, $attributes, $object); } Symfony\Component\Security\Core\Authorization \AccessDecisionManagerInterface Any string that represent a permission anything that can help to make a decision -
Symfony\Component\Security\Core\Authorization \AccessDecisionManager The object needs: • voters • a strategy
• $allowIfAllAbstainDecisions • $allowIfEqualGrantedDeniedDecisions -
VOTERS Voters are added in a compiler pass (Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddSecurityVotersPass )
The services must have a « security.voter » tag to be added in the AccessDecisionManager. -
VOTERS Voters are added in a compiler pass (Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddSecurityVotersPass )
The services must have a « security.voter » tag to be added in the AccessDecisionManager. All the voters are sorted by the attribute priority (the higher one is the last to be executed). If you don’t put any priority, => 0 (first to be executed) Why? To make sure that is doesn’t mess with other decisions taken -
$strategy, $allowIfAllAbstainDecisions, $allowIfEqualGrantedDeniedDecisions // Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension $container
-‐>getDefinition('security.access.decision_manager') -‐>addArgument($config['access_decision_manager']['strategy']) -‐>addArgument( $config['access_decision_manager']['allow_if_all_abstain'] ) -‐>addArgument( $config['access_decision_manager']['allow_if_equal_granted_denied'] ) ; -
$strategy = $allowIfAllAbstainDecisions = $allowIfEqualGrantedDeniedDecisions = true affirmative
or consensus or unanimous true or false true or false -
Symfony\Component\Security\Core\Authorization \AccessDecisionManager The object needs: • voters • a strategy
• $allowIfAllAbstainDecisions • $allowIfEqualGrantedDeniedDecisions -
//Symfony\Component\Security\Core\Authorization\AccessDecisionManager public function decide(TokenInterface $token, array $attributes, $object =
null) { return $this-‐>{$this-‐>strategy} ($token, $attributes, $object); } decideAffirmative decideConsensus decideUnanimous -
decideAffirmative • Donne accès si n’importe lequel des voter dit
ACCES_GRANTED. • Si tous les voters disent ACCES_ABSTAIN => $allowIfAllAbstainDecision prend la décision. One says yes. default = false -
decideConsensus • Donne accès si la majorité (sans les abstentions)
dit ACCES_GRANTED. • S’il y a autant de ACCES_GRANTED que d’ ACCES_DENIED => $allowIfEqualGrantedDeniedDecision prend la décision • Si tous les voters disent ACCES_ABSTAIN => $allowIfAllAbstainDecision prend la décision. Most of them says yes. default = true -
decideUnanimous • Donne accès si tous les voters (sans les
abstentions) disent ACCES_GRANTED. • Si tous les voters disent ACCES_ABSTAIN => $allowIfAllAbstainDecision prend la décision. All of them says yes. default = false -
decideXxxx //Symfony\Component\Security\Core\Authorization\AccessDecisionManager //… foreach ($this-‐>voters as $voter) {
$result = $voter-‐>vote($token, $object, $attributes); } // logic to return true or false 200 or 403 (forbidden) -
Controller or template or service from AuthorizationChecker::isGranted() Strategy Voters Decision
maker if all can’t make a decision Decision maker if all abstain needs AccessDecisionManager::decide() -
Controller or template or service from AuthorizationChecker::isGranted() AccessDecisionManager::decideAffirmative() or AccessDecisionManager::decideConsensus()
or AccessDecisionManager::decideUnanimous() Strategy Voters Decision maker if all can’t make a decision Decision maker if all abstain needs AccessDecisionManager::decide() -
Controller or template or service from VoterInterface::vote() … VoterInterface::vote() calls
AuthorizationChecker::isGranted() AccessDecisionManager::decideAffirmative() or AccessDecisionManager::decideConsensus() or AccessDecisionManager::decideUnanimous() Strategy Voters Decision maker if all can’t make a decision Decision maker if all abstain needs AccessDecisionManager::decide() -
Controller or template or service from VoterInterface::vote() … VoterInterface::vote() calls
AuthorizationChecker::isGranted() AccessDecisionManager::decideAffirmative() or AccessDecisionManager::decideConsensus() or AccessDecisionManager::decideUnanimous() false true returns Strategy Voters Decision maker if all can’t make a decision Decision maker if all abstain needs AccessDecisionManager::decide() -
Previously… with the SimplePreauthenticator (1/4) • Implement the SimplePreAuthenticatorInterface: 1.createToken(Request
$request, $providerKey) 2.authenticateToken(TokenInterface $token, UserProviderInterface $userProvider) 3.supportsToken(TokenInterface $token, $provider) -
Previously… with the SimplePreauthenticator (1/4) • Implement the SimplePreAuthenticatorInterface: 1.createToken(Request
$request, $providerKey) 2.authenticateToken(TokenInterface $token, UserProviderInterface $userProvider) 3.supportsToken(TokenInterface $token, $provider) declare it as a service (github_authenticator) -
Previously… with the SimplePreauthenticator (2/4) • Implement the UserProviderInterface: 1.loadUserByUsername($username)
2.refreshUser(UserInterface $user) 3.supportsToken($class) -
Previously… with the SimplePreauthenticator (2/4) • Implement the UserProviderInterface: 1.loadUserByUsername($username)
2.refreshUser(UserInterface $user) 3.supportsToken($class) declare it as a service (github_authenticator) -
Previously… with the SimplePreauthenticator (4/4) security: firewalls: secured_area: pattern: ^/admin
stateless: false simple_preauth: authenticator: github_authenticator provider: github_user_provider logout: path: /admin/logout target: / -
Implement the GuardAuthenticatorInterface 1.start(Request $request, AuthenticationException $exception) 2.getCredentials(Request $request) 3.getUser($credentials,
UserProviderInterface $userProvider) 4.checkCredentials($credentials, UserInterface $user) 5.onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) 6.onAuthenticationFailure(Request $request, AuthenticationException $exception) 7.supportsRememberMe() -
LINKS TO REVIEW • http://knpuniversity.com/blog/guard-authentication • https://knpuniversity.com/screencast/guard/api-token#play • https://github.com/knpuniversity/KnpUGuard/tree/master/ src
• https://github.com/knpuniversity/KnpUGuardBundle • https://github.com/symfony/symfony/pull/14673 -
• When to create a firewall (different way of authenticating)
• Voters must be light in case of unanimous or consensus (because of the fact that all voters are executed)