Overview
The CAS server implements the CAS protocol on server side and may even behave like an OAuth provider, an OpenID provider or a SAML IdP. Whatever the protocol, the CAS server is first of all a server.
But the CAS server can also act as a client using the pac4j library and delegate the authentication to:
- Another CAS server
- An OAuth provider: Facebook, Twitter, Google, LinkedIn, Yahoo and several other providers
- An OpenID provider: myopenid.com
- A SAML identity provider
- An OpenID Connect identity provider.
Support is enabled by including the following dependency in the Maven WAR overlay:
How to use CAS/OAuth/OpenID/SAML client support in CAS applications?
Information returned by a delegated authentication
Once you have configured (see information below) your CAS server to act as an OAuth, CAS, OpenID (Connect) or SAML client, users will be able to authenticate at a OAuth/CAS/OpenID/SAML provider (like Facebook) instead of authenticating directly inside the CAS server.
In the CAS server, after this kind of delegated authentication, users have specific authentication data.
The Authentication
object has:
- The attribute
AuthenticationManager.AUTHENTICATION_METHOD_ATTRIBUTE
(authenticationMethod) set toorg.jasig.cas.support.pac4j.authentication.handler.support.ClientAuthenticationHandler
- The attribute
clientName
set to the type of the provider used during authentication process.
The Principal
object of the Authentication
object has:
- An identifier which is the profile type +
#
+ the identifier of the user for this provider (i.eFacebookProfile#0000000001
) - Attributes populated by the data retrieved from the provider (first name, last name, birthdate…)
How to send profile attributes to CAS client applications?
In CAS applications, through service ticket validation, user information are pushed to the CAS client and therefore to the application itself.
The identifier of the user is always pushed to the CAS client. For user attributes, it involves both the configuration at the server and the way of validating service tickets.
On CAS server side, to push attributes to the CAS client, the service needs to be configured to allow attribute release:
On CAS client side, to receive attributes, you need to use the SAML validation or the CAS 3.0 validation, that is the /p3/serviceValidate
url.
How to recreate user profiles in CAS applications?
In the CAS server, the complete user profile is known but when attributes are sent back to the CAS client applications, there is some kind of “CAS serialization” which makes data uneasy to be restored at their original state.
Though, you can now completely rebuild the original user profile from data returned in the CAS Assertion
.
After validating the service ticket, an Assertion
is available in the CAS client from which you can get the identifier and the attributes of the authenticated user using the pac4j library:
As the identifier stores the kind of profile in its own definition (*clientName#idAtProvider*
), you can use the org.pac4j.core.profile.ProfileHelper.buildProfile(id, attributes)
method to recreate the original profile:
and then use it in your application!
Configuration
Add the required pac4j-* libraries
To add CAS client support, add the following dependency:
To add OAuth client support, add the following dependency:
To add OpenID client support, add the following dependency:
To add OpenID Connect client support, add the following dependency:
To add SAML support, add the following dependency:
Add the needed clients
A provider is a server which can authenticate user (like Google, Yahoo…) instead of a CAS server. If you want to delegate the CAS authentication to Twitter for example, you have to add an OAuth client for the provider: Twitter. Clients classes are defined in the pac4j library.
All the needed clients to authenticate against providers must be declared in the applicationContext.xml
file:
For each OAuth provider, the CAS server is considered as an OAuth client and therefore should be declared also at the OAuth provider. After declaration, a key and a secret is given by the OAuth provider which has to be defined in the beans (the_key_for_xxx and the_secret_for_xxx values for the key and secret properties).
For the CAS OAuth wrapping, the casOAuthUrl property must be set to the OAuth wrapping url of the other CAS server which is using OAuth wrapping (something like http://mycasserver2/oauth2.0).
To simplify configuration, all clients and the CAS server login url are gathered in the same Clients
configuration bean (in the applicationContext.xml
file):
Add the client action in webflow
In the login-webflow.xml
file, the ClientAction
must be added at the beginning of the webflow. Its role is to intercept callback calls from providers (like Facebook, Twitter…) after a delegated authentication:
This ClientAction
has to be defined in the cas-servlet.xml
file with all the needed clients:
This ClientAction
uses the centralAuthenticationService bean to finish the CAS authentication and references all the clients.
Add the handler and the metadata populator (optional) for authentication
To be able to finish authenticating users in the CAS server after a remote authentication by an external provider, you have to add the ClientAuthenticationHandler
class and might add the ClientAuthenticationMetaDataPopulator
class (to track the provider) in the deployerConfigContext.xml
file:
By default, the identifier returned by a delegated authentication is composed of the profile name and the technical identifier of the provider, like FacebookProfile#1234
, to ensure the identifier uniqueness. Though, you can remove this behaviour and only return the technical identifier by using:
Add links on the login page to authenticate on remote providers
To start authentication on a remote provider, these links must be added on the login page casLoginView.jsp
(ClientNameUrl attributes are automatically created by the ClientAction
):
Demo
Take a look at this demo: cas-pac4j-oauth-demo to see this authentication delegation mechanism in action.