Skip to content

Commit 4f4c054

Browse files
committed
Merge pull request #38 from ethanhann/feature-34-authentication_failure_listener
Feature 34 authentication failure listener
2 parents f5b10a7 + d747533 commit 4f4c054

File tree

3 files changed

+129
-75
lines changed

3 files changed

+129
-75
lines changed

Resources/config/security_listeners.xml

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<parameter key="security.authentication.listener.trusted_sso.class">BeSimple\SsoAuthBundle\Security\Http\Firewall\TrustedSsoAuthenticationListener</parameter>
1111
<parameter key="security.logout.handler.sso.class">BeSimple\SsoAuthBundle\Security\Http\Logout\SsoLogoutHandler</parameter>
1212
<parameter key="security.logout.sso_success_handler.class">BeSimple\SsoAuthBundle\Security\Http\Logout\SsoLogoutSuccessHandler</parameter>
13+
<parameter key="security.authentication.sso_authentication_failure_handler.class">BeSimple\SsoAuthBundle\Security\Http\Authentication\SsoAuthenticationFailureHandler</parameter>
14+
<parameter key="security.authentication.hide_user_not_found">FALSE</parameter>
1315
</parameters>
1416

1517
<services>
@@ -41,5 +43,9 @@
4143
<argument type="service" id="be_simple.sso_auth.factory" />
4244
</call>
4345
</service>
46+
47+
<service id="security.authentication.sso.authentication_failure_handler" class="%security.authentication.sso_authentication_failure_handler.class%" abstract="false">
48+
<argument type="service" id="templating" />
49+
</service>
4450
</services>
4551
</container>

Resources/doc/example.md

+78-75
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,78 @@
1-
Authentication through SSO CAS Server with Symfony2
2-
===================================================
3-
4-
- use the Bundle : BeSimpleSsoAuthBundle (instal with Composer)
5-
- be careful on dependences : Buzz needs a recent version of libcurl (7.19 ??)
6-
7-
8-
Configure SSO
9-
-------------
10-
11-
In config.yml:
12-
13-
be_simple_sso_auth:
14-
admin_sso:
15-
protocol:
16-
id: cas
17-
version: 2
18-
server:
19-
id: cas
20-
login_url: https://cas.server.tld/login
21-
logout_url: https://cas.server.tld/logout
22-
validation_url: https://cas.server.tld/serviceValidate
23-
24-
25-
26-
Create a firewall
27-
-----------------
28-
29-
In security.yml:
30-
31-
my_firewall:
32-
pattern: ^/
33-
anonymous: ~
34-
trusted_sso:
35-
manager: admin_sso
36-
37-
login_action: false # BeSimpleSsoAuthBundle:TrustedSso:login
38-
logout_action: false # BeSimpleSsoAuthBundle:TrustedSso:logout
39-
create_users: true
40-
created_users_roles: [ROLE_USER ]
41-
check_path: /
42-
43-
44-
Create all routes (mandatory even if there is no controller)
45-
------------------------------------------------------------
46-
47-
In routing.yml :
48-
49-
login:
50-
pattern: /login
51-
52-
logout:
53-
pattern: /logout
54-
55-
56-
Providers
57-
---------
58-
59-
Example with Propel:
60-
61-
providers:
62-
administrators:
63-
propel:
64-
class: Altern\CdtBundle\Model\User
65-
property: username
66-
67-
The propel User Class must implement \Symfony\Component\Security\Core\User\UserInterface
68-
69-
70-
If necessary, you can disable SSL Certificat Verification
71-
---------------------------------------------------------
72-
73-
Add in parameters.ini :
74-
75-
be_simple.sso_auth.client.option.curlopt_ssl_verifypeer.value: FALSE
1+
Authentication through SSO CAS Server with Symfony2
2+
===================================================
3+
4+
- use the Bundle : BeSimpleSsoAuthBundle (install with Composer)
5+
- be careful on dependences : Buzz needs a recent version of libcurl (7.19 ??)
6+
7+
8+
Configure SSO
9+
-------------
10+
11+
In config.yml:
12+
13+
be_simple_sso_auth:
14+
admin_sso:
15+
protocol:
16+
id: cas
17+
version: 2
18+
server:
19+
id: cas
20+
login_url: https://cas.server.tld/login
21+
logout_url: https://cas.server.tld/logout
22+
validation_url: https://cas.server.tld/serviceValidate
23+
24+
25+
26+
Create a firewall
27+
-----------------
28+
29+
# app/config/security.yml
30+
my_firewall:
31+
pattern: ^/
32+
anonymous: ~
33+
trusted_sso:
34+
manager: admin_sso
35+
login_action: false # BeSimpleSsoAuthBundle:TrustedSso:login
36+
logout_action: false # BeSimpleSsoAuthBundle:TrustedSso:logout
37+
create_users: true
38+
created_users_roles: [ROLE_USER ]
39+
check_path: /
40+
41+
42+
Create all routes (mandatory even if there is no controller)
43+
------------------------------------------------------------
44+
45+
# app/config/routing.yml
46+
login:
47+
pattern: /login
48+
logout:
49+
pattern: /logout
50+
51+
52+
Providers
53+
---------
54+
55+
Example with Propel:
56+
57+
providers:
58+
administrators:
59+
propel:
60+
class: Altern\CdtBundle\Model\User
61+
property: username
62+
63+
The propel User Class must implement \Symfony\Component\Security\Core\User\UserInterface
64+
65+
Customize the "Username does not exist" error page
66+
--------------------------------------------------
67+
68+
When a user successfully authenticates, but is not in the user provider's data store (or a user provider is not configured at all),
69+
then a generic error page is shown indicating that the user was not found. You can customize this error page by overriding the Twig error template,
70+
as described here: http://symfony.com/doc/current/cookbook/controller/error_pages.html
71+
72+
If necessary, you can disable SSL Certificate Verification
73+
----------------------------------------------------------
74+
75+
This is handy when using a development server that does not have a valid certificate, but it should not be done in production.
76+
77+
# app/config/parameters.yml
78+
be_simple.sso_auth.client.option.curlopt_ssl_verifypeer.value: FALSE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace BeSimple\SsoAuthBundle\Security\Http\Authentication;
4+
5+
use Symfony\Component\HttpFoundation\Request;
6+
use Symfony\Component\HttpFoundation\Response;
7+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
8+
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
9+
10+
class SsoAuthenticationFailureHandler implements AuthenticationFailureHandlerInterface
11+
{
12+
private $templating;
13+
14+
/**
15+
* @param $templating Templating service for rendering responses.
16+
*/
17+
public function __construct($templating)
18+
{
19+
$this->templating = $templating;
20+
}
21+
22+
/**
23+
* This is called when an interactive authentication attempt fails.
24+
*
25+
* @param Request $request
26+
* @param AuthenticationException $exception
27+
*
28+
* @return Response
29+
*/
30+
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
31+
{
32+
if ($request->isXmlHttpRequest()) {
33+
$result = array('success' => false);
34+
return new Response(json_encode($result));
35+
} else {
36+
// Handle non XmlHttp request.
37+
$parameters = array(
38+
'status_text' => $exception->getMessage(),
39+
'status_code' => $exception->getCode(),
40+
);
41+
42+
return $this->templating->renderResponse('TwigBundle:Exception:error.html.twig', $parameters);
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)