Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.83 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.         System.debug('## generateTmpLinkPure content : '+ content);
  64.        
  65.         String sSignContent = signContent(content, secret);
  66.         System.debug('## generateTmpLinkPure return : '+ sSignContent);
  67.        
  68.         return sSignContent;
  69.     }
  70.    
  71.     /**
  72.      * Signature the content with a random factor
  73.      *
  74.      * @param string    content
  75.      * @param string    secret
  76.      *
  77.      * @return string   The content in hex with signature (hex too) separated by a '-'
  78.      */
  79.     public static String signContent(string content, string secret){
  80.         System.debug('## signContent content : '+ content);
  81.         System.debug('## signContent secret : ' + secret);
  82.        
  83.         String content1     = base64UrlEncode(Blob.valueOf(content));
  84.         System.debug('## signContent content1 : ' + content1);
  85.        
  86.         String content2     = signature(content, secret);
  87.         System.debug('## signContent content2 : ' + content2);
  88.        
  89.         String sSignContent = content1 + '-' + content2;
  90.         System.debug('## signContent return : ' + sSignContent);
  91.        
  92.         return sSignContent;
  93.     }
  94.    
  95.     /**
  96.      * Generate signature from content
  97.      *
  98.      * @param string    content
  99.      * @param string    secret
  100.      *
  101.      * @return string   Signature of the content in base 64 URL compatible
  102.      */
  103.     public static String signature(string content, string secret){
  104.         System.debug('## signature content param : '+ content);
  105.         System.debug('## signature secret param : '+ secret);
  106.        
  107.         Blob blobSignature = crypto.generateMac('HmacSHA256', Blob.valueOf(content), Blob.valueOf(secret));
  108.         //String signature = EncodingUtil.convertToHex(crypto.generateMac('HmacSHA256', Blob.valueOf(content), Blob.valueOf(secret)));
  109.        
  110.         /*String signature = EncodingUtil.base64Encode(blobSignature);
  111.         System.debug('## signature SingnatureSHA256 : '+ EncodingUtil.base64Encode(blobSignature));*/
  112.        
  113.         String signatureBase64 = base64UrlEncode(blobSignature);
  114.        
  115.         System.debug('## signature return :'+ signatureBase64);
  116.        
  117.         return signatureBase64;
  118.     }
  119.    
  120.    
  121.     /**
  122.      * Custom Base 64 URL compatible encode. Replace '+' or '=' by '._ *'
  123.      *
  124.      * @param string  input
  125.      *
  126.      * @return string input base 64 url compatible
  127.      */
  128.     public static String base64UrlEncode(Blob blobInput){
  129.         System.debug('## base64UrlEncode blobInput param : '+ blobInput);
  130.        
  131.         String input = EncodingUtil.base64Encode(blobInput);        
  132.         System.debug('## base64UrlEncode input Encode : '+ input);
  133.        
  134.         input = input.replace('+','.');
  135.         input = input.replace('=','*');
  136.         input = input.replace('/','_');
  137.         System.debug('## base64UrlEncode return : '+ input);
  138.         return input;
  139.     }
  140.    
  141.     /**
  142.      * Custom Base 64 URL compatible decode
  143.      *
  144.      * @param string    input
  145.      *
  146.      * @return string   output decoded
  147.      */
  148.     public static String base64UrlDecode(String input){
  149.         System.debug('## base64UrlDecode input param : '+ input);
  150.        
  151.         input = input.replace('.','+');
  152.         input = input.replace('*','=');
  153.         input = input.replace('_','/');
  154.         System.debug('## input with replace : '+ input);
  155.        
  156.         Blob blobInput = EncodingUtil.base64Decode(input);
  157.         System.debug('## base64UrlDecode return :' + blobInput.toString());
  158.        
  159.         return blobInput.toString();
  160.     }
  161.    
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement