Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.此内容没有您所选择的语言版本。
9.3. Enabling Claims in the STS
Demonstration location 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The sample code in this section is taken from an STS system test. If you download and install the source distribution of Apache CXF, you can find the system test Java code under the following directory:
CXFInstallDir/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts
CXFInstallDir/services/sts/systests/advanced/src/test/java/org/apache/cxf/systest/sts
And the system test resource files under the following directory:
CXFInstallDir/services/sts/systests/advanced/src/test/resources/org/apache/cxf/systest/sts
CXFInstallDir/services/sts/systests/advanced/src/test/resources/org/apache/cxf/systest/sts
What is a claim? 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
A claim is an additional piece of data (for example, e-mail address, telephone number, and so on) about a principal, which can be included in a token along with the basic token data. Because this additional data is subject to signing, verification, and authentication, along with the rest of the token, the recipient can be confident that this data is true and accurate.
Requesting claims in an IssuedToken policy 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
If you want to issue a token with claims embedded, you can add a WS-Trust
Claims element to the RequestSecurityTokenTemplate part of the issued token policy, as follows:
By adding the
Claims element to the RequestSecurityTokenTemplate element, you ensure that the STS client includes the specified claims in the token issue request that is sent to the STS. The STS responds to this request by retrieving the relevant claim data for the principal and embedding it into the issued token.
Processing claims 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Figure 9.9, “Processing Claims” shows an overview of the steps that the STS performs to process claims received in an issue token request.
Figure 9.9. Processing Claims
Steps to process claims 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The STS processes claims as follows:
- One of the first things the
TokenIssueOperationmust do is to prepare for parsing the incoming request message.If aClaimsManagerobject is registered with theTokenIssueOperation, theTokenIssueOperationinvokesgetClaimsParserson theClaimsManagerinstance, to obtain the list of available claims parsers. - The
TokenIssueOperationinitiates parsing of the request message by invoking theparseRequestmethod on theRequestParserobject, passing the list ofClaimsParserobjects as one of the arguments toparseRequest. This ensures that theRequestParseris capable of parsing anyClaimselements that might appear in the request message. - If no claims parsers are configured on the claims manager (so that list of claims parsers is
null), theRequestParsertries theIdentityClaimsParserclaims parser by default. But theIdentityClaimsParseris applied to theClaimselement, only if theDialectattribute of theClaimselement is equal to the identity claims dialect URI. - After parsing the request message, the
TokenIssueOperationtries to find the appropriate token provider, by callingcanHandleTokenon each of the registered token providers. - In the current scenario, we assume that the client has requested the STS to issue a SAML token, so that the
SAMLTokenProvideris selected to issue the token. TheTokenIssueOperationinvokescreateTokenon theSAMLTokenProvider. - Before proceeding to issue the token, the
SAMLTokenProviderchecks whether handlers are available to process all of the non-optional claims. If the required claim handlers are not available, an exception is raised and the SAML token is not issued.For example, in the identity claims dialect, a claim can be tagged as non-optional by setting theOptionalattribute tofalseon aClaimsTypeelement in theIssuedTokenpolicy, as follows:Copy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantIn the identity claims dialect, all claims are required (that is, non-optional) by default. - When specifying the list of SAML attribute statement providers explicitly, it is good practice to include the
DefaultAttributeStatementProviderinstance in the list, so that the default token issuing behavior of theSAMLTokenProvideris preserved. - In this example, the
CustomAttributeStatementProviderencapsulates the code that embeds the requisite claim values into the issued SAML token. TheSAMLTokenProviderinvokes thegetStatementmethod to obtain the SAML attribute statements containing the required claim values. - The
CustomAttributeStatementProviderobtains the claim values for the current principal, by invoking theretrieveClaimValuesmethod on theClaimsManagerobject.For example, if the request message included claims for the principal's e-mail address and phone number, it is at this point that the STS actually retrieves the principal's e-mail address and phone number. - The
ClaimsManagerretrieves the claim values by iterating over all of the claims handlers, where each claims handler returns data for as many claims as it can.A claims handler implementation is effectively an intermediate layer between theClaimsManagerand a database. The database stores secure data about each user—such as, address, e-mail, telephone number, department, and so on—which can be used to populate claim values. For example, the database could be an LDAP server and Apache CXF provides anLdapClaimsHandlerclass for this scenario—see the section called “The LdapClaimsHandler”. - After retrieving all of the claim values, the
CustomAttributeStatementProviderproceeds to repackage the claim values as attribute statements, so that they can be embedded in the issued SAML token.
Claim dialects 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
In order to be as extensible and flexible as possible, the WS-Trust claims mechanism is designed to be pluggable and does not define the syntax of claims. That is, the contents of a WS-Trust
Claims element is left unspecified by WS-Trust.
The detailed syntax of claims can be defined in third-party specifications, by defining a claim dialect. The Claim element allows you to specify the claim dialect in the
Dialect attribute, as follows:
<t:Claims Dialect="DialectURI" xmlns:DialectPrefix="DialectURI">
...
</t:Claims>
<t:Claims Dialect="DialectURI" xmlns:DialectPrefix="DialectURI">
...
</t:Claims>
You can then use the specified dialect to specify claims inside the
Claims element.
For example, some of the claim dialects defined by the Oasis open standards foundation are as follows:
- Identity claim dialect—defines the kind of data that is typically associated with a user account (for example, address, e-mail, telephone number) and is specified by the Identity Metasystem Interoperability Specification.
- Common claim dialect—(not supported) defines data that is used in WS-Federation and is specified by the WS-Federation Specification. Apache CXF does not provide an implementation of this claims dialect, but you could plug in a custom implementation to the STS, if you wish.
- XSPA claim dialect—(not supported) defines a claim dialect that is used in Cross-Enterprise Security and Privacy Authorization XSPA Specification, which is a security standard used in the context of healthcare organizations. Apache CXF does not provide an implementation of this claims dialect, but you could plug in a custom implementation to the STS, if you wish.
Identity claim dialect 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The identity claim dialect is supported by default in Apache CXF. It enables you to request the kind of data fields that are typically stored under a user's LDAP account—for example, address details, telephone number, department, role, and so on. The identity claim dialect is associated with the following dialect URI:
http://schemas.xmlsoap.org/ws/2005/05/identity
http://schemas.xmlsoap.org/ws/2005/05/identity
You can specify identity claims using the following syntax:
The identity claim dialect defines a single element,
ic:ClaimType, which has the following attributes:
Uri- Specifies the type of claim value that you want to include in the issued token. For example, the ClaimTypeURI might identify an e-mail address claim value, a phone number claim value, and so on.
Optional- Specifies whether or not this particular claim is optional or not. Setting to
truemeans that the STS must be capable of populating the issued token with the claim value for the principal, otherwise the token cannot be issued. Default istrue.
Claim type URIs for the identity claim dialect 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The identity claim dialect supports the following claim type URIs:
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname- The subject's first name.
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname- The subject's surname.
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress- The subject's e-mail address.
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/streetaddress- The subject's street address.
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/locality- The subject's locality, which could be a city, county, or other geographic region.
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/stateorprovince- The subject's state or province.
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/postalcode- The subject's postal code.
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/country- The subject's country.
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/homephone- The subject's home phone number.
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/otherphone- The subject's secondary phone number (for example, at work).
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/mobilephone- The subject's mobile phone number.
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth- The subject's date of birth.
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/gender- The subject's gender.
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifier- The subject's Private Personal Identifier (PPID). The PPID is described in detail in the Identity Metasystem Interoperability Oasis standard.
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/webpage- The subject's Web page.
Claims parsers 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Because WS-Trust claims have a pluggable architecture, you need a pluggable architecture for parsing claims. The STS allows you to configure a list of claims parsers to customize support for claims. Typically, you register a claims parser for each claim dialect you want to support.
The IdentityClaimsParser 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
By default, the STS provides a single claims parser implementation: the identity claims parser,
org.apache.cxf.sts.claims.IdentityClaimsParser, which can parse the identity claim dialect.
You can optionally configure the identity claims parser explicitly, by registering it with the
ClaimsManager instance. But this is not strictly necessary, because the request parser automatically defaults to the identity claims parser, even if you have not explicitly configured it.
Implementing a custom claims parser 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
You can extend the claims parsing capability of the STS by implementing a custom claims parser. For this, you would define a custom Java class that implements the following Java interface:
Claims handlers 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The purpose of a claims handler is to retrieve the requested claim values for the specified principal. Typically, a claims handler is an intermediate layer that looks up claim values in persistent storage.
For example, suppose that an incoming request includes claims for an e-mail address and a phone number (the request claims). When the STS is ready to start populating the issued token with claim values, it calls on the registered claims handlers to retrieve the required claim values for the specified principal. If the principal is the user,
Alice, for example, the claims handler would contact a database to retrieve Alice's e-mail address and phone number.
The LdapClaimsHandler 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Apache CXF provides the claims handler,
org.apache.cxf.sts.claims.LdapClaimsHandler, which is capable of retrieving claim values from an LDAP server.
Implementing a custom claims handler 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
You can provide a custom claims handler by defining a class that implements the following Java interface:
Configuring the ClaimsManager 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The
ClaimsManager class encapsulates most of the functionality required to support claims and you must configure it if you want to support claims in the STS. In particular, the claims manager encapsulates a list of claims parsers and a list of claims handlers. In practice, if you are using just the identity claims dialect, there is no need to configure the list of claims parsers explicitly; it is sufficient to configure just the list of claims handlers.
For example, the following Spring XML fragment shows how to register a
ClaimsManager instance with the TokenIssueOperation bean, where the claims manager is initialized with a claims handler list containing one claims handler, CustomClaimsHandler.
The
CustomClaimsHandler class is a trivial implementation of a claims handler that appears in one of the STS system tests. For the purposes of the test, it returns a few fixed claim values for a couple of different principals.
Embedding claim values in a SAML token 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The key step in processing claims is the point where the STS attempts to issue the token. Whichever token provider is selected to issue the token, it must be capable of inserting the retrieved claim values into the issued token. The token provider must therefore be customized or extended, so that it is capable of embedding the claims in the issued token.
In the case of issuing SAML tokens, the appropriate mechanism for embedding claim values is to generate SAML attribute statements containing the claim values. The appropriate way to extend the SAML token provider, therefore, is to implement a custom
AttributeStatementProvider class and to register this class with the SAMLTokenProvider instance (see the section called “SAMLTokenProvider”).
Sample AttributeStatementProvider 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Example 9.4, “The CustomAttributeStatementProvider Class” shows a sample implementation of an
AttributeStatementProvider class, which is capable of embedding claim values in a SAML token. This sample implementation, CustomAttributeStatementProvider, is taken from the STS system tests, but it is generally quite useful as a starting point for a custom attribute statement provider implementation.
Example 9.4. The CustomAttributeStatementProvider Class
- 1
- The first part of the
getStatementmethod implementation is centered around the invocation of theClaimsManager.retrieveClaimValuesmethod.In preparation for invoking theretrieveClaimValuesmethod, you populate theClaimsParametersobject, which encapsulates most of the parameters needed to invokeretrieveClaimValues. TheClaimsParametersobject is initialized simply by copying the relevant parameters from theTokenProviderParametersobject. - 2
- Invoke the
retrieveClaimValuesmethod on the claims manager instance. This has the effect of retrieving the requested claim values from persistent storage, with the help of the claims handlers plug-ins (see Figure 9.9, “Processing Claims”). - 3
- The
AttributeStatementBeanclass is a WSS4J class that is used to encapsulate a SAML attribute statement. - 4
- The WSS4J
AttributeBeanclass encapsulates a single SAML attribute. - 5
- Each claim value is inserted into an
AttributeBeaninstance.
Configuring the custom AttributeStatementProvider 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The custom attribute statement provider can be installed into the
SAMLTokenProvider instance, as follows:
Note that a
DefaultAttributeStatementProvider instance should also be included in the list of attribute statement providers, so that the issued SAML token also includes the default attribute statement.