Interface Sasl2TaskContext
public interface Sasl2TaskContext
A view on one SASL2 negotiation, as seen by one
Sasl2TaskProvider (and by the Sasl2Task instances
that it creates).
An instance is scoped to a single SASL2 negotiation attempt on a single session. When a peer restarts the
negotiation by sending a new <authenticate/> element, all state represented by this context is discarded and
a new context is created.
Attribute storage
The attribute methods provide scratch space that survives for the duration of the negotiation. Attributes are scoped to the provider that the context was created for: two providers that use the same attribute name will not see each other's values. This is the intended place to record, for example, that the peer opted in to a task in its<authenticate/> element, so that the decision can be acted on later, when
Sasl2TaskProvider.getOfferedTasks(Sasl2TaskContext) is invoked.
Phases
A context is created when the peer's<authenticate/> element is processed, which is before SASL
authentication has completed. During that phase isAuthenticated() returns false and
getAuthorizationIdentity() returns null. Every other callback in this API is invoked only after
SASL authentication has succeeded, at which point the authenticated identity is available.
Threading
Instances are not thread-safe. They are intended to be used only from the callbacks that receive them. The manager guarantees that no two callbacks for the same negotiation run concurrently.- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionList<org.dom4j.Element>The stream feature elements that this provider contributed to the SASL2<authentication/>stream feature for this session, as recorded when those features were advertised.getAttribute(String name) Returns a previously stored attribute value.<T> Optional<T>getAttribute(String name, Class<T> type) Returns a previously stored attribute value, if it is of the expected type.The authorization identity (typically the username, without domain-part) that the peer authenticated as.The channel binding type that was negotiated, if the selected mechanism uses channel binding.The names of the tasks that have already completed successfully during this negotiation, in the order in which they completed.The names of the tasks that were offered to the peer in the most recent<continue/>element.intgetRound()The number of<continue/>elements that have been sent to the peer during this negotiation, including the one currently being assembled.The name of the SASL mechanism that the peer selected, in upper case (for exampleSCRAM-SHA-256-PLUS).The session that is performing the SASL2 negotiation.The user agent information (XEP-0388 § 2.2) that the peer supplied in its<authenticate/>element.booleanIndicates whether the SASL exchange itself has completed successfully.voidsetAttribute(String name, Object value) Stores an attribute value for the duration of this negotiation.
-
Method Details
-
getSession
The session that is performing the SASL2 negotiation. Note that this session is not (yet) authenticated: it does not have an authentication token, and its address is not yet the address of the authenticating user, even afterisAuthenticated()starts returningtrue. UsegetAuthorizationIdentity()to identify the user.- Returns:
- the session that is negotiating (never null).
-
isAuthenticated
boolean isAuthenticated()Indicates whether the SASL exchange itself has completed successfully. This returnsfalseonly duringSasl2TaskProvider.onAuthenticateReceived(Sasl2TaskContext, Element).- Returns:
- true if SASL authentication has succeeded, otherwise false.
-
getAuthorizationIdentity
The authorization identity (typically the username, without domain-part) that the peer authenticated as. Returnsnullwhen authentication has not yet completed, and also when the peer authenticated anonymously. Implementations that cannot operate on an anonymous session must check for null.- Returns:
- the authenticated username, or null.
-
getSaslMechanismName
The name of the SASL mechanism that the peer selected, in upper case (for exampleSCRAM-SHA-256-PLUS). This is relevant to eligibility decisions: a task that requires the peer to know the account password must not be offered after a token-based authentication such as XEP-0484 FAST.Caveat: while
isAuthenticated()is stillfalse(e.g. when read fromSasl2TaskProvider.onAuthenticateReceived(Sasl2TaskContext, Element)) this is the mechanism name the peer claimed in its<authenticate/>element, not yet verified in any way. It is overwritten with the SASL layer's confirmed value only once authentication succeeds. Do not base an authorization or eligibility decision on this value beforeisAuthenticated()returnstrue; it is safe to use for anything read-only at that stage, such as logging or picking which opt-in parameters to parse.- Returns:
- the SASL mechanism name (never null).
-
getChannelBindingType
The channel binding type that was negotiated, if the selected mechanism uses channel binding.- Returns:
- a channel binding type (such as
tls-exporter), or null if none was negotiated.
-
getUserAgentInfo
The user agent information (XEP-0388 § 2.2) that the peer supplied in its<authenticate/>element. This can be used to make a task conditional on a particular client, or to identify the device that a credential is being provisioned for. Note that user agent information is client-provided, and should not be trusted.- Returns:
- user agent information, or null if the peer did not supply any.
-
getCompletedTaskNames
The names of the tasks that have already completed successfully during this negotiation, in the order in which they completed. A provider is expected to consult this when deciding what to offer: the manager will refuse to offer a task name that has already completed, but a provider that offers several alternatives (such as two second-factor methods) is responsible for withdrawing the alternatives once one of them has been performed.- Returns:
- an immutable list of completed task names (never null, possibly empty).
-
getOfferedTaskNames
The names of the tasks that were offered to the peer in the most recent<continue/>element.- Returns:
- an immutable set of task names (never null, possibly empty).
-
getRound
int getRound()The number of<continue/>elements that have been sent to the peer during this negotiation, including the one currently being assembled. The first invocation ofSasl2TaskProvider.getOfferedTasks(Sasl2TaskContext)in a negotiation sees a value of 1.- Returns:
- the current round number.
-
getAdvertisedFeatureElements
The stream feature elements that this provider contributed to the SASL2<authentication/>stream feature for this session, as recorded when those features were advertised. This exists so that a provider can verify that a task the peer asks for was in fact offered to it, which prevents a peer from driving a negotiation that was never advertised. It mirrors the check that Openfire performs on SASL mechanism names (see OF-3273).- Returns:
- detached copies of the elements this provider advertised (never null, possibly empty).
-
getAttribute
Returns a previously stored attribute value.- Parameters:
name- the name of the attribute (cannot be null).- Returns:
- the stored value, or null if no value is stored under this name.
-
getAttribute
Returns a previously stored attribute value, if it is of the expected type.- Type Parameters:
T- the expected type of the value.- Parameters:
name- the name of the attribute (cannot be null).type- the expected type of the value (cannot be null).- Returns:
- the stored value, or an empty optional if absent or of another type.
-
setAttribute
Stores an attribute value for the duration of this negotiation.- Parameters:
name- the name of the attribute (cannot be null).value- the value to store. A null value removes any previously stored value.
-