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

eu.unicore.xuudb.server.SecurityToken Maven / Gradle / Ivy

There is a newer version: 2.10.3
Show newest version
package eu.unicore.xuudb.server;

import java.security.cert.X509Certificate;

import eu.emi.security.authn.x509.impl.X500NameUtils;
import eu.unicore.xuudb.X509Utils;

/**
 *
 * this class is a wrapper for tokens in comparable DN format
 *
 * @author tweddell
 * @author schuller
 * @since 1.0.1
 */
public class SecurityToken {

	private final String token;

	/**
	 * create a new security token from the supplied PEM or DN
	 *
	 * @param rawToken - PEM or DN
	 */
	public SecurityToken(String rawToken) {
		this.token = convertToDN(rawToken);
	}

	protected String convertToDN(String source) {
		String dn = null;
		if (source.length() > 200) {
			dn = tryCertificate(source);
		}
		if (dn==null) {
			dn = X500NameUtils.getComparableForm(source);
		}
		return dn;
	}

	protected String tryCertificate(String source) {
		String dn = null;
		try {
			X509Certificate cert = X509Utils.getX509FromPEMString(source);
			dn = X500NameUtils.getComparableForm(cert.getSubjectX500Principal().getName());
		} catch (Exception ce) {}
		return dn;
	}

	@Override
	public String toString() {
		return this.token;
	}

	@Override
	public boolean equals(Object obj) {
		if(! (obj instanceof SecurityToken)) {
			return false;
		}
		return this.token.equalsIgnoreCase(obj.toString());
	}

	@Override
	public int hashCode() {
		return token.toLowerCase().hashCode()^0x36593265;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy