use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.
the class SingleUseTOTPBindRequest method decodeSASLCredentials.
/**
* Creates a new single-use TOTP bind request from the information contained
* in the provided encoded SASL credentials.
*
* @param saslCredentials The encoded SASL credentials to be decoded in
* order to create this single-use TOTP bind request.
* It must not be {@code null}.
* @param controls The set of controls to include in the bind
* request.
*
* @return The single-use TOTP bind request decoded from the provided
* credentials.
*
* @throws LDAPException If the provided credentials are not valid for an
* UNBOUNDID-TOTP bind request.
*/
@NotNull()
public static SingleUseTOTPBindRequest decodeSASLCredentials(@NotNull final ASN1OctetString saslCredentials, @Nullable final Control... controls) throws LDAPException {
try {
String authenticationID = null;
String authorizationID = null;
String totpPassword = null;
ASN1OctetString staticPassword = null;
final ASN1Sequence s = ASN1Sequence.decodeAsSequence(saslCredentials.getValue());
for (final ASN1Element e : s.elements()) {
switch(e.getType()) {
case TYPE_AUTHENTICATION_ID:
authenticationID = e.decodeAsOctetString().stringValue();
break;
case TYPE_AUTHORIZATION_ID:
authorizationID = e.decodeAsOctetString().stringValue();
break;
case TYPE_TOTP_PASSWORD:
totpPassword = e.decodeAsOctetString().stringValue();
break;
case TYPE_STATIC_PASSWORD:
staticPassword = e.decodeAsOctetString();
break;
default:
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_SINGLE_USE_TOTP_DECODE_INVALID_ELEMENT_TYPE.get(StaticUtils.toHex(e.getType())));
}
}
if (authenticationID == null) {
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_SINGLE_USE_TOTP_DECODE_MISSING_AUTHN_ID.get());
}
if (totpPassword == null) {
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_SINGLE_USE_TOTP_DECODE_MISSING_TOTP_PW.get());
}
return new SingleUseTOTPBindRequest(authenticationID, authorizationID, totpPassword, staticPassword, controls);
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_SINGLE_USE_TOTP_DECODE_ERROR.get(StaticUtils.getExceptionMessage(e)), e);
}
}
use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.
the class UnboundIDDeliveredOTPBindRequest method decodeSASLCredentials.
/**
* Creates a new delivered one-time password bind request from the information
* contained in the provided encoded SASL credentials.
*
* @param saslCredentials The encoded SASL credentials to be decoded in
* order to create this delivered one-time password
* bind request. It must not be {@code null}.
* @param controls The set of controls to include in the bind
* request. It may be {@code null} or empty if no
* controls should be included.
*
* @return The delivered one-time password bind request decoded from the
* provided credentials.
*
* @throws LDAPException If the provided credentials are not valid for an
* UNBOUNDID-DELIVERED-OTP bind request.
*/
@NotNull()
public static UnboundIDDeliveredOTPBindRequest decodeSASLCredentials(@NotNull final ASN1OctetString saslCredentials, @Nullable final Control... controls) throws LDAPException {
String authenticationID = null;
String authorizationID = null;
String oneTimePassword = null;
try {
final ASN1Sequence s = ASN1Sequence.decodeAsSequence(saslCredentials.getValue());
for (final ASN1Element e : s.elements()) {
switch(e.getType()) {
case TYPE_AUTHENTICATION_ID:
authenticationID = e.decodeAsOctetString().stringValue();
break;
case TYPE_AUTHORIZATION_ID:
authorizationID = e.decodeAsOctetString().stringValue();
break;
case TYPE_OTP:
oneTimePassword = e.decodeAsOctetString().stringValue();
break;
default:
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_DOTP_DECODE_INVALID_ELEMENT_TYPE.get(StaticUtils.toHex(e.getType())));
}
}
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_DOTP_DECODE_ERROR.get(StaticUtils.getExceptionMessage(e)), e);
}
if (authenticationID == null) {
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_DOTP_DECODE_MISSING_AUTHN_ID.get());
}
if (oneTimePassword == null) {
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_DOTP_DECODE_MISSING_OTP.get());
}
return new UnboundIDDeliveredOTPBindRequest(authenticationID, authorizationID, oneTimePassword, controls);
}
use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.
the class UnboundIDExternallyProcessedAuthenticationBindRequest method getEncodedCredentials.
/**
* Retrieves an encoded representation of the SASL credentials for this bind
* request.
*
* @return An encoded representation of the SASL credentials for this bind
* request.
*/
@NotNull()
public ASN1OctetString getEncodedCredentials() {
if (encodedCredentials == null) {
final ArrayList<ASN1Element> credElements = new ArrayList<>(8);
credElements.add(new ASN1OctetString(TYPE_AUTHENTICATION_ID, authenticationID));
credElements.add(new ASN1OctetString(TYPE_EXTERNAL_MECHANISM_NAME, externalMechanismName));
credElements.add(new ASN1Boolean(TYPE_EXTERNAL_AUTH_WAS_SUCCESSFUL, externalAuthWasSuccessful));
if (externalAuthFailureReason != null) {
credElements.add(new ASN1OctetString(TYPE_EXTERNAL_AUTH_FAILURE_REASON, externalAuthFailureReason));
}
if (!externalAuthWasPasswordBased) {
credElements.add(new ASN1Boolean(TYPE_EXTERNAL_AUTH_WAS_PASSWORD_BASED, false));
}
if (externalAuthWasSecure) {
credElements.add(new ASN1Boolean(TYPE_EXTERNAL_AUTH_WAS_SECURE, true));
}
if (endClientIPAddress != null) {
credElements.add(new ASN1OctetString(TYPE_END_CLIENT_IP_ADDRESS, endClientIPAddress));
}
if (!additionalAccessLogProperties.isEmpty()) {
final ArrayList<ASN1Element> logElements = new ArrayList<>(additionalAccessLogProperties.size());
for (final Map.Entry<String, String> e : additionalAccessLogProperties.entrySet()) {
logElements.add(new ASN1Sequence(new ASN1OctetString(e.getKey()), new ASN1OctetString(e.getValue())));
}
credElements.add(new ASN1Sequence(TYPE_ADDITIONAL_ACCESS_LOG_PROPERTIES, logElements));
}
final ASN1Sequence credSequence = new ASN1Sequence(credElements);
encodedCredentials = new ASN1OctetString(credSequence.encode());
}
return encodedCredentials;
}
use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.
the class IntermediateClientResponseValue method decode.
/**
* Decodes the provided ASN.1 sequence as an intermediate client response
* value.
*
* @param sequence The sequence to be decoded as an intermediate client
* response value.
*
* @return The decoded intermediate client response value.
*
* @throws LDAPException If the provided sequence cannot be decoded as an
* intermediate client response value.
*/
@NotNull()
public static IntermediateClientResponseValue decode(@NotNull final ASN1Sequence sequence) throws LDAPException {
Boolean upstreamServerSecure = null;
IntermediateClientResponseValue upstreamResponse = null;
String upstreamServerAddress = null;
String serverName = null;
String serverResponseID = null;
String serverSessionID = null;
for (final ASN1Element element : sequence.elements()) {
switch(element.getType()) {
case TYPE_UPSTREAM_RESPONSE:
try {
final ASN1Sequence s = ASN1Sequence.decodeAsSequence(element);
upstreamResponse = decode(s);
} catch (final LDAPException le) {
Debug.debugException(le);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_ICRESP_CANNOT_DECODE_UPSTREAM_RESPONSE.get(le.getMessage()), le);
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_ICRESP_CANNOT_DECODE_UPSTREAM_RESPONSE.get(StaticUtils.getExceptionMessage(e)), e);
}
break;
case TYPE_UPSTREAM_SERVER_ADDRESS:
upstreamServerAddress = ASN1OctetString.decodeAsOctetString(element).stringValue();
break;
case TYPE_UPSTREAM_SERVER_SECURE:
try {
upstreamServerSecure = ASN1Boolean.decodeAsBoolean(element).booleanValue();
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_ICRESP_CANNOT_DECODE_UPSTREAM_SECURE.get(StaticUtils.getExceptionMessage(e)), e);
}
break;
case TYPE_SERVER_NAME:
serverName = ASN1OctetString.decodeAsOctetString(element).stringValue();
break;
case TYPE_SERVER_SESSION_ID:
serverSessionID = ASN1OctetString.decodeAsOctetString(element).stringValue();
break;
case TYPE_SERVER_RESPONSE_ID:
serverResponseID = ASN1OctetString.decodeAsOctetString(element).stringValue();
break;
default:
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_ICRESP_INVALID_ELEMENT_TYPE.get(StaticUtils.toHex(element.getType())));
}
}
return new IntermediateClientResponseValue(upstreamResponse, upstreamServerAddress, upstreamServerSecure, serverName, serverSessionID, serverResponseID);
}
use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.
the class JoinResultControl method encodeValue.
/**
* Encodes the provided information as appropriate for use as the value of
* this control.
*
* @param resultCode The result code for the join processing. It
* must not be {@code null}.
* @param diagnosticMessage A message with additional information about the
* result of the join processing. It may be
* {@code null} if no message is needed.
* @param matchedDN The matched DN for the join processing. It may
* be {@code null} if no matched DN is needed.
* @param referralURLs The set of referral URLs for any referrals
* encountered while processing the join. It may
* be {@code null} or empty if no referral URLs
* are needed.
* @param joinResults The set of entries that have been joined with
* associated search result entry. It may be
* {@code null} or empty if no entries were joined
* with the search result entry.
*
* @return An ASN.1 element containing an encoded representation of the
* value for this control.
*/
@NotNull()
private static ASN1OctetString encodeValue(@NotNull final ResultCode resultCode, @Nullable final String diagnosticMessage, @Nullable final String matchedDN, @Nullable final List<String> referralURLs, @Nullable final List<JoinedEntry> joinResults) {
Validator.ensureNotNull(resultCode);
final ArrayList<ASN1Element> elements = new ArrayList<>(5);
elements.add(new ASN1Enumerated(resultCode.intValue()));
if (matchedDN == null) {
elements.add(new ASN1OctetString());
} else {
elements.add(new ASN1OctetString(matchedDN));
}
if (diagnosticMessage == null) {
elements.add(new ASN1OctetString());
} else {
elements.add(new ASN1OctetString(diagnosticMessage));
}
if ((referralURLs != null) && (!referralURLs.isEmpty())) {
final ArrayList<ASN1Element> refElements = new ArrayList<>(referralURLs.size());
for (final String s : referralURLs) {
refElements.add(new ASN1OctetString(s));
}
elements.add(new ASN1Sequence(TYPE_REFERRAL_URLS, refElements));
}
if ((joinResults == null) || joinResults.isEmpty()) {
elements.add(new ASN1Sequence(TYPE_JOIN_RESULTS));
} else {
final ArrayList<ASN1Element> entryElements = new ArrayList<>(joinResults.size());
for (final JoinedEntry e : joinResults) {
entryElements.add(e.encode());
}
elements.add(new ASN1Sequence(TYPE_JOIN_RESULTS, entryElements));
}
return new ASN1OctetString(new ASN1Sequence(elements).encode());
}
Aggregations