All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.appslandia.plum.base.CredentialInvalidResult Maven / Gradle / Ivy

// The MIT License (MIT)
// Copyright © 2015 AppsLandia. All rights reserved.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.appslandia.plum.base;

import java.util.Set;

import javax.security.enterprise.CallerPrincipal;
import javax.security.enterprise.identitystore.CredentialValidationResult;

import com.appslandia.common.utils.AssertUtils;

/**
 *
 * @author Loc Ha
 *
 */
public class CredentialInvalidResult extends CredentialValidationResult {
	static final CallerPrincipal NULL_PRINCIPAL = new CallerPrincipal("null");

	final String invalidCode;
	final CredentialValidationResult result = CredentialValidationResult.INVALID_RESULT;

	private CredentialInvalidResult(String invalidCode) {
		// DONT WORK: super((String) null, null, null, null, null);
		super((String) null, NULL_PRINCIPAL, null, null, null);

		this.invalidCode = AssertUtils.assertNotNull(invalidCode);
	}

	@Override
	public Status getStatus() {
		return this.result.getStatus();
	}

	@Override
	public String getIdentityStoreId() {
		return this.result.getIdentityStoreId();
	}

	@Override
	public CallerPrincipal getCallerPrincipal() {
		return this.result.getCallerPrincipal();
	}

	@Override
	public String getCallerUniqueId() {
		return this.result.getCallerUniqueId();
	}

	@Override
	public String getCallerDn() {
		return this.result.getCallerDn();
	}

	@Override
	public Set getCallerGroups() {
		return this.result.getCallerGroups();
	}

	public String getInvalidCode() {
		return this.invalidCode;
	}

	public boolean matches(String invalidCode) {
		return this.invalidCode.equalsIgnoreCase(invalidCode);
	}

	public static final CredentialInvalidResult CREDENTIAL_INVALID = new CredentialInvalidResult("credential_invalid");
	public static final CredentialInvalidResult PASSWORD_INVALID = new CredentialInvalidResult("password_invalid");

	public static final CredentialInvalidResult CREDENTIAL_SUSPENDED = new CredentialInvalidResult("credential_suspended");
	public static final CredentialInvalidResult CREDENTIAL_NOT_ACTIVATED = new CredentialInvalidResult("credential_not_activated");
	public static final CredentialInvalidResult CREDENTIAL_NOT_APPROVED = new CredentialInvalidResult("credential_not_approved");

	public static final CredentialInvalidResult TOKEN_INVALID = new CredentialInvalidResult("token_invalid");
	public static final CredentialInvalidResult TOKEN_THEFTED = new CredentialInvalidResult("token_thefted");
	public static final CredentialInvalidResult TOKEN_EXPIRED = new CredentialInvalidResult("token_expired");

	public static CredentialInvalidResult valueOf(String invalidCode) {
		AssertUtils.assertNotNull(invalidCode);

		if (CREDENTIAL_INVALID.matches(invalidCode)) {
			return CREDENTIAL_INVALID;
		}
		if (PASSWORD_INVALID.matches(invalidCode)) {
			return PASSWORD_INVALID;
		}

		if (CREDENTIAL_SUSPENDED.matches(invalidCode)) {
			return CREDENTIAL_SUSPENDED;
		}
		if (CREDENTIAL_NOT_ACTIVATED.matches(invalidCode)) {
			return CREDENTIAL_NOT_ACTIVATED;
		}
		if (CREDENTIAL_NOT_APPROVED.matches(invalidCode)) {
			return CREDENTIAL_NOT_APPROVED;
		}
		if (TOKEN_INVALID.matches(invalidCode)) {
			return TOKEN_INVALID;
		}
		if (TOKEN_THEFTED.matches(invalidCode)) {
			return TOKEN_THEFTED;
		}
		if (TOKEN_EXPIRED.matches(invalidCode)) {
			return TOKEN_EXPIRED;
		}
		return new CredentialInvalidResult(invalidCode);
	}

	public static String getInvalidCode(CredentialValidationResult result) {
		AssertUtils.assertTrue(result.getStatus() != Status.VALID);

		return (result instanceof CredentialInvalidResult) ? ((CredentialInvalidResult) result).getInvalidCode() : CREDENTIAL_INVALID.getInvalidCode();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy