{"id":1563,"date":"2022-08-30T15:17:34","date_gmt":"2022-08-30T15:17:34","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/how-to-setup-a-securityserviceprovider-with-custom-rest-api-and-success-failure-handlers-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:34","modified_gmt":"2022-08-30T15:17:34","slug":"how-to-setup-a-securityserviceprovider-with-custom-rest-api-and-success-failure-handlers-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-setup-a-securityserviceprovider-with-custom-rest-api-and-success-failure-handlers-collection-of-common-programming-errors\/","title":{"rendered":"How to Setup a SecurityServiceProvider with Custom REST API and Success\/Failure Handlers?-Collection of common programming errors"},"content":{"rendered":"<p>I have an AngularJS front end being served via domain.com\/index.html. My API calls are all in the same domain scope at domain.com\/api\/*. I want to enable standard username\/password authentication. However, all I need the Silex back end to do is set the session up and return a 200 success or 401 failure status codes at login time. I found pieces of code from the silex, symfony2 docs as SO but just can&#8217;t get it quite right. I basically setup my custom Success\/Failure custom handlers to return the codes and apply them to the firewall. My &#8220;check_path&#8221; in the firewall\/form settings seems to be the only real peace that&#8217;s important.<\/p>\n<p>And here is most of the code. Hope it&#8217;s formatted well enough for you to help. Thanks!<\/p>\n<p>app.php<\/p>\n<pre><code>\/\/ Setup sessions\n$app-&gt;register(new Silex\\Provider\\SessionServiceProvider());\n\n\/\/ General Service Provder for Controllers\n$app-&gt;register(new Silex\\Provider\\ServiceControllerServiceProvider());\n\n$app['security.authentication.success_handler.auth'] = $app-&gt;share(function ($app) {\n    return new ChrDb\\Security\\AuthSuccessHandler();\n});\n\n$app['security.authentication.failure_handler.auth'] = $app-&gt;share(function ($app) {\n    return new ChrDb\\Security\\AuthFailureHandler();\n});\n\n\/\/ Define a custom encoder for Security\/Authentication\n$app['security.encoder.digest'] = $app-&gt;share(function ($app) {\n    \/\/ uses the password-compat encryption\n    return new BCryptPasswordEncoder(10);\n});\n\n\/\/ Security definition.\n$app-&gt;register(new SecurityServiceProvider(), array(\n    'security.firewalls' =&gt; array(\n        \/\/ Login URL is open to everybody.\n        \/\/ 'login' =&gt; array(\n        \/\/      'pattern' =&gt; '^\/api\/login$',\n        \/\/      'anonymous' =&gt; true,\n        \/\/  ),\n        \/\/ Any other URL requires auth.\n        'auth' =&gt; array(\n            \/\/'pattern' =&gt; '^.*$',\n            'pattern' =&gt; '^\/api$',\n            'form'      =&gt; array(\n                'login_path'         =&gt; '\/api\/auth\/login',\n                'check_path'         =&gt; '\/api\/login',\n                'username_parameter' =&gt; 'username',\n                'password_parameter' =&gt; 'password'\n            ),\n            'logout'    =&gt; array('logout_path' =&gt; '\/api\/auth\/logout'),\n            'users'     =&gt; $app-&gt;share(function() use ($app) {\n                return new ChrDb\\Security\\UserProvider($app);\n            }),\n        ),\n    ),\n));\n\n$app['api.auth.controller'] = $app-&gt;share(function() use ($app) {\n    return new ChrDb\\Api\\AuthController();\n});\n$app-&gt;get('\/api\/auth\/login', \"api.auth.controller:loginAction\");\n$app-&gt;get('\/api\/auth\/logout', \"api.auth.controller:logoutAction\");\n<\/code><\/pre>\n<p>Here is the rest of the relevant code:<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-27 11:51:53. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have an AngularJS front end being served via domain.com\/index.html. My API calls are all in the same domain scope at domain.com\/api\/*. I want to enable standard username\/password authentication. However, all I need the Silex back end to do is set the session up and return a 200 success or 401 failure status codes at [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1563","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1563"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1563\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}