/*
 * oxTrust is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.gluu.oxtrust.model;

import java.io.Serializable;
import java.util.List;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import lombok.Data;
import lombok.EqualsAndHashCode;

import org.gluu.site.ldap.persistence.annotation.LdapAttribute;
import org.gluu.site.ldap.persistence.annotation.LdapEntry;
import org.gluu.site.ldap.persistence.annotation.LdapObjectClass;
import org.jboss.seam.annotations.In;
import org.xdi.config.CryptoConfigurationFile;
import org.xdi.ldap.model.Entry;
import org.xdi.oxauth.model.common.ResponseType;
import org.xdi.util.security.StringEncrypter;
import org.xdi.util.security.StringEncrypter.EncryptionException;

/**
 * oxAuthClient
 * 
 * @author Reda Zerrad Date: 06.08.2012
 * @author Yuriy Movchan Date: 05/22/2013
 */
@LdapEntry(sortBy = { "displayName" })
@LdapObjectClass(values = { "top", "oxAuthClient" })
@EqualsAndHashCode(callSuper=false)
public @Data class OxAuthClient extends Entry implements Serializable {

	private static final long serialVersionUID = -2310140703735705346L;

	@LdapAttribute(ignoreDuringUpdate = true)
	private String inum;

	@LdapAttribute(ignoreDuringUpdate = true)
	private String iname;

	@NotNull
	@Size(min = 0, max = 60, message = "Length of the Display Name should not exceed 60")
	@LdapAttribute
	private String displayName;

	@NotNull
	@LdapAttribute(name = "oxAuthAppType")
	private OxAuthApplicationType oxAuthAppType;

	@LdapAttribute(name = "oxAuthRedirectURI")
	private List<String> oxAuthRedirectURIs;

	@LdapAttribute(name = "oxAuthPostLogoutRedirectURI")
	private List<String> oxAuthPostLogoutRedirectURIs;

	@LdapAttribute(name = "oxAuthScope")
	private List<String> oxAuthScopes;

	@NotNull
	@LdapAttribute(name = "oxAuthClientSecret")
	private String encodedClientSecret;

	@LdapAttribute(ignoreDuringUpdate = true)
	private String userPassword;

	@LdapAttribute(name = "oxAuthIdTokenSignedResponseAlg")
	private TokenResponseAlgs oxAuthIdTokenSignedResponseAlg;

	@LdapAttribute(name = "associatedPerson")
	private List<String> associatedPersons;

	@LdapAttribute(name = "oxAuthTrustedClient")
	private OxAuthTrustedClientBox oxAuthTrustedClient;

	@LdapAttribute(name = "oxAuthClientUserGroup")
	private List<String> oxAuthClientUserGroups;

	@LdapAttribute(name = "oxAuthResponseType")
	private ResponseType[] responseTypes;

    @LdapAttribute(name = "oxAuthTokenEndpointAuthMethod")
    private OxAuthAuthenticationMethod tokenEndpointAuthMethod;
	
    @LdapAttribute(name = "oxAuthPostLogoutRedirectURI")
    private String[] postLogoutRedirectUris;
    
	@In(value = "#{oxTrustConfiguration.cryptoConfiguration}")
	private CryptoConfigurationFile cryptoConfiguration;

	private String oxAuthClientSecret;

	public void setOxAuthClientSecret(String oxAuthClientSecret) throws EncryptionException {
		this.oxAuthClientSecret = oxAuthClientSecret;
		if (oxAuthClientSecret != null && oxAuthClientSecret.length() > 1) {
			setEncodedClientSecret(StringEncrypter.defaultInstance().encrypt(oxAuthClientSecret, cryptoConfiguration.getEncodeSalt()));
		}
	}

}

@@ -364,24 +364,77 @@
 dHashCode;%0A%0A
+import org.gluu.oxtrust.config.OxTrustConfiguration;%0A
 import org.g
@@ -615,93 +615,8 @@
 ss;%0A
-import org.jboss.seam.annotations.In;%0Aimport org.xdi.config.CryptoConfigurationFile;%0A
 impo
@@ -693,16 +693,50 @@
 seType;%0A
+import org.xdi.util.StringHelper;%0A
 import o
@@ -2742,127 +2742,8 @@
 ris;
-%0A    %0A%09@In(value = %22#%7BoxTrustConfiguration.cryptoConfiguration%7D%22)%0A%09private CryptoConfigurationFile cryptoConfiguration;
 %0A%0A%09p
@@ -2926,38 +2926,32 @@
 if (
-oxAuthClientSecret != null && 
+StringHelper.isNotEmpty(
 oxAu
@@ -2968,21 +2968,9 @@
 cret
-.length() %3E 1
+)
 ) %7B%0A
@@ -3057,17 +3057,52 @@
 Secret, 
-c
+OxTrustConfiguration.instance().getC
 ryptoCon
@@ -3111,16 +3111,18 @@
 guration
+()
 .getEnco

Fix encoding client password