Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //YSA le 04/12/2017
- //Permets la génération de liens privés et temporaires vers l'espace donateur
- //Classe de test : ALS_Generate_Tmp_Link_Test
- public class ALS_Generate_Tmp_Link {
- /**
- * Returns the temporary link
- *
- * @param list listContact
- *
- * @return void
- */
- public static void generateTmpLink(List<Contact> listContact){
- //Instance for custom settings TemporaryLink
- Temporary_link__c cs = Temporary_link__c.getInstance();
- //Get key
- String secret = cs.Encrypt_Key__c;
- //Link
- String temporaryLink;
- //get the number of seconds since january 1 1970
- DateTime dt = Datetime.now();
- Long gettimeMs = dt.getTime();
- Long gettimeS = gettimeMs / 1000;
- //Build the link
- for(Contact ctc : listContact){
- if( ! String.isBlank(ctc.Email)){
- temporaryLink = generateTmpLinkPure(ctc.Email , ctc.id, 1513177342, secret);
- ctc.Lien_prive_et_temporaire__c = cs.URL__c + temporaryLink;
- }
- }
- }
- /**
- * Generates a temporary link containing :
- * - the donor email
- * - the signature of the idSF (because it must not be public)
- * - the time : timestamp to calculate when the link must expire
- *
- * @param string email Contact email
- * @param string idSf SalesForce Contact Id
- * @param int time time()
- * @param string secret 'secret' defined in the parameters
- *
- * @return string Temporary link
- */
- public static String generateTmpLinkPure(String email, String idSf, Long timeNow, String secret){
- System.debug('## generateTmpLinkPure email : ' + email);
- System.debug('## generateTmpLinkPure idContact : ' + idSf);
- System.debug('## generateTmpLinkPure timeNow : ' + timeNow);
- System.debug('## generateTmpLinkPure secret : ' + secret);
- JSONGenerator gen = JSON.createGenerator(true);
- gen.writeStartObject();
- gen.writeStringField('email',email);
- gen.writeStringField('sign', signature(idSf, secret));
- gen.writeStringField('time', String.ValueOf(timeNow));
- String content = gen.getAsString();
- content = content.replace('\n','');
- content = content.replace(' ','');
- System.debug('## generateTmpLinkPure content : '+ content);
- String sSignContent = signContent(content, secret);
- System.debug('## generateTmpLinkPure return : '+ sSignContent);
- return sSignContent;
- }
- /**
- * Signature the content with a random factor
- *
- * @param string content
- * @param string secret
- *
- * @return string The content in hex with signature (hex too) separated by a '-'
- */
- public static String signContent(string content, string secret){
- System.debug('## signContent content : '+ content);
- System.debug('## signContent secret : ' + secret);
- String content1 = base64UrlEncode(Blob.valueOf(content));
- System.debug('## signContent content1 : ' + content1);
- String content2 = signature(content, secret);
- System.debug('## signContent content2 : ' + content2);
- String sSignContent = content1 + '-' + content2;
- System.debug('## signContent return : ' + sSignContent);
- return sSignContent;
- }
- /**
- * Generate signature from content
- *
- * @param string content
- * @param string secret
- *
- * @return string Signature of the content in base 64 URL compatible
- */
- public static String signature(string content, string secret){
- System.debug('## signature content param : '+ content);
- System.debug('## signature secret param : '+ secret);
- Blob blobSignature = crypto.generateMac('HmacSHA256', Blob.valueOf(content), Blob.valueOf(secret));
- //String signature = EncodingUtil.convertToHex(crypto.generateMac('HmacSHA256', Blob.valueOf(content), Blob.valueOf(secret)));
- /*String signature = EncodingUtil.base64Encode(blobSignature);
- System.debug('## signature SingnatureSHA256 : '+ EncodingUtil.base64Encode(blobSignature));*/
- String signatureBase64 = base64UrlEncode(blobSignature);
- System.debug('## signature return :'+ signatureBase64);
- return signatureBase64;
- }
- /**
- * Custom Base 64 URL compatible encode. Replace '+' or '=' by '._ *'
- *
- * @param string input
- *
- * @return string input base 64 url compatible
- */
- public static String base64UrlEncode(Blob blobInput){
- System.debug('## base64UrlEncode blobInput param : '+ blobInput);
- String input = EncodingUtil.base64Encode(blobInput);
- System.debug('## base64UrlEncode input Encode : '+ input);
- input = input.replace('+','.');
- input = input.replace('=','*');
- input = input.replace('/','_');
- System.debug('## base64UrlEncode return : '+ input);
- return input;
- }
- /**
- * Custom Base 64 URL compatible decode
- *
- * @param string input
- *
- * @return string output decoded
- */
- public static String base64UrlDecode(String input){
- System.debug('## base64UrlDecode input param : '+ input);
- input = input.replace('.','+');
- input = input.replace('*','=');
- input = input.replace('_','/');
- System.debug('## input with replace : '+ input);
- Blob blobInput = EncodingUtil.base64Decode(input);
- System.debug('## base64UrlDecode return :' + blobInput.toString());
- return blobInput.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement