Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.91 KB | None | 0 0
  1. //YSA le 04/12/2017
  2. //Permets la génération de liens privés et temporaires vers l'espace donateur
  3. //Classe de test : ALS_Generate_Tmp_Link_Test
  4. public class ALS_Generate_Tmp_Link {
  5.    
  6.    /**
  7.      * Returns the temporary link
  8.      *
  9.      * @param list      listContact
  10.      *
  11.      * @return void  
  12.      */
  13.     public static void generateTmpLink(List<Contact> listContact){
  14.        
  15.         //Instance for custom settings TemporaryLink
  16.         Temporary_link__c cs = Temporary_link__c.getInstance();
  17.         //Get key
  18.         String secret = cs.Encrypt_Key__c;
  19.        
  20.         //Link
  21.         String temporaryLink;
  22.        
  23.         //get the number of seconds since january 1 1970
  24.         DateTime dt = Datetime.now();      
  25.         Long gettimeMs = dt.getTime();
  26.         Long gettimeS = gettimeMs / 1000;
  27.        
  28.         //Build the link
  29.         for(Contact ctc : listContact){
  30.             if( ! String.isBlank(ctc.Email)){
  31.  
  32.                 temporaryLink     = generateTmpLinkPure(ctc.Email , ctc.id, 1513177342, secret);              
  33.                 ctc.Lien_prive_et_temporaire__c = cs.URL__c + temporaryLink;
  34.             }            
  35.         }
  36.     }
  37.    
  38.     /**
  39.      * Generates a temporary link containing :
  40.      *    - the donor email
  41.      *    - the signature of the idSF (because it must not be public)
  42.      *    - the time : timestamp to calculate when the link must expire
  43.      *
  44.      * @param string    email Contact email
  45.      * @param string    idSf SalesForce Contact Id
  46.      * @param int       time time()
  47.      * @param string    secret 'secret' defined in the parameters
  48.      *
  49.      * @return string   Temporary link
  50.      */
  51.     public static String generateTmpLinkPure(String email, String idSf, Long timeNow, String secret){
  52.         System.debug('## generateTmpLinkPure email : '      + email);
  53.         System.debug('## generateTmpLinkPure idContact : '  + idSf);
  54.         System.debug('## generateTmpLinkPure timeNow : '    + timeNow);
  55.         System.debug('## generateTmpLinkPure secret : '     + secret);
  56.        
  57.         JSONGenerator gen = JSON.createGenerator(true);
  58.         gen.writeStartObject();
  59.         gen.writeStringField('email',email);
  60.         gen.writeStringField('sign', signature(idSf, secret));
  61.         gen.writeStringField('time', String.ValueOf(timeNow));     
  62.         String content = gen.getAsString();  
  63.         content = content.replace('\n','');
  64.         content = content.replace(' ','');
  65.         System.debug('## generateTmpLinkPure content : '+ content);
  66.        
  67.         String sSignContent = signContent(content, secret);
  68.         System.debug('## generateTmpLinkPure return : '+ sSignContent);
  69.        
  70.         return sSignContent;
  71.     }
  72.    
  73.     /**
  74.      * Signature the content with a random factor
  75.      *
  76.      * @param string    content
  77.      * @param string    secret
  78.      *
  79.      * @return string   The content in hex with signature (hex too) separated by a '-'
  80.      */
  81.     public static String signContent(string content, string secret){
  82.         System.debug('## signContent content : '+ content);
  83.         System.debug('## signContent secret : ' + secret);
  84.        
  85.         String content1     = base64UrlEncode(Blob.valueOf(content));
  86.         System.debug('## signContent content1 : ' + content1);
  87.        
  88.         String content2     = signature(content, secret);
  89.         System.debug('## signContent content2 : ' + content2);
  90.        
  91.         String sSignContent = content1 + '-' + content2;
  92.         System.debug('## signContent return : ' + sSignContent);
  93.        
  94.         return sSignContent;
  95.     }
  96.    
  97.     /**
  98.      * Generate signature from content
  99.      *
  100.      * @param string    content
  101.      * @param string    secret
  102.      *
  103.      * @return string   Signature of the content in base 64 URL compatible
  104.      */
  105.     public static String signature(string content, string secret){
  106.         System.debug('## signature content param : '+ content);
  107.         System.debug('## signature secret param : '+ secret);
  108.        
  109.         Blob blobSignature = crypto.generateMac('HmacSHA256', Blob.valueOf(content), Blob.valueOf(secret));
  110.         //String signature = EncodingUtil.convertToHex(crypto.generateMac('HmacSHA256', Blob.valueOf(content), Blob.valueOf(secret)));
  111.        
  112.         /*String signature = EncodingUtil.base64Encode(blobSignature);
  113.         System.debug('## signature SingnatureSHA256 : '+ EncodingUtil.base64Encode(blobSignature));*/
  114.        
  115.         String signatureBase64 = base64UrlEncode(blobSignature);
  116.        
  117.         System.debug('## signature return :'+ signatureBase64);
  118.        
  119.         return signatureBase64;
  120.     }
  121.    
  122.    
  123.     /**
  124.      * Custom Base 64 URL compatible encode. Replace '+' or '=' by '._ *'
  125.      *
  126.      * @param string  input
  127.      *
  128.      * @return string input base 64 url compatible
  129.      */
  130.     public static String base64UrlEncode(Blob blobInput){
  131.         System.debug('## base64UrlEncode blobInput param : '+ blobInput);
  132.        
  133.         String input = EncodingUtil.base64Encode(blobInput);        
  134.         System.debug('## base64UrlEncode input Encode : '+ input);
  135.        
  136.         input = input.replace('+','.');
  137.         input = input.replace('=','*');
  138.         input = input.replace('/','_');
  139.         System.debug('## base64UrlEncode return : '+ input);
  140.         return input;
  141.     }
  142.    
  143.     /**
  144.      * Custom Base 64 URL compatible decode
  145.      *
  146.      * @param string    input
  147.      *
  148.      * @return string   output decoded
  149.      */
  150.     public static String base64UrlDecode(String input){
  151.         System.debug('## base64UrlDecode input param : '+ input);
  152.        
  153.         input = input.replace('.','+');
  154.         input = input.replace('*','=');
  155.         input = input.replace('_','/');
  156.         System.debug('## input with replace : '+ input);
  157.        
  158.         Blob blobInput = EncodingUtil.base64Decode(input);
  159.         System.debug('## base64UrlDecode return :' + blobInput.toString());
  160.        
  161.         return blobInput.toString();
  162.     }
  163.    
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement