package com.onelogin;

import java.io.ByteArrayInputStream;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;

import org.apache.commons.codec.binary.Base64;


public class AccountSettings {
	private String certificate;
	private Certificate idp_cert;
	private String idp_sso_target_url;
	
	public String getCertificate() {
		return certificate;
	}
	public void setCertificate(String certificate) {
		this.certificate = certificate;
	}
	public String getIdp_sso_target_url() {
		return idp_sso_target_url;
	}
	public void setIdpSsoTargetUrl(String idp_sso_target_url) {
		this.idp_sso_target_url = idp_sso_target_url;
	}
	
	/**
	 * Loads certificate from a base64 encoded string
	 * @param certificate an base64 encoded string.
	 */
 	public void loadCertificate(String certificate) throws CertificateException {
		CertificateFactory fty = CertificateFactory.getInstance("X.509");
		ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decodeBase64(certificate.getBytes()));
		this.idp_cert = fty.generateCertificate(bais);
	}
	

	public Certificate getIdpCert() throws CertificateException {
		if(this.idp_cert == null){
			loadCertificate(this.certificate);
		}
		return this.idp_cert;
	}
	
	/**
	 * load and get a certificate from a encoded base64 byte array.
	 * @param certificate an encoded base64 byte array.
	 * @throws CertificateException In case it can't load the certificate.
	 */
	public Certificate getCert(byte[] certificate) throws CertificateException {
		CertificateFactory fty = CertificateFactory.getInstance("X.509");
		ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decodeBase64(certificate));
		idp_cert = fty.generateCertificate(bais);
		return idp_cert;
	}
}

@@ -894,76 +894,355 @@
 %7B%0A%09%09
-CertificateFactory fty = CertificateFactory.getInstance(%22X.509%22);%0A%09%09
+loadCertificate(certificate, false);%0A%09%7D%0A%09%0A%09public void loadCertificate(String certificate, boolean isBase64) throws CertificateException %7B%0A        CertificateFactory fty = CertificateFactory.getInstance(%22X.509%22);%0A        byte%5B%5D cert = certificate.getBytes();%0A        if (isBase64) %7B%0A            cert = Base64.decodeBase64(cert);%0A        %7D%0A        
 Byte
@@ -1294,56 +1294,23 @@
 eam(
-Base64.decodeBase64(certificate.getBytes()));%0A%09%09
+cert);%0A        
 this
@@ -1352,21 +1352,26 @@
 (bais);%0A
-%09%7D%0A%09%0A
+    %7D%0A    
 %0A%09public

allow load certificate by base 64