Transcript
Page 1: Love and Loss: A Symfony Security Play

Love & LossA Symfony Security Play

Page 2: Love and Loss: A Symfony Security Play

brewcycleportland.com

Page 3: Love and Loss: A Symfony Security Play

@kriswallsmith

Page 4: Love and Loss: A Symfony Security Play

assetic

Page 5: Love and Loss: A Symfony Security Play

Buzz

Page 6: Love and Loss: A Symfony Security Play

Spork

Page 7: Love and Loss: A Symfony Security Play
Page 8: Love and Loss: A Symfony Security Play
Page 9: Love and Loss: A Symfony Security Play

“…the current implementation of the Security Component is … not easily accessible”

http://www.testically.org/2011/03/14/why-i-gave-up-on-the-symfony2-security-component/

Page 10: Love and Loss: A Symfony Security Play

“I would rather see Symfony2 postponed again or the Security Component removed …

I don’t think it is even near of being usable to the community outside the core.”

http://www.testically.org/2011/03/14/why-i-gave-up-on-the-symfony2-security-component/

Page 11: Love and Loss: A Symfony Security Play

“The past few days I have really be struggling with the Symfony2 security component. It is the most complex component of

Symfony2 if you ask me!”

http://blog.vandenbrand.org/2012/06/19/symfony2-authentication-provider-authenticate-against-webservice/

Page 12: Love and Loss: A Symfony Security Play

“(I’m) wondering if I should just work around rather than work with the framework”

https://groups.google.com/forum/#!msg/symfony2/AZpgbEk4Src/73P99zOmq2YJ

Page 13: Love and Loss: A Symfony Security Play
Page 14: Love and Loss: A Symfony Security Play
Page 15: Love and Loss: A Symfony Security Play

Enhance yourPHPfun!

Page 16: Love and Loss: A Symfony Security Play

http://curiouscomedy.org

Page 17: Love and Loss: A Symfony Security Play
Page 18: Love and Loss: A Symfony Security Play
Page 19: Love and Loss: A Symfony Security Play

HttpKernel

kernel.exception

kernel.request kernel.terminatekernel.controller kernel.view kernel.response

Page 20: Love and Loss: A Symfony Security Play

kernel.request kernel.controller kernel.view kernel.response kernel.terminate

kernel.exception

HttpKernel

Page 21: Love and Loss: A Symfony Security Play

kernel.request kernel.controller kernel.view kernel.response kernel.terminate

kernel.exception

HttpKernel

Page 22: Love and Loss: A Symfony Security Play

HttpKernelGet the response and get out

Page 23: Love and Loss: A Symfony Security Play

kernel.request

Routeretc…

Firewall

Page 24: Love and Loss: A Symfony Security Play

FirewallJust another listener

Page 25: Love and Loss: A Symfony Security Play

class YesFirewall{ public function handle($event) { // always say yes }}

Page 26: Love and Loss: A Symfony Security Play

use Symfony\Component\HttpFoundation\Response;

class NoFirewall{ public function handle($event) { // always say no $event->setResponse( new Response('go away', 401) ); }}

Page 27: Love and Loss: A Symfony Security Play

use Symfony\Component\HttpFoundation\Response;

class PickyFirewall{ public function handle($event) { $request = $event->getRequest(); $user = $request->headers->get('PHP_AUTH_USER');

// only names that start with "Q" if ('Q' == $user[0]) return;

$event->setResponse(new Response('go away', 401)); }}

Page 28: Love and Loss: A Symfony Security Play

Security ListenersThe firewall’s henchmen

Page 29: Love and Loss: A Symfony Security Play

Firewall

Listeners

kernel.request

Page 30: Love and Loss: A Symfony Security Play

class Firewall{ public $listeners = array();

public function handle($event) { foreach ($this->listeners as $listener) { $listener->handle($event);

if ($event->hasResponse()) return; } }}

Page 31: Love and Loss: A Symfony Security Play

class YesListener{ public function handle($event) { // always say yes }}

Page 32: Love and Loss: A Symfony Security Play

use Symfony\Component\HttpFoundation\Response;

class NoListener{ public function handle($event) { // always say no $event->setResponse( new Response('go away', 401) ); }}

Page 33: Love and Loss: A Symfony Security Play

use Symfony\Component\HttpFoundation\Response;

class PickyListener{ public function handle($event) { $request = $event->getRequest(); $user = $request->headers->get('PHP_AUTH_USER');

// only names that start with "Q" if ('Q' == $user[0]) return;

$event->setResponse(new Response('go away', 401)); }}

Page 34: Love and Loss: A Symfony Security Play

AuthenticationAre you who you say you are?

Page 35: Love and Loss: A Symfony Security Play

AuthorizationAre you allowed to ____?

Page 36: Love and Loss: A Symfony Security Play

TokensThe Language of Security

Page 37: Love and Loss: A Symfony Security Play

Authentication ListenersMap from request to token

Page 38: Love and Loss: A Symfony Security Play

Request

Response (?) Token

CoreHTTP

Page 39: Love and Loss: A Symfony Security Play
Page 40: Love and Loss: A Symfony Security Play
Page 41: Love and Loss: A Symfony Security Play

AuthenticationListener A

AuthenticationListener B

AuthenticationManager

Firewall

Page 42: Love and Loss: A Symfony Security Play

class AuthenticationListener{ public $authMan, $context;

public function handle($e) { $r = $e->getRequest(); $u = $r->headers->get('PHP_AUTH_USER');

$t = new AnonToken($u); $t = $this->authMan->authenticate($t);

$this->context->setToken($t); }}

Page 43: Love and Loss: A Symfony Security Play

class AuthenticationManager{ public function authenticate($t) { // always say no }}

Page 44: Love and Loss: A Symfony Security Play

class AuthenticationManager{ public function authenticate($t) { // always say yes return new AuthToken($t->getUser()); }}

Page 45: Love and Loss: A Symfony Security Play

class AuthenticationManager{ public function authenticate($t) { $u = $t->getUser(); // only names that start with "Q" if ('Q' == $u[0]) { return new AuthToken($u); } }}

Page 46: Love and Loss: A Symfony Security Play

Authentication ManagerResponsible for authenticating

the token

Page 47: Love and Loss: A Symfony Security Play

Authentication ProvidersDo the actual authentication work

Page 48: Love and Loss: A Symfony Security Play

UserProviders

AuthenticationProviders

AuthenticationListener A

AuthenticationListener B

AuthenticationManager

Page 49: Love and Loss: A Symfony Security Play

User ProvidersAccess the repository of users

Page 50: Love and Loss: A Symfony Security Play

class AuthenticationManager{ public $providers = array();

public function authenticate($t) { foreach ($this->providers as $p) { if ($p->supports($t)) { return $p->authenticate($t); } } }}

Page 51: Love and Loss: A Symfony Security Play

class AuthenticationProvider{ public $up;

public function authenticate($t) { $u = $t->getUser(); $u = $this->up->loadUserByUsername($u);

if ($u) return new AuthToken($u); }}

Page 52: Love and Loss: A Symfony Security Play

class UserProvider{ public $repo;

public function loadUserByUsername($u) { return ($this->repo->find(array( 'username' => $u, ))); }}

Page 53: Love and Loss: A Symfony Security Play

Authentication

Page 54: Love and Loss: A Symfony Security Play

Authentication Listeners

• Map client data from request to token

• Pass token to authentication manager

• Update state of security context

Page 55: Love and Loss: A Symfony Security Play

Authentication Manager

• Responsible for authenticating the token

• Calls the appropriate authentication provider

• Handles exceptions

Page 56: Love and Loss: A Symfony Security Play

Authentication Providers

• Performs authentication using client data in the token

• Marks the token as authenticated

• Attaches the user object to the token

Page 57: Love and Loss: A Symfony Security Play

User Providers

• Retrieves the user from the database

Page 58: Love and Loss: A Symfony Security Play

Authorization

Page 59: Love and Loss: A Symfony Security Play

class AuthorizationListener{ public function handle($e) { // always say yes }}

Page 60: Love and Loss: A Symfony Security Play

use Symfony\Component\HttpFoundation\Response;

class AuthorizationListener{ public function handle($e) { // always say no $e->setResponse( new Response('go away', 403) ); }}

Page 61: Love and Loss: A Symfony Security Play

Access MapLooks at a request and determines

token requirements

Page 62: Love and Loss: A Symfony Security Play

Access Decision ManagerThe gatekeeper

Page 63: Love and Loss: A Symfony Security Play

VotersDecisionManager

Listener Map

Page 64: Love and Loss: A Symfony Security Play

use Symfony\Component\HttpFoundation\Response;

class AccessListener{ public $context, $map, $decider;

public function handle($e) { $r = $e->getRequest(); $t = $this->context->getToken();

$reqs = $this->map->getRequirements($r);

if (!$this->decider->decide($t, $reqs)) { $e->setResponse( new Response('go away', 403) ); } }}

Page 65: Love and Loss: A Symfony Security Play

class AccessMap{ public function getRequirements($r) { $path = $r->getPathInfo(); if (0 === strpos($path, '/admin')) { return array('ADMIN'); } }}

Page 66: Love and Loss: A Symfony Security Play

class AccessDecisionManager{ public $voters;

public function decide($t, $reqs) { foreach ($this->voters as $v) { if ($v->vote($t, null, $reqs)) { return true; } }

return false; }}

Page 67: Love and Loss: A Symfony Security Play

class AccessVoter{ public function vote($t, $obj, $reqs) { foreach ($reqs as $req) { if (!$t->hasAttribute($req)) { return false; } }

return true; }}

Page 68: Love and Loss: A Symfony Security Play

Authorization

Page 69: Love and Loss: A Symfony Security Play

Extension Points

Page 70: Love and Loss: A Symfony Security Play

The firewall has many listeners

Page 71: Love and Loss: A Symfony Security Play

The authentication manager has many authentication providers

Page 72: Love and Loss: A Symfony Security Play

Which MAY rely onuser providers

Page 73: Love and Loss: A Symfony Security Play

The access decision manager has many voters

Authenticated

Roles

ACL

Page 74: Love and Loss: A Symfony Security Play

Questions?

Page 75: Love and Loss: A Symfony Security Play

is hiring

Page 76: Love and Loss: A Symfony Security Play
Page 77: Love and Loss: A Symfony Security Play

“Horrible”“Worst talk ever”

“Go back to high school”

https://joind.in/8665