PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/com.ebmwebsourcing.petals.studio/src/com/ebmwebsourcing/petals/studio/welcome/RegistrationManager.java

https://github.com/petalslink/petals-studio
Java | 338 lines | 194 code | 79 blank | 65 comment | 41 complexity | 099e81f6b1fcba3676b2971e31f34fde MD5 | raw file
  1. /******************************************************************************
  2. * Copyright (c) 2010-2013, Linagora
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution, and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Linagora - initial API and implementation
  11. *******************************************************************************/
  12. package com.ebmwebsourcing.petals.studio.welcome;
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.security.GeneralSecurityException;
  18. import java.security.Key;
  19. import java.util.Properties;
  20. import javax.crypto.Cipher;
  21. import javax.crypto.spec.SecretKeySpec;
  22. import org.eclipse.core.runtime.IStatus;
  23. import com.ebmwebsourcing.petals.studio.PetalsStudioPlugin;
  24. import com.ebmwebsourcing.petals.studio.utils.Base64;
  25. import com.ebmwebsourcing.petals.studio.utils.VersionUtils;
  26. /**
  27. * @author Vincent Zurczak - EBM WebSourcing
  28. */
  29. public final class RegistrationManager {
  30. private final static String EMAIL = "petals.studio.email";
  31. private final static String COMPANY = "petals.studio.company";
  32. private final static String NAME = "petals.studio.name";
  33. private final static String PHONE = "petals.studio.phone";
  34. private final static String LANG = "petals.studio.language";
  35. private final static String REGISTERED = "petals.studio.done";
  36. private final static String LAST_STUDIO_VERSION = "petals.studio.version";
  37. private final static String JOKER = "petals.studio.joker";
  38. private final static String PROXY_HOST = "petals.studio.proxy.host";
  39. private final static String PROXY_USER = "petals.studio.proxy.user";
  40. private final static String PROXY_PWD = "petals.studio.proxy.password";
  41. private final static String PROXY_PORT = "petals.studio.proxy.port";
  42. private final File registrationFile;
  43. private final Key secretKey;
  44. private final Cipher cipher;
  45. private RegistrationBean backupRegistrationBean;
  46. private static RegistrationManager instance;
  47. /**
  48. * Constructor.
  49. * @throws IOException if the registration file does not exist and could not be created
  50. * @throws GeneralSecurityException
  51. */
  52. private RegistrationManager() throws IOException, GeneralSecurityException {
  53. // Get or create the backup file
  54. this.registrationFile = new File( System.getProperty( "user.home" ), ".PetalsStudio.backup" );
  55. if( ! this.registrationFile.exists() && ! this.registrationFile.createNewFile()) {
  56. IOException e = new IOException( "Could not create the registration file." );
  57. PetalsStudioPlugin.log( e, IStatus.ERROR, "Could not create the registration file." );
  58. throw e;
  59. }
  60. // Get the key (always the same, we do not store critical data).
  61. // Plus, they are stored on the user's machine.
  62. // PBEKeySpec keySpec = new PBEKeySpec( "BlabluBloBlik".toCharArray());
  63. // SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( "PBKDF2WithHmacSHA1" );
  64. // this.secretKey = keyFactory.generateSecret( keySpec );
  65. this.secretKey = new SecretKeySpec( "BlabluBloBlik".getBytes(), "Blowfish" );
  66. // Get the cipher
  67. this.cipher = Cipher.getInstance( "Blowfish" );
  68. }
  69. /**
  70. * Backup registration data.
  71. * @param bean a registration bean
  72. * @return true if the backup succeeded, false otherwise
  73. */
  74. public boolean backupRegistrationData( RegistrationBean bean ) {
  75. boolean registered = false;
  76. try {
  77. // Prepare the elements to serialize
  78. Properties properties = new Properties();
  79. properties.setProperty( NAME, encode( bean.getName()));
  80. properties.setProperty( EMAIL, encode( bean.getEmail()));
  81. properties.setProperty( COMPANY, encode( bean.getCompany()));
  82. properties.setProperty( PHONE, encode( bean.getPhone()));
  83. properties.setProperty( LANG, encode( bean.getLanguage()));
  84. properties.setProperty( REGISTERED, encode( String.valueOf( bean.isRegistered())));
  85. properties.setProperty( LAST_STUDIO_VERSION, encode( bean.getLastRegisteredVersion()));
  86. if( bean.getProxyHost() != null )
  87. properties.setProperty( PROXY_HOST, encode( bean.getProxyHost()));
  88. if( bean.getProxyPassword() != null )
  89. properties.setProperty( PROXY_PWD, encode( bean.getProxyPassword()));
  90. if( bean.getProxyUser() != null )
  91. properties.setProperty( PROXY_USER, encode( bean.getProxyUser()));
  92. if( bean.getProxyPort() != null )
  93. properties.setProperty( PROXY_PORT, encode( String.valueOf( bean.getProxyPort())));
  94. // Serialize the elements
  95. FileOutputStream os = null;
  96. try {
  97. os = new FileOutputStream( this.registrationFile );
  98. properties.store( os, "Written by Petals Studio" );
  99. registered = true;
  100. this.backupRegistrationBean = bean;
  101. } catch( Exception e ) {
  102. PetalsStudioPlugin.log( e, IStatus.WARNING );
  103. } finally {
  104. try {
  105. if( os != null )
  106. os.close();
  107. } catch( IOException e ) {
  108. PetalsStudioPlugin.log( e, IStatus.ERROR );
  109. }
  110. }
  111. } catch( Exception e ) {
  112. PetalsStudioPlugin.log( e, IStatus.ERROR );
  113. }
  114. return registered;
  115. }
  116. /**
  117. * Restores registration data from the backup file.
  118. * @return a registration bean (never null)
  119. */
  120. public RegistrationBean restoreRegistrationData() {
  121. if( this.backupRegistrationBean == null ) {
  122. // Get the elements to read
  123. Properties properties = new Properties();
  124. FileInputStream is = null;
  125. try {
  126. is = new FileInputStream( this.registrationFile );
  127. properties.load( is );
  128. } catch( Exception e ) {
  129. PetalsStudioPlugin.log( e, IStatus.WARNING );
  130. } finally {
  131. try {
  132. if( is != null )
  133. is.close();
  134. } catch( IOException e ) {
  135. PetalsStudioPlugin.log( e, IStatus.ERROR );
  136. }
  137. }
  138. // Fill-in the registration bean
  139. try {
  140. this.backupRegistrationBean = new RegistrationBean();
  141. String name = properties.getProperty( NAME );
  142. this.backupRegistrationBean.setName( decode( name ));
  143. String email = properties.getProperty( EMAIL );
  144. this.backupRegistrationBean.setEmail( decode( email ));
  145. String company = properties.getProperty( COMPANY );
  146. this.backupRegistrationBean.setCompany( decode( company ));
  147. String phone = properties.getProperty( PHONE );
  148. this.backupRegistrationBean.setPhone( decode( phone ));
  149. String lang = properties.getProperty( LANG );
  150. this.backupRegistrationBean.setLanguage( decode( lang ));
  151. String registeredAS = properties.getProperty( REGISTERED );
  152. registeredAS = decode( registeredAS );
  153. boolean registered = Boolean.valueOf( registeredAS );
  154. this.backupRegistrationBean.setRegistered( registered );
  155. String jokerAS = properties.getProperty( JOKER );
  156. jokerAS = decode( jokerAS );
  157. boolean joker = Boolean.valueOf( jokerAS );
  158. this.backupRegistrationBean.setJoker( joker );
  159. String lastVersion = properties.getProperty( LAST_STUDIO_VERSION );
  160. this.backupRegistrationBean.setLastRegisteredVersion( decode( lastVersion ));
  161. String proxyHost = properties.getProperty( PROXY_HOST );
  162. if( proxyHost != null )
  163. this.backupRegistrationBean.setProxyHost( decode( proxyHost ));
  164. String proxyUser = properties.getProperty( PROXY_USER );
  165. if( proxyUser != null )
  166. this.backupRegistrationBean.setProxyUser( decode( proxyUser ));
  167. String proxyPwd = properties.getProperty( PROXY_PWD );
  168. if( proxyPwd != null )
  169. this.backupRegistrationBean.setProxyPassword( decode( proxyPwd ));
  170. String proxyPort = properties.getProperty( PROXY_PORT );
  171. if( proxyPort != null ) {
  172. String value = decode( proxyPort );
  173. int port = Integer.valueOf( value );
  174. this.backupRegistrationBean.setProxyPort( port );
  175. }
  176. } catch( Exception e ) {
  177. PetalsStudioPlugin.log( e, IStatus.ERROR );
  178. }
  179. }
  180. return this.backupRegistrationBean;
  181. }
  182. /**
  183. * Encodes user information using the Blowfish encryption algorithm and a Base64.
  184. * @param s the string to encode
  185. * @return the encoded string
  186. * @throws Exception if something went wrong
  187. */
  188. public String encode( String s ) throws Exception {
  189. String result = "";
  190. if( s != null && s.trim().length() > 0 ) {
  191. this.cipher.init( Cipher.ENCRYPT_MODE, this.secretKey );
  192. byte[] encrypted = this.cipher.doFinal( s.getBytes());
  193. encrypted = Base64.encode( encrypted );
  194. result = new String( encrypted );
  195. }
  196. return result;
  197. }
  198. /**
  199. * Decodes user information using the Blowfish encryption algorithm and a Base64.
  200. * @param s the string to decode
  201. * @return the decoded string
  202. * @throws Exception if something went wrong
  203. */
  204. private String decode( String s ) throws Exception {
  205. String result = "";
  206. if( s != null && s.trim().length() > 0 ) {
  207. byte[] decrypted = Base64.decode( s.getBytes());
  208. this.cipher.init( Cipher.DECRYPT_MODE, this.secretKey );
  209. decrypted = this.cipher.doFinal( decrypted );
  210. result = new String( decrypted );
  211. }
  212. return result;
  213. }
  214. /**
  215. * @return the unique instance of this class
  216. * @throws IOException
  217. * @throws GeneralSecurityException
  218. */
  219. public synchronized static RegistrationManager getInstance()
  220. throws IOException, GeneralSecurityException {
  221. if( instance == null )
  222. instance = new RegistrationManager();
  223. return instance;
  224. }
  225. /**
  226. * Determine whether the registration process is required.
  227. * @return true if the registration is required, false otherwise
  228. */
  229. public static boolean needsRegistration() {
  230. boolean needsRegistration = true;
  231. try {
  232. // Get the saved data
  233. RegistrationManager mng = getInstance();
  234. RegistrationBean bean = mng.restoreRegistrationData();
  235. // Check the registration status and the last registered version
  236. String registeredVersion = VersionUtils.getTwoDigitVersion( bean.getLastRegisteredVersion());
  237. String currentVersion = VersionUtils.getProductVersion( false );
  238. // Dev mode
  239. if( VersionUtils.DEV_VERSION.equals( VersionUtils.getProductVersion( true )))
  240. needsRegistration = false;
  241. // Consultants
  242. else if( bean.isJoker())
  243. needsRegistration = false;
  244. // "1.0".compareTo( "1.1" ) returns a negative number
  245. else if( registeredVersion.trim().length() == 0 // Not registered
  246. || registeredVersion.compareTo( currentVersion ) < 0 ) // Former version
  247. needsRegistration = true;
  248. else if( ! bean.isRegistered())
  249. needsRegistration = true;
  250. else
  251. needsRegistration = false;
  252. } catch( IOException e ) {
  253. PetalsStudioPlugin.log( e, IStatus.WARNING, "It could not be determined if the registration process was required. (IO)" );
  254. } catch( GeneralSecurityException e ) {
  255. PetalsStudioPlugin.log( e, IStatus.WARNING, "It could not be determined if the registration process was required. (Security)" );
  256. }
  257. return needsRegistration;
  258. }
  259. }