Last active
April 5, 2017 17:52
-
-
Save tstachl/692a4ffc2f6f1b10a6ff to your computer and use it in GitHub Desktop.
Apex version to generate a multipass token for Desk.com.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Multipass | |
{ | |
private static String SITE_KEY = 'site_key'; | |
private static String API_KEY = 'api_key'; | |
public static void generate() | |
{ | |
System.debug('== Generating =='); | |
System.debug(' Create the encryption key using a 16 byte SHA1 digest of your api key and subdomain'); | |
Blob key = Crypto.generateDigest('SHA1', Blob.valueOf(API_KEY + SITE_KEY)); | |
System.debug(' Cut to correct length'); | |
key = EncodingUtil.base64Decode(EncodingUtil.base64Encode(key).left(22)); | |
System.debug(' Build json data'); | |
String data = JSON.serialize(new Map<String, String>{ | |
'uid' => '1', | |
'expires' => DateTime.now().addSeconds(300).formatGMT('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\''), | |
'customer_email' => 'john@example.com', | |
'customer_name' => 'John' | |
}); | |
System.debug(' Data: ' + data); | |
System.debug(' Encrypt data using AES128-cbc'); | |
Blob multipass = Crypto.encryptWithManagedIV('AES128', key, Blob.valueOf(data)); | |
System.debug(' Base64 encode the encrypted data'); | |
String multipass_string = EncodingUtil.base64Encode(multipass); | |
System.debug(' Build an HMAC-SHA1 signature using the encoded string and your api key'); | |
Blob signature = Crypto.generateMac('hmacSHA1', Blob.valueOf(multipass_string), Blob.valueOf(API_KEY)); | |
String signature_string = EncodingUtil.base64Encode(signature); | |
System.debug(' Finally, URL encode the multipass and signature'); | |
multipass_string = EncodingUtil.urlEncode(multipass_string, 'UTF-8'); | |
signature_string = EncodingUtil.urlEncode(signature_string, 'UTF-8'); | |
System.debug('== Finished =='); | |
System.debug('https://' + SITE_KEY + '.desk.com/customer/authentication/multipass/callback?multipass=' + multipass_string + '&signature=' + signature_string); | |
} | |
} |
@imranqurehi you'll have to generate the KEY_BASE64
string in a different environment. A ruby example is commented out in the code on the lines 9-11 but you can use PHP, .net or any other programming language that allows you to generate a SHA1 digest and cut out the first 16 bytes of that digest.
Got it working in apex . Made some changes to the code to fetch out the first 16 bytes .
blob blobToGenerateSHA = blob.valueof(API_KEY + SITE_KEY);
system.debug('*****'+string.valueof(Crypto.generateDigest('SHA1', blobToGenerateSHA)));
blob codeSHA = Crypto.generateDigest('SHA1', blobToGenerateSHA);
string b64= EncodingUtil.base64Encode(codeSHA);
KEY_BASE64 = b64.left(22);
System.debug('String b64: [' + KEY_BASE64 );
System.debug('== Generating ==');
Blob key = EncodingUtil.base64Decode(KEY_BASE64);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is not working, getting error "Invalid private key. Must be 16 bytes." and when I changed to get first 16 bytes from key then It gave me multipass and signature but that are not valid as its not allowing me to login into Desk.com with multipass and signature.