Changeset 279


Ignore:
Timestamp:
12/02/11 14:11:54 (13 years ago)
Author:
vmipsl
Message:

Encrypt password

Location:
ether_megapoli/trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • ether_megapoli/trunk/common/implementation/com/ether/EtherHelper.java

    r218 r279  
    88import java.io.FileWriter; 
    99import java.io.IOException; 
     10import java.security.DigestException; 
     11import java.security.MessageDigest; 
     12import java.security.NoSuchAlgorithmException; 
    1013import java.util.ArrayList; 
    1114import java.util.List; 
     
    138141    } 
    139142 
     143 
     144    /** 
     145     * Returns the specified String as an encrypted hash 
     146     * 
     147     * @param stringToEncrypt 
     148     * @return 
     149     */ 
     150    @NotNull 
     151    public static byte[] encryptStringToHash( @NotNull final String stringToEncrypt ) 
     152            throws DigestException, NoSuchAlgorithmException 
     153    { 
     154        final byte[] input = stringToEncrypt.getBytes(); 
     155        final MessageDigest digest = MessageDigest.getInstance( "SHA-1" ); 
     156        digest.update( input, 0, input.length ); 
     157        final int hashLength = 20; // SHA-1 donne un hash de longueur 20 
     158        final byte[] hash = new byte[hashLength]; 
     159        digest.digest( hash, 0, hashLength ); 
     160        return hash; 
     161    } 
     162 
     163    /** 
     164     * Returns the specified bytes array hash as a String 
     165     * 
     166     * @param encryptedHash the hash as a bytes array 
     167     * @return the hash as a String 
     168     */ 
     169    @NotNull 
     170    public static String displayEncryptedHash( @NotNull final byte[] encryptedHash ) 
     171    { 
     172        final StringBuilder stringBuilder = new StringBuilder(); 
     173        for( final byte aHash : encryptedHash ) 
     174        { 
     175            final int v = aHash & 0xFF; 
     176            if( v < 16 ) 
     177                stringBuilder.append( '0' ); 
     178 
     179            stringBuilder.append( Integer.toString( v, 16 ) ); 
     180        } 
     181        return stringBuilder.toString(); 
     182    } 
     183 
     184    /** 
     185     * This method creates a encrypted password with SHA-1 hash 
     186     * 
     187     * @param password 
     188     * @return 
     189     */ 
     190    @NotNull 
     191    public static String encryptPassword( @NotNull final String password ) 
     192            throws DigestException, NoSuchAlgorithmException 
     193    { 
     194        final byte[] sHAPassword = EtherHelper.encryptStringToHash( password ); 
     195        return EtherHelper.displayEncryptedHash( sHAPassword ); 
     196    } 
     197 
    140198} 
  • ether_megapoli/trunk/service/test/com/ether/OtherTest.java

    r194 r279  
    44import org.junit.Assert; 
    55import org.junit.Test; 
    6  
     6import sun.misc.BASE64Decoder; 
     7import sun.misc.BASE64Encoder; 
     8 
     9import java.io.IOException; 
     10import java.security.DigestException; 
     11import java.security.NoSuchAlgorithmException; 
    712import java.text.DateFormat; 
    813import java.text.SimpleDateFormat; 
     
    172177    } 
    173178 
     179    @Test 
     180    public void testEcnryptSHA1() 
     181    { 
     182        try 
     183        { 
     184            final String password = "boum"; 
     185            final byte[] sHAPassword = EtherHelper.encryptStringToHash( password ); 
     186            final String rePassword = EtherHelper.displayEncryptedHash( sHAPassword ); 
     187 
     188            final BASE64Decoder decoder = new BASE64Decoder(); 
     189            final byte[] base64Password = decoder.decodeBuffer( password ); 
     190 
     191            final BASE64Encoder endecoder = new BASE64Encoder(); 
     192            final String base64DecodedPassword = endecoder.encode( base64Password ); 
     193 
     194            final String tot = "tit"; 
     195        } 
     196        catch( DigestException e ) 
     197        { 
     198            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates. 
     199        } 
     200        catch( NoSuchAlgorithmException e ) 
     201        { 
     202            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates. 
     203        } 
     204        catch( IOException e ) 
     205        { 
     206            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates. 
     207        } 
     208    } 
    174209} 
  • ether_megapoli/trunk/web/resources/jsp/dataProtocol_en.jsp

    r278 r279  
    99 
    1010<BR/> 
    11 <button onclick="javascript:onClickDownloadDataProtocol()" class="dataProtocolDownloadButton">Download</button> 
     11<a href="SendFileToClient.do?path=dataProtocol_en.pdf"><button class="dataProtocolDownloadButton">Download</button></a> 
    1212 
    1313<BR/><BR/> 
    1414<hr width="50%"> 
    1515 
    16 <div class="title1">Ask for account</div> 
     16<div class="title1">Ask for an account</div> 
    1717<BR/> 
    1818 
     
    3131    </tr> 
    3232    <tr> 
     33        <td><label for="password">Password</label></td> 
     34        <td><input id="password" name="password" type="text" size="30"></td> 
     35    </tr> 
     36    <tr> 
    3337        <td> 
    3438            <input id="checkboxUser" type="checkbox" class="dataProtocolCheckbox"> 
  • ether_megapoli/trunk/web/resources/jsp/dataProtocol_fr.jsp

    r278 r279  
    99 
    1010<BR/> 
    11 <button class="dataProtocolDownloadButton"><a href="/SendFileToClient.do?path=dataProtocol.pdf">Téléchargement</a></button> 
    12 <a href="SendFileToClient.do?path=dataProtocol_fr.pdf">Téléchargement</a> 
     11<a href="SendFileToClient.do?path=dataProtocol_fr.pdf"><button class="dataProtocolDownloadButton">Téléchargement</button></a> 
    1312 
    1413<BR/><BR/> 
     
    3231    </tr> 
    3332    <tr> 
     33        <td><label for="password">Mot de passe</label></td> 
     34        <td><input id="password" name="password" type="text" size="30"></td> 
     35    </tr> 
     36    <tr> 
    3437        <td> 
    3538            <input id="checkboxUser" type="checkbox" class="dataProtocolCheckbox"> 
  • ether_megapoli/trunk/web/resources/jsp/megapoliHead.jsp

    r278 r279  
    174174/** *********** ACCOUNT *********** **/ 
    175175/** ******************************* **/ 
    176 function onClickDownloadDataProtocol() 
    177 { 
    178     alert( "downLoad" ); 
    179 } 
    180  
    181176function onClickAcceptDataProtocol() 
    182177{ 
    183     if( '' == $( "#lastName" ).val() || '' == $( "#email" ).val() ) 
     178    if( '' == $( "#lastName" ).val() || '' == $( "#email" ).val() || '' == $( "#password" ).val() ) 
    184179    { 
    185180        showErrorAccount( null, templateTexts["app.dataProtocolFields"] ); 
     
    195190function createAccount() 
    196191{ 
    197     var parametersUrl = "name=" + $( "#lastName" ).val() + "&firstName=" + $( "#firstName" ).val() + "&email=" + $( "#email" ).val(); 
     192    var parametersUrl = "name=" + $( "#lastName" ).val() + "&firstName=" + $( "#firstName" ).val() + "&email=" + $( "#email" ).val() + "&pwd=" + $( "#password" ).val(); 
    198193    var request = $.ajax( { 
    199194        url: "project?methodName=createAccount&" + parametersUrl, 
  • ether_megapoli/trunk/web/src/ApplicationResources.properties

    r278 r279  
    443443app.infos=Informations 
    444444app.dataProtocolAccept=Vous devez accepter le protocole d'utilisation des donn\u00E9es pour demander un compte 
    445 app.dataProtocolFields=Les champs 'Nom' et 'Email' sont incomplets 
     445app.dataProtocolFields=Les champs 'Nom', 'Email' et 'Mot de passe' sont incomplets 
    446446app.dataProtocol.account=Votre compte est en attente de validation par l'administrateur. Vous recevrez un email lors de son activation. 
    447447app.dataProtocol.alreadyExist=Cette email est d\u00E9j\u00E0 utilis\u00E9 par un compte, veuillez en fournir un autre. 
  • ether_megapoli/trunk/web/src/ApplicationResources_en.properties

    r278 r279  
    442442app.infos=Informations 
    443443app.dataProtocolAccept=You have to accept the data protocol to ask for an account 
    444 app.dataProtocolFields=Fields 'LastName' and 'Email' must be filled 
     444app.dataProtocolFields=Fields 'LastName', 'Email' and 'Password' must be filled 
    445445app.dataProtocol.account=Your account is waiting for administrator validation. You will receive an email when it will be activated. 
    446446app.dataProtocol.alreadyExist=This email is already used, you have to give another one 
     
    535535bo.user.email=Email 
    536536bo.user.password=Password 
     537bo.user.password.same=Keep same password 
     538bo.user.password.same.help=(only to modify a password) 
    537539bo.user.role=Role 
    538540bo.user.creationDate=Creation date 
  • ether_megapoli/trunk/web/src/com/ether/ControllerBackoffice.java

    r278 r279  
    1616 
    1717import javax.mail.MessagingException; 
     18import java.security.DigestException; 
     19import java.security.NoSuchAlgorithmException; 
    1820import java.util.ArrayList; 
    1921import java.util.Date; 
     
    6971            throws WebException, ServiceException 
    7072    { 
    71         final User existingUser = _etherService.getUserByEmail( email ); 
    72         if( null == existingUser ) 
    73         { 
    74             final Date creationDate = new Date(); 
    75             final User user = new User( lastName, firstName, email, pwd, role, state, hasAccessToBO, creationDate ); 
    76  
    77             _etherService.createUser( user ); 
    78         } 
    79         else 
    80             throw new WebException( WebException.WebCode.USER_ALREADY_EXISTS, "This email already corresponds to an User", WebException.getExceptionThrowable() ); 
     73        try 
     74        { 
     75            final User existingUser = _etherService.getUserByEmail( email ); 
     76            if( null == existingUser ) 
     77            { 
     78                final Date creationDate = new Date(); 
     79                final String encryptedPassword = EtherHelper.encryptPassword( pwd ); 
     80                final User user = new User( lastName, firstName, email, encryptedPassword, role, state, hasAccessToBO, creationDate ); 
     81 
     82                _etherService.createUser( user ); 
     83            } 
     84            else 
     85                throw new WebException( WebException.WebCode.USER_ALREADY_EXISTS, "This email already corresponds to an User", WebException.getExceptionThrowable() ); 
     86 
     87        } 
     88        catch( DigestException e ) 
     89        { 
     90            throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); 
     91        } 
     92        catch( NoSuchAlgorithmException e ) 
     93        { 
     94            throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); 
     95        } 
    8196 
    8297        return getAllAndWaitingUsers(); 
     
    97112                                  @Nullable @ParamName(ParameterConstants.PARAMETER_FIRST_NAME) final String firstName, 
    98113                                  @NotNull @ParamName(ParameterConstants.PARAMETER_EMAIL) final String email, 
    99                                   @NotNull @ParamName(ParameterConstants.PARAMETER_PWD) final String pwd, 
     114                                  @Nullable @ParamName(ParameterConstants.PARAMETER_PWD) final String pwd, 
    100115                                  @Nullable @ParamName(ParameterConstants.PARAMETER_ROLE) final String role, 
    101116                                  @NotNull @ParamName(ParameterConstants.PARAMETER_STATE) final String state, 
    102                                   @NotNull @ParamName(ParameterConstants.PARAMETER_HAS_ACCESS) final Boolean hasAccessToBO ) 
    103             throws WebException, ServiceException 
    104     { 
    105         final User existingUser = _etherService.getUserByEmail( email ); 
    106         if( null == existingUser || userId.equals( existingUser.getId() ) ) 
    107         { 
    108             final User user = _etherService.getUserById( userId ); 
    109             user.setLastName( lastName ); 
    110             user.setFirstName( firstName ); 
    111             user.setEmail( email ); 
    112             user.setPassword( pwd ); 
    113             user.setRole( UserRole.valueOf( role ) ); 
    114             user.setState( UserState.valueOf( state ) ); 
    115             user.setAccessToBO( hasAccessToBO ); 
    116  
    117             _etherService.updateUser( user ); 
    118         } 
    119         else 
    120             throw new WebException( WebException.WebCode.USER_ALREADY_EXISTS, "This email already corresponds to an User", WebException.getExceptionThrowable() ); 
     117                                  @NotNull @ParamName(ParameterConstants.PARAMETER_HAS_ACCESS) final Boolean hasAccessToBO, 
     118                                  @NotNull @ParamName(ParameterConstants.PARAMETER_KEEP_SAME_PASSWORD) final Boolean keepSamePassword ) 
     119            throws WebException, ServiceException 
     120    { 
     121        try 
     122        { 
     123            final User existingUser = _etherService.getUserByEmail( email ); 
     124            if( null == existingUser || userId.equals( existingUser.getId() ) ) 
     125            { 
     126                final User user = _etherService.getUserById( userId ); 
     127                user.setLastName( lastName ); 
     128                user.setFirstName( firstName ); 
     129                user.setEmail( email ); 
     130                if( !keepSamePassword && null != pwd ) 
     131                { 
     132                    final String encryptedPassword = EtherHelper.encryptPassword( pwd ); 
     133                    user.setPassword( encryptedPassword ); 
     134                } 
     135                user.setRole( UserRole.valueOf( role ) ); 
     136                user.setState( UserState.valueOf( state ) ); 
     137                user.setAccessToBO( hasAccessToBO ); 
     138 
     139                _etherService.updateUser( user ); 
     140            } 
     141            else 
     142                throw new WebException( WebException.WebCode.USER_ALREADY_EXISTS, "This email already corresponds to an User", WebException.getExceptionThrowable() ); 
     143        } 
     144        catch( DigestException e ) 
     145        { 
     146            throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); 
     147        } 
     148        catch( NoSuchAlgorithmException e ) 
     149        { 
     150            throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); 
     151        } 
    121152 
    122153        return getAllAndWaitingUsers(); 
     
    167198        String content = "Dear user, \n\nThe Megapoli administrator has "; 
    168199        if( isAccepted ) 
    169         { 
    170             content += "accepted your inscription. You can now access to data with : \n"; 
    171             content += "   - login : " + user.getEmail() + '\n'; 
    172             content += "   - password : " + user.getPassword() + '\n'; 
    173         } 
     200            content += "accepted your inscription. You can now access to data with you login (" + user.getEmail() + ") and the password you gave during inscription.\n"; 
    174201        else 
    175202            content += "refused your inscription. You can reply to this email if you want more information.\n"; 
  • ether_megapoli/trunk/web/src/com/ether/ControllerEponge.java

    r278 r279  
    1919import javax.mail.MessagingException; 
    2020import javax.servlet.http.HttpServletRequest; 
     21import java.security.DigestException; 
     22import java.security.NoSuchAlgorithmException; 
    2123import java.util.Date; 
    2224import java.util.HashMap; 
     
    5052    /** *********************** CALLS ***************************** **/ 
    5153    /** *********************************************************** **/ 
    52     // TODO : crypter les login/pwd 
    5354    @ControllerMethod(requestMandatory = true, jsonResult = true) 
    5455    public JSONObject login( @ParamName(ParameterConstants.PARAMETER_LOGIN) final String login, 
     
    8788    } 
    8889 
    89  
    9090    @ControllerMethod(requestMandatory = true, jsonResult = true) 
    9191    public JSONObject logout( @NotNull final HttpServletRequest request ) 
     
    101101    public JSONObject createAccount( @NotNull @ParamName(ParameterConstants.PARAMETER_NAME) final String lastName, 
    102102                                     @Nullable @ParamName(ParameterConstants.PARAMETER_FIRST_NAME) final String firstName, 
    103                                      @NotNull @ParamName(ParameterConstants.PARAMETER_EMAIL) final String email ) 
     103                                     @NotNull @ParamName(ParameterConstants.PARAMETER_EMAIL) final String email, 
     104                                     @NotNull @ParamName(ParameterConstants.PARAMETER_PWD) final String password ) 
    104105            throws ServiceException, WebException 
    105106    { 
    106         final User existingUser = _etherService.getUserByEmail( email ); 
    107         if( null == existingUser ) 
     107        try 
    108108        { 
    109             final Date creationDate = new Date(); 
    110             // TODO : encrypt password 
    111             final String password = "boum"; 
    112             final User user = new User( lastName, firstName, email, password, UserRole.COORDINATOR, UserState.WAITING, false, creationDate ); 
     109            final User existingUser = _etherService.getUserByEmail( email ); 
     110            if( null == existingUser ) 
     111            { 
     112                final Date creationDate = new Date(); 
    113113 
    114             _etherService.createUser( user ); 
     114                final String encryptedPassword = EtherHelper.encryptPassword( password ); 
     115                final User user = new User( lastName, firstName, email, encryptedPassword, UserRole.COORDINATOR, UserState.WAITING, false, creationDate ); 
    115116 
    116             // Send email to administrator to inform there is a new account 
    117             sendEmailToAdministrator( user ); 
     117                _etherService.createUser( user ); 
     118 
     119                // Send email to administrator to inform there is a new account 
     120                sendEmailToAdministrator( user ); 
     121            } 
     122            else 
     123                throw new WebException( WebException.WebCode.USER_ALREADY_EXISTS, "This email already corresponds to an User", WebException.getExceptionThrowable() ); 
    118124        } 
    119         else 
    120             throw new WebException( WebException.WebCode.USER_ALREADY_EXISTS, "This email already corresponds to an User", WebException.getExceptionThrowable() ); 
     125        catch( DigestException e ) 
     126        { 
     127            throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); 
     128        } 
     129        catch( NoSuchAlgorithmException e ) 
     130        { 
     131            throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); 
     132        } 
    121133 
    122134        return new JSONObject(); 
     
    136148            final MailFactory mailFactory = (MailFactory) getServletContext().getAttribute( "APP_MAILFACTORY" ); 
    137149            final String from = (String) getServletContext().getAttribute( "APP_WEBMASTER" ); 
     150            final String toPI = (String) getServletContext().getAttribute( "APP_PI" ); 
    138151            final String subject = "[MEGAPOLI] Nouvelle demande de compte utilisateur"; 
    139152            final String content = "Hello Matthias,\n\nUne nouvelle demande de compte vient d'arriver.\n\n" + 
     
    144157                    "Bonne soirée,\nLe serveur masqué"; 
    145158 
    146             final Mail mail = new Mail( from, from, null, content, subject ); 
    147             mailFactory.sendMail( mail ); 
     159            final Mail mailAdministrator = new Mail( from, from, null, content, subject ); 
     160            mailFactory.sendMail( mailAdministrator ); 
     161            final Mail mailPI = new Mail( from, toPI, null, content, subject ); 
     162            mailFactory.sendMail( mailPI ); 
    148163        } 
    149164        catch( MessagingException e ) 
  • ether_megapoli/trunk/web/src/com/ether/WebException.java

    r278 r279  
    5656        PARAMETER_IS_NULL, 
    5757        USER_ALREADY_EXISTS, 
    58         ERROR_EMAIL_CANNOT_BE_SEND 
     58        ERROR_EMAIL_CANNOT_BE_SEND, 
     59        ERROR_ENCRYPT_PASSWORD 
    5960    } 
    6061 
Note: See TracChangeset for help on using the changeset viewer.